KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > map > Mapper


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.OtterFileNode;
31 import org.enhydra.kelp.common.node.OtterProject;
32 import org.enhydra.kelp.common.node.OtterXMLCNode;
33
34 // Standard imports
35
import java.io.File JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 /**
39  * Class declaration
40  *
41  *
42  * @author Paul Mahar
43  */

44 public class Mapper {
45     static ResourceBundle JavaDoc res =
46         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
47
private OtterProject project = null;
48     private String JavaDoc[][] map = new String JavaDoc[0][0];
49     private String JavaDoc[] path = new String JavaDoc[0];
50     private int scope = OtterProject.MAP_SCOPE_ALL;
51
52     /**
53      * Constructor declaration
54      *
55      *
56      * @param p
57      */

58     public Mapper(OtterProject p) {
59         MapEntry[] entries;
60
61         project = p;
62         scope = project.getMapScope();
63         path = project.getSourcePathArray();
64         if (scope != OtterProject.MAP_SCOPE_NONE) {
65             map = project.getPackageMap();
66         }
67     }
68
69     /**
70      * Method declaration
71      *
72      *
73      * @param node
74      *
75      * @return
76      */

77     public String JavaDoc getMappedOutputPath(OtterFileNode node) {
78         String JavaDoc sourcePath = new String JavaDoc();
79         String JavaDoc sourceFile = new String JavaDoc();
80
81         sourceFile = node.getFilePath();
82         sourcePath = project.getSourcePathOf(node);
83         return getMappedOutputPath(sourcePath, sourceFile);
84     }
85
86     public String JavaDoc getMappedOutputPath(String JavaDoc sourcePath, String JavaDoc sourceFile) {
87         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
88         String JavaDoc out = new String JavaDoc();
89
90         out = project.getClassOutputPath();
91         buf.append(out);
92         buf.append(File.separatorChar);
93         out = getMappedPath(sourcePath, sourceFile);
94         buf.append(out);
95         out = PathHandle.createPathString(buf.toString());
96         return out;
97     }
98
99     /**
100      * Method declaration
101      *
102      *
103      * @param node
104      *
105      * @return
106      */

107     public String JavaDoc getMappedSourcePath(OtterFileNode node) {
108         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
109         String JavaDoc sourcePath = new String JavaDoc();
110         String JavaDoc sourceFile = new String JavaDoc();
111
112         sourceFile = node.getFilePath();
113         sourcePath = project.getSourcePathOf(node);
114         buf.append(sourcePath);
115         buf.append(File.separatorChar);
116         buf.append(getMappedPath(sourcePath, sourceFile));
117         return PathHandle.createPathString(buf.toString());
118     }
119
120     /**
121      * Method declaration
122      *
123      *
124      * @param node
125      *
126      * @return
127      */

128     private String JavaDoc getMappedPath(String JavaDoc sourcePath, String JavaDoc sourceFile) {
129         String JavaDoc mappedPath = null;
130         int index = -1;
131
132         index = sourceFile.lastIndexOf('.');
133         if (index > sourcePath.length()) {
134             mappedPath = sourceFile.substring(sourcePath.length() + 1, index);
135         } else {
136             mappedPath = sourceFile.substring(sourcePath.length() + 1);
137         }
138         mappedPath = getMappedName(mappedPath, sourceFile);
139         mappedPath = mappedPath.replace('.', File.separatorChar);
140         if (index > sourcePath.length()) {
141             mappedPath = mappedPath + sourceFile.substring(index);
142         }
143         mappedPath = PathHandle.createPathString(mappedPath);
144         return mappedPath;
145     }
146
147     /**
148      * Method declaration
149      *
150      *
151      * @param node
152      *
153      * @return
154      */

155     public String JavaDoc getClassName(OtterXMLCNode node) {
156         boolean custom = false;
157         int type = node.getClassNameType();
158         String JavaDoc sourcePath = project.getSourcePathOf(node);
159         String JavaDoc rawFilePath =
160             (new File JavaDoc(node.getFilePath())).getAbsolutePath();
161         String JavaDoc mappedName = new String JavaDoc();
162
163         if (sourcePath.length() > 0) {
164             mappedName = rawFilePath.substring(sourcePath.length(),
165                                                rawFilePath.lastIndexOf('.'));
166         } else {
167             System.out.println(res.getString("Warning_file_not_in")
168                                + rawFilePath);
169             rawFilePath.substring(rawFilePath.lastIndexOf(File.separator)
170                                   + 1, rawFilePath.lastIndexOf('.'));
171         }
172         switch (type) {
173         case OtterXMLCNode.CLASS_NAME_CUSTOM:
174             if (node.getCustomClassName().trim().length() > 0) {
175                 mappedName = node.getCustomClassName();
176                 custom = true;
177             }
178             break;
179         case OtterXMLCNode.CLASS_NAME_DEFAULT:
180
181             // already at default.
182
break;
183         default:
184             mappedName = getMappedName(mappedName, rawFilePath);
185         }
186         mappedName = validateClassName(mappedName, rawFilePath, type, custom);
187         return mappedName;
188     }
189
190     /**
191      * Method declaration
192      *
193      *
194      * @param mappedName
195      * @param rawFilePath
196      * @param type
197      * @param custom
198      *
199      * @return
200      */

201     public String JavaDoc validateClassName(String JavaDoc mappedName, String JavaDoc inPath,
202                                     int type, boolean custom) {
203         String JavaDoc validName = new String JavaDoc();
204         PathHandle handle = null;
205
206         handle = PathHandle.createPathHandle(inPath);
207         validName = mappedName.trim();
208         validName = validName.replace('/', '.');
209         validName = validName.replace('\\', '.');
210         if (validName.charAt(0) == '.') {
211             validName = validName.substring(1);
212         }
213         if (!custom) {
214             String JavaDoc baseName = new String JavaDoc();
215             int baseStart = -1;
216             int baseEnd = -1;
217
218             baseStart = handle.getPath().lastIndexOf('/') + 1;
219             if (validName.length() == 0) {
220
221                 // not in path, create packageless default name.
222
validName = handle.getPath().substring(baseStart);
223                 baseEnd = validName.lastIndexOf('.');
224                 validName = validName.substring(0, baseEnd);
225             }
226             baseEnd = handle.getPath().lastIndexOf('.');
227             baseName = handle.getPath().substring(baseStart, baseEnd);
228             baseEnd = validName.lastIndexOf('.');
229             validName = validName.substring(0, baseEnd + 1) + baseName
230                         + handle.getExtension().toUpperCase();
231         }
232         return validName;
233     }
234
235     /**
236      * Method declaration
237      *
238      *
239      * @param mappedName
240      * @param rawFilePath
241      *
242      * @return
243      */

244     private String JavaDoc getMappedName(String JavaDoc mappedName, String JavaDoc rawFilePath) {
245         String JavaDoc mappedPath = rawFilePath;
246         String JavaDoc matchPack = new String JavaDoc();
247         String JavaDoc[] parentPath = null;
248         String JavaDoc toPackage = new String JavaDoc();
249         boolean useMap = false;
250         boolean mapped = false;
251         boolean isDir = false;
252         int max = -1;
253         int matchMax = -1;
254
255         isDir = FileUtil.isDirectory(rawFilePath);
256         if (map != null) {
257             for (int i = 0; i < map.length; i++) {
258                 parentPath = new String JavaDoc[1];
259                 parentPath[0] = map[i][0];
260                 for (int j = 0; j < parentPath.length; j++) {
261                     parentPath[j] = parentPath[j].trim();
262                     if ((parentPath[j].length() > max) || (!mapped)) {
263                         PathHandle parentHandle = null;
264
265                         parentHandle =
266                             PathHandle.createPathHandle(parentPath[j]);
267                         max = parentPath[j].length();
268                         toPackage = map[i][1].trim();
269                         if (isDir) {
270                             if (parentHandle.equals(rawFilePath)) {
271                                 matchPack = toPackage;
272                                 matchMax = max;
273                                 mapped = true;
274                                 break;
275                             }
276                         } else if (parentHandle.parentOf(rawFilePath)) {
277                             matchPack = toPackage;
278                             matchMax = max;
279                             mapped = true;
280                             break;
281                         }
282                     }
283                 }
284             }
285         }
286         int index = 0;
287
288         if (mapped) {
289             index = rawFilePath.lastIndexOf('.');
290             if (index > matchMax) {
291                 mappedName = matchPack
292                              + rawFilePath.substring(matchMax, index);
293             } else {
294                 mappedName = matchPack + rawFilePath.substring(matchMax);
295             }
296         }
297         return mappedName;
298     }
299
300 }
301
Popular Tags