KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > importer > MakefileReader


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.kelp.common.importer;
23
24 // ToolBox imports
25
import org.enhydra.tool.ToolBoxInfo;
26 import org.enhydra.tool.common.FileUtil;
27 import org.enhydra.tool.common.PathHandle;
28
29 // Kelp imports
30
import org.enhydra.kelp.common.map.MapEntry;
31 import org.enhydra.kelp.common.map.MapUtil;
32 import org.enhydra.kelp.common.node.OtterProject;
33 import org.enhydra.kelp.common.node.OtterXMLCNode;
34 import org.enhydra.kelp.common.node.PropertyKeys;
35
36 // Standard imports
37
import java.io.BufferedReader JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileReader JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 //
44
public class MakefileReader {
45
46     // strings not to be translated
47
private final String JavaDoc KEEP = "-keep"; // nores
48
private final String JavaDoc IMPLEMENTS = "-implements"; // nores
49
private final String JavaDoc ROOT = "ROOT"; // nores
50
private final String JavaDoc PACKAGEDIR = "PACKAGEDIR"; // nores
51
private final String JavaDoc XMLC_ = "XMLC_"; // nores
52
private final String JavaDoc _CLASSES = "_CLASSES"; // nores
53
private final String JavaDoc _DIR = "_DIR"; // nores
54
private final String JavaDoc _OPTS = "_OPTS"; // nores
55
private final String JavaDoc _OPTS_FILE = "_OPTS_FILE"; // nores
56
private final String JavaDoc PE = "+="; // nores
57

58     //
59
private OtterProject project = null;
60     private MakefileFilter filter = null;
61     private MakefileData makeCursor = null;
62     private MakefileData[] makeData = new MakefileData[0];
63     private Vector JavaDoc makeVector = new Vector JavaDoc();
64     private String JavaDoc[] docTypes = new String JavaDoc[0];
65     private String JavaDoc sourcePath = new String JavaDoc();
66     private String JavaDoc continueName = new String JavaDoc();
67     private boolean continueLine = false;
68     private boolean needRead = true;
69     private boolean keepFound = false;
70
71     public MakefileReader() {
72         filter = new MakefileFilter();
73         docTypes = ToolBoxInfo.getSupportedDocTypes();
74     }
75
76     public OtterProject getProject() {
77         return project;
78     }
79
80     public void setProject(OtterProject p) {
81         project = p;
82     }
83
84     public void invalidate() {
85         needRead = true;
86     }
87
88     public void read() {
89         File JavaDoc root = null;
90         String JavaDoc[] sourcePaths = getProject().getSourcePathArray();
91
92         for (int i = 0; i < sourcePaths.length; i++) {
93             sourcePath = sourcePaths[i];
94             root = new File JavaDoc(sourcePath);
95             readDirectory(root);
96         }
97         makeData = new MakefileData[makeVector.size()];
98         makeData = (MakefileData[]) makeVector.toArray(makeData);
99         makeVector.clear();
100         makeCursor = null;
101         needRead = false;
102     }
103
104     public String JavaDoc[][] getPackageMap() {
105         String JavaDoc[][] map = new String JavaDoc[0][2];
106         String JavaDoc[] docs = new String JavaDoc[0];
107         String JavaDoc javaPackage = new String JavaDoc();
108         Vector JavaDoc mapVector = new Vector JavaDoc();
109         MapEntry[] entries = new MapEntry[0];
110         PathHandle path = null;
111
112         if (needRead) {
113             read();
114         }
115         for (int i = 0; i < makeData.length; i++) {
116             javaPackage = makeData[i].getJavaPackage();
117             docs = makeData[i].getSourceDocPaths();
118             for (int j = 0; j < docs.length; j++) {
119                 MapEntry entry = null;
120
121                 path = PathHandle.createPathHandle(docs[j]);
122                 path = path.getParent();
123                 entry = new MapEntry(path.getPath(), javaPackage);
124                 if (!mapVector.contains(entry)) {
125                     mapVector.addElement(entry);
126                 }
127             }
128         }
129         mapVector.trimToSize();
130         entries = new MapEntry[mapVector.size()];
131         entries = (MapEntry[]) mapVector.toArray(entries);
132         entries = MapUtil.optimize(entries);
133         map = MapUtil.toStringArray(entries);
134         entries = new MapEntry[0];
135         mapVector.clear();
136         return map;
137     }
138
139     public MakefileData[] getSymbolicMakefiles() {
140         MakefileData[] syms = new MakefileData[0];
141         Vector JavaDoc symVector = new Vector JavaDoc();
142
143         if (needRead) {
144             read();
145         }
146         for (int i = 0; i < makeData.length; i++) {
147             if (makeData[i].isSymbolic()) {
148                 symVector.addElement(makeData[i]);
149             }
150         }
151         syms = new MakefileData[symVector.size()];
152         syms = (MakefileData[]) symVector.toArray(syms);
153         symVector.clear();
154         return syms;
155     }
156
157     public void updateProject() {
158         if (needRead) {
159             read();
160         }
161         for (int i = 0; i < makeData.length; i++) {
162             makeCursor = makeData[i];
163             setupXMLCNodes();
164         }
165
166         // Generate files into source path if keep found.
167
if (keepFound) {
168             project.setProperty(PropertyKeys.NAME_GENTO, (new String JavaDoc()) + 0);
169         }
170
171         // Turn of auto package if class output is within
172
// a source path
173
String JavaDoc[] src = new String JavaDoc[0];
174         String JavaDoc out = new String JavaDoc();
175         PathHandle outPath = null;
176
177         src = project.getSourcePathArray();
178         out = project.getClassOutputPath();
179         outPath = PathHandle.createPathHandle(out);
180         for (int i = 0; i < src.length; i++) {
181             PathHandle srcCursor = null;
182
183             srcCursor = PathHandle.createPathHandle(src[i]);
184             if (srcCursor.parentOf(outPath)) {
185                 project.setProperty(PropertyKeys.NAME_AUTO_PACK,
186                                     Boolean.FALSE.toString());
187                 break;
188             }
189         }
190     }
191
192     private void readDirectory(File JavaDoc root) {
193         File JavaDoc[] list = new File JavaDoc[0];
194
195         list = root.listFiles(filter);
196         if (list != null) {
197             for (int i = 0; i < list.length; i++) {
198                 if (list[i].isDirectory()) {
199                     readDirectory(list[i]);
200                 } else {
201                     readFile(list[i]);
202                 }
203             }
204         }
205     }
206
207     private void setupXMLCNodes() {
208         OtterXMLCNode nodes[] = getProject().getAllXMLCNodes();
209
210         for (int i = 0; i < nodes.length; i++) {
211             setupNodeSelection(nodes[i]);
212             setupNodeOption(nodes[i]);
213         }
214     }
215
216     private void setupNodeSelection(OtterXMLCNode node) {
217         String JavaDoc[] docPaths = makeCursor.getSourceDocPaths();
218         String JavaDoc selectPath = new String JavaDoc();
219         String JavaDoc optionFile = new String JavaDoc();
220         String JavaDoc parameters = new String JavaDoc();
221         PathHandle nodePath = null;
222
223         nodePath = PathHandle.createPathHandle(node.getFilePath());
224         for (int i = 0; i < docPaths.length; i++) {
225             if (nodePath.equals(docPaths[i])) {
226                 node.setSelected(true);
227                 optionFile = makeCursor.getXMLCOptionFilePath();
228                 parameters = makeCursor.getXMLCParameters();
229                 if (optionFile.length() > 0) {
230                     node.getParent().setXMLCOptionFilePath(optionFile);
231                 }
232                 if (parameters.length() > 0) {
233                     node.getParent().setXMLCParameters(parameters);
234                 }
235                 break;
236             }
237         }
238     }
239
240     private void checkForKeep(String JavaDoc params) {
241         if (keepFound || (params == null) || (params.trim().length() == 0)) {
242
243             // done
244
} else {
245             keepFound = (params.indexOf(KEEP) > -1);
246         }
247     }
248
249     private void setupNodeOption(OtterXMLCNode node) {
250         MakeItemData[] items = makeCursor.getItems();
251         PathHandle docPath = null;
252         PathHandle nodePath = null;
253         StringBuffer JavaDoc pathBuf = new StringBuffer JavaDoc();
254         boolean breakOut = false;
255
256         nodePath = PathHandle.createPathHandle(node.getFilePath());
257         for (int i = 0; i < items.length; i++) {
258             for (int j = 0; j < docTypes.length; j++) {
259                 pathBuf.setLength(0);
260                 pathBuf.append(makeCursor.getDocPath());
261                 pathBuf.append(File.separator);
262                 pathBuf.append(items[i].getItem());
263                 pathBuf.append('.');
264                 pathBuf.append(docTypes[j].toLowerCase());
265                 docPath = PathHandle.createPathHandle(pathBuf.toString());
266                 if (docPath.equals(nodePath)) {
267                     node.setXMLCParameters(items[i].getOptionList());
268                     breakOut = true;
269                     break;
270                 }
271             }
272             if (breakOut) {
273                 break;
274             }
275         }
276     }
277
278     private void readFile(File JavaDoc make) {
279         makeCursor = new MakefileData(make.getParent(), sourcePath);
280         BufferedReader JavaDoc reader = null;
281
282         try {
283             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(make));
284             String JavaDoc line = reader.readLine();
285
286             while (line != null) {
287                 readLine(line);
288                 line = reader.readLine();
289             }
290             reader.close();
291         } catch (IOException JavaDoc e) {
292             e.printStackTrace();
293         }
294         if (makeCursor.isXMLC()) {
295             makeVector.addElement(makeCursor);
296         }
297     }
298
299     private void readLine(String JavaDoc in) {
300         String JavaDoc line = new String JavaDoc(in.trim());
301         String JavaDoc name = new String JavaDoc();
302         String JavaDoc value = new String JavaDoc();
303         int index = -1;
304
305         if (line.length() > 0) {
306             index = line.indexOf('=');
307             if (index > -1) {
308                 if (line.indexOf(PE) > -1) { // +=
309
name = line.substring(0, index - 1).trim();
310                     value = line.substring(index + 1).trim();
311                 } else {
312                     name = line.substring(0, index).trim();
313                     value = line.substring(index + 1).trim();
314                 }
315             } else if (continueLine) {
316                 name = continueName;
317                 value = line;
318             }
319             name = trimContinue(name);
320             value = trimContinue(value);
321             value = preprocessValue(value);
322             if (name.trim().length() == 0 || value.trim().length() == 0) {
323
324                 // empty
325
} else if (name.equals(ROOT)) {
326                 makeCursor.readRootPath(value);
327             } else if (name.equals(PACKAGEDIR)) {
328                 makeCursor.readPackagePath(value);
329             } else {
330                 boolean keyFound = false;
331
332                 for (int i = 0; i < docTypes.length; i++) {
333                     String JavaDoc type = docTypes[i].toUpperCase().trim();
334
335                     if (name.equals(XMLC_ + type + _OPTS_FILE)) {
336                         makeCursor.readXMLCOptionFilePath(value);
337                         keyFound = true;
338                         break;
339                     } else if (name.equals(XMLC_ + type + _OPTS)) {
340                         checkForKeep(value);
341                         makeCursor.setXMLCParameters(value);
342                         keyFound = true;
343                         break;
344                     } else if (name.equals(type + _DIR)) {
345                         makeCursor.readDocPath(value);
346                         keyFound = true;
347                         break;
348                     } else if (name.equals(type + _CLASSES)) {
349                         makeCursor.addSourceDocPath(value);
350                         keyFound = true;
351                         break;
352                     }
353                 }
354                 if (!keyFound) {
355                     if (name.startsWith(XMLC_) && name.endsWith(_OPTS)) {
356                         String JavaDoc item = name.substring(XMLC_.length(),
357                                                      (name.length()
358                                                       - _OPTS.length()));
359
360                         makeCursor.addItem(item, value);
361                     }
362                 }
363             }
364         }
365         continueLine = (line.length() > 0)
366                        && (line.charAt(line.length() - 1) == '\\');
367         if (name.length() > 0) {
368             continueName = name;
369         }
370     }
371
372     private String JavaDoc preprocessValue(String JavaDoc in) {
373         String JavaDoc out = null;
374         String JavaDoc key = new String JavaDoc();
375         String JavaDoc remainder = new String JavaDoc();
376         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
377         int index = -1;
378
379         if (in == null) {
380             out = null;
381         } else {
382             out = new String JavaDoc(in);
383             key = IMPLEMENTS;
384             index = out.indexOf(key);
385             if (index > -1) {
386                 remainder = out.substring(index + key.length()).trim();
387                 if (remainder.indexOf('.') == -1) {
388                     buf.append(out.substring(0, index));
389                     buf.append(key);
390                     buf.append(' ');
391                     buf.append(makeCursor.getJavaPackage());
392                     buf.append('.');
393                     buf.append(remainder);
394                     out = buf.toString();
395                 }
396             }
397         }
398         return out;
399     }
400
401     private String JavaDoc trimContinue(String JavaDoc in) {
402         String JavaDoc out = null;
403
404         if (in != null) {
405             out = new String JavaDoc(in);
406             while ((out.length() > 0)
407                    && (out.charAt(out.length() - 1) == '\\')) {
408                 out = out.substring(0, out.length() - 1).trim();
409             }
410         }
411         return out;
412     }
413
414 }
415
Popular Tags