| 1 8 package org.apache.avalon.excalibur.catalog; 9 10 import java.io.FileNotFoundException; 11 import java.io.IOException; 12 import java.lang.Integer; 13 import java.net.MalformedURLException; 14 import java.net.URL; 15 import java.util.Enumeration; 16 import java.util.Hashtable; 17 import java.util.Vector; 18 import org.xml.sax.SAXException; 19 20 122 public class Catalog 123 { 124 138 public int debug = 0; 139 140 144 private URL base; 145 146 149 private URL catalogCwd; 150 151 154 private Vector catalogEntries = new Vector(); 155 156 159 private boolean default_override = true; 160 161 173 private Vector catalogFiles = new Vector(); 174 175 194 private Vector localCatalogFiles = new Vector(); 195 196 214 private Vector catalogs = new Vector(); 215 216 232 private Vector localDelegate = new Vector(); 233 234 244 private String parserClass = null; 245 246 254 public Catalog() 255 { 256 String property = System.getProperty( "xml.catalog.debug" ); 257 258 if( property != null ) 259 { 260 try 261 { 262 debug = Integer.parseInt( property ); 263 } 264 catch( NumberFormatException e ) 265 { 266 debug = 0; 267 } 268 } 269 270 property = System.getProperty( "xml.catalog.override" ); 271 272 if( property != null ) 273 { 274 default_override = ( property.equalsIgnoreCase( "true" ) 275 || property.equalsIgnoreCase( "yes" ) 276 || property.equalsIgnoreCase( "1" ) ); 277 } 278 } 279 280 292 public void loadSystemCatalogs() 293 throws MalformedURLException, IOException 294 { 295 String PCS = System.getProperty( "path.separator" ); 296 String catalog_files = System.getProperty( "xml.catalog.files" ); 297 298 while( catalog_files != null ) 299 { 300 int pos = catalog_files.indexOf( PCS ); 301 String catfile = null; 302 303 if( pos > 0 ) 304 { 305 catfile = catalog_files.substring( 0, pos ); 306 catalog_files = catalog_files.substring( pos + 1 ); 307 } 308 else 309 { 310 catfile = catalog_files; 311 catalog_files = null; 312 } 313 314 catalogFiles.addElement( catfile ); 315 } 316 317 if( catalogFiles.size() > 0 ) 318 { 319 String catfile = (String)catalogFiles.lastElement(); 332 catalogFiles.removeElement( catfile ); 333 parseCatalog( catfile ); 334 } 335 } 336 337 347 public synchronized void parseCatalog( String fileName ) 348 throws MalformedURLException, IOException 349 { 350 351 catalogFiles.addElement( fileName ); 355 356 int curCat = 0; 360 while( curCat < catalogFiles.size() ) 361 { 362 String catfile = (String)catalogFiles.elementAt( curCat++ ); 363 364 if( catalogEntries.size() == 0 && catalogs.size() == 0 ) 365 { 366 parseCatalogFile( catfile ); 369 } 370 else 371 { 372 catalogs.addElement( catfile ); 375 } 376 377 if( !localCatalogFiles.isEmpty() ) 378 { 379 Vector newQueue = new Vector(); 382 Enumeration q = localCatalogFiles.elements(); 383 while( q.hasMoreElements() ) 384 { 385 newQueue.addElement( q.nextElement() ); 386 } 387 388 while( curCat < catalogFiles.size() ) 390 { 391 catfile = (String)catalogFiles.elementAt( curCat++ ); 392 newQueue.addElement( catfile ); 393 } 394 395 localCatalogFiles = new Vector(); 396 catalogFiles = newQueue; 397 curCat = 0; 398 } 399 400 if( !localDelegate.isEmpty() ) 401 { 402 Enumeration e = localDelegate.elements(); 403 while( e.hasMoreElements() ) 404 { 405 catalogEntries.addElement( e.nextElement() ); 406 } 407 localDelegate = new Vector(); 408 } 409 } 410 411 catalogFiles = new Vector(); 413 } 414 415 450 public void parseAllCatalogs() 451 throws MalformedURLException, IOException 452 { 453 454 for( int catPos = 0; catPos < catalogs.size(); catPos++ ) 456 { 457 Catalog c = null; 458 459 try 460 { 461 c = (Catalog)catalogs.elementAt( catPos ); 462 } 463 catch( ClassCastException e ) 464 { 465 String catfile = (String)catalogs.elementAt( catPos ); 466 c = new Catalog(); 467 c.setParserClass( parserClass ); 468 c.debug = debug; 469 470 c.parseCatalog( catfile ); 471 catalogs.setElementAt( c, catPos ); 472 c.parseAllCatalogs(); 473 } 474 } 475 476 Enumeration enum = catalogEntries.elements(); 478 while( enum.hasMoreElements() ) 479 { 480 CatalogEntry e = (CatalogEntry)enum.nextElement(); 481 if( e.entryType() == CatalogEntry.DELEGATE ) 482 { 483 Catalog dcat = new Catalog(); 484 dcat.setParserClass( parserClass ); 485 dcat.debug = debug; 486 dcat.parseCatalog( e.formalSystemIdentifier() ); 487 } 488 } 489 } 490 491 492 508 public String resolveDoctype( String entityName, 509 String publicId, 510 String systemId ) 511 throws MalformedURLException, IOException 512 { 513 String resolved = null; 514 515 if( systemId != null ) 516 { 517 resolved = resolveLocalSystem( systemId ); 519 if( resolved != null ) 520 { 521 return resolved; 522 } 523 } 524 525 if( publicId != null ) 526 { 527 resolved = resolveLocalPublic( CatalogEntry.DOCTYPE, 529 entityName, 530 publicId, 531 systemId ); 532 if( resolved != null ) 533 { 534 return resolved; 535 } 536 } 537 538 boolean over = default_override; 540 Enumeration enum = catalogEntries.elements(); 541 while( enum.hasMoreElements() ) 542 { 543 CatalogEntry e = (CatalogEntry)enum.nextElement(); 544 if( e.entryType() == CatalogEntry.OVERRIDE ) 545 { 546 over = e.yes_or_no().equalsIgnoreCase( "YES" ); 547 continue; 548 } 549 550 if( e.entryType() == CatalogEntry.DOCTYPE 551 && e.entityName().equals( entityName ) ) 552 { 553 if( over || systemId == null ) 554 { 555 return e.formalSystemIdentifier(); 556 } 557 } 558 } 559 560 return resolveSubordinateCatalogs( CatalogEntry.DOCTYPE, 562 entityName, 563 publicId, 564 systemId ); 565 } 566 567 577 public String resolveDocument() 578 throws MalformedURLException, IOException 579 { 580 Enumeration enum = catalogEntries.elements(); 582 while( enum.hasMoreElements() ) 583 { 584 CatalogEntry e = (CatalogEntry)enum.nextElement(); 585 if( e.entryType() == CatalogEntry.DOCUMENT ) 586 { 587 return e.formalSystemIdentifier(); 588 } 589 } 590 591 return resolveSubordinateCatalogs( CatalogEntry.DOCUMENT, 592 null, null, null ); 593 } 594 595 611 public String resolveEntity( String entityName, 612 String publicId, 613 String systemId ) 614 throws MalformedURLException, IOException 615 { 616 String resolved = null; 617 618 if( systemId != null ) 619 { 620 resolved = resolveLocalSystem( systemId ); 622 if( resolved != null ) 623 { 624 return resolved; 625 } 626 } 627 628 if( publicId != null ) 629 { 630 resolved = resolveLocalPublic( CatalogEntry.ENTITY, 632 entityName, 633 publicId, 634 systemId ); 635 if( resolved != null ) 636 { 637 return resolved; 638 } 639 } 640 641 boolean over = default_override; 643 Enumeration enum = catalogEntries.elements(); 644 while( enum.hasMoreElements() ) 645 { 646 CatalogEntry e = (CatalogEntry)enum.nextElement(); 647 if( e.entryType() == CatalogEntry.OVERRIDE ) 648 { 649 over = e.yes_or_no().equalsIgnoreCase( "YES" ); 650 continue; 651 } 652 653 if( e.entryType() == CatalogEntry.ENTITY 654 && e.entityName().equals( entityName ) ) 655 { 656 if( over || systemId == null ) 657 { 658 return e.formalSystemIdentifier(); 659 } 660 } 661 } 662 663 return resolveSubordinateCatalogs( CatalogEntry.ENTITY, 665 entityName, 666 publicId, 667 systemId ); 668 } 669 670 686 public String resolveNotation( String notationName, 687 String publicId, 688 String systemId ) 689 throws MalformedURLException, IOException 690 { 691 String resolved = null; 692 693 if( systemId != null ) 694 { 695 resolved = resolveLocalSystem( systemId ); 697 if( resolved != null ) 698 { 699 return resolved; 700 } 701 } 702 703 if( publicId != null ) 704 { 705 resolved = resolveLocalPublic( CatalogEntry.NOTATION, 707 notationName, 708 publicId, 709 systemId ); 710 if( resolved != null ) 711 { 712 return resolved; 713 } 714 } 715 716 boolean over = default_override; 718 Enumeration enum = catalogEntries.elements(); 719 while( enum.hasMoreElements() ) 720 { 721 CatalogEntry e = (CatalogEntry)enum.nextElement(); 722 if( e.entryType() == CatalogEntry.OVERRIDE ) 723 { 724 over = e.yes_or_no().equalsIgnoreCase( "YES" ); 725 continue; 726 } 727 728 if( e.entryType() == CatalogEntry.NOTATION 729 && e.entityName().equals( notationName ) ) 730 { 731 if( over || systemId == null ) 732 { 733 return e.formalSystemIdentifier(); 734 } 735 } 736 } 737 738 return resolveSubordinateCatalogs( CatalogEntry.NOTATION, 740 notationName, 741 publicId, 742 systemId ); 743 } 744 745 765 public String resolvePublic( String publicId, String systemId ) 766 throws MalformedURLException, IOException 767 { 768 769 if( systemId != null ) 771 { 772 String resolved = resolveLocalSystem( systemId ); 773 if( resolved != null ) 774 { 775 return resolved; 776 } 777 } 778 779 String resolved = resolveLocalPublic( CatalogEntry.PUBLIC, 781 null, 782 publicId, 783 systemId ); 784 if( resolved != null ) 785 { 786 return resolved; 787 } 788 789 return resolveSubordinateCatalogs( CatalogEntry.PUBLIC, 791 null, 792 publicId, 793 systemId ); 794 } 795 796 819 public String resolveSystem( String systemId ) 820 throws MalformedURLException, IOException 821 { 822 823 if( systemId != null ) 825 { 826 String resolved = resolveLocalSystem( systemId ); 827 if( resolved != null ) 828 { 829 return resolved; 830 } 831 } 832 833 return resolveSubordinateCatalogs( CatalogEntry.SYSTEM, 835 null, 836 null, 837 systemId ); 838 } 839 840 853 public void setParserClass( String parser ) 854 { 855 parserClass = parser; 856 } 857 858 868 private synchronized void parseCatalogFile( String fileName ) 869 throws MalformedURLException, IOException 870 { 871 872 CatalogEntry entry; 873 874 try 878 { 879 String userdir = fixSlashes( System.getProperty( "user.dir" ) ); 881 catalogCwd = new URL( new StringBuffer("file:///").append(userdir).append("/basename").toString() ); 882 } 883 catch( MalformedURLException e ) 884 { 885 String userdir = fixSlashes( System.getProperty( "user.dir" ) ); 886 debug( 1, "Malformed URL on cwd", userdir ); 887 catalogCwd = null; 888 } 889 890 try 892 { 893 base = new URL( catalogCwd, fixSlashes( fileName ) ); 894 } 895 catch( MalformedURLException e ) 896 { 897 try 898 { 899 base = new URL( "file:///" + fixSlashes( fileName ) ); 900 } 901 catch( MalformedURLException e2 ) 902 { 903 debug( 1, "Malformed URL on catalog filename", 904 fixSlashes( fileName ) ); 905 base = null; 906 } 907 } 908 909 debug( 1, "Loading catalog", fileName ); 910 debug( 3, "Default BASE", base.toString() ); 911 912 fileName = base.toString(); 913 914 if( parserClass != null ) 915 { 916 try 917 { 918 XMLCatalogReader catfile = new XMLCatalogReader(); 919 catfile.setParserClass( parserClass ); 920 catfile.parseCatalog( fileName ); 921 922 CatalogEntry ce = null; 923 while( ( ce = catfile.nextEntry() ) != null ) 924 { 925 addEntry( ce ); 926 } 927 return; 928 } 929 catch( SAXException e1 ) 930 { 931 } 933 catch( NoXMLParserException e2 ) 934 { 935 } 937 catch( NotXMLCatalogException e2 ) 938 { 939 } 941 catch( InstantiationException e3 ) 942 { 943 debug( 1, "Cannot instantiate XML Parser class", parserClass ); 944 } 945 catch( IllegalAccessException e4 ) 946 { 947 debug( 1, "Cannot access XML Parser class", parserClass ); 948 } 949 catch( ClassNotFoundException e5 ) 950 { 951 debug( 1, "Cannot load XML Parser class", parserClass ); 952 } 953 catch( UnknownCatalogFormatException e6 ) 954 { 955 debug( 1, "Unrecognized XML Catalog format." ); 956  
|