Source code: org/enhydra/kelp/common/PathUtil.java
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;
23
24 // ToolBox imports
25 import org.enhydra.tool.common.PathHandle;
26 import org.enhydra.tool.common.FileUtil;
27
28 // Kelp imports
29 import org.enhydra.kelp.common.map.Mapper;
30 import org.enhydra.kelp.common.importer.ResourceFilter;
31 import org.enhydra.kelp.common.node.OtterNode;
32 import org.enhydra.kelp.common.node.OtterNodeFactory;
33 import org.enhydra.kelp.common.node.OtterProject;
34 import org.enhydra.kelp.common.node.OtterTemplateNode;
35
36 // Standard imports
37 import java.io.File;
38 import java.io.FileFilter;
39 import java.util.Vector;
40
41 //
42 public class PathUtil {
43
44 // string not to be translated
45 private final static String UNTITLED = "untitled"; // nores
46 private final static String _PATH = "_PATH@"; // nores
47
48 //
49 public PathUtil() {}
50
51 public static boolean isInputInSource(OtterProject project) {
52 PathHandle inPath = null;
53 PathHandle srcPath = null;
54 String[] srcs = new String[0];
55 boolean inSource = false;
56
57 inPath = PathHandle.createPathHandle(project.getDeployInputPath());
58 srcs = project.getSourcePathArray();
59 for (int i = 0; i < srcs.length; i++) {
60 srcPath = PathHandle.createPathHandle(srcs[i]);
61 if (srcPath.parentOf(inPath) || srcPath.equals(inPath)) {
62 inSource = true;
63 break;
64 }
65 }
66 return inSource;
67 }
68
69 public static boolean isTemplate(OtterNode node) {
70 boolean temp = false;
71 PathHandle handleNode = null;
72 PathHandle handleDeploy = null;
73
74 handleNode = PathHandle.createPathHandle(node.getFilePath());
75 if (handleNode.hasExtension(Constants.TYPE_IN)) {
76 OtterProject project = node.getProject();
77
78 handleDeploy =
79 PathHandle.createPathHandle(project.getDeployInputPath());
80 if (handleDeploy.parentOf(handleNode)) {
81 temp = true;
82 }
83 }
84 return temp;
85 }
86
87 public static String getDeployArchivePath(OtterProject project) {
88 return getDeployArchivePath(project.getDeployRootPath(), project);
89 }
90
91 public static String getDeployArchivePath(String root,
92 OtterProject project) {
93 File f = new File(root);
94 StringBuffer path = new StringBuffer();
95
96 path.append(f.getAbsolutePath());
97 path.append(File.separator);
98 path.append(Constants.DIR_ARCHIVE);
99 path.append(File.separator);
100 if (project == null) {
101 path.append(PathUtil.UNTITLED);
102 } else {
103 path.append(project.getProjectName());
104 }
105 if (project == null || project.isWebApplication()) {
106 path.append('.');
107 path.append(Constants.TYPE_WAR);
108 } else {
109 path.append('.');
110 path.append(Constants.TYPE_JAR);
111 }
112 return PathHandle.createPathString(path.toString());
113 }
114
115 public static String[][] expandReplacementTable(OtterProject project,
116 String[][] table) {
117 for (int i = 0; i < table.length; i++) {
118 if (table[i][0].endsWith(PathUtil._PATH)) {
119 table[i][1] = PathUtil.expandPathRelativeToProject(project,
120 table[i][1]);
121 }
122 }
123 return table;
124 }
125
126 public static String getDeployConfPath(OtterProject project) {
127 return PathUtil.getDeployConfPath(project.getDeployRootPath());
128 }
129
130 public static String getDeployConfPath(String root) {
131 StringBuffer path = new StringBuffer();
132
133 path.append(root);
134 path.append(File.separator);
135 path.append(Constants.DIR_CONF);
136 return PathHandle.createPathString(path.toString());
137 }
138
139 public static String getDeployContentPath(OtterProject project) {
140 return PathUtil.getDeployContentPath(project,
141 project.getDeployRootPath());
142 }
143
144 public static String getDeployContentPath(OtterProject project,
145 String root) {
146 StringBuffer buf = new StringBuffer();
147 String path = new String();
148 Mapper mapper = null;
149
150 if (project.isWebApplication()) {
151 buf.append(root);
152 buf.append(File.separator);
153 buf.append(Constants.DIR_CONTENT);
154 path = PathHandle.createPathString(buf.toString());
155 } else {
156 String resourcePath = new String();
157 String sourcePath = new String();
158
159 resourcePath = project.getDeployResourcePath();
160 sourcePath = PathUtil.getSourcePathOf(project, resourcePath);
161 mapper = new Mapper(project);
162 path = mapper.getMappedOutputPath(sourcePath, resourcePath);
163 }
164 return path;
165 }
166
167 public static String getDefaultDeployResourcePath(OtterProject project,
168 String appPackPath, String sourcePath) {
169 String path = sourcePath;
170 StringBuffer searchRootPath = new StringBuffer();
171 File firstFound = null;
172 ResourceFilter resourceFilter = null;
173
174 resourceFilter = new ResourceFilter();
175 resourceFilter.setResourcePath(true);
176 resourceFilter.setProject(project);
177 searchRootPath.append(sourcePath);
178 searchRootPath.append(File.separator);
179 searchRootPath.append(appPackPath);
180 firstFound = FileUtil.findFirst((FileFilter) resourceFilter,
181 searchRootPath.toString());
182 if (firstFound == null) {
183 resourceFilter.setResourcePath(false);
184 firstFound = FileUtil.findFirst((FileFilter) resourceFilter,
185 searchRootPath.toString());
186 }
187 if (firstFound != null) {
188 path = firstFound.getParent();
189 }
190 return PathHandle.createPathString(path);
191 }
192
193 /**
194 * Method declaration
195 *
196 *
197 * @param node
198 * @param propertyName
199 * @param inFilename
200 *
201 * @see
202 */
203 public static void putFileRelativeToProject(OtterNode node,
204 String propertyName,
205 String inFilename) {
206 String outFilename = compressPathRelativeToProject(node, inFilename);
207
208 node.setProperty(propertyName, outFilename);
209 }
210
211 /**
212 * Method declaration
213 *
214 *
215 * @param node
216 * @param propertyName
217 *
218 * @return
219 *
220 * @see
221 */
222 public static File getFileRelativeToProject(OtterNode node,
223 String propertyName) {
224 File file = null;
225 String filename = node.getProperty(propertyName);
226
227 if ((filename != null) && (filename.trim().length() > 0)) {
228 filename = PathUtil.expandPathRelativeToProject(node, filename);
229 file = new File(filename);
230 }
231 return file;
232 }
233
234 /**
235 * Method declaration
236 *
237 *
238 * @param node
239 * @param inPath
240 *
241 * @return
242 *
243 * @see
244 */
245 public static String compressPathRelativeToProject(OtterNode node,
246 String inPath) {
247 StringBuffer outPath = new StringBuffer();
248 String absolutePath = null;
249
250 if ((inPath.length() > 0) && (inPath.charAt(0) == '.')) {
251 outPath.append(inPath);
252 } else {
253 File file = new File(node.getProject().getFilePath());
254 PathHandle projectParent = null;
255
256 projectParent = PathHandle.createPathHandle(file.getParent());
257 file = new File(inPath);
258 absolutePath = file.getAbsolutePath().trim();
259 if (absolutePath.length() > inPath.trim().length()) {
260 outPath.append(inPath);
261
262 // Not a valid path.
263 } else {
264 if (projectParent.parentOf(absolutePath)) {
265 if (absolutePath.length()
266 == projectParent.getPath().length()) {
267 outPath.append('.');
268 outPath.append(File.separator);
269 } else {
270 outPath.append('.');
271 outPath.append(File.separator);
272 outPath.append(absolutePath.substring(projectParent.getPath().length()
273 + 1));
274 }
275 } else {
276 outPath.append(absolutePath);
277 }
278 }
279 }
280
281 // Return with separators set to / for any OS but don't do
282 // path handle to avoid decompress.
283 return outPath.toString().replace('\\', '/');
284 }
285
286 /**
287 * Method declaration
288 *
289 *
290 * @param node
291 * @param inPath
292 *
293 * @return
294 *
295 * @see
296 */
297 public static String expandPathRelativeToProject(OtterNode node,
298 String inPath) {
299 StringBuffer outPath = new StringBuffer();
300 File projectFile = null;
301
302 // Set separators to current OS
303 inPath = inPath.replace('\\', File.separatorChar);
304 inPath = inPath.replace('/', File.separatorChar);
305 if ((inPath.length() == 1) && (inPath.charAt(0) == '.')) {
306 projectFile = new File(node.getProject().getFilePath());
307 outPath.append(projectFile.getParent());
308 } else if ((inPath.length() > 0) && (inPath.charAt(0) == '.')) {
309
310 // expand path relative to the project.
311 projectFile = new File(node.getProject().getFilePath());
312 outPath.append(projectFile.getParent());
313 if (!projectFile.getParent().endsWith(File.separator)) {
314 outPath.append(File.separator);
315 }
316 if ((inPath.length() > 1) && (inPath.charAt(1) == '.')) {
317 outPath.append(inPath);
318 } else {
319 outPath.append(inPath.substring(2));
320 }
321
322 } else {
323 outPath.append(inPath);
324 }
325 return PathHandle.createPathString(outPath.toString());
326 }
327
328 public static OtterTemplateNode[] getInputTemplates(OtterProject project) {
329 OtterNode[] input = null;
330 OtterTemplateNode[] templates = null;
331 OtterNodeFactory factory = null;
332 Vector tempVector = new Vector();
333
334 input = project.getAllInput();
335 factory = project.getNodeFactory();
336 for (int i = 0; i < input.length; i++) {
337 PathHandle cursor = null;
338
339 cursor = PathHandle.createPathHandle(input[i].getFilePath());
340 if (cursor.hasExtension(Constants.TYPE_IN)) {
341 OtterTemplateNode newNode = null;
342
343 newNode = factory.getTemplateNode(input[i]);
344 tempVector.addElement(newNode);
345 }
346 }
347 templates = new OtterTemplateNode[tempVector.size()];
348 templates = (OtterTemplateNode[]) tempVector.toArray(templates);
349 tempVector.clear();
350 return templates;
351 }
352
353 public static OtterNode[] getInputPassthrough(OtterProject project) {
354 OtterNode[] input = null;
355 OtterNode[] pass = null;
356 Vector passVector = new Vector();
357
358 input = project.getAllInput();
359 for (int i = 0; i < input.length; i++) {
360 PathHandle cursor = null;
361
362 cursor = PathHandle.createPathHandle(input[i].getFilePath());
363 if (cursor.hasExtension(Constants.TYPE_IN)) {
364
365 // skip templates
366 } else {
367 passVector.addElement(input[i]);
368 }
369 }
370 pass = new OtterNode[passVector.size()];
371 pass = (OtterNode[]) passVector.toArray(pass);
372 passVector.clear();
373 return pass;
374 }
375
376 public static String getSourcePathOf(OtterProject project,
377 String sourceFile) {
378 String[] path = project.getSourcePathArray();
379 PathHandle sourcePath = null;
380 PathHandle filePath = null;
381 PathHandle parentPath = null;
382
383 filePath = PathHandle.createPathHandle(sourceFile);
384 sourcePath = filePath.getParent();
385 for (int i = 0; i < path.length; i++) {
386 parentPath = PathHandle.createPathHandle(path[i]);
387 if (parentPath.parentOf(filePath)) {
388 sourcePath.setPath(parentPath.getPath());
389 break;
390 }
391 }
392 return sourcePath.getPath();
393 }
394
395 }