1 22 package org.enhydra.kelp.common.importer; 23 24 import org.enhydra.tool.ToolBoxInfo; 26 import org.enhydra.tool.common.FileUtil; 27 import org.enhydra.tool.common.PathHandle; 28 29 import org.enhydra.kelp.common.map.MapEntry; 31 import org.enhydra.kelp.common.map.MapUtil; 32 import org.enhydra.kelp.common.node.OtterProject; 33 import org.enhydra.kelp.common.node.OtterXMLCNode; 34 import org.enhydra.kelp.common.node.PropertyKeys; 35 36 import java.io.BufferedReader ; 38 import java.io.IOException ; 39 import java.io.File ; 40 import java.io.FileReader ; 41 import java.util.Vector ; 42 43 public class MakefileReader { 45 46 private final String KEEP = "-keep"; private final String IMPLEMENTS = "-implements"; private final String ROOT = "ROOT"; private final String PACKAGEDIR = "PACKAGEDIR"; private final String XMLC_ = "XMLC_"; private final String _CLASSES = "_CLASSES"; private final String _DIR = "_DIR"; private final String _OPTS = "_OPTS"; private final String _OPTS_FILE = "_OPTS_FILE"; private final String PE = "+="; 58 private OtterProject project = null; 60 private MakefileFilter filter = null; 61 private MakefileData makeCursor = null; 62 private MakefileData[] makeData = new MakefileData[0]; 63 private Vector makeVector = new Vector (); 64 private String [] docTypes = new String [0]; 65 private String sourcePath = new String (); 66 private String continueName = new String (); 67 private boolean continueLine = false; 68 private boolean needRead = true; 69 private boolean keepFound = false; 70 71 public MakefileReader() { 72 filter = new MakefileFilter(); 73 docTypes = ToolBoxInfo.getSupportedDocTypes(); 74 } 75 76 public OtterProject getProject() { 77 return project; 78 } 79 80 public void setProject(OtterProject p) { 81 project = p; 82 } 83 84 public void invalidate() { 85 needRead = true; 86 } 87 88 public void read() { 89 File root = null; 90 String [] sourcePaths = getProject().getSourcePathArray(); 91 92 for (int i = 0; i < sourcePaths.length; i++) { 93 sourcePath = sourcePaths[i]; 94 root = new File (sourcePath); 95 readDirectory(root); 96 } 97 makeData = new MakefileData[makeVector.size()]; 98 makeData = (MakefileData[]) makeVector.toArray(makeData); 99 makeVector.clear(); 100 makeCursor = null; 101 needRead = false; 102 } 103 104 public String [][] getPackageMap() { 105 String [][] map = new String [0][2]; 106 String [] docs = new String [0]; 107 String javaPackage = new String (); 108 Vector mapVector = new Vector (); 109 MapEntry[] entries = new MapEntry[0]; 110 PathHandle path = null; 111 112 if (needRead) { 113 read(); 114 } 115 for (int i = 0; i < makeData.length; i++) { 116 javaPackage = makeData[i].getJavaPackage(); 117 docs = makeData[i].getSourceDocPaths(); 118 for (int j = 0; j < docs.length; j++) { 119 MapEntry entry = null; 120 121 path = PathHandle.createPathHandle(docs[j]); 122 path = path.getParent(); 123 entry = new MapEntry(path.getPath(), javaPackage); 124 if (!mapVector.contains(entry)) { 125 mapVector.addElement(entry); 126 } 127 } 128 } 129 mapVector.trimToSize(); 130 entries = new MapEntry[mapVector.size()]; 131 entries = (MapEntry[]) mapVector.toArray(entries); 132 entries = MapUtil.optimize(entries); 133 map = MapUtil.toStringArray(entries); 134 entries = new MapEntry[0]; 135 mapVector.clear(); 136 return map; 137 } 138 139 public MakefileData[] getSymbolicMakefiles() { 140 MakefileData[] syms = new MakefileData[0]; 141 Vector symVector = new Vector (); 142 143 if (needRead) { 144 read(); 145 } 146 for (int i = 0; i < makeData.length; i++) { 147 if (makeData[i].isSymbolic()) { 148 symVector.addElement(makeData[i]); 149 } 150 } 151 syms = new MakefileData[symVector.size()]; 152 syms = (MakefileData[]) symVector.toArray(syms); 153 symVector.clear(); 154 return syms; 155 } 156 157 public void updateProject() { 158 if (needRead) { 159 read(); 160 } 161 for (int i = 0; i < makeData.length; i++) { 162 makeCursor = makeData[i]; 163 setupXMLCNodes(); 164 } 165 166 if (keepFound) { 168 project.setProperty(PropertyKeys.NAME_GENTO, (new String ()) + 0); 169 } 170 171 String [] src = new String [0]; 174 String out = new String (); 175 PathHandle outPath = null; 176 177 src = project.getSourcePathArray(); 178 out = project.getClassOutputPath(); 179 outPath = PathHandle.createPathHandle(out); 180 for (int i = 0; i < src.length; i++) { 181 PathHandle srcCursor = null; 182 183 srcCursor = PathHandle.createPathHandle(src[i]); 184 if (srcCursor.parentOf(outPath)) { 185 project.setProperty(PropertyKeys.NAME_AUTO_PACK, 186 Boolean.FALSE.toString()); 187 break; 188 } 189 } 190 } 191 192 private void readDirectory(File root) { 193 File [] list = new File [0]; 194 195 list = root.listFiles(filter); 196 if (list != null) { 197 for (int i = 0; i < list.length; i++) { 198 if (list[i].isDirectory()) { 199 readDirectory(list[i]); 200 } else { 201 readFile(list[i]); 202 } 203 } 204 } 205 } 206 207 private void setupXMLCNodes() { 208 OtterXMLCNode nodes[] = getProject().getAllXMLCNodes(); 209 210 for (int i = 0; i < nodes.length; i++) { 211 setupNodeSelection(nodes[i]); 212 setupNodeOption(nodes[i]); 213 } 214 } 215 216 private void setupNodeSelection(OtterXMLCNode node) { 217 String [] docPaths = makeCursor.getSourceDocPaths(); 218 String selectPath = new String (); 219 String optionFile = new String (); 220 String parameters = new String (); 221 PathHandle nodePath = null; 222 223 nodePath = PathHandle.createPathHandle(node.getFilePath()); 224 for (int i = 0; i < docPaths.length; i++) { 225 if (nodePath.equals(docPaths[i])) { 226 node.setSelected(true); 227 optionFile = makeCursor.getXMLCOptionFilePath(); 228 parameters = makeCursor.getXMLCParameters(); 229 if (optionFile.length() > 0) { 230 node.getParent().setXMLCOptionFilePath(optionFile); 231 } 232 if (parameters.length() > 0) { 233 node.getParent().setXMLCParameters(parameters); 234 } 235 break; 236 } 237 } 238 } 239 240 private void checkForKeep(String params) { 241 if (keepFound || (params == null) || (params.trim().length() == 0)) { 242 243 } else { 245 keepFound = (params.indexOf(KEEP) > -1); 246 } 247 } 248 249 private void setupNodeOption(OtterXMLCNode node) { 250 MakeItemData[] items = makeCursor.getItems(); 251 PathHandle docPath = null; 252 PathHandle nodePath = null; 253 StringBuffer pathBuf = new StringBuffer (); 254 boolean breakOut = false; 255 256 nodePath = PathHandle.createPathHandle(node.getFilePath()); 257 for (int i = 0; i < items.length; i++) { 258 for (int j = 0; j < docTypes.length; j++) { 259 pathBuf.setLength(0); 260 pathBuf.append(makeCursor.getDocPath()); 261 pathBuf.append(File.separator); 262 pathBuf.append(items[i].getItem()); 263 pathBuf.append('.'); 264 pathBuf.append(docTypes[j].toLowerCase()); 265 docPath = PathHandle.createPathHandle(pathBuf.toString()); 266 if (docPath.equals(nodePath)) { 267 node.setXMLCParameters(items[i].getOptionList()); 268 breakOut = true; 269 break; 270 } 271 } 272 if (breakOut) { 273 break; 274 } 275 } 276 } 277 278 private void readFile(File make) { 279 makeCursor = new MakefileData(make.getParent(), sourcePath); 280 BufferedReader reader = null; 281 282 try { 283 reader = new BufferedReader (new FileReader (make)); 284 String line = reader.readLine(); 285 286 while (line != null) { 287 readLine(line); 288 line = reader.readLine(); 289 } 290 reader.close(); 291 } catch (IOException e) { 292 e.printStackTrace(); 293 } 294 if (makeCursor.isXMLC()) { 295 makeVector.addElement(makeCursor); 296 } 297 } 298 299 private void readLine(String in) { 300 String line = new String (in.trim()); 301 String name = new String (); 302 String value = new String (); 303 int index = -1; 304 305 if (line.length() > 0) { 306 index = line.indexOf('='); 307 if (index > -1) { 308 if (line.indexOf(PE) > -1) { name = line.substring(0, index - 1).trim(); 310 value = line.substring(index + 1).trim(); 311 } else { 312 name = line.substring(0, index).trim(); 313 value = line.substring(index + 1).trim(); 314 } 315 } else if (continueLine) { 316 name = continueName; 317 value = line; 318 } 319 name = trimContinue(name); 320 value = trimContinue(value); 321 value = preprocessValue(value); 322 if (name.trim().length() == 0 || value.trim().length() == 0) { 323 324 } else if (name.equals(ROOT)) { 326 makeCursor.readRootPath(value); 327 } else if (name.equals(PACKAGEDIR)) { 328 makeCursor.readPackagePath(value); 329 } else { 330 boolean keyFound = false; 331 332 for (int i = 0; i < docTypes.length; i++) { 333 String type = docTypes[i].toUpperCase().trim(); 334 335 if (name.equals(XMLC_ + type + _OPTS_FILE)) { 336 makeCursor.readXMLCOptionFilePath(value); 337 keyFound = true; 338 break; 339 } else if (name.equals(XMLC_ + type + _OPTS)) { 340 checkForKeep(value); 341 makeCursor.setXMLCParameters(value); 342 keyFound = true; 343 break; 344 } else if (name.equals(type + _DIR)) { 345 makeCursor.readDocPath(value); 346 keyFound = true; 347 break; 348 } else if (name.equals(type + _CLASSES)) { 349 makeCursor.addSourceDocPath(value); 350 keyFound = true; 351 break; 352 } 353 } 354 if (!keyFound) { 355 if (name.startsWith(XMLC_) && name.endsWith(_OPTS)) { 356 String item = name.substring(XMLC_.length(), 357 (name.length() 358 - _OPTS.length())); 359 360 makeCursor.addItem(item, value); 361 } 362 } 363 } 364 } 365 continueLine = (line.length() > 0) 366 && (line.charAt(line.length() - 1) == '\\'); 367 if (name.length() > 0) { 368 continueName = name; 369 } 370 } 371 372 private String preprocessValue(String in) { 373 String out = null; 374 String key = new String (); 375 String remainder = new String (); 376 StringBuffer buf = new StringBuffer (); 377 int index = -1; 378 379 if (in == null) { 380 out = null; 381 } else { 382 out = new String (in); 383 key = IMPLEMENTS; 384 index = out.indexOf(key); 385 if (index > -1) { 386 remainder = out.substring(index + key.length()).trim(); 387 if (remainder.indexOf('.') == -1) { 388 buf.append(out.substring(0, index)); 389 buf.append(key); 390 buf.append(' '); 391 buf.append(makeCursor.getJavaPackage()); 392 buf.append('.'); 393 buf.append(remainder); 394 out = buf.toString(); 395 } 396 } 397 } 398 return out; 399 } 400 401 private String trimContinue(String in) { 402 String out = null; 403 404 if (in != null) { 405 out = new String (in); 406 while ((out.length() > 0) 407 && (out.charAt(out.length() - 1) == '\\')) { 408 out = out.substring(0, out.length() - 1).trim(); 409 } 410 } 411 return out; 412 } 413 414 } 415 | Popular Tags |