1 22 package org.enhydra.kelp.common.map; 23 24 import org.enhydra.tool.common.PathHandle; 26 import org.enhydra.tool.common.FileUtil; 27 28 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 import java.io.File ; 36 import java.util.ResourceBundle ; 37 38 44 public class Mapper { 45 static ResourceBundle res = 46 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private OtterProject project = null; 48 private String [][] map = new String [0][0]; 49 private String [] path = new String [0]; 50 private int scope = OtterProject.MAP_SCOPE_ALL; 51 52 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 77 public String getMappedOutputPath(OtterFileNode node) { 78 String sourcePath = new String (); 79 String sourceFile = new String (); 80 81 sourceFile = node.getFilePath(); 82 sourcePath = project.getSourcePathOf(node); 83 return getMappedOutputPath(sourcePath, sourceFile); 84 } 85 86 public String getMappedOutputPath(String sourcePath, String sourceFile) { 87 StringBuffer buf = new StringBuffer (); 88 String out = new String (); 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 107 public String getMappedSourcePath(OtterFileNode node) { 108 StringBuffer buf = new StringBuffer (); 109 String sourcePath = new String (); 110 String sourceFile = new String (); 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 128 private String getMappedPath(String sourcePath, String sourceFile) { 129 String 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 155 public String getClassName(OtterXMLCNode node) { 156 boolean custom = false; 157 int type = node.getClassNameType(); 158 String sourcePath = project.getSourcePathOf(node); 159 String rawFilePath = 160 (new File (node.getFilePath())).getAbsolutePath(); 161 String mappedName = new String (); 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 break; 183 default: 184 mappedName = getMappedName(mappedName, rawFilePath); 185 } 186 mappedName = validateClassName(mappedName, rawFilePath, type, custom); 187 return mappedName; 188 } 189 190 201 public String validateClassName(String mappedName, String inPath, 202 int type, boolean custom) { 203 String validName = new String (); 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 baseName = new String (); 215 int baseStart = -1; 216 int baseEnd = -1; 217 218 baseStart = handle.getPath().lastIndexOf('/') + 1; 219 if (validName.length() == 0) { 220 221 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 244 private String getMappedName(String mappedName, String rawFilePath) { 245 String mappedPath = rawFilePath; 246 String matchPack = new String (); 247 String [] parentPath = null; 248 String toPackage = new String (); 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 [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 |