1 package org.enhydra.shark.xpdl; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.StringReader ; 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.HashSet ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 import javax.xml.parsers.DocumentBuilder ; 16 import javax.xml.parsers.DocumentBuilderFactory ; 17 18 import org.apache.xerces.parsers.DOMParser; 19 import org.enhydra.shark.utilities.SequencedHashMap; 20 import org.enhydra.shark.xpdl.elements.ExternalPackage; 21 import org.enhydra.shark.xpdl.elements.Package; 22 import org.w3c.dom.Document ; 23 import org.xml.sax.InputSource ; 24 25 30 public class XMLInterfaceForJDK13 implements XMLInterface { 31 32 protected Map idToPackages=new SequencedHashMap(); 33 protected Map xmlFileToPackage=new SequencedHashMap(); 34 protected Map packageToParentDirectory=new SequencedHashMap(); 35 36 protected String mainPackageReference; 37 38 protected Map parsingErrorMessages=new SequencedHashMap(); 39 protected boolean isValidationON=true; 40 public void setValidation (boolean isActive) { 41 isValidationON=isActive; 42 } 43 44 public void clearParserErrorMessages () { 45 parsingErrorMessages.clear(); 46 } 47 48 public synchronized boolean isPackageOpened (String pkgId) { 49 return idToPackages.containsKey(pkgId); 50 } 51 52 public synchronized Package getPackageById (String pkgId) { 53 ArrayList l=(ArrayList )idToPackages.get(pkgId); 54 Package toRet=null; 55 if (l!=null) { 56 Iterator it=l.iterator(); 57 String lastVersion="-1"; 58 while (it.hasNext()) { 59 Package p=(Package )it.next(); 60 String v=p.getInternalVersion(); 61 if (v.compareTo(lastVersion)>=0) { 62 lastVersion=v; 63 toRet=p; 64 } 65 } 66 } 67 return toRet; 68 } 69 70 public synchronized Package getPackageByIdAndVersion (String pkgId,String version) { 71 ArrayList l=(ArrayList )idToPackages.get(pkgId); 72 Package toRet=null; 73 if (l!=null) { 74 Iterator it=l.iterator(); 75 while (it.hasNext()) { 76 Package p=(Package )it.next(); 77 String v=p.getInternalVersion(); 78 if (v.equals(version)) { 79 toRet=p; 80 break; 81 } 82 } 83 } 84 return toRet; 85 } 86 87 public synchronized Package getPackageByFilename (String filename) { 88 filename=XMLUtil.getCanonicalPath(filename,"",false); 89 return (Package )xmlFileToPackage.get(filename); 90 } 91 92 public synchronized Package getExternalPackageByRelativeFilePath ( 93 String relativePathToExtPkg,Package rootPkg) { 94 95 File f=new File (relativePathToExtPkg); 96 if (!f.isAbsolute()) { 97 f=new File (getParentDirectory(rootPkg)+File.separator+relativePathToExtPkg); 98 } 99 if (f.exists()) { 100 return getPackageByFilename(f.getAbsolutePath()); 102 } else { 103 return null; 105 } 106 } 107 108 public synchronized String getAbsoluteFilePath (Package pkg) { 109 Iterator it=xmlFileToPackage.entrySet().iterator(); 110 String fullPath=null; 111 while (it.hasNext()) { 112 Map.Entry me=(Map.Entry )it.next(); 113 String u=(String )me.getKey(); 114 Package p=(Package )me.getValue(); 115 if (p.equals(pkg)) { 116 fullPath=u; 117 break; 118 } 119 } 120 return fullPath; 121 } 122 123 public synchronized Collection getAllPackages () { 124 ArrayList l=new ArrayList (); 125 Iterator it=idToPackages.values().iterator(); 126 while (it.hasNext()) { 127 l.addAll((ArrayList )it.next()); 128 } 129 return l; 130 } 131 132 public synchronized Collection getAllPackageIds () { 133 return idToPackages.keySet(); 134 } 135 136 public Collection getAllPackageVersions (String pkgId) { 137 ArrayList l=new ArrayList (); 138 ArrayList all=(ArrayList )idToPackages.get(pkgId); 139 if (all!=null) { 140 Iterator it=all.iterator(); 141 while (it.hasNext()) { 142 l.add(((Package )it.next()).getInternalVersion()); 143 } 144 } 145 return l; 146 } 147 148 public synchronized Collection getAllPackageFilenames () { 149 return xmlFileToPackage.keySet(); 150 } 151 152 public synchronized boolean doesPackageFileExists (String xmlFile) { 153 if (new File (xmlFile).exists()) { return true; 155 } else { 156 return false; 157 } 158 } 159 160 public synchronized String getParentDirectory (Package pkg) { 161 return (String )packageToParentDirectory.get(pkg); 162 } 163 164 public Package openPackage (String pkgReference, 165 boolean handleExternalPackages) { 166 parsingErrorMessages.clear(); 169 mainPackageReference=pkgReference; 170 171 Package pkg=openPackageRecursively(pkgReference,handleExternalPackages); 174 175 return pkg; 179 } 180 181 public void printDebug () { 182 System.out.println("idToPackage="+idToPackages); 183 System.out.println("xmlFileToPackage="+xmlFileToPackage); 184 System.out.println("packageToWorkingDirectory="+packageToParentDirectory); 185 } 187 188 protected Package openPackageRecursively (String pkgReference, 191 boolean handleExternalPackages) { 192 193 Package pkg=null; 194 File f=null; 195 String oldP=pkgReference; 196 197 String baseDirectory=null; 198 pkgReference=XMLUtil.getCanonicalPath(pkgReference,"",false); 199 if (pkgReference==null) { 200 Set fem=new HashSet (); 201 fem.add("File does not exist"); 202 parsingErrorMessages.put(oldP,fem); 203 return null; 204 } else { 205 f=new File (pkgReference); 206 try { 207 baseDirectory=f.getParentFile().getCanonicalPath(); 208 } catch (Exception ex) { 209 baseDirectory=f.getParentFile().getAbsolutePath(); 210 } 211 } 212 213 if (xmlFileToPackage.containsKey(pkgReference)) { 214 return getPackageByFilename(pkgReference); 215 } 216 217 pkg=parseDocument(pkgReference,true); 218 219 if (pkg!=null) { 220 String pkgId=pkg.getId(); 221 if (idToPackages.containsKey(pkgId)) { 223 if (xmlFileToPackage.containsKey(pkgReference)) { 225 return getPackageById(pkgId); 226 } else { 227 throw new RuntimeException ("Can't open two packages with the same Id"); 228 } 229 } 230 ArrayList l=(ArrayList )idToPackages.get(pkgId); 231 if (l==null) { 232 l=new ArrayList (); 233 } 234 l.add(pkg); 235 idToPackages.put(pkgId,l); 236 xmlFileToPackage.put(pkgReference,pkg); 237 try { 238 packageToParentDirectory.put(pkg,f.getParentFile().getCanonicalPath()); 239 } catch (Exception ex) { 240 packageToParentDirectory.put(pkg,f.getParentFile().getAbsolutePath()); 241 } 242 243 Iterator eps=pkg.getExternalPackages().toElements().iterator(); 247 while (eps.hasNext()) { 248 String pathToExtPackage=((ExternalPackage)eps.next()).getHref(); 249 String extPkgId=null; 250 if (handleExternalPackages) { 251 String ptep=XMLUtil.getCanonicalPath(pathToExtPackage,baseDirectory,false); 253 Package extPkg=openPackageRecursively(ptep,handleExternalPackages); 255 extPkgId=extPkg.getId(); 256 } else { 257 extPkgId=XMLUtil.getExternalPackageId(pathToExtPackage); 258 } 259 pkg.addExternalPackageMapping(pathToExtPackage, extPkgId); 260 } 261 } else { 262 System.err.println("Problems with opening file "+pkgReference); 263 } 264 return pkg; 265 } 266 267 268 275 public Package openPackagesFromStreams (List pkgContents,boolean isFileStream) throws Exception { 276 Package pkg=null; 277 for (int i=0; i<pkgContents.size(); i++) { 278 byte[] pkgCont=(byte[])pkgContents.get(i); 279 Package p=openPackageFromStream(pkgCont,isFileStream); 280 if (i==0) { 281 pkg=p; 282 } 283 } 284 return pkg; 285 } 286 287 293 public Package openPackageFromStream (byte[] pkgContent,boolean isFileStream) throws Exception { 294 Package pkg=null; 295 if (isFileStream) { 296 String fileContStr=new String (pkgContent,"UTF8"); 297 pkg=parseDocument(fileContStr, false); 298 } else { 299 pkg=(Package )XMLUtil.deserialize(pkgContent); 300 } 301 if (pkg!=null) { 302 String pkgId=pkg.getId(); 303 ArrayList l=(ArrayList )idToPackages.get(pkgId); 304 if (l==null) { 305 l=new ArrayList (); 306 } 307 if (!l.contains(pkg)) { 308 l.add(pkg); 309 } 310 idToPackages.put(pkgId,l); 311 Iterator eps=pkg.getExternalPackages().toElements().iterator(); 312 while (eps.hasNext()) { 313 String pathToExtPackage=((ExternalPackage)eps.next()).getHref(); 314 String extPkgId=pkg.getExternalPackageId(pathToExtPackage); 315 if (extPkgId==null) { 316 extPkgId=XMLUtil.getExternalPackageId(pathToExtPackage); 317 pkg.addExternalPackageMapping(pathToExtPackage, extPkgId); 318 } 319 } 320 } 321 return pkg; 322 } 323 324 public Package parseDocument (String toParse,boolean isFile) { 325 Package pkg=null; 328 DOMParser parser = new DOMParser(); 330 331 try { 333 parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error",true); 334 ParsingErrors pErrors=new ParsingErrors(); 335 parser.setErrorHandler(pErrors); 336 if (isValidationON) { 337 parser.setEntityResolver(new XPDLEntityResolver()); 338 parser.setFeature("http://xml.org/sax/features/validation",true); 339 parser.setFeature("http://apache.org/xml/features/validation/schema",true); 340 } 342 if (isFile) { 343 File f=new File (toParse); 346 if (!f.exists()) { 347 f=new File (f.getCanonicalPath()); 348 } 349 parser.parse(new InputSource (new FileInputStream (f))); } else { 351 parser.parse(new InputSource (new StringReader (toParse))); 353 } 354 Document document = parser.getDocument(); 355 Set errorMessages = pErrors.getErrorMessages(); 356 if (errorMessages.size()>0) { 357 if (isFile) { 359 parsingErrorMessages.put(toParse,errorMessages); 360 } else { 361 parsingErrorMessages.put("",errorMessages); 362 } 363 } 364 if (document!=null) { 365 pkg=new Package (); 367 XMLUtil.fromXML(document.getDocumentElement(),pkg); 369 } 371 } catch (Exception ex) { 372 ex.printStackTrace(); 373 System.err.println("Fatal error while parsing document"); 374 Set fem=new HashSet (); 375 fem.add("Fatal error while parsing document"); 376 if (isFile) { 377 parsingErrorMessages.put(toParse,fem); 378 } else { 379 parsingErrorMessages.put("",fem); 380 } 381 return null; 382 } 383 388 return pkg; 389 } 390 391 397 public Map getParsingErrorMessages () { 398 return parsingErrorMessages; 399 } 400 401 public synchronized List closePackages (String pkgId) { 402 ArrayList l=(ArrayList )idToPackages.remove(pkgId); 403 if (l!=null) { 404 Iterator itr=l.iterator(); 406 while (itr.hasNext()) { 407 Package toRemove=(Package )itr.next(); 408 Iterator it=xmlFileToPackage.entrySet().iterator(); 409 Object keyToRemove=null; 410 while (it.hasNext()) { 411 Map.Entry me=(Map.Entry )it.next(); 412 Object key=me.getKey(); 413 Object val=me.getValue(); 414 if (val.equals(toRemove)) { 415 keyToRemove=key; 416 break; 417 } 418 } 419 if (keyToRemove!=null) { 420 xmlFileToPackage.remove(keyToRemove); 421 } 422 packageToParentDirectory.remove(toRemove); 423 } 424 425 } 426 return l; 427 } 428 429 public synchronized Package closePackageVersion (String pkgId,String pkgVer) { 430 ArrayList l=(ArrayList )idToPackages.get(pkgId); 431 Package toRemove=null; 432 if (l!=null) { 433 Iterator itr=l.iterator(); 435 while (itr.hasNext()) { 436 Package p=(Package )itr.next(); 437 if (p.getInternalVersion().equals(pkgVer)) { 438 toRemove=p; 439 break; 440 } 441 } 442 if (toRemove!=null) { 443 Iterator it=xmlFileToPackage.entrySet().iterator(); 444 Object keyToRemove=null; 445 while (it.hasNext()) { 446 Map.Entry me=(Map.Entry )it.next(); 447 Object key=me.getKey(); 448 Object val=me.getValue(); 449 if (val.equals(toRemove)) { 450 keyToRemove=key; 451 break; 452 } 453 } 454 if (keyToRemove!=null) { 455 xmlFileToPackage.remove(keyToRemove); 456 } 457 packageToParentDirectory.remove(toRemove); 458 } 459 460 } 461 return toRemove; 462 } 463 464 public synchronized void closeAllPackages () { 465 471 idToPackages.clear(); 472 xmlFileToPackage.clear(); 473 packageToParentDirectory.clear(); 474 } 475 476 public synchronized String getIdFromFile (String xmlFile) { 477 try { 478 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 480 factory.setValidating(false); 481 DocumentBuilder parser = factory.newDocumentBuilder(); 482 Document document=null; 483 484 try { 486 File f=new File (xmlFile); 488 if (!f.exists()) { 489 f=new File (f.getCanonicalPath()); 490 } 491 document=parser.parse(new InputSource (new FileInputStream (f))); } catch (Exception ex) { 493 document=parser.parse(new InputSource (new StringReader (xmlFile))); 494 } 495 return XMLUtil.getId(document.getDocumentElement()); 496 } catch (Exception ex) { 497 return ""; 498 } 499 } 500 501 public synchronized void synchronizePackages (XMLInterface xmlInterface) { 502 closeAllPackages(); 503 504 Iterator it=xmlInterface.getAllPackages().iterator(); 505 while (it.hasNext()) { 506 Package pkg=(Package )it.next(); 507 String pkgId=pkg.getId(); 508 509 ArrayList l=(ArrayList )idToPackages.get(pkgId); 510 if (l==null) { 511 l=new ArrayList (); 512 } 513 l.add(pkg); 514 idToPackages.put(pkgId,l); 515 String fp=xmlInterface.getAbsoluteFilePath(pkg); 516 if (fp!=null) { 517 xmlFileToPackage.put(fp,pkg); 518 } 519 String pd=xmlInterface.getParentDirectory(pkg); 520 if (pd!=null) { 521 packageToParentDirectory.put(pkg,pd); 522 } 523 } 524 } 525 526 } 527 528 | Popular Tags |