KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > PathUtil


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  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common;
24
25 // ToolBox
26
import org.enhydra.tool.ToolBoxInfo;
27 import org.enhydra.tool.common.PathHandle;
28 import org.enhydra.tool.common.FileUtil;
29
30 // AddinCore
31
import org.enhydra.kelp.common.map.Mapper;
32 import org.enhydra.kelp.common.importer.ResourceFilter;
33 import org.enhydra.kelp.common.node.OtterNode;
34 import org.enhydra.kelp.common.node.OtterFileNode;
35 import org.enhydra.kelp.common.node.OtterNodeFactory;
36 import org.enhydra.kelp.common.node.OtterProject;
37 import org.enhydra.kelp.common.node.OtterTemplateNode;
38
39 // JDK
40
import java.io.File JavaDoc;
41 import java.io.FileFilter JavaDoc;
42 import java.util.ArrayList JavaDoc;
43
44 //
45
public class PathUtil {
46
47     // string not to be translated
48
private final static String JavaDoc UNTITLED = "untitled"; // nores
49
private final static String JavaDoc _PATH = "_PATH@"; // nores
50

51     //
52
public PathUtil() {}
53
54     public static boolean isInputInSource(OtterProject project) {
55         PathHandle inPath = null;
56         PathHandle srcPath = null;
57         String JavaDoc[] srcs = new String JavaDoc[0];
58         boolean inSource = false;
59
60         inPath = PathHandle.createPathHandle(project.getDeployInputPath());
61         srcs = project.getSourcePathArray();
62         for (int i = 0; i < srcs.length; i++) {
63             srcPath = PathHandle.createPathHandle(srcs[i]);
64             if (srcPath.parentOf(inPath) || srcPath.equals(inPath)) {
65                 inSource = true;
66                 break;
67             }
68         }
69         return inSource;
70     }
71
72     public static boolean isTemplate(OtterFileNode node) {
73         boolean temp = false;
74         PathHandle handleNode = null;
75         PathHandle handleDeploy = null;
76
77         handleNode = PathHandle.createPathHandle(node.getFilePath());
78         if (handleNode.hasExtension(Constants.TYPE_IN)) {
79             OtterProject project = node.getProject();
80
81             handleDeploy =
82                 PathHandle.createPathHandle(project.getDeployInputPath());
83             if (handleDeploy.parentOf(handleNode)) {
84                 temp = true;
85             }
86         }
87         return temp;
88     }
89
90     public static String JavaDoc getDefaultDeployArchivePath(OtterProject project) {
91         File JavaDoc f = null;
92         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
93 //Dusan
94
int type = OtterProject.TYPE_UNKNOWN;
95
96         f = new File JavaDoc(project.getDeployRootPath());
97         type = project.getDeployType();
98         path.append(f.getAbsolutePath());
99         path.append(File.separator);
100         path.append(Constants.DIR_LIB);
101         path.append(File.separator);
102         if (project == null) {
103             path.append(PathUtil.UNTITLED);
104         } else {
105             path.append(project.getProjectRootName());
106         }
107         path.append('.');
108         switch (type) {
109         case OtterProject.TYPE_WEBAPP:
110             path.append(Constants.TYPE_WAR);
111             break;
112         case OtterProject.TYPE_EN3APP:
113 //Dusan
114
// case OtterProject.TYPE_SERVICE:
115
path.append(Constants.TYPE_JAR);
116             break;
117         case OtterProject.TYPE_UNKNOWN:
118             path.append(Constants.TYPE_JAR);
119             break;
120         }
121         return PathHandle.createPathString(path.toString());
122     }
123
124     public static String JavaDoc[][] expandReplacementTable(OtterProject project,
125             String JavaDoc[][] table) {
126         for (int i = 0; i < table.length; i++) {
127             if (table[i][0].endsWith(PathUtil._PATH)) {
128                 table[i][1] = PathUtil.expandPathRelativeToProject(project,
129                         table[i][1]);
130             }
131         }
132         return table;
133     }
134
135     public static String JavaDoc getDefaultDeployBootstrapPath(OtterProject project) {
136         String JavaDoc conf = PathUtil.getDeployConfPath(project);
137         String JavaDoc def = new String JavaDoc();
138         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
139         PathHandle ph = null;
140
141         if (ToolBoxInfo.isEnhydra3()) {
142             path.append(conf);
143             path.append(File.separator);
144             path.append(Constants.FILE_MULTISERVER_CONF);
145 //Dusan
146
/* } else if (project.getDeployType() == OtterProject.TYPE_SERVICE) {
147             path.append(PathUtil.getAutoDeployPath(OtterProject.TYPE_SERVICE));
148         } else { */

149         } else {
150             path.append(conf);
151             path.append(File.separator);
152             path.append(Constants.FILE_BOOTSTRAP_CONF);
153         }
154         ph = PathHandle.createPathHandle(path.toString());
155         if (!ph.isFile()) {
156             ph.createPathHandle(Backward.createDefaultBootstrapPath(path.toString(),
157                     project));
158         }
159         return ph.getPath();
160     }
161
162     public static String JavaDoc getDeployConfPath(OtterProject project) {
163         return PathUtil.getDeployConfPath(project.getDeployRootPath());
164     }
165
166     public static String JavaDoc getDeployConfPath(String JavaDoc root) {
167         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
168
169         path.append(root);
170         path.append(File.separator);
171         path.append(Constants.DIR_CONF);
172         return PathHandle.createPathString(path.toString());
173     }
174
175     public static String JavaDoc getDeployContentPath(OtterProject project) {
176         return PathUtil.getDeployContentPath(project,
177                                              project.getDeployRootPath());
178     }
179
180     public static String JavaDoc getDeployContentPath(OtterProject project,
181                                               String JavaDoc root) {
182         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
183         String JavaDoc path = new String JavaDoc();
184         Mapper mapper = null;
185         int type = OtterProject.TYPE_UNKNOWN;
186
187         if (project != null) {
188             type = project.getDeployType();
189         }
190         switch (type) {
191 //Dusan
192
/* case OtterProject.TYPE_SERVICE:
193             path = project.getClassOutputPath();
194             break; */

195         case OtterProject.TYPE_EN3APP:
196             String JavaDoc resourcePath = new String JavaDoc();
197             String JavaDoc sourcePath = new String JavaDoc();
198
199             resourcePath = project.getDeployResourcePath();
200             sourcePath = PathUtil.getSourcePathOf(project, resourcePath);
201             mapper = new Mapper(project);
202             path = mapper.getMappedOutputPath(sourcePath, resourcePath);
203             break;
204         default:
205             buf.append(root);
206             buf.append(File.separator);
207             buf.append(Constants.DIR_CONTENT);
208             path = buf.toString();
209             break;
210         }
211         return PathHandle.createPathString(path);
212     }
213
214     public static String JavaDoc getDefaultDeployResourcePath(OtterProject project,
215             String JavaDoc appPackPath, String JavaDoc sourcePath) {
216         String JavaDoc path = sourcePath;
217         StringBuffer JavaDoc searchRootPath = new StringBuffer JavaDoc();
218         File JavaDoc firstFound = null;
219         ResourceFilter resourceFilter = null;
220
221         resourceFilter = new ResourceFilter();
222         resourceFilter.setResourcePath(true);
223         resourceFilter.setProject(project);
224         searchRootPath.append(sourcePath);
225         searchRootPath.append(File.separator);
226         searchRootPath.append(appPackPath);
227         firstFound = FileUtil.findFirst((FileFilter JavaDoc) resourceFilter,
228                                         searchRootPath.toString());
229         if (firstFound == null) {
230             resourceFilter.setResourcePath(false);
231             firstFound = FileUtil.findFirst((FileFilter JavaDoc) resourceFilter,
232                                             searchRootPath.toString());
233         }
234         if (firstFound != null) {
235             path = firstFound.getParent();
236         }
237         return PathHandle.createPathString(path);
238     }
239
240     /**
241      * Method declaration
242      *
243      *
244      * @param node
245      * @param propertyName
246      * @param inFilename
247      *
248      * @see
249      */

250     public static void putFileRelativeToProject(OtterNode node,
251                                                 String JavaDoc propertyName,
252                                                 String JavaDoc inFilename) {
253         String JavaDoc outFilename = compressPathRelativeToProject(node, inFilename);
254
255         node.setProperty(propertyName, outFilename);
256     }
257
258     /**
259      * Method declaration
260      *
261      *
262      * @param node
263      * @param propertyName
264      *
265      * @return
266      *
267      * @see
268      */

269     public static File JavaDoc getFileRelativeToProject(OtterNode node,
270                                                 String JavaDoc propertyName) {
271         File JavaDoc file = null;
272         String JavaDoc filename = node.getProperty(propertyName);
273
274         if ((filename != null) && (filename.trim().length() > 0)) {
275             filename = PathUtil.expandPathRelativeToProject(node, filename);
276             file = new File JavaDoc(filename);
277         }
278         return file;
279     }
280
281     /**
282      * Method declaration
283      *
284      *
285      * @param node
286      * @param inPath
287      *
288      * @return
289      *
290      * @see
291      */

292     public static String JavaDoc compressPathRelativeToProject(OtterNode node,
293             String JavaDoc in) {
294         PathHandle ph = null;
295         String JavaDoc out = null;
296
297         ph = PathHandle.createPathHandle(node.getProject().getRootPath());
298         out = ph.getRelativePath(in);
299         return out;
300     }
301
302     /**
303      * Method declaration
304      *
305      *
306      * @param node
307      * @param inPath
308      *
309      * @return
310      *
311      * @see
312      */

313     public static String JavaDoc expandPathRelativeToProject(OtterNode node,
314             String JavaDoc inPath) {
315         PathHandle ph = null;
316
317         ph = PathHandle.createPathHandle(node.getProject().getRootPath());
318         ph = ph.expandRelativePath(inPath);
319         return ph.getPath();
320     }
321
322     public static OtterTemplateNode[] getInputTemplates(OtterProject project) {
323         OtterFileNode[] input = null;
324         OtterTemplateNode[] templates = null;
325         OtterNodeFactory factory = null;
326         ArrayList JavaDoc list = new ArrayList JavaDoc();
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.createPathHandle(input[i].getFilePath());
334             if (cursor.hasExtension(Constants.TYPE_IN)) {
335                 OtterTemplateNode newNode = null;
336
337                 newNode = factory.getTemplateNode(input[i]);
338                 list.add(newNode);
339             }
340         }
341         list.trimToSize();
342         templates = new OtterTemplateNode[list.size()];
343         templates = (OtterTemplateNode[]) list.toArray(templates);
344         list.clear();
345         return templates;
346     }
347
348     public static OtterFileNode[] getInputPassthrough(OtterProject project) {
349         OtterFileNode[] input = null;
350         OtterFileNode[] pass = null;
351         ArrayList JavaDoc list = new ArrayList JavaDoc();
352
353         input = project.getAllInput();
354         for (int i = 0; i < input.length; i++) {
355             PathHandle cursor = null;
356
357             cursor = PathHandle.createPathHandle(input[i].getFilePath());
358             if (cursor.hasExtension(Constants.TYPE_IN)) {
359
360                 // skip templates
361
} else {
362                 list.add(input[i]);
363             }
364         }
365         list.trimToSize();
366         pass = new OtterFileNode[list.size()];
367         pass = (OtterFileNode[]) list.toArray(pass);
368         list.clear();
369         return pass;
370     }
371
372     public static String JavaDoc getSourcePathOf(OtterProject project,
373                                          String JavaDoc sourceFile) {
374         String JavaDoc[] path = project.getSourcePathArray();
375         PathHandle sourcePath = null;
376         PathHandle filePath = null;
377         PathHandle parentPath = null;
378
379         filePath = PathHandle.createPathHandle(sourceFile);
380         sourcePath = filePath.getParent();
381         for (int i = 0; i < path.length; i++) {
382             parentPath = PathHandle.createPathHandle(path[i]);
383             if (parentPath.parentOf(filePath)) {
384                 sourcePath.setPath(parentPath.getPath());
385                 break;
386             }
387         }
388         return sourcePath.getPath();
389     }
390
391     public static String JavaDoc getOutputPath(OtterTemplateNode node) {
392         PathHandle phInput = null;
393         PathHandle phOut = null;
394         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
395         String JavaDoc thisPath = null;
396         OtterProject project = null;
397
398         project = node.getProject();
399         phInput = PathHandle.createPathHandle(project.getDeployInputPath());
400         thisPath = node.getFilePath();
401         path.append(project.getDeployRootPath());
402         if (phInput.parentOf(thisPath)) {
403             int start = project.getDeployInputPath().length();
404             int end = thisPath.length() - 3;
405
406             path.append(thisPath.substring(start, end));
407         } else {
408             File JavaDoc thisFile = new File JavaDoc(thisPath);
409
410             path.append(File.separator);
411             path.append(thisFile.getName().substring(0,
412                                                      thisFile.getName().length()
413                                                      - 3));
414         }
415         phOut = PathHandle.createPathHandle(path.toString());
416         return phOut.getPath();
417     }
418
419     public static String JavaDoc getAutoDeployPath(int type) {
420         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
421
422         buf.append(ToolBoxInfo.getEnhydraRoot());
423         buf.append(File.separator);
424         if (type == OtterProject.TYPE_EN3APP) {
425             buf.append("apps");
426         } else {
427             buf.append("deploy");
428         }
429         return PathHandle.createPathString(buf.toString());
430     }
431
432     public static String JavaDoc getAutoDodsPath(int type) {
433         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
434
435         buf.append(ToolBoxInfo.getEnhydraRoot());
436         buf.append(File.separator);
437         if (type == OtterProject.TYPE_EN3APP) {
438             buf.append("apps");
439         } else {
440             buf.append("dods");
441         }
442         return PathHandle.createPathString(buf.toString());
443     }
444
445 }
446
Popular Tags