Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/enhydra/kelp/common/importer/ImportPaths.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.importer;
23  
24  // ToolBox imports
25  import org.enhydra.tool.common.FileUtil;
26  import org.enhydra.tool.common.PathHandle;
27  
28  // Kelp imports
29  import org.enhydra.kelp.common.Constants;
30  import org.enhydra.kelp.common.PathUtil;
31  import org.enhydra.kelp.common.ValidationException;
32  import org.enhydra.kelp.common.ValidationUtil;
33  import org.enhydra.kelp.common.map.MapEntry;
34  import org.enhydra.kelp.common.map.MapUtil;
35  import org.enhydra.kelp.common.node.OtterProject;
36  import org.enhydra.kelp.common.node.PropertyKeys;
37  
38  // Standard imports
39  import java.io.File;
40  import java.util.ResourceBundle;
41  
42  //
43  public class ImportPaths implements PropertyKeys {
44  
45      // strings not to be translated
46      static ResourceBundle  res =
47          ResourceBundle.getBundle("org.enhydra.kelp.common.Res");    // nores
48      private final String   PRESENTATION = "presentation";           // nores
49      private final String[] PACKAGE_KEYS = {
50          ".business", ".data", '.' + PRESENTATION
51      };                                                              // nores
52  
53      //
54      private OtterProject   project = null;
55      private String         pack = new String();
56      private String[][]     packMap = new String[0][2];
57      private String         deployRootPath = null;
58      private String         resourcePath = null;
59      private String         sourcePath = null;
60      private String         rootPath = null;
61      private String         defaultSourcePath = null;
62      private String         defaultPackage = null;
63      private ResourceFilter resourceFilter = new ResourceFilter();
64      private JavaFilter     javaFilter = new JavaFilter();
65      private MakefileReader makefileReader = new MakefileReader();
66  
67      public ImportPaths() {}
68  
69      protected void initProject() {
70          WebXMLFilter filter = new WebXMLFilter();
71          File         firstFound = null;
72  
73          getProject().setWorkingPath(getRootPath());    // 1st set
74          getProject().setDeployRootPath(getDeployRootPath());
75          getProject().setDeployResourcePath(getResourcePath());
76          getProject().setClassOutputPath(getClassOutputPath());
77          getProject().setPackageMap(getPackageMap());
78          firstFound = FileUtil.findFirst(filter, getSourcePath());
79          getProject().setWebApplication(firstFound != null);
80          getProject().setProperty(PropertyKeys.NAME_LIBRARIES,
81                                   PropertyKeys.VALUE_ENHYDRA);
82      }
83  
84      public OtterProject getProject() {
85          return project;
86      }
87  
88      public MakefileReader getMakefileReader() {
89          return makefileReader;
90      }
91  
92      public JavaFilter getJavaFilter() {
93          return javaFilter;
94      }
95  
96      public void setProject(OtterProject p) {
97          project = p;
98          resourceFilter.setProject(project);
99          makefileReader.setProject(project);
100     }
101 
102     /**
103      * Method declaration
104      *
105      *
106      * @return
107      */
108     public String getPackage() {
109         return pack;
110     }
111 
112     /**
113      * Method declaration
114      *
115      *
116      * @param pac
117      *
118      * @exception ValidationException
119      */
120     public void setPackage(String pac) throws ValidationException {
121         if (ValidationUtil.isJavaPackage(pac)) {
122             pack = pac;
123         } else {
124             throw new ValidationException(res.getString("Not_a_valid_package")
125                                           + pac);
126         }
127     }
128 
129     public void initPackageMap(boolean write) {
130         String[][] readMap = makefileReader.getPackageMap();
131 
132         makefileReader.invalidate();
133         String[][] defMap = getDefaultPackageMap();
134         MapEntry[] entries = MapUtil.combine(MapUtil.toEntryArray(readMap),
135                                              MapUtil.toEntryArray(defMap));
136 
137         packMap = MapUtil.toStringArray(entries);
138         if (write) {
139             getProject().setPackageMap(packMap);
140         }
141     }
142 
143     /**
144      * Method declaration
145      *
146      *
147      * @return
148      */
149     public String[][] getPackageMap() {
150         return packMap;
151     }
152 
153     /**
154      * Method declaration
155      *
156      *
157      * @param m
158      */
159     public void setPackageMap(String[][] m) {
160         packMap = m;
161     }
162 
163     /**
164      *
165      */
166     protected String getPackagePath() {
167         return createPackagePath(getPackage());
168     }
169 
170     /**
171      *
172      */
173     private String createPackagePath(String packPath) {
174         String path = new String();
175 
176         if (packPath != null) {
177             path = packPath.replace('.', '/');
178         }
179         return path;
180     }
181 
182     /**
183      * Method declaration
184      *
185      *
186      * @return
187      */
188     public String getResourcePath() {
189         if (resourcePath == null) {
190             resourcePath = getDefaultResourcePath();
191         }
192         return resourcePath;
193     }
194 
195     /**
196      * Method declaration
197      *
198      *
199      * @param dir
200      *
201      * @exception ValidationException
202      */
203     public void setResourcePath(String path) throws ValidationException {
204         if (ValidationUtil.isDirectory(path)) {
205             resourcePath = PathHandle.createPathString(path);
206         } else {
207             throw new ValidationException(res.getString("Resource_not_found")
208                                           + path);
209         }
210     }
211 
212     /**
213      * Method declaration
214      *
215      *
216      * @return
217      */
218     public String getRootPath() {
219         return rootPath;
220     }
221 
222     /**
223      * Method declaration
224      *
225      *
226      * @param dir
227      *
228      * @exception ValidationException
229      */
230     public void setRootPath(String path) throws ValidationException {
231         if (ValidationUtil.isDirectory(path)) {
232             rootPath = PathHandle.createPathString(path);
233             defaultSourcePath = null;
234         } else {
235             throw new ValidationException(res.getString("Project_root_not_found"));
236         }
237     }
238 
239     /**
240      * Method declaration
241      *
242      *
243      * @return
244      */
245     public String getSourcePath() {
246         return sourcePath;
247     }
248 
249     /**
250      * Method declaration
251      *
252      *
253      * @param dir
254      *
255      * @exception ValidationException
256      */
257     public void setSourcePath(String path) throws ValidationException {
258         File dir = null;
259 
260         if ((path == null) || (path.trim().length() == 0)) {
261             throw new ValidationException(res.getString("Source_path_empty"));
262         } else {
263             dir = new File(path);
264         }
265         if (ValidationUtil.isDirectory(dir)) {
266             sourcePath = PathHandle.createPathString(dir);
267             defaultPackage = null;
268         } else {
269             StringBuffer message = new StringBuffer();
270 
271             if (dir == null) {
272                 message.append(res.getString("Source_null"));
273             } else {
274                 message.append(res.getString("Source_not_found"));
275                 message.append(dir.getAbsolutePath());
276             }
277             throw new ValidationException(message.toString());
278         }
279     }
280 
281     /**
282      * Method declaration
283      *
284      *
285      * @return
286      */
287     public String getDefaultPackage() {
288         File firstFound = null;
289         int  index = 0;
290 
291         if (defaultPackage == null) {
292             if (getSourcePath() != null) {
293                 firstFound = FileUtil.findFirst(javaFilter, getSourcePath());
294             }
295             if (firstFound != null) {
296                 defaultPackage = ImportTool.readPackage(firstFound);
297                 for (int i = 0; i < PACKAGE_KEYS.length; i++) {
298                     index = defaultPackage.indexOf(PACKAGE_KEYS[i]);
299                     if (index > 0) {
300                         defaultPackage = defaultPackage.substring(0, index);
301                         break;
302                     }
303                 }
304             }
305         }
306         return defaultPackage;
307     }
308 
309     /**
310      * Method declaration
311      *
312      *
313      * @return
314      */
315     public String[][] getDefaultPackageMap() {
316         String[][]   defaultMap = new String[1][2];
317         StringBuffer buf = new StringBuffer();
318         File         firstFound = null;
319 
320         if (getPackage().length() == 0) {
321             buf.append(getDefaultPackage());
322         } else {
323             buf.append(getPackage());
324         }
325         if (buf.toString().trim().length() > 0) {
326             buf.append('.');
327         }
328         buf.append(PRESENTATION);
329         defaultMap[0][0] = getResourcePath();
330         defaultMap[0][1] = buf.toString();
331 
332         // That normally works, but lets check a different parallel
333         // html to java structure.
334         if (getResourcePath() != null) {
335             File resource = new File(getResourcePath());
336 
337             firstFound = FileUtil.findFirst(resourceFilter, resource, 0);
338         }
339         if (firstFound != null) {
340             File   parent = firstFound.getParentFile().getParentFile();
341             File[] dirList = parent.listFiles();
342             File[] javaList = null;
343             String baseName = firstFound.getName();
344 
345             baseName = baseName.substring(0, baseName.indexOf('.'));
346             for (int i = 0; i < dirList.length; i++) {
347                 if (dirList[i].isDirectory()) {
348                     javaList = dirList[i].listFiles(javaFilter);
349                     for (int j = 0; j < javaList.length; j++) {
350                         if (javaList[j].getName().startsWith(baseName)) {
351                             defaultMap[0][0] =
352                                 PathHandle.createPathString(firstFound.getParentFile());
353                             defaultMap[0][1] =
354                                 ImportTool.readPackage(javaList[j]);
355                             break;
356                         }
357                     }
358                 }
359             }
360         }
361         return defaultMap;
362     }
363 
364     public String getDefaultResourcePath() {
365         String srcPath = new String();
366         String appPackagePath = new String();
367         String path = new String();
368 
369         if (getPackage() == null) {
370             appPackagePath = createPackagePath(getDefaultPackage());
371         } else {
372             appPackagePath = getPackagePath();
373         }
374         if (getSourcePath() != null) {
375             srcPath = getSourcePath();
376         } else if (getDefaultSourcePath() != null) {
377             srcPath = getDefaultSourcePath();
378         } else if (getRootPath() != null) {
379             srcPath = getRootPath();
380         }
381         path = PathUtil.getDefaultDeployResourcePath(project, appPackagePath,
382                 srcPath);
383         return path;
384     }
385 
386     /**
387      * Method declaration
388      *
389      *
390      * @return
391      */
392     public String getDefaultSourcePath() {
393         File   firstFound = null;
394         String inPack = new String();
395         int    depth = 0;
396 
397         if (defaultSourcePath == null) {
398             if (rootPath != null) {
399                 defaultSourcePath = rootPath;
400                 firstFound = FileUtil.findFirst(javaFilter, rootPath);
401             }
402             if (firstFound != null) {
403                 inPack = ImportTool.readPackage(firstFound);
404                 if (ValidationUtil.isJavaPackage(inPack)) {
405                     if (inPack.length() > 0) {
406                         depth = charCount(inPack, '.') + 1;
407                     }
408                 }
409                 File foundParent = firstFound.getParentFile();
410 
411                 while (depth > 0) {
412                     foundParent = foundParent.getParentFile();
413                     depth--;
414                 }
415                 defaultSourcePath = foundParent.getAbsolutePath();
416             }
417         }
418         defaultSourcePath = PathHandle.createPathString(defaultSourcePath);
419         return defaultSourcePath;
420     }
421 
422     public void setDeployRootPath(String p) {
423         deployRootPath = p;
424     }
425 
426     public String getDeployRootPath() {
427         String p = deployRootPath;
428 
429         if (p == null) {
430             StringBuffer buf = new StringBuffer();
431 
432             buf.append(getRootPath());
433             buf.append(File.separator);
434             buf.append(Constants.DIR_OUTPUT);
435             p = PathHandle.createPathString(buf.toString());
436         }
437         return p;
438     }
439 
440     // /
441     // / PROTECTED
442     // /
443     protected String getClassOutputPath() {
444         StringBuffer buf = new StringBuffer();
445 
446         buf.append(getRootPath());
447         buf.append(File.separator);
448         buf.append(Constants.DIR_CLASSES);
449         return PathHandle.createPathString(buf.toString());
450     }
451 
452     /**
453      * Method declaration
454      *
455      *
456      * @param in
457      * @param lookFor
458      *
459      * @return
460      */
461     private int charCount(String in, char lookFor) {
462         String search = new String(in);
463         int    index = search.indexOf(lookFor);
464         int    count = 0;
465 
466         while (index > 0) {
467             count++;
468             search = search.substring(index + 1);
469             index = search.indexOf(lookFor);
470         }
471         return count;
472     }
473 
474 }