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 import java.nio.*; 19 import java.nio.channels.*; 20 21 import javax.xml.transform.*; 22 import javax.xml.transform.dom.*; 23 import javax.xml.transform.stream.*; 24 import org.w3c.dom.*; 25 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.DocumentBuilder ; 28 import org.apache.xml.serialize.XMLSerializer; 29 import org.apache.xml.serialize.OutputFormat; 30 import org.apache.xerces.parsers.DOMParser; 31 import org.xml.sax.*; 32 33 40 public class XML implements XMLInterface { 41 42 private LinkedHashMap idToPackage=new LinkedHashMap(); 43 private LinkedHashMap xmlFileToPackage=new LinkedHashMap(); 44 private LinkedHashMap pkgIdToFileContent=new LinkedHashMap(); 45 private LinkedHashMap packageToParentDirectory=new LinkedHashMap(); 46 47 private Map fileLocks=new HashMap(); 48 private Map rndAccessFiles=new HashMap(); 49 50 private String mainPackageReference; 51 52 private boolean fileLocking=true; 53 54 private Map parsingErrorMessages=new HashMap(); 55 private boolean isValidationON=true; 56 57 public void setValidation (boolean isActive) { 58 isValidationON=isActive; 59 } 60 61 public void setFileLockingStatus (boolean status) { 62 fileLocking=status; 63 } 64 65 public void clearParserErrorMessages () { 66 parsingErrorMessages.clear(); 67 } 68 69 74 public void register (Package pkg) { 75 idToPackage.put(pkg.get("Id").toString(),pkg); 76 } 77 78 84 public void registerPackageFilename (String filename,Package pkg) { 85 String pkgId=pkg.get("Id").toString(); 86 Iterator it=xmlFileToPackage.entrySet().iterator(); 87 String uToRem=getAbsoluteFilePath(pkg); 88 if (uToRem!=null) { 89 xmlFileToPackage.remove(uToRem); 90 } 91 try { 93 FileLock fl=(FileLock)fileLocks.remove(pkg); 94 fl.release(); 95 } catch (Exception ex) {} 96 try { 98 RandomAccessFile raf=(RandomAccessFile)rndAccessFiles.remove(pkg); 99 raf.close(); 100 } catch (Exception ex) {} 101 102 String cp=XMLUtil.getCanonicalPath(filename, false); 103 xmlFileToPackage.put(cp,pkg); 104 105 File f=new File(cp); 106 107 try { 108 RandomAccessFile raf=new RandomAccessFile(f,"rw"); 109 rndAccessFiles.put(pkg,raf); 110 if (fileLocking) { 111 FileLock fl=raf.getChannel().tryLock(); 112 if (fl!=null) { 113 fileLocks.put(pkg,fl); 114 } else { 116 System.out.println("Can't lock"); 117 } 118 } 119 } catch (IOException ex) { 122 } catch (Exception ex) {} 123 124 try { 126 packageToParentDirectory.put(pkg,f.getParentFile().getCanonicalPath()); 127 } catch (Exception ex) { 128 packageToParentDirectory.put(pkg,f.getParentFile().getAbsolutePath()); 129 } 130 } 131 132 public boolean isPackageOpened (String pkgId) { 133 return idToPackage.containsKey(pkgId); 134 } 135 136 public void putPkgIdToFileContentMapping (String pkgId, 137 String fileContent) { 138 pkgIdToFileContent.put(pkgId,fileContent); 140 } 141 142 public String getPackageFileContent (String pkgId) { 143 return (String )pkgIdToFileContent.get(pkgId); 144 } 145 146 public Package getPackageById (String pkgId) { 147 return (Package )idToPackage.get(pkgId); 148 } 149 150 public Package getPackageByFilename (String filename) { 151 filename=XMLUtil.getCanonicalPath(filename, false); 152 return (Package )xmlFileToPackage.get(filename); 153 } 154 155 public RandomAccessFile getRaf (Package pkg){ 156 return (RandomAccessFile)rndAccessFiles.get(pkg); 157 } 158 159 public Package getExternalPackageByRelativeFilePath ( 160 String relativePathToExtPkg,Package rootPkg) { 161 162 File f=new File(relativePathToExtPkg); 163 if (!f.isAbsolute()) { 164 f=new File(getParentDirectory(rootPkg)+File.separator+relativePathToExtPkg); 165 } 166 if (f.exists()) { 167 return getPackageByFilename(f.getAbsolutePath()); 169 } else { 170 return null; 172 } 173 } 174 175 public String getAbsoluteFilePath (Package pkg) { 176 Iterator it=xmlFileToPackage.entrySet().iterator(); 177 String fullPath=null; 178 while (it.hasNext()) { 179 Map.Entry me=(Map.Entry)it.next(); 180 String u=(String )me.getKey(); 181 Package p=(Package )me.getValue(); 182 if (p.equals(pkg)) { 183 fullPath=u; 184 break; 185 } 186 } 187 return fullPath; 188 } 189 190 public Collection getAllPackages () { 191 return idToPackage.values(); 192 } 193 194 public Collection getAllPackageIds () { 195 return idToPackage.keySet(); 196 } 197 198 public Collection getAllPackageFilenames () { 199 return xmlFileToPackage.keySet(); 200 } 201 202 public boolean doesPackageFileExists (String xmlFile) { 203 if (new File(xmlFile).exists() || getPackageFileContent(xmlFile)!=null) { 204 return true; 205 } else { 206 return false; 207 } 208 } 209 210 public String getParentDirectory (Package pkg) { 211 return (String )packageToParentDirectory.get(pkg); 212 } 213 214 public Package openPackage (String pkgReference,boolean openFromStream) { 215 parsingErrorMessages.clear(); 216 if (!openFromStream) { 217 mainPackageReference=pkgReference; 218 } 219 Set pre=new HashSet(getAllPackages()); 221 Package pkg=openDocument(pkgReference,openFromStream); 223 Set post=new HashSet(getAllPackages()); 225 post.removeAll(pre); 227 Iterator newPackages=post.iterator(); 233 while (newPackages.hasNext()) { 234 Package p=(Package )newPackages.next(); 235 appendAllExternalPackagesForPackage(p); 236 } 238 239 newPackages=post.iterator(); 242 while (newPackages.hasNext()) { 243 Package p=(Package )newPackages.next(); 244 p.afterImporting(); 245 } 246 if (pkg!=null && !openFromStream) { 247 System.setProperty("user.dir",getParentDirectory(pkg)); 248 } 249 return pkg; 251 } 252 253 public void printDebug () { 254 System.out.println("idToPackage="+idToPackage); 255 System.out.println("xmlFileToPackage="+xmlFileToPackage); 256 System.out.println("pkgIdToFileContent="+pkgIdToFileContent); 257 System.out.println("packageToWorkingDirectory="+packageToParentDirectory); 258 System.out.println("fileLocks="+fileLocks); 259 System.out.println("rndAccessFiles="+rndAccessFiles); 260 } 261 262 private Package openDocument (String pkgReference,boolean openFromStream) { 264 265 Package pkg=null; 266 File f=null; 267 String oldP=pkgReference; 268 269 if (!openFromStream) { 270 pkgReference=XMLUtil.getCanonicalPath(pkgReference, false); 271 if (pkgReference==null) { 272 Set fem=new HashSet(); 273 fem.add("File does not exist"); 274 parsingErrorMessages.put(oldP,fem); 275 return null; 276 } else { 277 278 f=new File(pkgReference); 279 try { 281 System.setProperty("user.dir",f.getParentFile().getCanonicalPath()); 282 } catch (Exception ex) { 283 System.setProperty("user.dir",f.getParentFile().getAbsolutePath()); 284 } 285 } 286 } 287 288 if (xmlFileToPackage.containsKey(pkgReference)) { 289 return getPackageByFilename(pkgReference); 290 } 291 292 if (openFromStream && idToPackage.containsKey(pkgReference)) { 293 return getPackageById(pkgReference); 294 } 295 296 if (!openFromStream) { 297 pkg=parseDocument(pkgReference,true); 298 try { 299 if (oldP.equals(mainPackageReference)) { 302 RandomAccessFile raf=new RandomAccessFile(f,"rw"); 303 rndAccessFiles.put(pkg,raf); 304 if (fileLocking) { 305 FileLock fl=raf.getChannel().tryLock(); 306 if (fl!=null) { 308 fileLocks.put(pkg,fl); 309 } else { 312 Set errorMessages = new HashSet(); 313 errorMessages.add(XMLUtil.getLanguageDependentString("ErrorTheFileIsLocked")); 314 parsingErrorMessages.put(pkgReference,errorMessages); 315 return null; 316 } 317 } 318 } else { 321 RandomAccessFile raf=new RandomAccessFile(f,"r"); 322 rndAccessFiles.put(pkg,raf); 323 if (fileLocking) { 324 FileLock fl=raf.getChannel().tryLock(0L,Long.MAX_VALUE,true); 325 if (fl!=null) { 328 fileLocks.put(pkg,fl); 329 } else { 331 Set errorMessages = new HashSet(); 332 errorMessages.add(XMLUtil.getLanguageDependentString("ErrorTheFileIsLocked")); 333 parsingErrorMessages.put(pkgReference,errorMessages); 334 return null; 335 } 336 } 337 } 338 } catch (Exception ex) { 340 } 342 } else { 343 pkg=parseDocument(getPackageFileContent(pkgReference),false); 344 } 345 346 if (pkg!=null) { 347 String pkgId=pkg.get("Id").toString(); 348 if (idToPackage.containsKey(pkgId)) { 350 if ((!openFromStream && xmlFileToPackage.containsKey(pkgReference)) || 352 (openFromStream && getPackageFileContent(pkgReference)!=null)) { 353 return getPackageById(pkgId); 354 } else { 355 return null; 356 } 357 } 358 idToPackage.put(pkgId,pkg); 359 if (!openFromStream) { 360 xmlFileToPackage.put(pkgReference,pkg); 361 try { 362 packageToParentDirectory.put(pkg,f.getParentFile().getCanonicalPath()); 363 } catch (Exception ex) { 364 packageToParentDirectory.put(pkg,f.getParentFile().getAbsolutePath()); 365 } 366 } 367 368 Iterator eps=((ExternalPackages)pkg.get("ExternalPackages")). 370 toCollection().iterator(); 371 while (eps.hasNext()) { 372 String pathToExtPackage=((ExternalPackage)eps.next()).get("href").toString(); 373 if (!openFromStream) { 375 System.setProperty("user.dir",packageToParentDirectory.get(pkg).toString()); 376 } 377 Package extPkg=openDocument(pathToExtPackage,openFromStream); 378 if (extPkg!=null) { 379 pkg.addExternalPackage(extPkg); 380 } 381 } 382 } else { 383 System.err.println("Problems with opening file "+pkgReference); 384 } 385 return pkg; 386 } 387 388 private void appendAllExternalPackagesForPackage(Package m) { 389 Stack s = new Stack(); 390 s.addAll(m.getAllExternalPackages()); 391 Set result=new HashSet(); 392 while (!s.isEmpty()) { 393 Package tmp=(Package )s.pop(); 394 Iterator extPkgs=tmp.getAllExternalPackages().iterator(); 395 while (extPkgs.hasNext()) { 396 Object nextP=extPkgs.next(); 397 if (!m.getAllExternalPackages().contains(nextP) && !s.contains(nextP) && nextP!=m) { 398 s.add(nextP); 399 m.addExternalPackage((Package )nextP); 400 } 401 } 402 } 403 } 404 405 public Package parseDocument (String toParse,boolean isFile) { 406 Package pkg=null; 407 DOMParser parser = new DOMParser(); 409 410 try { 412 parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error",true); 413 ParsingErrors pErrors=new ParsingErrors(); 414 parser.setErrorHandler(pErrors); 415 if (isValidationON) { 416 parser.setEntityResolver(new XPDLEntityResolver()); 417 parser.setFeature("http://xml.org/sax/features/validation",true); 418 parser.setFeature("http://apache.org/xml/features/validation/schema",true); 419 } 421 if (isFile) { 422 File f=new File(toParse); 424 if (!f.exists()) { 425 f=new File(f.getCanonicalPath()); 426 } 427 parser.parse(new InputSource(new FileInputStream(f))); } else { 430 parser.parse(new InputSource(new StringReader(toParse))); 432 } 433 Document document = parser.getDocument(); 434 Set errorMessages = pErrors.getErrorMessages(); 435 if (errorMessages.size()>0) { 436 if (isFile) { 438 parsingErrorMessages.put(toParse,errorMessages); 439 } else { 440 parsingErrorMessages.put("",errorMessages); 441 } 442 } 443 if (document!=null) { 444 pkg=new Package (this); 445 pkg.fromXML(document.getDocumentElement()); 446 } 448 } catch (Exception ex) { 449 System.err.println("Fatal error while parsing document"); 451 Set fem=new HashSet(); 452 fem.add("Fatal error while parsing document"); 453 if (isFile) { 454 parsingErrorMessages.put(toParse,fem); 455 } else { 456 parsingErrorMessages.put("",fem); 457 } 458 return null; 459 } 460 return pkg; 461 } 462 463 469 public Map getParsingErrorMessages () { 470 return parsingErrorMessages; 471 } 472 473 public void closePackage (String pkgId) { 474 Package toRemove=(Package )idToPackage.remove(pkgId); 475 if (toRemove!=null) { 476 Iterator it=xmlFileToPackage.entrySet().iterator(); 478 Object keyToRemove=null; 479 while (it.hasNext()) { 480 Map.Entry me=(Map.Entry)it.next(); 481 Object key=me.getKey(); 482 Object val=me.getValue(); 483 if (val.equals(toRemove)) { 484 keyToRemove=key; 485 break; 486 } 487 } 488 if (keyToRemove!=null) { 489 xmlFileToPackage.remove(keyToRemove); 490 } 491 492 packageToParentDirectory.remove(toRemove); 493 RandomAccessFile raf=(RandomAccessFile)rndAccessFiles.remove(toRemove); 495 try { 496 raf.close(); 497 } catch (Exception ex) {} 498 FileLock fl=(FileLock)fileLocks.remove(toRemove); 500 try { 501 fl.release(); 502 } catch (Exception ex) {} 503 } 504 505 pkgIdToFileContent.remove(pkgId); 506 } 507 508 public void closeAllPackages () { 509 idToPackage.clear(); 510 xmlFileToPackage.clear(); 511 packageToParentDirectory.clear(); 512 pkgIdToFileContent.clear(); 513 Iterator it=rndAccessFiles.values().iterator(); 515 while (it.hasNext()) { 516 RandomAccessFile raf=(RandomAccessFile)it.next(); 517 try { 518 raf.close(); 519 } catch (Exception ex) {} 520 } 521 rndAccessFiles.clear(); 522 unlockAllFiles(); 523 } 524 525 public void unlockAllFiles () { 526 Iterator it=fileLocks.values().iterator(); 528 while (it.hasNext()) { 529 FileLock fl=(FileLock)it.next(); 530 try { 531 fl.release(); 532 } catch (Exception ex) {} 533 } 534 fileLocks.clear(); 535 } 536 537 public void lockAllFiles () { 538 if (!fileLocking) return; 539 Iterator it=rndAccessFiles.entrySet().iterator(); 540 while (it.hasNext()) { 541 Map.Entry me=(Map.Entry)it.next(); 542 Package pkg=(Package )me.getKey(); 543 RandomAccessFile raf=(RandomAccessFile)me.getValue(); 544 545 try { 546 if (pkg.equals(mainPackageReference)) { 548 FileLock fl=raf.getChannel().tryLock(); 549 if (fl!=null) { 551 fileLocks.put(pkg,fl); 552 } else { 555 continue; 556 } 557 } else { 559 FileLock fl=raf.getChannel().tryLock(0L,Long.MAX_VALUE,true); 560 if (fl!=null) { 563 fileLocks.put(pkg,fl); 564 } else { 566 continue; 567 } 568 } 569 } catch (Exception ex) { 571 } 573 } 574 } 575 576 public String getIDFromFile (String xmlFile) { 577 try { 578 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 580 factory.setValidating(false); 581 DocumentBuilder parser = factory.newDocumentBuilder(); 582 Document document=null; 583 584 try { 586 File f=new File(xmlFile); 587 if (!f.exists()) { 588 f=new File(f.getCanonicalPath()); 589 } 590 document=parser.parse(new InputSource(new FileInputStream(f))); } catch (Exception ex) { 593 document=parser.parse(new InputSource(new StringReader(getPackageFileContent(xmlFile)))); 594 } 595 return XMLUtil.getID(document.getDocumentElement()); 596 } catch (Exception ex) { 597 return ""; 598 } 599 } 600 601 } 602 603 | Popular Tags |