001: package org.enhydra.kelp.ant.node;
002:
003: //AntKelp imports
004: import org.enhydra.kelp.ant.AntFacade;
005: import org.enhydra.kelp.ant.node.AntNodeReader; //Kelp imports
006: import org.enhydra.kelp.common.node.OtterNode;
007: import org.enhydra.kelp.common.node.OtterDocumentNode;
008: import org.enhydra.kelp.common.node.OtterProject;
009: import org.enhydra.kelp.common.node.OtterFileNode;
010: import org.enhydra.kelp.common.node.OtterTextFileNode;
011: import org.enhydra.kelp.common.node.OtterNodeFactory; //Toolbox imports
012: import org.enhydra.tool.common.PathHandle;
013: import org.enhydra.tool.ToolBoxInfo; // JDK
014: import java.io.File;
015: import java.util.ArrayList;
016: import java.io.FileReader;
017: import java.io.BufferedReader;
018: import java.io.IOException;
019: import java.io.FileWriter;
020: import java.io.BufferedWriter;
021: import java.io.PrintWriter;
022: import java.util.HashMap;
023:
024: /**
025: * <p>Title: </p>
026: * <p>Description: This class implementing OtterProject independet from IDE. It
027: * use <code>build.xml</code> file for holding properties and <code>Ant tool
028: * </code> for: XML compiling, DODS generator and application deploing. When
029: * this class is instantiaded, must call <code>setWorkingPath()</code> where
030: * <code>build.xml</code> file is placed.
031: * </p>
032: * <p>Copyright: Copyright (c) 2003</p>
033: * <p>Company: </p>
034: * @author Damir Milovic
035: * @version 1.0
036: */
037:
038: public class AntProject extends OtterProject {
039:
040: //Ant Project property keys:
041: public final static String PROJECT_NAME = "project.name";
042: public final static String RESOURCES_DIR = "resources.dir";
043: public final static String SRC_DIR = "src.dir";
044: public final static String CLASSES_DIR = "classes.dir";
045: public final static String CLASS_PATH = "class.path";
046: public final static String ENHYDRA_DIR = "enhydra.dir";
047: public final static String OUTPUT_FILE_NAME = "output.file.name"; //log file
048: //XMLC
049: public final static String XMLC_INVOKE = "xmlc.invoke";
050: public final static String XMLC_VERBOSE = "xmlc.verbose";
051: public final static String XMLC_PACKAGE_NAME = "xmlc.package.name";
052: public final static String XMLC_LOG_ENABLED = "xmlc.log.enabled";
053: public final static String XMLC_DEFAULT_PACKAGE = "xmlc.default.package"; //for internal use
054: public final static String XMLC_GENERETED_SRC_DIR = "xmlc.src.dir";
055: public final static String XMLC_OPTIONS_FILE = "xmlc.options.file";
056: public final static String XMLC_INCLUDES = "xmlc.includes";
057: public final static String XMLC_RESOURCES_ALL = "xmlc.resources.all";
058: public final static String XMLC_RESOURCES_SELECTED = "xmlc.resources.selected";
059: public final static String XMLC_RESOURCE_EXTENSIONS = "xmlc.resources.all";
060: //Deployer
061: public final static String DEPLOY_INVOKE = "deploy.invoke";
062: public final static String DEPLOY_CONTENT_ENABLED = "deploy.content.enabled";
063: public final static String DEPLOY_INPUT_ENABLED = "deploy.input.enabled";
064: public final static String DEPLOY_WAR_ENABLED = "deploy.war.enabled";
065: public final static String DEPLOY_WAR_ENHYDRA_LIBS = "deploy.war.enhydra.libs";
066: private final static String DEPLOY_WAR_ENHYDRA_LIBS_TRUE = "enhydra.libs.true";
067: private final static String DEPLOY_WAR_ENHYDRA_LIBS_FALSE = "enhydra.libs.false";
068: public final static String INPUT_DIR = "input.dir";
069: public final static String OUTPUT_DIR = "output.dir";
070: public final static String CONTENT_DIR = "content.dir";
071: public final static String CONTENT_PACKAGE_DIR = "content.package.dir";
072: public final static String CONTENT_TYPE_EXTENSIONS = "content.types.selected";
073: //DODS
074: public final static String DODS_GENERATED_SRC_DIR = "dods.src.dir";
075: public final static String DODS_DOML = "dods.doml";
076: public final static String DODS_ACTION = "dods.action";
077: public final static String DODS_TEMPLATE_SET = "dods.template.set";
078: public final static String DODS_INVOKE = "dods.invoke";
079:
080: private String workingDir = null;
081: private String enhydraDir = ToolBoxInfo.getEnhydraRoot();
082: private String rootPath;
083: private AntNodeFactory factory = new AntNodeFactory();
084: private AntFacade antFacade = null;
085: private boolean webType;
086: private AntNodeReader nodeReader = null;
087: private String outputFileName = null;
088: private boolean outputFileEnabled = false;
089: private String defaultPackage = "";
090:
091: public AntProject(String rootPath) throws Exception {
092: workingDir = rootPath;
093: antFacade = new AntFacade(this );
094: webType = true;
095: nodeReader = new AntNodeReader(this );
096: defaultPackage = new String(this .getProperty(PROJECT_NAME)
097: + ".presentation");
098: if (antFacade.getPropertyLocation(CONTENT_DIR) == null) {
099: webType = false;
100: }
101: }
102:
103: public OtterFileNode[] getAllInput() {
104: return nodeReader.getTextFileNodes(this
105: .getProperty(AntProject.INPUT_DIR));
106: }
107:
108: public void configureRunClass() {
109: /**@todo: implement this org.enhydra.kelp.common.node.OtterProject abstract method*/
110: }
111:
112: public OtterFileNode[] findFileNodesByType(String[] parm1) {
113: return new OtterFileNode[0];
114: /**@todo: implement this org.enhydra.kelp.common.node.OtterProject abstract method*/
115: }
116:
117: public void save() {
118: antFacade.save();
119: }
120:
121: public OtterDocumentNode[] getAllDocuments() {
122: return nodeReader.getDocumentNodes(this
123: .getProperty(AntProject.RESOURCES_DIR));
124: }
125:
126: /**
127: * AntXMLCTool call this method when selection is changed
128: * @param docNodes
129: */
130: public void setAllDocuments(OtterDocumentNode[] docNodes) {
131:
132: ArrayList selectedList = new ArrayList();
133: for (int i = 0; i < docNodes.length; i++) {
134: if (docNodes[i].isSelected())
135: selectedList.add(docNodes[i]);
136: }
137: Object[] objs = selectedList.toArray();
138: OtterDocumentNode[] selectedDocNodes = new OtterDocumentNode[objs.length];
139: for (int i = 0; i < objs.length; i++) {
140: selectedDocNodes[i] = (OtterDocumentNode) objs[i];
141: }
142:
143: if (selectedDocNodes.length == docNodes.length) { //All documents are selected
144: this .setProperty(this .XMLC_INCLUDES,
145: this .XMLC_RESOURCES_ALL); //Set all for XML compilation
146: antFacade.replacePatternset(this .XMLC_RESOURCES_SELECTED,
147: new String[0]); //clear previous selected docs
148: } else {
149: String resourceDir = relativeToAbsolute(getProperty(this .RESOURCES_DIR));
150: this .setProperty(this .XMLC_INCLUDES,
151: this .XMLC_RESOURCES_SELECTED);//Set selected for XML compilation
152: String[] selectedPaths = new String[selectedDocNodes.length];
153: for (int i = 0; i < selectedDocNodes.length; i++) {
154: selectedPaths[i] = selectedDocNodes[i].getFilePath();
155: selectedPaths[i] = absoluteToRelative(resourceDir,
156: selectedPaths[i]);//make relative to resources dir
157: }
158: antFacade.replacePatternset(this .XMLC_RESOURCES_SELECTED,
159: selectedPaths);
160: }
161: }
162:
163: /**
164: * Get selected documents for XMLC from build.xml file
165: * @return selected documents
166: */
167: public OtterDocumentNode[] getSelectedDocuments() {
168: String[] docPaths = antFacade
169: .getPatternset(this .XMLC_RESOURCES_SELECTED);
170: OtterDocumentNode[] docNodes = null;
171: if (docPaths != null) {
172: docNodes = new OtterDocumentNode[docPaths.length];
173: for (int i = 0; i < docPaths.length; i++) {
174: docNodes[i] = factory.createDocumentNode(this ,
175: resourceRelativeToAbsolute(docPaths[i]));
176: }
177: } else {
178: docNodes = new OtterDocumentNode[0];
179: }
180: return docNodes;
181: }
182:
183: public String getRootPath() {
184: return getWorkingPath(); //FIXME, for now it is the same
185: }
186:
187: public Object getNativeNode() {
188: return null;
189: /**@todo: implement this org.enhydra.kelp.common.node.OtterProject abstract method*/
190: }
191:
192: public String getProperty(String property) {
193: String s = null;
194: if (property.equalsIgnoreCase(NAME_WORKING_DIR)) {
195: s = workingDir;
196: } else if (property.equalsIgnoreCase(ENHYDRA_DIR)) {
197: s = enhydraDir;
198: } else if (property.equalsIgnoreCase(RESOURCES_DIR)) {
199: s = relativeToAbsolute(antFacade
200: .getPropertyValue(RESOURCES_DIR));
201: } else if (property.equalsIgnoreCase(DODS_DOML)) {
202: s = relativeToAbsolute(antFacade
203: .getPropertyLocation(DODS_DOML));
204: } else if (property.equalsIgnoreCase(AntProject.XMLC_INVOKE)) {
205: boolean checkInvoke = antFacade.checkDependedTarget("make",
206: "xmlc");
207: if (checkInvoke)
208: s = "true";
209: else
210: s = "false";
211: } else if (property.equalsIgnoreCase(AntProject.DODS_INVOKE)) {
212: boolean checkInvoke = antFacade.checkDependedTarget("make",
213: "dods");
214: if (checkInvoke)
215: s = "true";
216: else
217: s = "false";
218: } else if (property
219: .equalsIgnoreCase(AntProject.DODS_TEMPLATE_SET)) {
220: s = antFacade.getPropertyValue(property);
221: } else if (property.equalsIgnoreCase(AntProject.DODS_ACTION)) {
222: s = antFacade.getPropertyValue(property);
223: } else if (property
224: .equalsIgnoreCase(AntProject.DEPLOY_CONTENT_ENABLED)) {
225: boolean checkInvoke = antFacade.checkDependedTarget(
226: "deploy", "content");
227: if (checkInvoke)
228: s = "true";
229: else
230: s = "false";
231: } else if (property
232: .equalsIgnoreCase(AntProject.DEPLOY_INPUT_ENABLED)) {
233: boolean checkInvoke = antFacade.checkDependedTarget(
234: "deploy", "input");
235: if (checkInvoke)
236: s = "true";
237: else
238: s = "false";
239: } else if (property
240: .equalsIgnoreCase(AntProject.DEPLOY_WAR_ENABLED)) {
241: boolean checkInvoke = antFacade.checkDependedTarget(
242: "deploy", "makeWAR");
243: if (checkInvoke)
244: s = "true";
245: else
246: s = "false";
247: } else if (property.equalsIgnoreCase(AntProject.DEPLOY_INVOKE)) {
248: boolean checkInvoke = antFacade.checkDependedTarget("make",
249: "deploy");
250: if (checkInvoke)
251: s = "true";
252: else
253: s = "false";
254: } else if (property
255: .equalsIgnoreCase(AntProject.DEPLOY_WAR_ENHYDRA_LIBS)) {
256: String value = antFacade.getPropertyValue(property);
257: if (value
258: .equalsIgnoreCase(this .DEPLOY_WAR_ENHYDRA_LIBS_TRUE))
259: s = "true";
260: else
261: s = "false";
262: } else if (property
263: .equalsIgnoreCase(AntProject.OUTPUT_FILE_NAME)) {
264: s = outputFileName;
265: } else if (property
266: .equalsIgnoreCase(AntProject.XMLC_DEFAULT_PACKAGE)) {
267: s = defaultPackage;
268: } else if (s == null) {
269: if ((s = antFacade.getPropertyValue(property)) == null)
270: s = relativeToAbsolute(antFacade
271: .getPropertyLocation(property));
272: }
273: return s;
274:
275: }
276:
277: public void setProperty(String property, String value) {
278: if (property.equalsIgnoreCase(DEPLOY_INVOKE)) {
279: if (value.equalsIgnoreCase("true")) {
280: antFacade.addDependedTarget("make", "deploy");
281: } else {
282: antFacade.removeDependedTarget("make", "deploy");
283: }
284: } else if (property.equalsIgnoreCase(OUTPUT_DIR)) {
285: String relativePath = absoluteToRelative(this
286: .getWorkingPath(), value);
287: antFacade.setPropertyLocation(property, relativePath);
288: } else if (property.equalsIgnoreCase(DODS_DOML)) {
289: String relativePath = absoluteToRelative(this
290: .getWorkingPath(), value);
291: antFacade.setPropertyLocation(property, relativePath);
292: } else if (property.equalsIgnoreCase(DODS_ACTION)) {
293: antFacade.setPropertyValue(property, value);
294: } else if (property.equalsIgnoreCase(DODS_TEMPLATE_SET)) {
295: antFacade.setPropertyValue(property, value);
296: } else if (property.equalsIgnoreCase(INPUT_DIR)) {
297: String relativePath = absoluteToRelative(this
298: .getWorkingPath(), value);
299: antFacade.setPropertyLocation(property, relativePath);
300: } else if (property.equalsIgnoreCase(DEPLOY_INPUT_ENABLED)) {
301: if (value.equalsIgnoreCase("true")) {
302: antFacade.addDependedTarget("deploy", "input");
303: } else {
304: antFacade.removeDependedTarget("deploy", "input");
305: }
306: } else if (property.equalsIgnoreCase(DEPLOY_CONTENT_ENABLED)) {
307: if (value.equalsIgnoreCase("true")) {
308: antFacade.addDependedTarget("deploy", "content");
309: } else {
310: antFacade.removeDependedTarget("deploy", "content");
311: }
312: } else if (property.equalsIgnoreCase(DEPLOY_WAR_ENABLED)) {
313: if (value.equalsIgnoreCase("true")) {
314: antFacade.addDependedTarget("deploy", "makeWAR");
315: } else {
316: antFacade.removeDependedTarget("deploy", "makeWAR");
317: }
318: } else if (property.equalsIgnoreCase(DEPLOY_WAR_ENHYDRA_LIBS)) {
319: if (value.equalsIgnoreCase("true"))
320: antFacade.setPropertyValue(property,
321: DEPLOY_WAR_ENHYDRA_LIBS_TRUE);
322: else
323: antFacade.setPropertyValue(property,
324: DEPLOY_WAR_ENHYDRA_LIBS_FALSE);
325:
326: } else if (property.equalsIgnoreCase(XMLC_OPTIONS_FILE)) {
327: antFacade.setPropertyValue(property, value);
328: } else if (property.equalsIgnoreCase(XMLC_INVOKE)) {
329: if (value.equalsIgnoreCase("true")) {
330: antFacade.addDependedTarget("make", "xmlc");
331: } else {
332: antFacade.removeDependedTarget("make", "xmlc");
333: }
334: } else if (property.equalsIgnoreCase(DODS_INVOKE)) {
335: if (value.equalsIgnoreCase("true")) {
336: antFacade.addDependedTarget("make", "dods");
337: } else {
338: antFacade.removeDependedTarget("make", "dods");
339: }
340: } else if (property.equalsIgnoreCase(XMLC_VERBOSE)) {
341: antFacade.setPropertyValue(property, value);
342: } else if (property.equalsIgnoreCase(XMLC_PACKAGE_NAME)) {
343: antFacade.setPropertyValue(property, value);
344: } else if (property.equalsIgnoreCase(OUTPUT_FILE_NAME)) {
345: outputFileName = value;
346: } else if (property.equalsIgnoreCase(this .XMLC_INCLUDES)) {
347: antFacade.setPropertyValue(property, value);
348: }
349: //antFacade.setProperty(property,value);
350: }
351:
352: public void setProperty(String property, int value) {
353: setProperty(property, Integer.toString(value));
354: }
355:
356: public void setNativeNode(Object parm1) {
357: ;//Don't need NativeNode any more
358: }
359:
360: public String[] getSourcePathArray() {
361: //FIXME include /src and genereted sources dirs (xmlc & dods)
362: return null;
363: }
364:
365: public String getSourcePath() {
366: return getProperty(AntProject.SRC_DIR);
367: }
368:
369: public boolean isOpenBuild() {
370: return true;
371: /**@todo: implement this org.enhydra.kelp.common.node.OtterProject abstract method*/
372: }
373:
374: public String getClassPath() {
375: //return rootPath+"/classes";
376: return getProperty(AntProject.CLASS_PATH);//FIXME TODO MAYBE implement method getClassPath() in antFacade
377: }
378:
379: public boolean isDefaultProject() {
380: return false;
381: }
382:
383: public OtterTextFileNode[] getAllDeploymentDescs() {
384: return new OtterTextFileNode[0]; //FIXME
385:
386: }
387:
388: public Object getNativeProject() {
389: return null; //No native project for now
390: }
391:
392: public OtterNodeFactory getNodeFactory() {
393: return factory;
394:
395: }
396:
397: public OtterNode getParent() {
398: return null;
399: }
400:
401: public String getWorkingPath() {
402: return getProperty(NAME_WORKING_DIR); //FIXME for now workingPath = rootPath
403: }
404:
405: public void setWorkingPath(String path) {
406: setProperty(NAME_WORKING_DIR, path);
407: }
408:
409: public OtterProject getProject() {
410: return this ;
411: }
412:
413: public String getClassOutputPath() {
414: return getProperty(AntProject.CLASSES_DIR);
415: }
416:
417: public void setClassOutputPath(String p) {
418: setProperty(AntProject.CLASSES_DIR, p);
419: }
420:
421: public String getCodeGenDefaultDestination() {
422: return rootPath + "/presentation/src-generated"; //FIXME read from build.xml
423:
424: }
425:
426: public void setSourcePathArray(String[] parm1) {
427: /**@todo: implement this org.enhydra.kelp.common.node.OtterProject abstract method*/
428: }
429:
430: public OtterTextFileNode getFirstPolicy() {
431: return null;
432: /**@todo: implement this org.enhydra.kelp.common.node.OtterProject abstract method*/
433: }
434:
435: /**
436: * Converts relative paths from "foo" to "./foo" to comply with
437: * PathHandle.expandRelativePath()
438: */
439: private String relativePathAdapter(String path) {
440: String newPath = null;
441: File file = new File(path);
442: if (!file.isAbsolute())
443: newPath = "./" + path;
444: else
445: newPath = path;
446: return newPath;
447: }
448:
449: /**
450: * Make absolute path from path relative to working dir.
451: * @param relativePath Relative path
452: * @return Absolute path
453: */
454: private String relativeToAbsolute(String relativePath) {
455: if (relativePath != null) {
456: PathHandle workDirHandle = PathHandle
457: .createPathHandle(workingDir);
458: PathHandle absoluteHandle = workDirHandle
459: .expandRelativePath(relativePathAdapter(relativePath));
460: return absoluteHandle.getPath();
461: } else
462: return null;
463: }
464:
465: private String resourceRelativeToAbsolute(String relativePath) {
466: if (relativePath != null) {
467: String resourceDir = relativeToAbsolute(getProperty(this .RESOURCES_DIR));
468: PathHandle resourseHandle = PathHandle
469: .createPathHandle(resourceDir);
470: PathHandle absoluteHandle = resourseHandle
471: .expandRelativePath(relativePathAdapter(relativePath));
472: return absoluteHandle.getPath();
473: } else
474: return null;
475: }
476:
477: private String absoluteToRelative(String homePath, String inPath) {
478: PathHandle homePathHandle = PathHandle
479: .createPathHandle(homePath);
480: PathHandle inPathHandle = PathHandle.createPathHandle(inPath);
481: String relativePath = homePathHandle
482: .getRelativePath(inPathHandle);
483: //cut "./"
484: if (relativePath.startsWith("./"))
485: relativePath = relativePath.substring(2);
486: return relativePath;
487: }
488:
489: /**
490: * Read options from file
491: * @return lines from xmlc options file
492: */
493: public String[] getXMLCOptionsFileParams() {
494: String optFilePath = getProperty(AntProject.XMLC_OPTIONS_FILE);
495: optFilePath = relativePathAdapter(optFilePath);
496: PathHandle resourcesHandle = PathHandle
497: .createPathHandle(getProperty(AntProject.RESOURCES_DIR));
498: PathHandle optFileHandle = resourcesHandle
499: .expandRelativePath(optFilePath);
500: FileReader optFileReader = null;
501: BufferedReader br = null;
502: ArrayList optionsList = new ArrayList();
503: try {
504: optFileReader = new FileReader(optFileHandle.getFile());
505: br = new BufferedReader(optFileReader, 4096);
506: String optLine = br.readLine();
507: while (optLine != null) {
508: if (!optLine.equals(""))
509: optionsList.add(optLine);
510: optLine = br.readLine();
511: }
512: br.close();
513: } catch (IOException ex) {
514: ex.printStackTrace();
515: }
516: Object[] objs = optionsList.toArray();
517: String[] options = new String[objs.length];
518: for (int i = 0; i < objs.length; i++) {
519: options[i] = (String) objs[i];
520: }
521:
522: return options;
523: }
524:
525: /**
526: * Write options to file
527: * @param options XMLC options
528: */
529: public void setXMLCOptionsFileParams(String[] options) {
530: String optFilePath = getProperty(AntProject.XMLC_OPTIONS_FILE);
531: optFilePath = relativePathAdapter(optFilePath);
532: PathHandle resourcesHandle = PathHandle
533: .createPathHandle(getProperty(AntProject.RESOURCES_DIR));
534: PathHandle optFileHandle = resourcesHandle
535: .expandRelativePath(optFilePath);
536: FileWriter fw;
537: try {
538: fw = new FileWriter(optFileHandle.getFile());
539: } catch (IOException e) {
540: return;
541: }
542: BufferedWriter bw = new BufferedWriter(fw, 4096);
543: PrintWriter prw = new PrintWriter(bw, false /* auto flush */);
544: for (int i = 0; i < options.length; i++) {
545: prw.println(options[i]);
546: }
547: prw.println();
548: prw.flush();
549: prw.close();
550: }
551:
552: /**
553: *
554: * @return true if application type is web, otherwise it is enhydra app
555: */
556: public boolean isWebType() {
557: return webType;
558: }
559:
560: public String[] getExtensions(String patternsetId) {
561: String[] extensions = antFacade.getPatternset(patternsetId);
562: for (int i = 0; i < extensions.length; i++) {
563: extensions[i] = extensions[i].substring(extensions[i]
564: .indexOf("**/*.") + 5);
565: extensions[i] = extensions[i].trim();
566: }
567: return extensions;
568: }
569:
570: public void setExtensions(String patternsetId, String[] extensions) {
571: String[] expandedExts = new String[extensions.length];
572: for (int i = 0; i < extensions.length; i++) {
573: expandedExts[i] = "**/*." + extensions[i];
574: }
575: antFacade.replacePatternset(patternsetId, expandedExts);
576: }
577:
578: public HashMap getReplacements() {
579: return antFacade.getInputFilters();
580: }
581:
582: public void setReplacements(HashMap replacements) {
583: antFacade.replaceInputFilters(replacements);
584: }
585:
586: public boolean isOutputFileEnabled() {
587: return outputFileEnabled;
588: }
589:
590: public void setOutputFileEnabled(boolean enable) {
591: outputFileEnabled = enable;
592: }
593:
594: /**
595: * Method declaration
596: *
597: *
598: * @return
599: */
600: public String getOutputFilename() {
601: String out = null;
602:
603: out = getProperty(OUTPUT_FILE_NAME);
604: return out;
605: }
606:
607: /**
608: * Method declaration
609: *
610: *
611: * @param n
612: */
613: public void setOutputFilename(String n) {
614: setProperty(OUTPUT_FILE_NAME, n);
615: }
616: }
|