1 8 package org.apache.avalon.excalibur.catalog; 9 10 import java.io.DataInputStream ; 11 import java.io.FileNotFoundException ; 12 import java.io.IOException ; 13 import java.lang.Integer ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 17 34 public class CatalogReader 35 { 36 49 public int debug = 0; 50 51 54 private String catfilename = null; 55 56 59 private DataInputStream catfile = null; 60 61 65 private int[] stack = new int[3]; 66 67 70 private int top = -1; 71 72 77 public CatalogReader() 78 { 79 String property = System.getProperty( "xml.catalog.debug" ); 80 81 if( property != null ) 82 { 83 try 84 { 85 debug = Integer.parseInt( property ); 86 } 87 catch( NumberFormatException e ) 88 { 89 debug = 0; 90 } 91 } 92 } 93 94 96 118 public static String normalize( String publicId ) 119 { 120 String normal = publicId.replace( '\t', ' ' ); 121 normal = normal.replace( '\r', ' ' ); 122 normal = normal.replace( '\n', ' ' ); 123 normal = normal.trim(); 124 125 int pos; 126 127 while( ( pos = normal.indexOf( " " ) ) >= 0 ) 128 { 129 normal = normal.substring( 0, pos ) + normal.substring( pos + 1 ); 130 } 131 132 return normal; 133 } 134 135 145 public void parseCatalog( String fileUrl ) 146 throws MalformedURLException , IOException 147 { 148 catfilename = fileUrl; 149 URL catalog; 150 151 try 152 { 153 catalog = new URL ( fileUrl ); 154 } 155 catch( MalformedURLException e ) 156 { 157 catalog = new URL ( "file:///" + fileUrl ); 158 } 159 160 try 161 { 162 catfile = new DataInputStream ( catalog.openStream() ); 163 } 164 catch( FileNotFoundException e ) 165 { 166 debug( 1, "Failed to load catalog, file not found", catalog.toString() ); 167 } 168 } 169 170 178 public CatalogEntry nextEntry() 179 throws IOException 180 { 181 if( catfile == null ) 182 { 183 return null; 184 } 185 186 boolean confused = false; 187 188 while( true ) 189 { 190 String token = nextToken(); 191 192 if( token == null ) 193 { 194 catfile.close(); 195 catfile = null; 196 return null; 197 } 198 199 if( token.equalsIgnoreCase( "BASE" ) 200 || token.equalsIgnoreCase( "CATALOG" ) 201 || token.equalsIgnoreCase( "DOCUMENT" ) 202 || token.equalsIgnoreCase( "OVERRIDE" ) 203 || token.equalsIgnoreCase( "SGMLDECL" ) ) 204 { 205 String spec = nextToken(); 206 confused = false; 207 208 try 209 { 210 if( token.equalsIgnoreCase( "BASE" ) ) 211 { 212 return new CatalogEntry( CatalogEntry.BASE, spec ); 213 } 214 if( token.equalsIgnoreCase( "CATALOG" ) ) 215 { 216 return new CatalogEntry( CatalogEntry.CATALOG, spec ); 217 } 218 if( token.equalsIgnoreCase( "DOCUMENT" ) ) 219 { 220 return new CatalogEntry( CatalogEntry.DOCUMENT, spec ); 221 } 222 if( token.equalsIgnoreCase( "OVERRIDE" ) ) 223 { 224 return new CatalogEntry( CatalogEntry.OVERRIDE, spec ); 225 } 226 if( token.equalsIgnoreCase( "SGMLDECL" ) ) 227 { 228 return new CatalogEntry( CatalogEntry.SGMLDECL, spec ); 229 } 230 } 231 catch( InvalidCatalogEntryTypeException icete ) 232 { 233 debug( 1, "Invalid catalog entry type", token ); 234 confused = true; 235 } 236 catch( InvalidCatalogEntryException icete ) 237 { 238 debug( 1, "Invalid catalog entry", token, spec ); 239 confused = true; 240 } 241 } 242 243 if( token.equalsIgnoreCase( "DELEGATE" ) 244 || token.equalsIgnoreCase( "DOCTYPE" ) 245 || token.equalsIgnoreCase( "DTDDECL" ) 246 || token.equalsIgnoreCase( "ENTITY" ) 247 || token.equalsIgnoreCase( "LINKTYPE" ) 248 || token.equalsIgnoreCase( "NOTATION" ) 249 || token.equalsIgnoreCase( "PUBLIC" ) 250 || token.equalsIgnoreCase( "SYSTEM" ) ) 251 { 252 String spec1 = nextToken(); 253 String spec2 = nextToken(); 254 confused = false; 255 try 256 { 257 if( token.equalsIgnoreCase( "DELEGATE" ) ) 258 { 259 return new CatalogEntry( CatalogEntry.DELEGATE, 260 normalize( spec1 ), spec2 ); 261 } 262 if( token.equalsIgnoreCase( "DOCTYPE" ) ) 263 { 264 return new CatalogEntry( CatalogEntry.DOCTYPE, 265 spec1, spec2 ); 266 } 267 if( token.equalsIgnoreCase( "DTDDECL" ) ) 268 { 269 return new CatalogEntry( CatalogEntry.DTDDECL, 270 normalize( spec1 ), spec2 ); 271 } 272 if( token.equalsIgnoreCase( "ENTITY" ) ) 273 { 274 return new CatalogEntry( CatalogEntry.ENTITY, 275 spec1, spec2 ); 276 } 277 if( token.equalsIgnoreCase( "LINKTYPE" ) ) 278 { 279 return new CatalogEntry( CatalogEntry.LINKTYPE, 280 spec1, spec2 ); 281 } 282 if( token.equalsIgnoreCase( "NOTATION" ) ) 283 { 284 return new CatalogEntry( CatalogEntry.NOTATION, 285 spec1, spec2 ); 286 } 287 if( token.equalsIgnoreCase( "PUBLIC" ) ) 288 { 289 return new CatalogEntry( CatalogEntry.PUBLIC, 290 normalize( spec1 ), spec2 ); 291 } 292 if( token.equalsIgnoreCase( "SYSTEM" ) ) 293 { 294 return new CatalogEntry( CatalogEntry.SYSTEM, 295 spec1, spec2 ); 296 } 297 } 298 catch( InvalidCatalogEntryTypeException icete ) 299 { 300 debug( 1, "Invalid catalog entry type", token ); 301 confused = true; 302 } 303 catch( InvalidCatalogEntryException icete ) 304 { 305 debug( 1, "Invalid catalog entry", token, spec1, spec2 ); 306 confused = true; 307 } 308 } 309 310 if( !confused ) 311 { 312 if( debug > 1 ) 313 { 314 System.out.println( "Unrecognized token parsing catalog: '" 315 + catfilename 316 + "': " 317 + token ); 318 System.out.println( "\tSkipping to next recognized token." ); 319 } 320 confused = true; 321 } 322 } 323 } 324 325 334 protected void finalize() 335 throws IOException 336 { 337 if( catfile != null ) 338 { 339 catfile.close(); 340 } 341 catfile = null; 342 } 343 344 346 354 private String nextToken() 355 throws IOException 356 { 357 String token = ""; 358 int ch; 359 int nextch; 360 361 while( true ) 363 { 364 ch = catfile.read(); 366 while( ch <= ' ' ) 367 { 368 ch = catfile.read(); 370 if( ch < 0 ) 371 { 372 return null; 373 } 374 } 375 376 nextch = catfile.read(); 378 if( nextch < 0 ) 379 { 380 return null; 381 } 382 383 if( ch == '-' && nextch == '-' ) 384 { 385 ch = ' '; 387 nextch = nextChar(); 388 while( ch != '-' || nextch != '-' ) 389 { 390 ch = nextch; 391 nextch = nextChar(); 392 } 393 394 } 397 else 398 { 399 stack[++top] = nextch; 400 stack[++top] = ch; 401 break; 402 } 403 } 404 405 ch = nextChar(); 406 if( ch == '"' || ch == '\'' ) 407 { 408 int quote = ch; 409 while( ( ch = nextChar() ) != quote ) 410 { 411 char[] chararr = new char[1]; 412 chararr[0] = (char)ch; 413 String s = new String ( chararr ); 414 token = token.concat( s ); 415 } 416 return token; 417 } 418 else 419 { 420 while( ch > ' ' ) 423 { 424 nextch = nextChar(); 425 if( ch == '-' && nextch == '-' ) 426 { 427 stack[++top] = ch; 428 stack[++top] = nextch; 429 return token; 430 } 431 else 432 { 433 char[] chararr = new char[1]; 434 chararr[0] = (char)ch; 435 String s = new String ( chararr ); 436 token = token.concat( s ); 437 ch = nextch; 438 } 439 } 440 return token; 441 } 442 } 443 444 453 private int nextChar() 454 throws IOException 455 { 456 if( top < 0 ) 457 { 458 return catfile.read(); 459 } 460 else 461 { 462 return stack[top--]; 463 } 464 } 465 466 477 private void debug( int level, String message, String token ) 478 { 479 if( debug >= level ) 480 { 481 System.out.println( message + ": " + token ); 482 } 483 } 484 485 497 private void debug( int level, String message, String token, String spec ) 498 { 499 if( debug >= level ) 500 { 501 System.out.println( message + ": " + token + " " + spec ); 502 } 503 } 504 505 516 private void debug( int level, String message, 517 String token, String spec1, String spec2 ) 518 { 519 if( debug >= level ) 520 { 521 System.out.println( message + ": " + token + " " + spec1 ); 522 System.out.println( "\t" + spec2 ); 523 } 524 } 525 } 526 | Popular Tags |