1 8 package org.apache.avalon.excalibur.catalog; 9 10 import java.io.IOException ; 11 import java.lang.Integer ; 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 import java.util.Enumeration ; 15 import java.util.Vector ; 16 import org.xml.sax.*; 17 18 37 public class XMLCatalogReader 38 implements DocumentHandler 39 { 40 43 private final static int NOTXMLCATALOG = -1; 44 45 48 private final static int UNKNOWNCATALOG = 0; 49 50 53 private final static int XCATALOG = 1; 54 55 68 public int debug = 0; 69 70 74 private String catfilename = null; 75 76 79 private int catalogType = NOTXMLCATALOG; 80 81 93 private Vector catalogEntries = new Vector (); 94 95 98 private Enumeration catalogEnum = null; 99 100 110 private String parserClass = null; 111 112 117 public XMLCatalogReader() 118 { 119 String property = System.getProperty( "xml.catalog.debug" ); 120 121 if( property != null ) 122 { 123 try 124 { 125 debug = Integer.parseInt( property ); 126 } 127 catch( NumberFormatException e ) 128 { 129 debug = 0; 130 } 131 } 132 } 133 134 150 public void parseCatalog( String fileUrl ) 151 throws SAXException, IOException , 152 NotXMLCatalogException, NoXMLParserException, 153 UnknownCatalogFormatException, ClassNotFoundException , 154 InstantiationException , IllegalAccessException , 155 ClassCastException 156 { 157 if( parserClass == null ) 159 { 160 throw new NoXMLParserException(); 161 } 162 163 Parser parser = (Parser)Class.forName( parserClass ).newInstance(); 164 165 catfilename = fileUrl; 166 parser.setDocumentHandler( this ); 167 parser.parse( fileUrl ); 168 169 if( catalogType == NOTXMLCATALOG ) 170 { 171 throw new NotXMLCatalogException(); 174 } 175 176 if( catalogType == UNKNOWNCATALOG ) 177 { 178 throw new UnknownCatalogFormatException(); 179 } 180 } 181 182 190 public CatalogEntry nextEntry() 191 throws IOException 192 { 193 if( catalogEnum == null ) 194 { 195 catalogEnum = catalogEntries.elements(); 196 } 197 198 if( catalogEnum.hasMoreElements() ) 199 { 200 return (CatalogEntry)catalogEnum.nextElement(); 201 } 202 else 203 { 204 return null; 205 } 206 } 207 208 215 public void startDocument() 216 throws SAXException 217 { 218 return; 219 } 220 221 228 public void endDocument() 229 throws SAXException 230 { 231 return; 232 } 233 234 250 public void startElement( String name, AttributeList atts ) 251 throws SAXException 252 { 253 254 if( catalogType == UNKNOWNCATALOG || catalogType == NOTXMLCATALOG ) 255 { 256 if( name.equals( "XMLCatalog" ) ) 257 { 258 catalogType = XCATALOG; 259 return; 260 } 261 } 262 263 if( catalogType == XCATALOG ) 264 { 265 xCatalogEntry( name, atts ); 266 } 267 } 268 269 277 public void endElement( String name ) 278 throws SAXException 279 { 280 return; 281 } 282 283 293 public void characters( char ch[], int start, int length ) 294 throws SAXException 295 { 296 return; 297 } 298 299 309 public void ignorableWhitespace( char ch[], int start, int length ) 310 throws SAXException 311 { 312 return; 313 } 314 315 324 public void processingInstruction( String target, String data ) 325 throws SAXException 326 { 327 return; 328 } 329 330 343 public void setParserClass( String parser ) 344 { 345 parserClass = parser; 346 } 347 348 351 358 public void setDocumentLocator( Locator locator ) 359 { 360 return; 361 } 362 363 365 377 private void xCatalogEntry( String name, AttributeList atts ) 378 { 379 CatalogEntry ce = null; 380 381 try 382 { 383 if( name.equals( "Base" ) ) 384 { 385 ce = new CatalogEntry( CatalogEntry.BASE, 386 atts.getValue( "HRef" ) ); 387 debug( 3, "Base", atts.getValue( "HRef" ) ); 388 } 389 390 if( name.equals( "Delegate" ) ) 391 { 392 ce = new CatalogEntry( CatalogEntry.DELEGATE, 393 CatalogReader.normalize( atts.getValue( "PublicId" ) ), 394 atts.getValue( "HRef" ) ); 395 debug( 3, "Delegate", 396 CatalogReader.normalize( atts.getValue( "PublicId" ) ), 397 atts.getValue( "HRef" ) ); 398 } 399 400 if( name.equals( "Extend" ) ) 401 { 402 ce = new CatalogEntry( CatalogEntry.CATALOG, 403 atts.getValue( "HRef" ) ); 404 debug( 3, "Extend", atts.getValue( "HRef" ) ); 405 } 406 407 if( name.equals( "Map" ) ) 408 { 409 ce = new CatalogEntry( CatalogEntry.PUBLIC, 410 CatalogReader.normalize( atts.getValue( "PublicId" ) ), 411 atts.getValue( "HRef" ) ); 412 debug( 3, "Map", 413 CatalogReader.normalize( atts.getValue( "PublicId" ) ), 414 atts.getValue( "HRef" ) ); 415 } 416 417 if( name.equals( "Remap" ) ) 418 { 419 ce = new CatalogEntry( CatalogEntry.SYSTEM, 420 atts.getValue( "SystemId" ), 421 atts.getValue( "HRef" ) ); 422 debug( 3, "Remap", 423 CatalogReader.normalize( atts.getValue( "SystemId" ) ), 424 atts.getValue( "HRef" ) ); 425 } 426 427 if( ce == null ) 428 { 429 debug( 1, "Invalid catalog entry type", name ); 431 } 432 } 433 catch( InvalidCatalogEntryTypeException icete ) 434 { 435 debug( 1, "Invalid catalog entry type", name ); 436 } 437 catch( InvalidCatalogEntryException icete ) 438 { 439 debug( 1, "Invalid catalog entry", name ); 440 } 441 442 if( ce != null ) 443 { 444 catalogEntries.addElement( ce ); 445 } 446 } 447 448 450 461 private void debug( int level, String message, String token ) 462 { 463 if( debug >= level ) 464 { 465 System.out.println( message + ": " + token ); 466 } 467 } 468 469 481 private void debug( int level, String message, String token, String spec ) 482 { 483 if( debug >= level ) 484 { 485 System.out.println( message + ": " + token + " " + spec ); 486 } 487 } 488 489 502 private void debug( int level, String message, 503 String token, String spec1, String spec2 ) 504 { 505 if( debug >= level ) 506 { 507 System.out.println( message + ": " + token + " " + spec1 ); 508 System.out.println( "\t" + spec2 ); 509 } 510 } 511 } 512 | Popular Tags |