1 package org.enhydra.kelp.ant.node; 2 3 import org.enhydra.kelp.common.node.OtterProject; 5 import org.enhydra.kelp.common.node.OtterFileNode; 6 import org.enhydra.kelp.common.node.OtterDocumentNode; 7 import org.enhydra.kelp.common.node.OtterNode; 8 import org.enhydra.kelp.common.node.OtterFolderNode; 9 import org.enhydra.tool.common.PathHandle; 11 import java.io.File ; 13 import java.util.ArrayList ; 14 import org.enhydra.kelp.common.node.OtterTextFileNode; 15 import java.io.IOException ; 16 17 27 28 public class AntNodeReader { 29 private ArrayList list = new ArrayList (); 30 private AntProject project = null; 32 private AntNodeFactory nodeFactory = null; 33 34 public AntNodeReader(AntProject p) { 35 project = p; 36 nodeFactory = (AntNodeFactory)project.getNodeFactory(); 37 } 38 39 45 public OtterDocumentNode[] getDocumentNodes(String rootDir){ 46 OtterDocumentNode[] nodes = new OtterDocumentNode[0]; 47 list.clear(); 48 PathHandle resourcesPathHandle = PathHandle.createPathHandle(rootDir); 50 OtterFolderNode rootFolder = nodeFactory.createFolderNode(project,resourcesPathHandle.getPath()); 51 String [] docTypes = project.getDocTypes(); 52 readDocument(rootFolder, docTypes); nodes = new OtterDocumentNode[list.size()]; 54 nodes = (OtterDocumentNode[]) list.toArray(nodes); 55 list.clear(); 56 return nodes; 57 58 } 59 64 public OtterTextFileNode[] getTextFileNodes(String rootDir){ 65 OtterTextFileNode[] nodes = new OtterTextFileNode[0]; 66 list.clear(); 67 PathHandle rootPathHandle = PathHandle.createPathHandle(rootDir); 68 OtterFolderNode rootFolder = nodeFactory.createFolderNode(project,rootPathHandle.getPath()); 69 readTextFile(rootFolder); 70 nodes = new OtterTextFileNode[list.size()]; 71 nodes = (OtterTextFileNode[]) list.toArray(nodes); 72 list.clear(); 73 return nodes; 74 } 75 76 private void readDocument(OtterNode container,String [] extFilters) { 77 78 PathHandle current = PathHandle.createPathHandle(((OtterFileNode)container).getFilePath()); 79 80 if(container instanceof OtterFolderNode){ 81 File directory = current.getFile(); 83 File [] files = directory.listFiles(); for(int i=0;i<files.length;i++){ 85 File newFile = files[i]; 87 if(newFile.isDirectory()){ readDocument(nodeFactory.createFolderNode(container,newFile.getPath()),extFilters); 89 }else{ readDocument(nodeFactory.createDocumentNode(container,newFile.getPath()),extFilters); 91 } 92 } 93 }else if(container instanceof OtterDocumentNode){ 94 if (current.getFile().exists() && current.hasExtension(extFilters)){ 95 OtterDocumentNode docNode = null; 96 docNode = nodeFactory.createDocumentNode(container,current.getFile().getAbsolutePath()); 97 checkSelected(docNode); 98 list.add(docNode); 99 } 100 } 101 } 102 private void readTextFile(OtterNode container){ 104 PathHandle current = PathHandle.createPathHandle(((OtterFileNode)container).getFilePath()); 105 106 if(container instanceof OtterFolderNode){ 107 File directory = current.getFile(); 109 File [] files = directory.listFiles(); for(int i=0;i<files.length;i++){ 111 File newFile = files[i]; 113 if(newFile.isDirectory()){ 115 readTextFile(nodeFactory.createFolderNode(container,newFile.getPath())); 116 }else{ readTextFile(nodeFactory.createTextFileNode(container,newFile.getPath())); 118 } 119 } 120 }else if(container instanceof OtterTextFileNode){ 121 if (current.getFile().exists()){ 122 OtterTextFileNode textFileNode = null; 123 textFileNode = nodeFactory.createTextFileNode(container,current.getFile().getAbsolutePath()); 124 list.add(textFileNode); 125 } 126 } 127 128 } 129 private void checkSelected(OtterDocumentNode docNode){ 130 if(project.getProperty(AntProject.XMLC_INCLUDES).equalsIgnoreCase(AntProject.XMLC_RESOURCES_ALL)){ 131 docNode.setSelected(true); 133 }else{ 134 OtterDocumentNode[] selectedDoc = project.getSelectedDocuments(); 136 docNode.setSelected(false); 137 String docPath = docNode.getFilePath().replace('\\','/'); 140 for (int i = 0 ; i<selectedDoc.length;i++){ 141 String selectedPath = selectedDoc[i].getFilePath().replace('\\','/'); 142 if(docPath.equalsIgnoreCase(selectedPath)){ 143 docNode.setSelected(true); 145 break; 146 } 147 } 148 } 149 150 } 151 } 152 | Popular Tags |