KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > importer > ImportPaths


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.importer;
24
25 // ToolBox
26
import org.enhydra.tool.common.FileUtil;
27 import org.enhydra.tool.common.FilenameFilter;
28 import org.enhydra.tool.common.PathHandle;
29
30 // AddinCore
31
import org.enhydra.kelp.common.Constants;
32 import org.enhydra.kelp.common.PathUtil;
33 import org.enhydra.kelp.common.ValidationException;
34 import org.enhydra.kelp.common.ValidationUtil;
35 import org.enhydra.kelp.common.map.MapEntry;
36 import org.enhydra.kelp.common.map.MapUtil;
37 import org.enhydra.kelp.common.node.OtterProject;
38 import org.enhydra.kelp.common.node.PropertyKeys;
39
40 // JDK
41
import java.io.File JavaDoc;
42 import java.util.ResourceBundle JavaDoc;
43
44 //
45
public class ImportPaths implements PropertyKeys {
46
47     // strings not to be translated
48
static ResourceBundle JavaDoc res =
49         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
50
private final String JavaDoc PRESENTATION = "presentation"; // nores
51
private final String JavaDoc[] PACKAGE_KEYS = {
52         ".business", ".data", '.' + PRESENTATION
53     }; // nores
54

55     //
56
private OtterProject project = null;
57     private String JavaDoc pack = new String JavaDoc();
58     private String JavaDoc[][] packMap = new String JavaDoc[0][2];
59     private String JavaDoc deployRootPath = null;
60     private String JavaDoc resourcePath = null;
61     private String JavaDoc sourcePath = null;
62     private String JavaDoc rootPath = null;
63     private String JavaDoc defaultSourcePath = null;
64     private String JavaDoc defaultPackage = null;
65     private ResourceFilter resourceFilter = new ResourceFilter();
66     private JavaFilter javaFilter = new JavaFilter();
67     private MakefileReader makefileReader = new MakefileReader();
68
69     public ImportPaths() {}
70
71     protected void initProject() {
72         FilenameFilter filter = null;
73         File JavaDoc firstFound = null;
74
75         getProject().setWorkingPath(getRootPath()); // 1st set
76
getProject().setDeployRootPath(getDeployRootPath());
77         getProject().setDeployResourcePath(getResourcePath());
78         getProject().setClassOutputPath(getClassOutputPath());
79         getProject().setPackageMap(getPackageMap());
80
81         // determine type
82
filter = new FilenameFilter();
83         filter.setDirectoryValid(false);
84         filter.setIncludeName(Constants.FILE_WEB_XML);
85         firstFound = FileUtil.findFirst(filter, getSourcePath());
86         if (firstFound == null) {
87             filter.setIncludeName(Constants.FILE_ENHYDRA_SERVICE_XML);
88             firstFound = FileUtil.findFirst(filter, getSourcePath());
89             if (firstFound == null) {
90                 getProject().setDeployType(OtterProject.TYPE_EN3APP);
91             } else {
92 //Dusan
93
// getProject().setDeployType(OtterProject.TYPE_SERVICE);
94
}
95         } else {
96             getProject().setDeployType(OtterProject.TYPE_WEBAPP);
97         }
98         getProject().setProperty(PropertyKeys.NAME_LIBRARIES,
99                                  PropertyKeys.VALUE_ENHYDRA);
100     }
101
102     public OtterProject getProject() {
103         return project;
104     }
105
106     public MakefileReader getMakefileReader() {
107         return makefileReader;
108     }
109
110     public JavaFilter getJavaFilter() {
111         return javaFilter;
112     }
113
114     public void setProject(OtterProject p) {
115         project = p;
116         resourceFilter.setProject(project);
117         makefileReader.setProject(project);
118     }
119
120     /**
121      * Method declaration
122      *
123      *
124      * @return
125      */

126     public String JavaDoc getPackage() {
127         return pack;
128     }
129
130     /**
131      * Method declaration
132      *
133      *
134      * @param pac
135      *
136      * @exception ValidationException
137      */

138     public void setPackage(String JavaDoc pac) throws ValidationException {
139         if (ValidationUtil.isJavaPackage(pac)) {
140             pack = pac;
141         } else {
142             throw new ValidationException(res.getString("Not_a_valid_package")
143                                           + pac);
144         }
145     }
146
147     public void initPackageMap(boolean write) {
148         String JavaDoc[][] readMap = makefileReader.getPackageMap();
149
150         makefileReader.invalidate();
151         String JavaDoc[][] defMap = getDefaultPackageMap();
152         MapEntry[] entries = MapUtil.combine(MapUtil.toEntryArray(readMap),
153                                              MapUtil.toEntryArray(defMap));
154
155         packMap = MapUtil.toStringArray(entries);
156         if (write) {
157             getProject().setPackageMap(packMap);
158         }
159     }
160
161     /**
162      * Method declaration
163      *
164      *
165      * @return
166      */

167     public String JavaDoc[][] getPackageMap() {
168         return packMap;
169     }
170
171     /**
172      * Method declaration
173      *
174      *
175      * @param m
176      */

177     public void setPackageMap(String JavaDoc[][] m) {
178         packMap = m;
179     }
180
181     /**
182      *
183      */

184     protected String JavaDoc getPackagePath() {
185         return createPackagePath(getPackage());
186     }
187
188     /**
189      *
190      */

191     private String JavaDoc createPackagePath(String JavaDoc packPath) {
192         String JavaDoc path = new String JavaDoc();
193
194         if (packPath != null) {
195             path = packPath.replace('.', '/');
196         }
197         return path;
198     }
199
200     /**
201      * Method declaration
202      *
203      *
204      * @return
205      */

206     public String JavaDoc getResourcePath() {
207         if (resourcePath == null) {
208             resourcePath = getDefaultResourcePath();
209         }
210         return resourcePath;
211     }
212
213     /**
214      * Method declaration
215      *
216      *
217      * @param dir
218      *
219      * @exception ValidationException
220      */

221     public void setResourcePath(String JavaDoc path) throws ValidationException {
222         if (ValidationUtil.isDirectory(path)) {
223             resourcePath = PathHandle.createPathString(path);
224         } else {
225             throw new ValidationException(res.getString("Resource_not_found")
226                                           + path);
227         }
228     }
229
230     /**
231      * Method declaration
232      *
233      *
234      * @return
235      */

236     public String JavaDoc getRootPath() {
237         return rootPath;
238     }
239
240     /**
241      * Method declaration
242      *
243      *
244      * @param dir
245      *
246      * @exception ValidationException
247      */

248     public void setRootPath(String JavaDoc path) throws ValidationException {
249         if (ValidationUtil.isDirectory(path)) {
250             rootPath = PathHandle.createPathString(path);
251             defaultSourcePath = null;
252         } else {
253             throw new ValidationException(res.getString("Project_root_not_found"));
254         }
255     }
256
257     /**
258      * Method declaration
259      *
260      *
261      * @return
262      */

263     public String JavaDoc getSourcePath() {
264         return sourcePath;
265     }
266
267     /**
268      * Method declaration
269      *
270      *
271      * @param dir
272      *
273      * @exception ValidationException
274      */

275     public void setSourcePath(String JavaDoc path) throws ValidationException {
276         File JavaDoc dir = null;
277
278         if ((path == null) || (path.trim().length() == 0)) {
279             throw new ValidationException(res.getString("Source_path_empty"));
280         } else {
281             dir = new File JavaDoc(path);
282         }
283         if (ValidationUtil.isDirectory(dir)) {
284             sourcePath = PathHandle.createPathString(dir);
285             defaultPackage = null;
286         } else {
287             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
288
289             if (dir == null) {
290                 message.append(res.getString("Source_null"));
291             } else {
292                 message.append(res.getString("Source_not_found"));
293                 message.append(dir.getAbsolutePath());
294             }
295             throw new ValidationException(message.toString());
296         }
297     }
298
299     /**
300      * Method declaration
301      *
302      *
303      * @return
304      */

305     public String JavaDoc getDefaultPackage() {
306         File JavaDoc firstFound = null;
307         int index = 0;
308
309         if (defaultPackage == null) {
310             if (getSourcePath() != null) {
311                 firstFound = FileUtil.findFirst(javaFilter, getSourcePath());
312             }
313             if (firstFound != null) {
314                 defaultPackage = ImportTool.readPackage(firstFound);
315                 for (int i = 0; i < PACKAGE_KEYS.length; i++) {
316                     index = defaultPackage.indexOf(PACKAGE_KEYS[i]);
317                     if (index > 0) {
318                         defaultPackage = defaultPackage.substring(0, index);
319                         break;
320                     }
321                 }
322             }
323         }
324         return defaultPackage;
325     }
326
327     /**
328      * Method declaration
329      *
330      *
331      * @return
332      */

333     public String JavaDoc[][] getDefaultPackageMap() {
334         String JavaDoc[][] defaultMap = new String JavaDoc[1][2];
335         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
336         File JavaDoc firstFound = null;
337
338         if (getPackage().length() == 0) {
339             buf.append(getDefaultPackage());
340         } else {
341             buf.append(getPackage());
342         }
343         if (buf.toString().trim().length() > 0) {
344             buf.append('.');
345         }
346         buf.append(PRESENTATION);
347         defaultMap[0][0] = getResourcePath();
348         defaultMap[0][1] = buf.toString();
349
350         // That normally works, but lets check a different parallel
351
// html to java structure.
352
if (getResourcePath() != null) {
353             File JavaDoc resource = new File JavaDoc(getResourcePath());
354
355             firstFound = FileUtil.findFirst(resourceFilter, resource, 0);
356         }
357         if (firstFound != null) {
358             File JavaDoc parent = firstFound.getParentFile().getParentFile();
359             File JavaDoc[] dirList = parent.listFiles();
360             File JavaDoc[] javaList = null;
361             String JavaDoc baseName = firstFound.getName();
362
363             baseName = baseName.substring(0, baseName.indexOf('.'));
364             for (int i = 0; i < dirList.length; i++) {
365                 if (dirList[i].isDirectory()) {
366                     javaList = dirList[i].listFiles(javaFilter);
367                     for (int j = 0; j < javaList.length; j++) {
368                         if (javaList[j].getName().startsWith(baseName)) {
369                             defaultMap[0][0] =
370                                 PathHandle.createPathString(firstFound.getParentFile());
371                             defaultMap[0][1] =
372                                 ImportTool.readPackage(javaList[j]);
373                             break;
374                         }
375                     }
376                 }
377             }
378         }
379         return defaultMap;
380     }
381
382     public String JavaDoc getDefaultResourcePath() {
383         String JavaDoc srcPath = new String JavaDoc();
384         String JavaDoc appPackagePath = new String JavaDoc();
385         String JavaDoc path = new String JavaDoc();
386
387         if (getPackage() == null) {
388             appPackagePath = createPackagePath(getDefaultPackage());
389         } else {
390             appPackagePath = getPackagePath();
391         }
392         if (getSourcePath() != null) {
393             srcPath = getSourcePath();
394         } else if (getDefaultSourcePath() != null) {
395             srcPath = getDefaultSourcePath();
396         } else if (getRootPath() != null) {
397             srcPath = getRootPath();
398         }
399         path = PathUtil.getDefaultDeployResourcePath(project, appPackagePath,
400                 srcPath);
401         return path;
402     }
403
404     /**
405      * Method declaration
406      *
407      *
408      * @return
409      */

410     public String JavaDoc getDefaultSourcePath() {
411         File JavaDoc firstFound = null;
412         String JavaDoc inPack = new String JavaDoc();
413         int depth = 0;
414
415         if (defaultSourcePath == null) {
416             if (rootPath != null) {
417                 defaultSourcePath = rootPath;
418                 firstFound = FileUtil.findFirst(javaFilter, rootPath);
419             }
420             if (firstFound != null) {
421                 inPack = ImportTool.readPackage(firstFound);
422                 if (ValidationUtil.isJavaPackage(inPack)) {
423                     if (inPack.length() > 0) {
424                         depth = charCount(inPack, '.') + 1;
425                     }
426                 }
427                 File JavaDoc foundParent = firstFound.getParentFile();
428
429                 while (depth > 0) {
430                     foundParent = foundParent.getParentFile();
431                     depth--;
432                 }
433                 defaultSourcePath = foundParent.getAbsolutePath();
434             }
435         }
436         defaultSourcePath = PathHandle.createPathString(defaultSourcePath);
437         return defaultSourcePath;
438     }
439
440     public void setDeployRootPath(String JavaDoc p) {
441         deployRootPath = p;
442     }
443
444     public String JavaDoc getDeployRootPath() {
445         String JavaDoc p = deployRootPath;
446
447         if (p == null) {
448             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
449
450             buf.append(getRootPath());
451             buf.append(File.separator);
452             buf.append(Constants.DIR_OUTPUT);
453             p = PathHandle.createPathString(buf.toString());
454         }
455         return p;
456     }
457
458     // /
459
// / PROTECTED
460
// /
461
protected String JavaDoc getClassOutputPath() {
462         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
463
464         buf.append(getRootPath());
465         buf.append(File.separator);
466         buf.append(Constants.DIR_CLASSES);
467         return PathHandle.createPathString(buf.toString());
468     }
469
470     /**
471      * Method declaration
472      *
473      *
474      * @param in
475      * @param lookFor
476      *
477      * @return
478      */

479     private int charCount(String JavaDoc in, char lookFor) {
480         String JavaDoc search = new String JavaDoc(in);
481         int index = search.indexOf(lookFor);
482         int count = 0;
483
484         while (index > 0) {
485             count++;
486             search = search.substring(index + 1);
487             index = search.indexOf(lookFor);
488         }
489         return count;
490     }
491
492 }
493
Popular Tags