1 3 56 57 package org.jboss.util.xml.catalog; 58 59 import java.io.InputStream ; 60 61 import java.net.URL ; 62 import java.net.MalformedURLException ; 63 64 import java.util.MissingResourceException ; 65 import java.util.PropertyResourceBundle ; 66 import java.util.ResourceBundle ; 67 import java.util.StringTokenizer ; 68 import java.util.Vector ; 69 70 import org.jboss.util.xml.catalog.Catalog; 71 import org.jboss.util.xml.catalog.helpers.BootstrapResolver; 72 import org.jboss.util.xml.catalog.helpers.Debug; 73 74 160 161 public class CatalogManager { 162 private static String pFiles = "xml.catalog.files"; 163 private static String pVerbosity = "xml.catalog.verbosity"; 164 private static String pPrefer = "xml.catalog.prefer"; 165 private static String pStatic = "xml.catalog.staticCatalog"; 166 private static String pAllowPI = "xml.catalog.allowPI"; 167 private static String pClassname = "xml.catalog.className"; 168 private static String pIgnoreMissing = "xml.catalog.ignoreMissing"; 169 170 171 private static CatalogManager staticManager = new CatalogManager(); 172 173 174 private BootstrapResolver bResolver = new BootstrapResolver(); 175 176 177 private boolean ignoreMissingProperties 178 = (System.getProperty(pIgnoreMissing) != null 179 || System.getProperty(pFiles) != null); 180 181 182 private ResourceBundle resources; 183 184 185 private String propertyFile = "CatalogManager.properties"; 186 187 188 private URL propertyFileURI = null; 189 190 191 private String defaultCatalogFiles = "./xcatalog"; 192 193 194 private String catalogFiles = null; 195 196 197 private boolean fromPropertiesFile = false; 198 199 200 private int defaultVerbosity = 1; 201 202 203 private Integer verbosity = null; 204 205 206 private boolean defaultPreferPublic = true; 207 208 209 private Boolean preferPublic = null; 210 211 212 private boolean defaultUseStaticCatalog = true; 213 214 215 private Boolean useStaticCatalog = null; 216 217 218 private static Catalog staticCatalog = null; 219 220 221 private boolean defaultOasisXMLCatalogPI = true; 222 223 224 private Boolean oasisXMLCatalogPI = null; 225 226 227 private boolean defaultRelativeCatalogs = true; 228 229 230 private Boolean relativeCatalogs = null; 231 232 233 private String catalogClassName = null; 234 235 240 public Debug debug = null; 241 242 243 public CatalogManager() { 244 debug = new Debug(); 245 } 251 252 253 public CatalogManager(String propertyFile) { 254 this.propertyFile = propertyFile; 255 256 debug = new Debug(); 257 } 263 264 265 public void setBootstrapResolver(BootstrapResolver resolver) { 266 bResolver = resolver; 267 } 268 269 270 public BootstrapResolver getBootstrapResolver() { 271 return bResolver; 272 } 273 274 278 private synchronized void readProperties() { 279 try { 280 propertyFileURI = CatalogManager.class.getResource("/"+propertyFile); 281 InputStream in = 282 CatalogManager.class.getResourceAsStream("/"+propertyFile); 283 if (in==null) { 284 if (!ignoreMissingProperties) { 285 System.err.println("Cannot find "+propertyFile); 286 ignoreMissingProperties = true; 288 } 289 return; 290 } 291 resources = new PropertyResourceBundle (in); 292 } catch (MissingResourceException mre) { 293 if (!ignoreMissingProperties) { 294 System.err.println("Cannot read "+propertyFile); 295 } 296 } catch (java.io.IOException e) { 297 if (!ignoreMissingProperties) { 298 System.err.println("Failure trying to read "+propertyFile); 299 } 300 } 301 302 if (verbosity == null) { 306 try { 307 String verbStr = resources.getString("verbosity"); 308 int verb = Integer.parseInt(verbStr.trim()); 309 debug.setDebug(verb); 310 verbosity = new Integer (verb); 311 } catch (Exception e) { 312 } 314 } 315 } 316 317 320 public static CatalogManager getStaticManager() { 321 return staticManager; 322 } 323 324 331 public boolean getIgnoreMissingProperties() { 332 return ignoreMissingProperties; 333 } 334 335 342 public void setIgnoreMissingProperties(boolean ignore) { 343 ignoreMissingProperties = ignore; 344 } 345 346 355 public void ignoreMissingProperties(boolean ignore) { 356 setIgnoreMissingProperties(ignore); 357 } 358 359 365 private int queryVerbosity () { 366 String verbStr = System.getProperty(pVerbosity); 367 368 if (verbStr == null) { 369 if (resources==null) readProperties(); 370 if (resources==null) return defaultVerbosity; 371 try { 372 verbStr = resources.getString("verbosity"); 373 } catch (MissingResourceException e) { 374 return defaultVerbosity; 375 } 376 } 377 378 try { 379 int verb = Integer.parseInt(verbStr.trim()); 380 return verb; 381 } catch (Exception e) { 382 System.err.println("Cannot parse verbosity: \"" + verbStr + "\""); 383 return defaultVerbosity; 384 } 385 } 386 387 390 public int getVerbosity() { 391 if (verbosity == null) { 392 verbosity = new Integer (queryVerbosity()); 393 } 394 395 return verbosity.intValue(); 396 } 397 398 401 public void setVerbosity (int verbosity) { 402 this.verbosity = new Integer (verbosity); 403 debug.setDebug(verbosity); 404 } 405 406 411 public int verbosity () { 412 return getVerbosity(); 413 } 414 415 421 private boolean queryRelativeCatalogs () { 422 if (resources==null) readProperties(); 423 424 if (resources==null) return defaultRelativeCatalogs; 425 426 try { 427 String allow = resources.getString("relative-catalogs"); 428 return (allow.equalsIgnoreCase("true") 429 || allow.equalsIgnoreCase("yes") 430 || allow.equalsIgnoreCase("1")); 431 } catch (MissingResourceException e) { 432 return defaultRelativeCatalogs; 433 } 434 } 435 436 456 public boolean getRelativeCatalogs () { 457 if (relativeCatalogs == null) { 458 relativeCatalogs = new Boolean (queryRelativeCatalogs()); 459 } 460 461 return relativeCatalogs.booleanValue(); 462 } 463 464 472 public void setRelativeCatalogs (boolean relative) { 473 relativeCatalogs = new Boolean (relative); 474 } 475 476 481 public boolean relativeCatalogs () { 482 return getRelativeCatalogs(); 483 } 484 485 490 private String queryCatalogFiles () { 491 String catalogList = System.getProperty(pFiles); 492 fromPropertiesFile = false; 493 494 if (catalogList == null) { 495 if (resources == null) readProperties(); 496 if (resources != null) { 497 try { 498 catalogList = resources.getString("catalogs"); 499 fromPropertiesFile = true; 500 } catch (MissingResourceException e) { 501 System.err.println(propertyFile + ": catalogs not found."); 502 catalogList = null; 503 } 504 } 505 } 506 507 if (catalogList == null) { 508 catalogList = defaultCatalogFiles; 509 } 510 511 return catalogList; 512 } 513 514 520 public Vector getCatalogFiles() { 521 if (catalogFiles == null) { 522 catalogFiles = queryCatalogFiles(); 523 } 524 525 StringTokenizer files = new StringTokenizer (catalogFiles, ";"); 526 Vector catalogs = new Vector (); 527 while (files.hasMoreTokens()) { 528 String catalogFile = files.nextToken(); 529 URL absURI = null; 530 531 if (fromPropertiesFile && !relativeCatalogs()) { 532 try { 533 absURI = new URL (propertyFileURI, catalogFile); 534 catalogFile = absURI.toString(); 535 } catch (MalformedURLException mue) { 536 absURI = null; 537 } 538 } 539 540 catalogs.add(catalogFile); 541 } 542 543 return catalogs; 544 } 545 546 549 public void setCatalogFiles(String fileList) { 550 catalogFiles = fileList; 551 fromPropertiesFile = false; 552 } 553 554 562 public Vector catalogFiles() { 563 return getCatalogFiles(); 564 } 565 566 575 private boolean queryPreferPublic () { 576 String prefer = System.getProperty(pPrefer); 577 578 if (prefer == null) { 579 if (resources==null) readProperties(); 580 if (resources==null) return defaultPreferPublic; 581 try { 582 prefer = resources.getString("prefer"); 583 } catch (MissingResourceException e) { 584 return defaultPreferPublic; 585 } 586 } 587 588 if (prefer == null) { 589 return defaultPreferPublic; 590 } 591 592 return (prefer.equalsIgnoreCase("public")); 593 } 594 595 600 public boolean getPreferPublic () { 601 if (preferPublic == null) { 602 preferPublic = new Boolean (queryPreferPublic()); 603 } 604 return preferPublic.booleanValue(); 605 } 606 607 612 public void setPreferPublic (boolean preferPublic) { 613 this.preferPublic = new Boolean (preferPublic); 614 } 615 616 623 public boolean preferPublic () { 624 return getPreferPublic(); 625 } 626 627 636 private boolean queryUseStaticCatalog () { 637 String staticCatalog = System.getProperty(pStatic); 638 639 if (useStaticCatalog == null) { 640 if (resources==null) readProperties(); 641 if (resources==null) return defaultUseStaticCatalog; 642 try { 643 staticCatalog = resources.getString("static-catalog"); 644 } catch (MissingResourceException e) { 645 return defaultUseStaticCatalog; 646 } 647 } 648 649 if (staticCatalog == null) { 650 return defaultUseStaticCatalog; 651 } 652 653 return (staticCatalog.equalsIgnoreCase("true") 654 || staticCatalog.equalsIgnoreCase("yes") 655 || staticCatalog.equalsIgnoreCase("1")); 656 } 657 658 661 public boolean getUseStaticCatalog() { 662 if (useStaticCatalog == null) { 663 useStaticCatalog = new Boolean (queryUseStaticCatalog()); 664 } 665 666 return useStaticCatalog.booleanValue(); 667 } 668 669 672 public void setUseStaticCatalog(boolean useStatic) { 673 useStaticCatalog = new Boolean (useStatic); 674 } 675 676 681 public boolean staticCatalog() { 682 return getUseStaticCatalog(); 683 } 684 685 690 public Catalog getPrivateCatalog() { 691 Catalog catalog = staticCatalog; 692 693 if (useStaticCatalog == null) { 694 useStaticCatalog = new Boolean (getUseStaticCatalog()); 695 } 696 697 if (catalog == null || !useStaticCatalog.booleanValue()) { 698 699 try { 700 String catalogClassName = getCatalogClassName(); 701 702 if (catalogClassName == null) { 703 catalog = new Catalog(); 704 } else { 705 try { 706 catalog = (Catalog) Class.forName(catalogClassName).newInstance(); 707 } catch (ClassNotFoundException cnfe) { 708 debug.message(1,"Catalog class named '" 709 + catalogClassName 710 + "' could not be found. Using default."); 711 catalog = new Catalog(); 712 } catch (ClassCastException cnfe) { 713 debug.message(1,"Class named '" 714 + catalogClassName 715 + "' is not a Catalog. Using default."); 716 catalog = new Catalog(); 717 } 718 } 719 720 catalog.setCatalogManager(this); 721 catalog.setupReaders(); 722 catalog.loadSystemCatalogs(); 723 } catch (Exception ex) { 724 ex.printStackTrace(); 725 } 726 727 if (useStaticCatalog.booleanValue()) { 728 staticCatalog = catalog; 729 } 730 } 731 732 return catalog; 733 } 734 735 741 public Catalog getCatalog() { 742 Catalog catalog = staticCatalog; 743 744 if (useStaticCatalog == null) { 745 useStaticCatalog = new Boolean (getUseStaticCatalog()); 746 } 747 748 if (catalog == null || !useStaticCatalog.booleanValue()) { 749 catalog = getPrivateCatalog(); 750 if (useStaticCatalog.booleanValue()) { 751 staticCatalog = catalog; 752 } 753 } 754 755 return catalog; 756 } 757 758 767 public boolean queryAllowOasisXMLCatalogPI () { 768 String allow = System.getProperty(pAllowPI); 769 770 if (allow == null) { 771 if (resources==null) readProperties(); 772 if (resources==null) return defaultOasisXMLCatalogPI; 773 try { 774 allow = resources.getString("allow-oasis-xml-catalog-pi"); 775 } catch (MissingResourceException e) { 776 return defaultOasisXMLCatalogPI; 777 } 778 } 779 780 if (allow == null) { 781 return defaultOasisXMLCatalogPI; 782 } 783 784 return (allow.equalsIgnoreCase("true") 785 || allow.equalsIgnoreCase("yes") 786 || allow.equalsIgnoreCase("1")); 787 } 788 789 792 public boolean getAllowOasisXMLCatalogPI () { 793 if (oasisXMLCatalogPI == null) { 794 oasisXMLCatalogPI = new Boolean (queryAllowOasisXMLCatalogPI()); 795 } 796 797 return oasisXMLCatalogPI.booleanValue(); 798 } 799 800 803 public void setAllowOasisXMLCatalogPI(boolean allowPI) { 804 oasisXMLCatalogPI = new Boolean (allowPI); 805 } 806 807 812 public boolean allowOasisXMLCatalogPI() { 813 return getAllowOasisXMLCatalogPI(); 814 } 815 816 820 public String queryCatalogClassName () { 821 String className = System.getProperty(pClassname); 822 823 if (className == null) { 824 if (resources==null) readProperties(); 825 if (resources==null) return null; 826 try { 827 return resources.getString("catalog-class-name"); 828 } catch (MissingResourceException e) { 829 return null; 830 } 831 } 832 833 return className; 834 } 835 836 839 public String getCatalogClassName() { 840 if (catalogClassName == null) { 841 catalogClassName = queryCatalogClassName(); 842 } 843 844 return catalogClassName; 845 } 846 847 850 public void setCatalogClassName(String className) { 851 catalogClassName = className; 852 } 853 854 859 public String catalogClassName() { 860 return getCatalogClassName(); 861 } 862 } 863 | Popular Tags |