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: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common;
024:
025: // ToolBox
026: import org.enhydra.tool.ToolBoxInfo;
027: import org.enhydra.tool.common.PathHandle;
028: import org.enhydra.tool.common.FileUtil;
029:
030: // AddinCore
031: import org.enhydra.kelp.common.map.Mapper;
032: import org.enhydra.kelp.common.importer.ResourceFilter;
033: import org.enhydra.kelp.common.node.OtterNode;
034: import org.enhydra.kelp.common.node.OtterFileNode;
035: import org.enhydra.kelp.common.node.OtterNodeFactory;
036: import org.enhydra.kelp.common.node.OtterProject;
037: import org.enhydra.kelp.common.node.OtterTemplateNode;
038:
039: // JDK
040: import java.io.File;
041: import java.io.FileFilter;
042: import java.util.ArrayList;
043:
044: //
045: public class PathUtil {
046:
047: // string not to be translated
048: private final static String UNTITLED = "untitled"; // nores
049: private final static String _PATH = "_PATH@"; // nores
050:
051: //
052: public PathUtil() {
053: }
054:
055: public static boolean isInputInSource(OtterProject project) {
056: PathHandle inPath = null;
057: PathHandle srcPath = null;
058: String[] srcs = new String[0];
059: boolean inSource = false;
060:
061: inPath = PathHandle.createPathHandle(project
062: .getDeployInputPath());
063: srcs = project.getSourcePathArray();
064: for (int i = 0; i < srcs.length; i++) {
065: srcPath = PathHandle.createPathHandle(srcs[i]);
066: if (srcPath.parentOf(inPath) || srcPath.equals(inPath)) {
067: inSource = true;
068: break;
069: }
070: }
071: return inSource;
072: }
073:
074: public static boolean isTemplate(OtterFileNode node) {
075: boolean temp = false;
076: PathHandle handleNode = null;
077: PathHandle handleDeploy = null;
078:
079: handleNode = PathHandle.createPathHandle(node.getFilePath());
080: if (handleNode.hasExtension(Constants.TYPE_IN)) {
081: OtterProject project = node.getProject();
082:
083: handleDeploy = PathHandle.createPathHandle(project
084: .getDeployInputPath());
085: if (handleDeploy.parentOf(handleNode)) {
086: temp = true;
087: }
088: }
089: return temp;
090: }
091:
092: public static String getDefaultDeployArchivePath(
093: OtterProject project) {
094: File f = null;
095: StringBuffer path = new StringBuffer();
096: //Dusan
097: int type = OtterProject.TYPE_UNKNOWN;
098:
099: f = new File(project.getDeployRootPath());
100: type = project.getDeployType();
101: path.append(f.getAbsolutePath());
102: path.append(File.separator);
103: path.append(Constants.DIR_LIB);
104: path.append(File.separator);
105: if (project == null) {
106: path.append(PathUtil.UNTITLED);
107: } else {
108: path.append(project.getProjectRootName());
109: }
110: path.append('.');
111: switch (type) {
112: case OtterProject.TYPE_WEBAPP:
113: path.append(Constants.TYPE_WAR);
114: break;
115: case OtterProject.TYPE_EN3APP:
116: //Dusan
117: // case OtterProject.TYPE_SERVICE:
118: path.append(Constants.TYPE_JAR);
119: break;
120: case OtterProject.TYPE_UNKNOWN:
121: path.append(Constants.TYPE_JAR);
122: break;
123: }
124: return PathHandle.createPathString(path.toString());
125: }
126:
127: public static String[][] expandReplacementTable(
128: OtterProject project, String[][] table) {
129: for (int i = 0; i < table.length; i++) {
130: if (table[i][0].endsWith(PathUtil._PATH)) {
131: table[i][1] = PathUtil.expandPathRelativeToProject(
132: project, table[i][1]);
133: }
134: }
135: return table;
136: }
137:
138: public static String getDefaultDeployBootstrapPath(
139: OtterProject project) {
140: String conf = PathUtil.getDeployConfPath(project);
141: StringBuffer path = new StringBuffer();
142: PathHandle ph = null;
143:
144: path.append(conf);
145: path.append(File.separator);
146: path.append(Constants.FILE_BOOTSTRAP_CONF);
147:
148: ph = PathHandle.createPathHandle(path.toString());
149: if (!ph.isFile()) {
150: ph.createPathHandle(Backward.createDefaultBootstrapPath(
151: path.toString(), project));
152: }
153: return ph.getPath();
154: }
155:
156: public static String getDeployConfPath(OtterProject project) {
157: return PathUtil.getDeployConfPath(project.getDeployRootPath());
158: }
159:
160: public static String getDeployConfPath(String root) {
161: StringBuffer path = new StringBuffer();
162:
163: path.append(root);
164: path.append(File.separator);
165: path.append(Constants.DIR_CONF);
166: return PathHandle.createPathString(path.toString());
167: }
168:
169: public static String getDeployContentPath(OtterProject project) {
170: return PathUtil.getDeployContentPath(project, project
171: .getDeployRootPath());
172: }
173:
174: public static String getDeployContentPath(OtterProject project,
175: String root) {
176: StringBuffer buf = new StringBuffer();
177: String path = new String();
178: Mapper mapper = null;
179: int type = OtterProject.TYPE_UNKNOWN;
180:
181: if (project != null) {
182: type = project.getDeployType();
183: }
184: switch (type) {
185: //Dusan
186: /* case OtterProject.TYPE_SERVICE:
187: path = project.getClassOutputPath();
188: break; */
189: case OtterProject.TYPE_EN3APP:
190: String resourcePath = new String();
191: String sourcePath = new String();
192:
193: resourcePath = project.getDeployResourcePath();
194: sourcePath = PathUtil
195: .getSourcePathOf(project, resourcePath);
196: mapper = new Mapper(project);
197: path = mapper.getMappedOutputPath(sourcePath, resourcePath);
198: break;
199: default:
200: buf.append(root);
201: buf.append(File.separator);
202: buf.append(Constants.DIR_CONTENT);
203: path = buf.toString();
204: break;
205: }
206: return PathHandle.createPathString(path);
207: }
208:
209: public static String getDefaultDeployResourcePath(
210: OtterProject project, String appPackPath, String sourcePath) {
211: String path = sourcePath;
212: StringBuffer searchRootPath = new StringBuffer();
213: File firstFound = null;
214: ResourceFilter resourceFilter = null;
215:
216: resourceFilter = new ResourceFilter();
217: resourceFilter.setResourcePath(true);
218: resourceFilter.setProject(project);
219: searchRootPath.append(sourcePath);
220: searchRootPath.append(File.separator);
221: searchRootPath.append(appPackPath);
222: firstFound = FileUtil.findFirst((FileFilter) resourceFilter,
223: searchRootPath.toString());
224: if (firstFound == null) {
225: resourceFilter.setResourcePath(false);
226: firstFound = FileUtil.findFirst(
227: (FileFilter) resourceFilter, searchRootPath
228: .toString());
229: }
230: if (firstFound != null) {
231: path = firstFound.getParent();
232: }
233: return PathHandle.createPathString(path);
234: }
235:
236: /**
237: * Method declaration
238: *
239: *
240: * @param node
241: * @param propertyName
242: * @param inFilename
243: *
244: * @see
245: */
246: public static void putFileRelativeToProject(OtterNode node,
247: String propertyName, String inFilename) {
248: String outFilename = compressPathRelativeToProject(node,
249: inFilename);
250:
251: node.setProperty(propertyName, outFilename);
252: }
253:
254: /**
255: * Method declaration
256: *
257: *
258: * @param node
259: * @param propertyName
260: *
261: * @return
262: *
263: * @see
264: */
265: public static File getFileRelativeToProject(OtterNode node,
266: String propertyName) {
267: File file = null;
268: String filename = node.getProperty(propertyName);
269:
270: if ((filename != null) && (filename.trim().length() > 0)) {
271: filename = PathUtil.expandPathRelativeToProject(node,
272: filename);
273: file = new File(filename);
274: }
275: return file;
276: }
277:
278: /**
279: * Method declaration
280: *
281: *
282: * @param node
283: * @param inPath
284: *
285: * @return
286: *
287: * @see
288: */
289: public static String compressPathRelativeToProject(OtterNode node,
290: String in) {
291: PathHandle ph = null;
292: String out = null;
293:
294: ph = PathHandle.createPathHandle(node.getProject()
295: .getRootPath());
296: out = ph.getRelativePath(in);
297: return out;
298: }
299:
300: /**
301: * Method declaration
302: *
303: *
304: * @param node
305: * @param inPath
306: *
307: * @return
308: *
309: * @see
310: */
311: public static String expandPathRelativeToProject(OtterNode node,
312: String inPath) {
313: PathHandle ph = null;
314:
315: ph = PathHandle.createPathHandle(node.getProject()
316: .getRootPath());
317: ph = ph.expandRelativePath(inPath);
318: return ph.getPath();
319: }
320:
321: public static OtterTemplateNode[] getInputTemplates(
322: OtterProject project) {
323: OtterFileNode[] input = null;
324: OtterTemplateNode[] templates = null;
325: OtterNodeFactory factory = null;
326: ArrayList list = new ArrayList();
327:
328: input = project.getAllInput();
329: factory = project.getNodeFactory();
330: for (int i = 0; i < input.length; i++) {
331: PathHandle cursor = null;
332:
333: cursor = PathHandle
334: .createPathHandle(input[i].getFilePath());
335: if (cursor.hasExtension(Constants.TYPE_IN)) {
336: OtterTemplateNode newNode = null;
337:
338: newNode = factory.getTemplateNode(input[i]);
339: list.add(newNode);
340: }
341: }
342: list.trimToSize();
343: templates = new OtterTemplateNode[list.size()];
344: templates = (OtterTemplateNode[]) list.toArray(templates);
345: list.clear();
346: return templates;
347: }
348:
349: public static OtterFileNode[] getInputPassthrough(
350: OtterProject project) {
351: OtterFileNode[] input = null;
352: OtterFileNode[] pass = null;
353: ArrayList list = new ArrayList();
354:
355: input = project.getAllInput();
356: for (int i = 0; i < input.length; i++) {
357: PathHandle cursor = null;
358:
359: cursor = PathHandle
360: .createPathHandle(input[i].getFilePath());
361: if (cursor.hasExtension(Constants.TYPE_IN)) {
362:
363: // skip templates
364: } else {
365: list.add(input[i]);
366: }
367: }
368: list.trimToSize();
369: pass = new OtterFileNode[list.size()];
370: pass = (OtterFileNode[]) list.toArray(pass);
371: list.clear();
372: return pass;
373: }
374:
375: public static String getSourcePathOf(OtterProject project,
376: String sourceFile) {
377: String[] path = project.getSourcePathArray();
378: PathHandle sourcePath = null;
379: PathHandle filePath = null;
380: PathHandle parentPath = null;
381:
382: filePath = PathHandle.createPathHandle(sourceFile);
383: sourcePath = filePath.getParent();
384: for (int i = 0; i < path.length; i++) {
385: parentPath = PathHandle.createPathHandle(path[i]);
386: if (parentPath.parentOf(filePath)) {
387: sourcePath.setPath(parentPath.getPath());
388: break;
389: }
390: }
391: return sourcePath.getPath();
392: }
393:
394: public static String getOutputPath(OtterTemplateNode node) {
395: PathHandle phInput = null;
396: PathHandle phOut = null;
397: StringBuffer path = new StringBuffer();
398: String this Path = null;
399: OtterProject project = null;
400:
401: project = node.getProject();
402: phInput = PathHandle.createPathHandle(project
403: .getDeployInputPath());
404: this Path = node.getFilePath();
405: path.append(project.getDeployRootPath());
406: if (phInput.parentOf(this Path)) {
407: int start = project.getDeployInputPath().length();
408: int end = this Path.length() - 3;
409:
410: path.append(this Path.substring(start, end));
411: } else {
412: File this File = new File(this Path);
413:
414: path.append(File.separator);
415: path.append(this File.getName().substring(0,
416: this File.getName().length() - 3));
417: }
418: phOut = PathHandle.createPathHandle(path.toString());
419: return phOut.getPath();
420: }
421:
422: public static String getAutoDeployPath(int type) {
423: StringBuffer buf = new StringBuffer();
424:
425: buf.append(ToolBoxInfo.getEnhydraRoot());
426: buf.append(File.separator);
427: if (type == OtterProject.TYPE_EN3APP) {
428: buf.append("apps");
429: } else {
430: buf.append("deploy");
431: }
432: return PathHandle.createPathString(buf.toString());
433: }
434:
435: public static String getAutoDodsPath(int type) {
436: StringBuffer buf = new StringBuffer();
437:
438: buf.append(ToolBoxInfo.getEnhydraRoot());
439: buf.append(File.separator);
440: if (type == OtterProject.TYPE_EN3APP) {
441: buf.append("apps");
442: } else {
443: buf.append("dods");
444: }
445: return PathHandle.createPathString(buf.toString());
446: }
447:
448: }
|