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

Quick Search    Search Deep

Source code: org/enhydra/kelp/common/map/Mapper.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.map;
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.node.OtterNode;
30  import org.enhydra.kelp.common.node.OtterProject;
31  import org.enhydra.kelp.common.node.OtterXMLCNode;
32  
33  // Standard imports
34  import java.io.File;
35  import java.util.ResourceBundle;
36  
37  /**
38   * Class declaration
39   *
40   *
41   * @author Paul Mahar
42   */
43  public class Mapper {
44      static ResourceBundle res =
45          ResourceBundle.getBundle("org.enhydra.kelp.common.Res");    // nores
46      private OtterProject  project = null;
47      private String[][]    map = new String[0][0];
48      private String[]      path = new String[0];
49      private int           scope = OtterProject.MAP_SCOPE_ALL;
50  
51      /**
52       * Constructor declaration
53       *
54       *
55       * @param p
56       */
57      public Mapper(OtterProject p) {
58          MapEntry[] entries;
59  
60          project = p;
61          scope = project.getMapScope();
62          path = project.getSourcePathArray();
63          if (scope != OtterProject.MAP_SCOPE_NONE) {
64              map = project.getPackageMap();
65          }
66      }
67  
68      /**
69       * Method declaration
70       *
71       *
72       * @param node
73       *
74       * @return
75       */
76      public String getMappedOutputPath(OtterNode node) {
77          String sourcePath = new String();
78          String sourceFile = new String();
79  
80          sourceFile = node.getFilePath();
81          sourcePath = project.getSourcePathOf(node);
82          return getMappedOutputPath(sourcePath, sourceFile);
83      }
84  
85      public String getMappedOutputPath(String sourcePath, String sourceFile) {
86          StringBuffer buf = new StringBuffer();
87          String       out = new String();
88  
89          out = project.getClassOutputPath();
90          buf.append(out);
91          buf.append(File.separatorChar);
92          out = getMappedPath(sourcePath, sourceFile);
93          buf.append(out);
94          out = PathHandle.createPathString(buf.toString());
95          return out;
96      }
97  
98      /**
99       * Method declaration
100      *
101      *
102      * @param node
103      *
104      * @return
105      */
106     public String getMappedSourcePath(OtterNode node) {
107         StringBuffer buf = new StringBuffer();
108         String       sourcePath = new String();
109         String       sourceFile = new String();
110 
111         sourceFile = node.getFilePath();
112         sourcePath = project.getSourcePathOf(node);
113         buf.append(sourcePath);
114         buf.append(File.separatorChar);
115         buf.append(getMappedPath(sourcePath, sourceFile));
116         return PathHandle.createPathString(buf.toString());
117     }
118 
119     /**
120      * Method declaration
121      *
122      *
123      * @param node
124      *
125      * @return
126      */
127     private String getMappedPath(String sourcePath, String sourceFile) {
128         String mappedPath = null;
129         int    index = -1;
130 
131         index = sourceFile.lastIndexOf('.');
132         if (index > sourcePath.length()) {
133             mappedPath = sourceFile.substring(sourcePath.length() + 1, index);
134         } else {
135             mappedPath = sourceFile.substring(sourcePath.length() + 1);
136         }
137         mappedPath = getMappedName(mappedPath, sourceFile);
138         mappedPath = mappedPath.replace('.', File.separatorChar);
139         if (index > sourcePath.length()) {
140             mappedPath = mappedPath + sourceFile.substring(index);
141         }
142         mappedPath = PathHandle.createPathString(mappedPath);
143         return mappedPath;
144     }
145 
146     /**
147      * Method declaration
148      *
149      *
150      * @param node
151      *
152      * @return
153      */
154     public String getClassName(OtterXMLCNode node) {
155         boolean custom = false;
156         int     type = node.getClassNameType();
157         String  sourcePath = project.getSourcePathOf(node);
158         String  rawFilePath =
159             (new File(node.getFilePath())).getAbsolutePath();
160         String  mappedName = new String();
161 
162         if (sourcePath.length() > 0) {
163             mappedName = rawFilePath.substring(sourcePath.length(),
164                                                rawFilePath.lastIndexOf('.'));
165         } else {
166             System.out.println(res.getString("Warning_file_not_in")
167                                + rawFilePath);
168             rawFilePath.substring(rawFilePath.lastIndexOf(File.separator)
169                                   + 1, rawFilePath.lastIndexOf('.'));
170         }
171         switch (type) {
172         case OtterXMLCNode.CLASS_NAME_CUSTOM:
173             if (node.getCustomClassName().trim().length() > 0) {
174                 mappedName = node.getCustomClassName();
175                 custom = true;
176             }
177             break;
178         case OtterXMLCNode.CLASS_NAME_DEFAULT:
179 
180             // already at default.
181             break;
182         default:
183             mappedName = getMappedName(mappedName, rawFilePath);
184         }
185         mappedName = validateClassName(mappedName, rawFilePath, type, custom);
186         return mappedName;
187     }
188 
189     /**
190      * Method declaration
191      *
192      *
193      * @param mappedName
194      * @param rawFilePath
195      * @param type
196      * @param custom
197      *
198      * @return
199      */
200     public String validateClassName(String mappedName, String inPath,
201                                     int type, boolean custom) {
202         String     validName = new String();
203         PathHandle handle = null;
204 
205         handle = PathHandle.createPathHandle(inPath);
206         validName = mappedName.trim();
207         validName = validName.replace('/', '.');
208         validName = validName.replace('\\', '.');
209         if (validName.charAt(0) == '.') {
210             validName = validName.substring(1);
211         }
212         if (!custom) {
213             String baseName = new String();
214             int    baseStart = -1;
215             int    baseEnd = -1;
216 
217             baseStart = handle.getPath().lastIndexOf('/') + 1;
218             if (validName.length() == 0) {
219 
220                 // not in path, create packageless default name.
221                 validName = handle.getPath().substring(baseStart);
222                 baseEnd = validName.lastIndexOf('.');
223                 validName = validName.substring(0, baseEnd);
224             }
225             baseEnd = handle.getPath().lastIndexOf('.');
226             baseName = handle.getPath().substring(baseStart, baseEnd);
227             baseEnd = validName.lastIndexOf('.');
228             validName = validName.substring(0, baseEnd + 1) + baseName
229                         + handle.getExtension().toUpperCase();
230         }
231         return validName;
232     }
233 
234     /**
235      * Method declaration
236      *
237      *
238      * @param mappedName
239      * @param rawFilePath
240      *
241      * @return
242      */
243     private String getMappedName(String mappedName, String rawFilePath) {
244         String   mappedPath = rawFilePath;
245         String   matchPack = new String();
246         String[] parentPath = null;
247         String   toPackage = new String();
248         boolean  useMap = false;
249         boolean  mapped = false;
250         boolean  isDir = false;
251         int      max = -1;
252         int      matchMax = -1;
253 
254         isDir = FileUtil.isDirectory(rawFilePath);
255         if (map != null) {
256             for (int i = 0; i < map.length; i++) {
257                 parentPath = new String[1];
258                 parentPath[0] = map[i][0];
259                 for (int j = 0; j < parentPath.length; j++) {
260                     parentPath[j] = parentPath[j].trim();
261                     if ((parentPath[j].length() > max) || (!mapped)) {
262                         PathHandle parentHandle = null;
263 
264                         parentHandle =
265                             PathHandle.createPathHandle(parentPath[j]);
266                         max = parentPath[j].length();
267                         toPackage = map[i][1].trim();
268                         if (isDir) {
269                             if (parentHandle.equals(rawFilePath)) {
270                                 matchPack = toPackage;
271                                 matchMax = max;
272                                 mapped = true;
273                                 break;
274                             }
275                         } else if (parentHandle.parentOf(rawFilePath)) {
276                             matchPack = toPackage;
277                             matchMax = max;
278                             mapped = true;
279                             break;
280                         }
281                     }
282                 }
283             }
284         }
285         int index = 0;
286 
287         if (mapped) {
288             index = rawFilePath.lastIndexOf('.');
289             if (index > matchMax) {
290                 mappedName = matchPack
291                              + rawFilePath.substring(matchMax, index);
292             } else {
293                 mappedName = matchPack + rawFilePath.substring(matchMax);
294             }
295         }
296         return mappedName;
297     }
298 
299 }