1 10 11 package org.enhydra.jawe.xml; 12 13 import org.enhydra.jawe.xml.elements.*; 14 import org.enhydra.jawe.xml.elements.Package; 15 16 import java.util.*; 17 import java.io.*; 18 19 import org.w3c.dom.*; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.DocumentBuilder ; 22 import org.apache.xerces.parsers.DOMParser; 23 import org.xml.sax.*; 24 25 32 public class XMLInterfaceForJDK13 implements XMLInterface { 33 34 protected Map idToPackage=new HashMap(); 35 protected Map xmlFileToPackage=new HashMap(); 36 protected Map pkgIdToFileContent=new HashMap(); 37 protected Map packageToParentDirectory=new HashMap(); 38 39 protected String mainPackageReference; 40 41 protected Map parsingErrorMessages=new HashMap(); 42 protected boolean isValidationON=true; 43 44 public void setValidation (boolean isActive) { 45 isValidationON=isActive; 46 } 47 48 public void clearParserErrorMessages () { 49 parsingErrorMessages.clear(); 50 } 51 52 57 public void register (Package pkg) { 58 idToPackage.put(pkg.get("Id").toString(),pkg); 59 } 60 61 67 public void registerPackageFilename (String filename,Package pkg) { 68 String pkgId=pkg.get("Id").toString(); 69 Iterator it=xmlFileToPackage.entrySet().iterator(); 70 String uToRem=getAbsoluteFilePath(pkg); 71 if (uToRem!=null) { 72 xmlFileToPackage.remove(uToRem); 73 } 74 75 String cp=XMLUtil.getCanonicalPath(filename, false); 76 xmlFileToPackage.put(cp,pkg); 77 78 File f=new File(cp); 79 80 try { 82 packageToParentDirectory.put(pkg,f.getParentFile().getCanonicalPath()); 83 } catch (Exception ex) { 84 packageToParentDirectory.put(pkg,f.getParentFile().getAbsolutePath()); 85 } 86 } 87 88 public boolean isPackageOpened (String pkgId) { 89 return idToPackage.containsKey(pkgId); 90 } 91 92 public void putPkgIdToFileContentMapping (String pkgId, 93 String fileContent) { 94 pkgIdToFileContent.put(pkgId,fileContent); 96 } 97 98 public String getPackageFileContent (String pkgId) { 99 return (String )pkgIdToFileContent.get(pkgId); 100 } 101 102 public Package getPackageById (String pkgId) { 103 return (Package )idToPackage.get(pkgId); 104 } 105 106 public Package getPackageByFilename (String filename) { 107 filename=XMLUtil.getCanonicalPath(filename, false); 108 return (Package )xmlFileToPackage.get(filename); 109 } 110 111 public Package getExternalPackageByRelativeFilePath ( 112 String relativePathToExtPkg,Package rootPkg) { 113 114 File f=new File(relativePathToExtPkg); 115 if (!f.isAbsolute()) { 116 f=new File(getParentDirectory(rootPkg)+File.separator+relativePathToExtPkg); 117 } 118 if (f.exists()) { 119 return getPackageByFilename(f.getAbsolutePath()); 121 } else { 122 return null; 124 } 125 } 126 127 public String getAbsoluteFilePath (Package pkg) { 128 Iterator it=xmlFileToPackage.entrySet().iterator(); 129 String fullPath=null; 130 while (it.hasNext()) { 131 Map.Entry me=(Map.Entry)it.next(); 132 String u=(String )me.getKey(); 133 Package p=(Package )me.getValue(); 134 if (p.equals(pkg)) { 135 fullPath=u; 136 break; 137 } 138 } 139 return fullPath; 140 } 141 142 public Collection getAllPackages () { 143 return idToPackage.values(); 144 } 145 146 public Collection getAllPackageIds () { 147 return idToPackage.keySet(); 148 } 149 150 public Collection getAllPackageFilenames () { 151 return xmlFileToPackage.keySet(); 152 } 153 154 public boolean doesPackageFileExists (String xmlFile) { 155 if (new File(xmlFile).exists() || getPackageFileContent(xmlFile)!=null) { 156 return true; 157 } else { 158 return false; 159 } 160 } 161 162 public String getParentDirectory (Package pkg) { 163 return (String )packageToParentDirectory.get(pkg); 164 } 165 166 public Package openPackage (String pkgReference,boolean openFromStream) { 167 parsingErrorMessages.clear(); 168 if (!openFromStream) { 169 mainPackageReference=pkgReference; 170 } 171 Set pre=new HashSet(getAllPackages()); 173 Package pkg=openDocument(pkgReference,openFromStream); 175 Set post=new HashSet(getAllPackages()); 177 post.removeAll(pre); 179 Iterator newPackages=post.iterator(); 185 while (newPackages.hasNext()) { 186 Package p=(Package )newPackages.next(); 187 appendAllExternalPackagesForPackage(p); 188 } 190 191 newPackages=post.iterator(); 194 while (newPackages.hasNext()) { 195 Package p=(Package )newPackages.next(); 196 p.afterImporting(); 197 } 198 if (pkg!=null && !openFromStream) { 199 System.setProperty("user.dir",getParentDirectory(pkg)); 200 } 201 return pkg; 203 } 204 205 public void printDebug () { 206 System.out.println("idToPackage="+idToPackage); 207 System.out.println("xmlFileToPackage="+xmlFileToPackage); 208 System.out.println("pkgIdToFileContent="+pkgIdToFileContent); 209 System.out.println("packageToWorkingDirectory="+packageToParentDirectory); 210 } 211 212 protected Package openDocument (String pkgReference,boolean openFromStream) { 214 215 Package pkg=null; 216 File f=null; 217 String oldP=pkgReference; 218 219 if (!openFromStream) { 220 pkgReference=XMLUtil.getCanonicalPath(pkgReference, false); 221 if (pkgReference==null) { 222 Set fem=new HashSet(); 223 fem.add("File does not exist"); 224 parsingErrorMessages.put(oldP,fem); 225 return null; 226 } else { 227 228 f=new File(pkgReference); 229 try { 231 System.setProperty("user.dir",f.getParentFile().getCanonicalPath()); 232 } catch (Exception ex) { 233 System.setProperty("user.dir",f.getParentFile().getAbsolutePath()); 234 } 235 } 236 } 237 238 if (xmlFileToPackage.containsKey(pkgReference)) { 239 return getPackageByFilename(pkgReference); 240 } 241 242 if (!openFromStream) { 243 pkg=parseDocument(pkgReference,true); 244 288 } else { 289 pkg=parseDocument(getPackageFileContent(pkgReference),false); 290 } 291 292 if (pkg!=null) { 293 String pkgId=pkg.get("Id").toString(); 294 if (idToPackage.containsKey(pkgId)) { 296 if ((!openFromStream && xmlFileToPackage.containsKey(pkgReference)) || 298 (openFromStream && getPackageFileContent(pkgReference)!=null)) { 299 return getPackageById(pkgId); 300 } else { 301 return null; 302 } 303 } 304 idToPackage.put(pkgId,pkg); 305 if (!openFromStream) { 306 xmlFileToPackage.put(pkgReference,pkg); 307 try { 308 packageToParentDirectory.put(pkg,f.getParentFile().getCanonicalPath()); 309 } catch (Exception ex) { 310 packageToParentDirectory.put(pkg,f.getParentFile().getAbsolutePath()); 311 } 312 } 313 314 Iterator eps=((ExternalPackages)pkg.get("ExternalPackages")). 316 toCollection().iterator(); 317 while (eps.hasNext()) { 318 String pathToExtPackage=((ExternalPackage)eps.next()).get("href").toString(); 319 if (!openFromStream) { 321 System.setProperty("user.dir",packageToParentDirectory.get(pkg).toString()); 322 } 323 Package extPkg=openDocument(pathToExtPackage,openFromStream); 324 if (extPkg!=null) { 325 pkg.addExternalPackage(extPkg); 326 } 327 } 328 } else { 329 System.err.println("Problems with opening file "+pkgReference); 330 } 331 return pkg; 332 } 333 334 protected void appendAllExternalPackagesForPackage(Package m) { 335 Stack s = new Stack(); 336 s.addAll(m.getAllExternalPackages()); 337 Set result=new HashSet(); 338 while (!s.isEmpty()) { 339 Package tmp=(Package )s.pop(); 340 Iterator extPkgs=tmp.getAllExternalPackages().iterator(); 341 while (extPkgs.hasNext()) { 342 Object nextP=extPkgs.next(); 343 if (!m.getAllExternalPackages().contains(nextP) && !s.contains(nextP) && nextP!=m) { 344 s.add(nextP); 345 m.addExternalPackage((Package )nextP); 346 } 347 } 348 } 349 } 350 351 public Package parseDocument (String toParse,boolean isFile) { 352 Package pkg=null; 353 DOMParser parser = new DOMParser(); 355 356 try { 358 parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error",true); 359 ParsingErrors pErrors=new ParsingErrors(); 360 parser.setErrorHandler(pErrors); 361 if (isValidationON) { 362 parser.setEntityResolver(new XPDLEntityResolver()); 363 parser.setFeature("http://xml.org/sax/features/validation",true); 364 parser.setFeature("http://apache.org/xml/features/validation/schema",true); 365 } 367 if (isFile) { 368 File f=new File(toParse); 370 if (!f.exists()) { 371 f=new File(f.getCanonicalPath()); 372 } 373 parser.parse(new InputSource(new FileInputStream(f))); } else { 376 parser.parse(new InputSource(new StringReader(toParse))); 378 } 379 Document document = parser.getDocument(); 380 Set errorMessages = pErrors.getErrorMessages(); 381 if (errorMessages.size()>0) { 382 if (isFile) { 384 parsingErrorMessages.put(toParse,errorMessages); 385 } else { 386 parsingErrorMessages.put("",errorMessages); 387 } 388 } 389 if (document!=null) { 390 pkg=new Package (this); 391 pkg.fromXML(document.getDocumentElement()); 392 } 394 } catch (Exception ex) { 395 System.err.println("Fatal error while parsing document"); 397 Set fem=new HashSet(); 398 fem.add("Fatal error while parsing document"); 399 if (isFile) { 400 parsingErrorMessages.put(toParse,fem); 401 } else { 402 parsingErrorMessages.put("",fem); 403 } 404 return null; 405 } 406 return pkg; 407 } 408 409 415 public Map getParsingErrorMessages () { 416 return parsingErrorMessages; 417 } 418 419 public void closePackage (String pkgId) { 420 Package toRemove=(Package )idToPackage.remove(pkgId); 421 if (toRemove!=null) { 422 Iterator it=xmlFileToPackage.entrySet().iterator(); 424 Object keyToRemove=null; 425 while (it.hasNext()) { 426 Map.Entry me=(Map.Entry)it.next(); 427 Object key=me.getKey(); 428 Object val=me.getValue(); 429 if (val.equals(toRemove)) { 430 keyToRemove=key; 431 break; 432 } 433 } 434 if (keyToRemove!=null) { 435 xmlFileToPackage.remove(keyToRemove); 436 } 437 438 packageToParentDirectory.remove(toRemove); 439 } 440 441 pkgIdToFileContent.remove(pkgId); 442 } 443 444 public void closeAllPackages () { 445 idToPackage.clear(); 446 xmlFileToPackage.clear(); 447 packageToParentDirectory.clear(); 448 pkgIdToFileContent.clear(); 449 } 450 451 public String getIDFromFile (String xmlFile) { 452 try { 453 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 455 factory.setValidating(false); 456 DocumentBuilder parser = factory.newDocumentBuilder(); 457 Document document=null; 458 459 try { 461 File f=new File(xmlFile); 462 if (!f.exists()) { 463 f=new File(f.getCanonicalPath()); 464 } 465 document=parser.parse(new InputSource(new FileInputStream(f))); } catch (Exception ex) { 468 document=parser.parse(new InputSource(new StringReader(getPackageFileContent(xmlFile)))); 469 } 470 return XMLUtil.getID(document.getDocumentElement()); 471 } catch (Exception ex) { 472 return ""; 473 } 474 } 475 476 } 477 478 | Popular Tags |