001: package org.enhydra.kelp.ant.node;
002:
003: //Kelp
004: import org.enhydra.kelp.common.node.OtterFileNode;
005: import org.enhydra.kelp.common.node.OtterDocumentNode;
006: import org.enhydra.kelp.common.node.OtterNode;
007: import org.enhydra.kelp.common.node.OtterFolderNode; //ToolBox
008: import org.enhydra.tool.common.PathHandle; //Java
009: import java.io.File;
010: import java.util.ArrayList;
011: import org.enhydra.kelp.common.node.OtterTextFileNode;
012:
013: /**
014: * <p>Title: </p>
015: * <p>Description: This class is used by AntProject. It read all files from
016: * resource directory using extension filtering and creates Document nodes
017: * array. All necessary data are provided by AntProject.</p>
018: * <p>Copyright: Copyright (c) 2003</p>
019: * <p>Company: </p>
020: * @author Damir Milovic
021: * @version 1.0
022: */
023:
024: public class AntNodeReader {
025: private ArrayList list = new ArrayList();
026: //private OtterProject otterProject = null;
027: private AntProject project = null;
028: private AntNodeFactory nodeFactory = null;
029:
030: public AntNodeReader(AntProject p) {
031: project = p;
032: nodeFactory = (AntNodeFactory) project.getNodeFactory();
033: }
034:
035: /**
036: * Read all files from resource directory and create Document nodes using
037: * extension filtering.
038: * @param rootDir root directory.
039: * @return array of all document nodes used for xml compiling.
040: */
041: public OtterDocumentNode[] getDocumentNodes(String rootDir) {
042: OtterDocumentNode[] nodes = new OtterDocumentNode[0];
043: list.clear();
044: //Read resources dir
045: PathHandle resourcesPathHandle = PathHandle
046: .createPathHandle(rootDir);
047: OtterFolderNode rootFolder = nodeFactory.createFolderNode(
048: project, resourcesPathHandle.getPath());
049: String[] docTypes = project.getDocTypes();
050: readDocument(rootFolder, docTypes); //fill the list
051: nodes = new OtterDocumentNode[list.size()];
052: nodes = (OtterDocumentNode[]) list.toArray(nodes);
053: list.clear();
054: return nodes;
055:
056: }
057:
058: /**
059: * Read all files from root directory and create TextFile nodes
060: * @param rootDir root directory.
061: * @return array of all text files inside rootDir
062: */
063: public OtterTextFileNode[] getTextFileNodes(String rootDir) {
064: OtterTextFileNode[] nodes = new OtterTextFileNode[0];
065: list.clear();
066: PathHandle rootPathHandle = PathHandle
067: .createPathHandle(rootDir);
068: OtterFolderNode rootFolder = nodeFactory.createFolderNode(
069: project, rootPathHandle.getPath());
070: readTextFile(rootFolder);
071: nodes = new OtterTextFileNode[list.size()];
072: nodes = (OtterTextFileNode[]) list.toArray(nodes);
073: list.clear();
074: return nodes;
075: }
076:
077: private void readDocument(OtterNode container, String[] extFilters) {
078:
079: PathHandle current = PathHandle
080: .createPathHandle(((OtterFileNode) container)
081: .getFilePath());
082:
083: if (container instanceof OtterFolderNode) {
084: //Read all files inside this directory
085: File directory = current.getFile();
086: File[] files = directory.listFiles(); //folders or files
087: for (int i = 0; i < files.length; i++) {
088: //Create node and read it
089: File newFile = files[i];
090: if (newFile.isDirectory()) { //Create OtterFolderNode
091: readDocument(nodeFactory.createFolderNode(
092: container, newFile.getPath()), extFilters);
093: } else { //Create OtterDocumentNode
094: readDocument(nodeFactory.createDocumentNode(
095: container, newFile.getPath()), extFilters);
096: }
097: }
098: } else if (container instanceof OtterDocumentNode) {
099: if (current.getFile().exists()
100: && current.hasExtension(extFilters)) {
101: OtterDocumentNode docNode = null;
102: docNode = nodeFactory.createDocumentNode(container,
103: current.getFile().getAbsolutePath());
104: checkSelected(docNode);
105: list.add(docNode);
106: }
107: }
108: }
109:
110: //FIXME should use extensions filter to resolve text files !?
111: private void readTextFile(OtterNode container) {
112: PathHandle current = PathHandle
113: .createPathHandle(((OtterFileNode) container)
114: .getFilePath());
115:
116: if (container instanceof OtterFolderNode) {
117: //Read all files inside this directory
118: File directory = current.getFile();
119: File[] files = directory.listFiles(); //folders or files
120: for (int i = 0; i < files.length; i++) {
121: //Create node and read it
122: File newFile = files[i];
123: if (newFile.isDirectory()) { //Create OtterFolderNode
124:
125: readTextFile(nodeFactory.createFolderNode(
126: container, newFile.getPath()));
127: } else { //Create OtterDocumentNode
128: readTextFile(nodeFactory.createTextFileNode(
129: container, newFile.getPath()));
130: }
131: }
132: } else if (container instanceof OtterTextFileNode) {
133: if (current.getFile().exists()) {
134: OtterTextFileNode textFileNode = null;
135: textFileNode = nodeFactory.createTextFileNode(
136: container, current.getFile().getAbsolutePath());
137: list.add(textFileNode);
138: }
139: }
140:
141: }
142:
143: private void checkSelected(OtterDocumentNode docNode) {
144: if (project.getProperty(AntProject.XMLC_INCLUDES)
145: .equalsIgnoreCase(AntProject.XMLC_RESOURCES_ALL)) {
146: //All documents need to compile
147: docNode.setSelected(true);
148: } else {
149: //Just selected documents need to compile
150: OtterDocumentNode[] selectedDoc = project
151: .getSelectedDocuments();
152: docNode.setSelected(false);
153: //File docFile = new File(docNode.getFilePath());
154: //File selectedFile = null;
155: String docPath = docNode.getFilePath().replace('\\', '/');
156: for (int i = 0; i < selectedDoc.length; i++) {
157: String selectedPath = selectedDoc[i].getFilePath()
158: .replace('\\', '/');
159: if (docPath.equalsIgnoreCase(selectedPath)) {
160: //found selected
161: docNode.setSelected(true);
162: break;
163: }
164: }
165: }
166:
167: }
168: }
|