1 3 19 20 package com.sun.org.apache.xml.internal.resolver; 21 22 import java.io.InputStream ; 23 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 27 import java.util.MissingResourceException ; 28 import java.util.PropertyResourceBundle ; 29 import java.util.ResourceBundle ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 33 import com.sun.org.apache.xml.internal.resolver.helpers.Debug; 34 import com.sun.org.apache.xml.internal.resolver.helpers.BootstrapResolver; 35 import com.sun.org.apache.xml.internal.resolver.Catalog; 36 37 123 124 public class CatalogManager { 125 private static String pFiles = "xml.catalog.files"; 126 private static String pVerbosity = "xml.catalog.verbosity"; 127 private static String pPrefer = "xml.catalog.prefer"; 128 private static String pStatic = "xml.catalog.staticCatalog"; 129 private static String pAllowPI = "xml.catalog.allowPI"; 130 private static String pClassname = "xml.catalog.className"; 131 private static String pIgnoreMissing = "xml.catalog.ignoreMissing"; 132 133 134 private static CatalogManager staticManager = new CatalogManager(); 135 136 137 private BootstrapResolver bResolver = new BootstrapResolver(); 138 139 140 private boolean ignoreMissingProperties 141 = (System.getProperty(pIgnoreMissing) != null 142 || System.getProperty(pFiles) != null); 143 144 145 private ResourceBundle resources; 146 147 148 private String propertyFile = "CatalogManager.properties"; 149 150 151 private URL propertyFileURI = null; 152 153 154 private String defaultCatalogFiles = "./xcatalog"; 155 156 157 private String catalogFiles = null; 158 159 160 private boolean fromPropertiesFile = false; 161 162 163 private int defaultVerbosity = 1; 164 165 166 private Integer verbosity = null; 167 168 169 private boolean defaultPreferPublic = true; 170 171 172 private Boolean preferPublic = null; 173 174 175 private boolean defaultUseStaticCatalog = true; 176 177 178 private Boolean useStaticCatalog = null; 179 180 181 private static Catalog staticCatalog = null; 182 183 184 private boolean defaultOasisXMLCatalogPI = true; 185 186 187 private Boolean oasisXMLCatalogPI = null; 188 189 190 private boolean defaultRelativeCatalogs = true; 191 192 193 private Boolean relativeCatalogs = null; 194 195 196 private String catalogClassName = null; 197 198 203 public Debug debug = null; 204 205 206 public CatalogManager() { 207 debug = new Debug(); 208 } 214 215 216 public CatalogManager(String propertyFile) { 217 this.propertyFile = propertyFile; 218 219 debug = new Debug(); 220 } 226 227 228 public void setBootstrapResolver(BootstrapResolver resolver) { 229 bResolver = resolver; 230 } 231 232 233 public BootstrapResolver getBootstrapResolver() { 234 return bResolver; 235 } 236 237 241 private synchronized void readProperties() { 242 try { 243 propertyFileURI = CatalogManager.class.getResource("/"+propertyFile); 244 InputStream in = 245 CatalogManager.class.getResourceAsStream("/"+propertyFile); 246 if (in==null) { 247 if (!ignoreMissingProperties) { 248 System.err.println("Cannot find "+propertyFile); 249 ignoreMissingProperties = true; 251 } 252 return; 253 } 254 resources = new PropertyResourceBundle (in); 255 } catch (MissingResourceException mre) { 256 if (!ignoreMissingProperties) { 257 System.err.println("Cannot read "+propertyFile); 258 } 259 } catch (java.io.IOException e) { 260 if (!ignoreMissingProperties) { 261 System.err.println("Failure trying to read "+propertyFile); 262 } 263 } 264 265 if (verbosity == null) { 269 try { 270 String verbStr = resources.getString("verbosity"); 271 int verb = Integer.parseInt(verbStr.trim()); 272 debug.setDebug(verb); 273 verbosity = new Integer (verb); 274 } catch (Exception e) { 275 } 277 } 278 } 279 280 283 public static CatalogManager getStaticManager() { 284 return staticManager; 285 } 286 287 294 public boolean getIgnoreMissingProperties() { 295 return ignoreMissingProperties; 296 } 297 298 305 public void setIgnoreMissingProperties(boolean ignore) { 306 ignoreMissingProperties = ignore; 307 } 308 309 318 public void ignoreMissingProperties(boolean ignore) { 319 setIgnoreMissingProperties(ignore); 320 } 321 322 328 private int queryVerbosity () { 329 String defaultVerbStr = Integer.toString(defaultVerbosity); 330 331 String verbStr = System.getProperty(pVerbosity); 332 333 if (verbStr == null) { 334 if (resources==null) readProperties(); 335 if (resources != null) { 336 try { 337 verbStr = resources.getString("verbosity"); 338 } catch (MissingResourceException e) { 339 verbStr = defaultVerbStr; 340 } 341 } else { 342 verbStr = defaultVerbStr; 343 } 344 } 345 346 int verb = defaultVerbosity; 347 348 try { 349 verb = Integer.parseInt(verbStr.trim()); 350 } catch (Exception e) { 351 System.err.println("Cannot parse verbosity: \"" + verbStr + "\""); 352 } 353 354 if (verbosity == null) { 358 debug.setDebug(verb); 359 verbosity = new Integer (verb); 360 } 361 362 return verb; 363 } 364 365 368 public int getVerbosity() { 369 if (verbosity == null) { 370 verbosity = new Integer (queryVerbosity()); 371 } 372 373 return verbosity.intValue(); 374 } 375 376 379 public void setVerbosity (int verbosity) { 380 this.verbosity = new Integer (verbosity); 381 debug.setDebug(verbosity); 382 } 383 384 389 public int verbosity () { 390 return getVerbosity(); 391 } 392 393 399 private boolean queryRelativeCatalogs () { 400 if (resources==null) readProperties(); 401 402 if (resources==null) return defaultRelativeCatalogs; 403 404 try { 405 String allow = resources.getString("relative-catalogs"); 406 return (allow.equalsIgnoreCase("true") 407 || allow.equalsIgnoreCase("yes") 408 || allow.equalsIgnoreCase("1")); 409 } catch (MissingResourceException e) { 410 return defaultRelativeCatalogs; 411 } 412 } 413 414 434 public boolean getRelativeCatalogs () { 435 if (relativeCatalogs == null) { 436 relativeCatalogs = new Boolean (queryRelativeCatalogs()); 437 } 438 439 return relativeCatalogs.booleanValue(); 440 } 441 442 447 public void setRelativeCatalogs (boolean relative) { 448 relativeCatalogs = new Boolean (relative); 449 } 450 451 456 public boolean relativeCatalogs () { 457 return getRelativeCatalogs(); 458 } 459 460 465 private String queryCatalogFiles () { 466 String catalogList = System.getProperty(pFiles); 467 fromPropertiesFile = false; 468 469 if (catalogList == null) { 470 if (resources == null) readProperties(); 471 if (resources != null) { 472 try { 473 catalogList = resources.getString("catalogs"); 474 fromPropertiesFile = true; 475 } catch (MissingResourceException e) { 476 System.err.println(propertyFile + ": catalogs not found."); 477 catalogList = null; 478 } 479 } 480 } 481 482 if (catalogList == null) { 483 catalogList = defaultCatalogFiles; 484 } 485 486 return catalogList; 487 } 488 489 495 public Vector getCatalogFiles() { 496 if (catalogFiles == null) { 497 catalogFiles = queryCatalogFiles(); 498 } 499 500 StringTokenizer files = new StringTokenizer (catalogFiles, ";"); 501 Vector catalogs = new Vector (); 502 while (files.hasMoreTokens()) { 503 String catalogFile = files.nextToken(); 504 URL absURI = null; 505 506 if (fromPropertiesFile && !relativeCatalogs()) { 507 try { 508 absURI = new URL (propertyFileURI, catalogFile); 509 catalogFile = absURI.toString(); 510 } catch (MalformedURLException mue) { 511 absURI = null; 512 } 513 } 514 515 catalogs.add(catalogFile); 516 } 517 518 return catalogs; 519 } 520 521 524 public void setCatalogFiles(String fileList) { 525 catalogFiles = fileList; 526 fromPropertiesFile = false; 527 } 528 529 537 public Vector catalogFiles() { 538 return getCatalogFiles(); 539 } 540 541 550 private boolean queryPreferPublic () { 551 String prefer = System.getProperty(pPrefer); 552 553 if (prefer == null) { 554 if (resources==null) readProperties(); 555 if (resources==null) return defaultPreferPublic; 556 try { 557 prefer = resources.getString("prefer"); 558 } catch (MissingResourceException e) { 559 return defaultPreferPublic; 560 } 561 } 562 563 if (prefer == null) { 564 return defaultPreferPublic; 565 } 566 567 return (prefer.equalsIgnoreCase("public")); 568 } 569 570 575 public boolean getPreferPublic () { 576 if (preferPublic == null) { 577 preferPublic = new Boolean (queryPreferPublic()); 578 } 579 return preferPublic.booleanValue(); 580 } 581 582 585 public void setPreferPublic (boolean preferPublic) { 586 this.preferPublic = new Boolean (preferPublic); 587 } 588 589 596 public boolean preferPublic () { 597 return getPreferPublic(); 598 } 599 600 609 private boolean queryUseStaticCatalog () { 610 String staticCatalog = System.getProperty(pStatic); 611 612 if (staticCatalog == null) { 613 if (resources==null) readProperties(); 614 if (resources==null) return defaultUseStaticCatalog; 615 try { 616 staticCatalog = resources.getString("static-catalog"); 617 } catch (MissingResourceException e) { 618 return defaultUseStaticCatalog; 619 } 620 } 621 622 if (staticCatalog == null) { 623 return defaultUseStaticCatalog; 624 } 625 626 return (staticCatalog.equalsIgnoreCase("true") 627 || staticCatalog.equalsIgnoreCase("yes") 628 || staticCatalog.equalsIgnoreCase("1")); 629 } 630 631 634 public boolean getUseStaticCatalog() { 635 if (useStaticCatalog == null) { 636 useStaticCatalog = new Boolean (queryUseStaticCatalog()); 637 } 638 639 return useStaticCatalog.booleanValue(); 640 } 641 642 645 public void setUseStaticCatalog(boolean useStatic) { 646 useStaticCatalog = new Boolean (useStatic); 647 } 648 649 654 public boolean staticCatalog() { 655 return getUseStaticCatalog(); 656 } 657 658 663 public Catalog getPrivateCatalog() { 664 Catalog catalog = staticCatalog; 665 666 if (useStaticCatalog == null) { 667 useStaticCatalog = new Boolean (getUseStaticCatalog()); 668 } 669 670 if (catalog == null || !useStaticCatalog.booleanValue()) { 671 672 try { 673 String catalogClassName = getCatalogClassName(); 674 675 if (catalogClassName == null) { 676 catalog = new Catalog(); 677 } else { 678 try { 679 catalog = (Catalog) Class.forName(catalogClassName).newInstance(); 680 } catch (ClassNotFoundException cnfe) { 681 debug.message(1,"Catalog class named '" 682 + catalogClassName 683 + "' could not be found. Using default."); 684 catalog = new Catalog(); 685 } catch (ClassCastException cnfe) { 686 debug.message(1,"Class named '" 687 + catalogClassName 688 + "' is not a Catalog. Using default."); 689 catalog = new Catalog(); 690 } 691 } 692 693 catalog.setCatalogManager(this); 694 catalog.setupReaders(); 695 catalog.loadSystemCatalogs(); 696 } catch (Exception ex) { 697 ex.printStackTrace(); 698 } 699 700 if (useStaticCatalog.booleanValue()) { 701 staticCatalog = catalog; 702 } 703 } 704 705 return catalog; 706 } 707 708 714 public Catalog getCatalog() { 715 Catalog catalog = staticCatalog; 716 717 if (useStaticCatalog == null) { 718 useStaticCatalog = new Boolean (getUseStaticCatalog()); 719 } 720 721 if (catalog == null || !useStaticCatalog.booleanValue()) { 722 catalog = getPrivateCatalog(); 723 if (useStaticCatalog.booleanValue()) { 724 staticCatalog = catalog; 725 } 726 } 727 728 return catalog; 729 } 730 731 740 public boolean queryAllowOasisXMLCatalogPI () { 741 String allow = System.getProperty(pAllowPI); 742 743 if (allow == null) { 744 if (resources==null) readProperties(); 745 if (resources==null) return defaultOasisXMLCatalogPI; 746 try { 747 allow = resources.getString("allow-oasis-xml-catalog-pi"); 748 } catch (MissingResourceException e) { 749 return defaultOasisXMLCatalogPI; 750 } 751 } 752 753 if (allow == null) { 754 return defaultOasisXMLCatalogPI; 755 } 756 757 return (allow.equalsIgnoreCase("true") 758 || allow.equalsIgnoreCase("yes") 759 || allow.equalsIgnoreCase("1")); 760 } 761 762 765 public boolean getAllowOasisXMLCatalogPI () { 766 if (oasisXMLCatalogPI == null) { 767 oasisXMLCatalogPI = new Boolean (queryAllowOasisXMLCatalogPI()); 768 } 769 770 return oasisXMLCatalogPI.booleanValue(); 771 } 772 773 776 public void setAllowOasisXMLCatalogPI(boolean allowPI) { 777 oasisXMLCatalogPI = new Boolean (allowPI); 778 } 779 780 785 public boolean allowOasisXMLCatalogPI() { 786 return getAllowOasisXMLCatalogPI(); 787 } 788 789 793 public String queryCatalogClassName () { 794 String className = System.getProperty(pClassname); 795 796 if (className == null) { 797 if (resources==null) readProperties(); 798 if (resources==null) return null; 799 try { 800 return resources.getString("catalog-class-name"); 801 } catch (MissingResourceException e) { 802 return null; 803 } 804 } 805 806 return className; 807 } 808 809 812 public String getCatalogClassName() { 813 if (catalogClassName == null) { 814 catalogClassName = queryCatalogClassName(); 815 } 816 817 return catalogClassName; 818 } 819 820 823 public void setCatalogClassName(String className) { 824 catalogClassName = className; 825 } 826 827 832 public String catalogClassName() { 833 return getCatalogClassName(); 834 } 835 } 836 | Popular Tags |