001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common.importer;
023:
024: // ToolBox imports
025: import org.enhydra.tool.ToolBoxInfo;
026: import org.enhydra.tool.common.FileUtil;
027: import org.enhydra.tool.common.PathHandle;
028:
029: // Kelp imports
030: import org.enhydra.kelp.common.map.MapEntry;
031: import org.enhydra.kelp.common.map.MapUtil;
032: import org.enhydra.kelp.common.node.OtterProject;
033: import org.enhydra.kelp.common.node.OtterXMLCNode;
034: import org.enhydra.kelp.common.node.PropertyKeys;
035:
036: // Standard imports
037: import java.io.BufferedReader;
038: import java.io.IOException;
039: import java.io.File;
040: import java.io.FileReader;
041: import java.util.Vector;
042:
043: //
044: public class MakefileReader {
045:
046: // strings not to be translated
047: private final String KEEP = "-keep"; // nores
048: private final String IMPLEMENTS = "-implements"; // nores
049: private final String ROOT = "ROOT"; // nores
050: private final String PACKAGEDIR = "PACKAGEDIR"; // nores
051: private final String XMLC_ = "XMLC_"; // nores
052: private final String _CLASSES = "_CLASSES"; // nores
053: private final String _DIR = "_DIR"; // nores
054: private final String _OPTS = "_OPTS"; // nores
055: private final String _OPTS_FILE = "_OPTS_FILE"; // nores
056: private final String PE = "+="; // nores
057:
058: //
059: private OtterProject project = null;
060: private MakefileFilter filter = null;
061: private MakefileData makeCursor = null;
062: private MakefileData[] makeData = new MakefileData[0];
063: private Vector makeVector = new Vector();
064: private String[] docTypes = new String[0];
065: private String sourcePath = new String();
066: private String continueName = new String();
067: private boolean continueLine = false;
068: private boolean needRead = true;
069: private boolean keepFound = false;
070:
071: public MakefileReader() {
072: filter = new MakefileFilter();
073: docTypes = ToolBoxInfo.getSupportedDocTypes();
074: }
075:
076: public OtterProject getProject() {
077: return project;
078: }
079:
080: public void setProject(OtterProject p) {
081: project = p;
082: }
083:
084: public void invalidate() {
085: needRead = true;
086: }
087:
088: public void read() {
089: File root = null;
090: String[] sourcePaths = getProject().getSourcePathArray();
091:
092: for (int i = 0; i < sourcePaths.length; i++) {
093: sourcePath = sourcePaths[i];
094: root = new File(sourcePath);
095: readDirectory(root);
096: }
097: makeData = new MakefileData[makeVector.size()];
098: makeData = (MakefileData[]) makeVector.toArray(makeData);
099: makeVector.clear();
100: makeCursor = null;
101: needRead = false;
102: }
103:
104: public String[][] getPackageMap() {
105: String[][] map = new String[0][2];
106: String[] docs = new String[0];
107: String javaPackage = new String();
108: Vector mapVector = new Vector();
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 symVector = new Vector();
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,
169: (new String()) + 0);
170: }
171:
172: // Turn of auto package if class output is within
173: // a source path
174: String[] src = new String[0];
175: String out = new String();
176: PathHandle outPath = null;
177:
178: src = project.getSourcePathArray();
179: out = project.getClassOutputPath();
180: outPath = PathHandle.createPathHandle(out);
181: for (int i = 0; i < src.length; i++) {
182: PathHandle srcCursor = null;
183:
184: srcCursor = PathHandle.createPathHandle(src[i]);
185: if (srcCursor.parentOf(outPath)) {
186: project.setProperty(PropertyKeys.NAME_AUTO_PACK,
187: Boolean.FALSE.toString());
188: break;
189: }
190: }
191: }
192:
193: private void readDirectory(File root) {
194: File[] list = new File[0];
195:
196: list = root.listFiles(filter);
197: if (list != null) {
198: for (int i = 0; i < list.length; i++) {
199: if (list[i].isDirectory()) {
200: readDirectory(list[i]);
201: } else {
202: readFile(list[i]);
203: }
204: }
205: }
206: }
207:
208: private void setupXMLCNodes() {
209: OtterXMLCNode nodes[] = getProject().getAllXMLCNodes();
210:
211: for (int i = 0; i < nodes.length; i++) {
212: setupNodeSelection(nodes[i]);
213: setupNodeOption(nodes[i]);
214: }
215: }
216:
217: private void setupNodeSelection(OtterXMLCNode node) {
218: String[] docPaths = makeCursor.getSourceDocPaths();
219: String selectPath = new String();
220: String optionFile = new String();
221: String parameters = new String();
222: PathHandle nodePath = null;
223:
224: nodePath = PathHandle.createPathHandle(node.getFilePath());
225: for (int i = 0; i < docPaths.length; i++) {
226: if (nodePath.equals(docPaths[i])) {
227: node.setSelected(true);
228: optionFile = makeCursor.getXMLCOptionFilePath();
229: parameters = makeCursor.getXMLCParameters();
230: if (optionFile.length() > 0) {
231: node.getParent().setXMLCOptionFilePath(optionFile);
232: }
233: if (parameters.length() > 0) {
234: node.getParent().setXMLCParameters(parameters);
235: }
236: break;
237: }
238: }
239: }
240:
241: private void checkForKeep(String params) {
242: if (keepFound || (params == null)
243: || (params.trim().length() == 0)) {
244:
245: // done
246: } else {
247: keepFound = (params.indexOf(KEEP) > -1);
248: }
249: }
250:
251: private void setupNodeOption(OtterXMLCNode node) {
252: MakeItemData[] items = makeCursor.getItems();
253: PathHandle docPath = null;
254: PathHandle nodePath = null;
255: StringBuffer pathBuf = new StringBuffer();
256: boolean breakOut = false;
257:
258: nodePath = PathHandle.createPathHandle(node.getFilePath());
259: for (int i = 0; i < items.length; i++) {
260: for (int j = 0; j < docTypes.length; j++) {
261: pathBuf.setLength(0);
262: pathBuf.append(makeCursor.getDocPath());
263: pathBuf.append(File.separator);
264: pathBuf.append(items[i].getItem());
265: pathBuf.append('.');
266: pathBuf.append(docTypes[j].toLowerCase());
267: docPath = PathHandle.createPathHandle(pathBuf
268: .toString());
269: if (docPath.equals(nodePath)) {
270: node.setXMLCParameters(items[i].getOptionList());
271: breakOut = true;
272: break;
273: }
274: }
275: if (breakOut) {
276: break;
277: }
278: }
279: }
280:
281: private void readFile(File make) {
282: makeCursor = new MakefileData(make.getParent(), sourcePath);
283: BufferedReader reader = null;
284:
285: try {
286: reader = new BufferedReader(new FileReader(make));
287: String line = reader.readLine();
288:
289: while (line != null) {
290: readLine(line);
291: line = reader.readLine();
292: }
293: reader.close();
294: } catch (IOException e) {
295: e.printStackTrace();
296: }
297: if (makeCursor.isXMLC()) {
298: makeVector.addElement(makeCursor);
299: }
300: }
301:
302: private void readLine(String in) {
303: String line = new String(in.trim());
304: String name = new String();
305: String value = new String();
306: int index = -1;
307:
308: if (line.length() > 0) {
309: index = line.indexOf('=');
310: if (index > -1) {
311: if (line.indexOf(PE) > -1) { // +=
312: name = line.substring(0, index - 1).trim();
313: value = line.substring(index + 1).trim();
314: } else {
315: name = line.substring(0, index).trim();
316: value = line.substring(index + 1).trim();
317: }
318: } else if (continueLine) {
319: name = continueName;
320: value = line;
321: }
322: name = trimContinue(name);
323: value = trimContinue(value);
324: value = preprocessValue(value);
325: if (name.trim().length() == 0 || value.trim().length() == 0) {
326:
327: // empty
328: } else if (name.equals(ROOT)) {
329: makeCursor.readRootPath(value);
330: } else if (name.equals(PACKAGEDIR)) {
331: makeCursor.readPackagePath(value);
332: } else {
333: boolean keyFound = false;
334:
335: for (int i = 0; i < docTypes.length; i++) {
336: String type = docTypes[i].toUpperCase().trim();
337:
338: if (name.equals(XMLC_ + type + _OPTS_FILE)) {
339: makeCursor.readXMLCOptionFilePath(value);
340: keyFound = true;
341: break;
342: } else if (name.equals(XMLC_ + type + _OPTS)) {
343: checkForKeep(value);
344: makeCursor.setXMLCParameters(value);
345: keyFound = true;
346: break;
347: } else if (name.equals(type + _DIR)) {
348: makeCursor.readDocPath(value);
349: keyFound = true;
350: break;
351: } else if (name.equals(type + _CLASSES)) {
352: makeCursor.addSourceDocPath(value);
353: keyFound = true;
354: break;
355: }
356: }
357: if (!keyFound) {
358: if (name.startsWith(XMLC_) && name.endsWith(_OPTS)) {
359: String item = name.substring(XMLC_.length(),
360: (name.length() - _OPTS.length()));
361:
362: makeCursor.addItem(item, value);
363: }
364: }
365: }
366: }
367: continueLine = (line.length() > 0)
368: && (line.charAt(line.length() - 1) == '\\');
369: if (name.length() > 0) {
370: continueName = name;
371: }
372: }
373:
374: private String preprocessValue(String in) {
375: String out = null;
376: String key = new String();
377: String remainder = new String();
378: StringBuffer buf = new StringBuffer();
379: int index = -1;
380:
381: if (in == null) {
382: out = null;
383: } else {
384: out = new String(in);
385: key = IMPLEMENTS;
386: index = out.indexOf(key);
387: if (index > -1) {
388: remainder = out.substring(index + key.length()).trim();
389: if (remainder.indexOf('.') == -1) {
390: buf.append(out.substring(0, index));
391: buf.append(key);
392: buf.append(' ');
393: buf.append(makeCursor.getJavaPackage());
394: buf.append('.');
395: buf.append(remainder);
396: out = buf.toString();
397: }
398: }
399: }
400: return out;
401: }
402:
403: private String trimContinue(String in) {
404: String out = null;
405:
406: if (in != null) {
407: out = new String(in);
408: while ((out.length() > 0)
409: && (out.charAt(out.length() - 1) == '\\')) {
410: out = out.substring(0, out.length() - 1).trim();
411: }
412: }
413: return out;
414: }
415:
416: }
|