001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common.map;
023:
024: // ToolBox imports
025: import org.enhydra.tool.common.PathHandle;
026: import org.enhydra.tool.common.FileUtil;
027:
028: // Kelp imports
029: import org.enhydra.kelp.common.node.OtterNode;
030: import org.enhydra.kelp.common.node.OtterFileNode;
031: import org.enhydra.kelp.common.node.OtterProject;
032: import org.enhydra.kelp.common.node.OtterXMLCNode;
033:
034: // Standard imports
035: import java.io.File;
036: import java.util.ResourceBundle;
037:
038: /**
039: * Class declaration
040: *
041: *
042: * @author Paul Mahar
043: */
044: public class Mapper {
045: static ResourceBundle res = ResourceBundle
046: .getBundle("org.enhydra.kelp.common.Res"); // nores
047: private OtterProject project = null;
048: private String[][] map = new String[0][0];
049: private String[] path = new String[0];
050: private int scope = OtterProject.MAP_SCOPE_ALL;
051:
052: /**
053: * Constructor declaration
054: *
055: *
056: * @param p
057: */
058: public Mapper(OtterProject p) {
059: MapEntry[] entries;
060:
061: project = p;
062: scope = project.getMapScope();
063: path = project.getSourcePathArray();
064: if (scope != OtterProject.MAP_SCOPE_NONE) {
065: map = project.getPackageMap();
066: }
067: }
068:
069: /**
070: * Method declaration
071: *
072: *
073: * @param node
074: *
075: * @return
076: */
077: public String getMappedOutputPath(OtterFileNode node) {
078: String sourcePath = new String();
079: String sourceFile = new String();
080:
081: sourceFile = node.getFilePath();
082: sourcePath = project.getSourcePathOf(node);
083: return getMappedOutputPath(sourcePath, sourceFile);
084: }
085:
086: public String getMappedOutputPath(String sourcePath,
087: String sourceFile) {
088: StringBuffer buf = new StringBuffer();
089: String out = new String();
090:
091: out = project.getClassOutputPath();
092: buf.append(out);
093: buf.append(File.separatorChar);
094: out = getMappedPath(sourcePath, sourceFile);
095: buf.append(out);
096: out = PathHandle.createPathString(buf.toString());
097: return out;
098: }
099:
100: /**
101: * Method declaration
102: *
103: *
104: * @param node
105: *
106: * @return
107: */
108: public String getMappedSourcePath(OtterFileNode node) {
109: StringBuffer buf = new StringBuffer();
110: String sourcePath = new String();
111: String sourceFile = new String();
112:
113: sourceFile = node.getFilePath();
114: sourcePath = project.getSourcePathOf(node);
115: buf.append(sourcePath);
116: buf.append(File.separatorChar);
117: buf.append(getMappedPath(sourcePath, sourceFile));
118: return PathHandle.createPathString(buf.toString());
119: }
120:
121: /**
122: * Method declaration
123: *
124: *
125: * @param node
126: *
127: * @return
128: */
129: private String getMappedPath(String sourcePath, String sourceFile) {
130: String mappedPath = null;
131: int index = -1;
132:
133: index = sourceFile.lastIndexOf('.');
134: if (index > sourcePath.length()) {
135: mappedPath = sourceFile.substring(sourcePath.length() + 1,
136: index);
137: } else {
138: mappedPath = sourceFile.substring(sourcePath.length() + 1);
139: }
140: mappedPath = getMappedName(mappedPath, sourceFile);
141: mappedPath = mappedPath.replace('.', File.separatorChar);
142: if (index > sourcePath.length()) {
143: mappedPath = mappedPath + sourceFile.substring(index);
144: }
145: mappedPath = PathHandle.createPathString(mappedPath);
146: return mappedPath;
147: }
148:
149: /**
150: * Method declaration
151: *
152: *
153: * @param node
154: *
155: * @return
156: */
157: public String getClassName(OtterXMLCNode node) {
158: boolean custom = false;
159: int type = node.getClassNameType();
160: String sourcePath = project.getSourcePathOf(node);
161: String rawFilePath = (new File(node.getFilePath()))
162: .getAbsolutePath();
163: String mappedName = new String();
164:
165: if (sourcePath.length() > 0) {
166: mappedName = rawFilePath.substring(sourcePath.length(),
167: rawFilePath.lastIndexOf('.'));
168: } else {
169: System.out.println(res.getString("Warning_file_not_in")
170: + rawFilePath);
171: rawFilePath.substring(rawFilePath
172: .lastIndexOf(File.separator) + 1, rawFilePath
173: .lastIndexOf('.'));
174: }
175: switch (type) {
176: case OtterXMLCNode.CLASS_NAME_CUSTOM:
177: if (node.getCustomClassName().trim().length() > 0) {
178: mappedName = node.getCustomClassName();
179: custom = true;
180: }
181: break;
182: case OtterXMLCNode.CLASS_NAME_DEFAULT:
183:
184: // already at default.
185: break;
186: default:
187: mappedName = getMappedName(mappedName, rawFilePath);
188: }
189: mappedName = validateClassName(mappedName, rawFilePath, type,
190: custom);
191: return mappedName;
192: }
193:
194: /**
195: * Method declaration
196: *
197: *
198: * @param mappedName
199: * @param rawFilePath
200: * @param type
201: * @param custom
202: *
203: * @return
204: */
205: public String validateClassName(String mappedName, String inPath,
206: int type, boolean custom) {
207: String validName = new String();
208: PathHandle handle = null;
209:
210: handle = PathHandle.createPathHandle(inPath);
211: validName = mappedName.trim();
212: validName = validName.replace('/', '.');
213: validName = validName.replace('\\', '.');
214: if (validName.charAt(0) == '.') {
215: validName = validName.substring(1);
216: }
217: if (!custom) {
218: String baseName = new String();
219: int baseStart = -1;
220: int baseEnd = -1;
221:
222: baseStart = handle.getPath().lastIndexOf('/') + 1;
223: if (validName.length() == 0) {
224:
225: // not in path, create packageless default name.
226: validName = handle.getPath().substring(baseStart);
227: baseEnd = validName.lastIndexOf('.');
228: validName = validName.substring(0, baseEnd);
229: }
230: baseEnd = handle.getPath().lastIndexOf('.');
231: baseName = handle.getPath().substring(baseStart, baseEnd);
232: baseEnd = validName.lastIndexOf('.');
233: validName = validName.substring(0, baseEnd + 1) + baseName
234: + handle.getExtension().toUpperCase();
235: }
236: return validName;
237: }
238:
239: /**
240: * Method declaration
241: *
242: *
243: * @param mappedName
244: * @param rawFilePath
245: *
246: * @return
247: */
248: private String getMappedName(String mappedName, String rawFilePath) {
249: String mappedPath = rawFilePath;
250: String matchPack = new String();
251: String[] parentPath = null;
252: String toPackage = new String();
253: boolean useMap = false;
254: boolean mapped = false;
255: boolean isDir = false;
256: int max = -1;
257: int matchMax = -1;
258:
259: isDir = FileUtil.isDirectory(rawFilePath);
260: if (map != null) {
261: for (int i = 0; i < map.length; i++) {
262: parentPath = new String[1];
263: parentPath[0] = map[i][0];
264: for (int j = 0; j < parentPath.length; j++) {
265: parentPath[j] = parentPath[j].trim();
266: if ((parentPath[j].length() > max) || (!mapped)) {
267: PathHandle parentHandle = null;
268:
269: parentHandle = PathHandle
270: .createPathHandle(parentPath[j]);
271: max = parentPath[j].length();
272: toPackage = map[i][1].trim();
273: if (isDir) {
274: if (parentHandle.equals(rawFilePath)) {
275: matchPack = toPackage;
276: matchMax = max;
277: mapped = true;
278: break;
279: }
280: } else if (parentHandle.parentOf(rawFilePath)) {
281: matchPack = toPackage;
282: matchMax = max;
283: mapped = true;
284: break;
285: }
286: }
287: }
288: }
289: }
290: int index = 0;
291:
292: if (mapped) {
293: index = rawFilePath.lastIndexOf('.');
294: if (index > matchMax) {
295: mappedName = matchPack
296: + rawFilePath.substring(matchMax, index);
297: } else {
298: mappedName = matchPack
299: + rawFilePath.substring(matchMax);
300: }
301: }
302: return mappedName;
303: }
304:
305: }
|