KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > node > AntNodeReader


1 package org.enhydra.kelp.ant.node;
2
3 //Kelp
4
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 //ToolBox
10
import org.enhydra.tool.common.PathHandle;
11 //Java
12
import java.io.File JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import org.enhydra.kelp.common.node.OtterTextFileNode;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * <p>Title: </p>
19  * <p>Description: This class is used by AntProject. It read all files from
20  * resource directory using extension filtering and creates Document nodes
21  * array. All necessary data are provided by AntProject.</p>
22  * <p>Copyright: Copyright (c) 2003</p>
23  * <p>Company: </p>
24  * @author Damir Milovic
25  * @version 1.0
26  */

27
28 public class AntNodeReader {
29     private ArrayList JavaDoc list = new ArrayList JavaDoc();
30     //private OtterProject otterProject = null;
31
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     /**
40      * Read all files from resource directory and create Document nodes using
41      * extension filtering.
42      * @param rootDir root directory.
43      * @return array of all document nodes used for xml compiling.
44      */

45     public OtterDocumentNode[] getDocumentNodes(String JavaDoc rootDir){
46         OtterDocumentNode[] nodes = new OtterDocumentNode[0];
47         list.clear();
48         //Read resources dir
49
PathHandle resourcesPathHandle = PathHandle.createPathHandle(rootDir);
50         OtterFolderNode rootFolder = nodeFactory.createFolderNode(project,resourcesPathHandle.getPath());
51         String JavaDoc[] docTypes = project.getDocTypes();
52         readDocument(rootFolder, docTypes); //fill the list
53
nodes = new OtterDocumentNode[list.size()];
54         nodes = (OtterDocumentNode[]) list.toArray(nodes);
55         list.clear();
56         return nodes;
57
58     }
59     /**
60      * Read all files from root directory and create TextFile nodes
61      * @param rootDir root directory.
62      * @return array of all text files inside rootDir
63      */

64     public OtterTextFileNode[] getTextFileNodes(String JavaDoc 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 JavaDoc[] extFilters) {
77
78           PathHandle current = PathHandle.createPathHandle(((OtterFileNode)container).getFilePath());
79
80           if(container instanceof OtterFolderNode){
81                 //Read all files inside this directory
82
File JavaDoc directory = current.getFile();
83                 File JavaDoc[] files = directory.listFiles(); //folders or files
84
for(int i=0;i<files.length;i++){
85                       //Create node and read it
86
File JavaDoc newFile = files[i];
87                       if(newFile.isDirectory()){ //Create OtterFolderNode
88
readDocument(nodeFactory.createFolderNode(container,newFile.getPath()),extFilters);
89                       }else{ //Create OtterDocumentNode
90
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     //FIXME should use extensions filter to resolve text files !?
103
private void readTextFile(OtterNode container){
104         PathHandle current = PathHandle.createPathHandle(((OtterFileNode)container).getFilePath());
105
106         if(container instanceof OtterFolderNode){
107             //Read all files inside this directory
108
File JavaDoc directory = current.getFile();
109             File JavaDoc[] files = directory.listFiles(); //folders or files
110
for(int i=0;i<files.length;i++){
111                   //Create node and read it
112
File JavaDoc newFile = files[i];
113                   if(newFile.isDirectory()){ //Create OtterFolderNode
114

115                         readTextFile(nodeFactory.createFolderNode(container,newFile.getPath()));
116                   }else{ //Create OtterDocumentNode
117
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             //All documents need to compile
132
docNode.setSelected(true);
133         }else{
134             //Just selected documents need to compile
135
OtterDocumentNode[] selectedDoc = project.getSelectedDocuments();
136             docNode.setSelected(false);
137             //File docFile = new File(docNode.getFilePath());
138
//File selectedFile = null;
139
String JavaDoc docPath = docNode.getFilePath().replace('\\','/');
140             for (int i = 0 ; i<selectedDoc.length;i++){
141                 String JavaDoc selectedPath = selectedDoc[i].getFilePath().replace('\\','/');
142                 if(docPath.equalsIgnoreCase(selectedPath)){
143                     //found selected
144
docNode.setSelected(true);
145                     break;
146                 }
147             }
148         }
149
150     }
151 }
152
Popular Tags