1 8 package org.apache.avalon.excalibur.catalog; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.lang.Integer ; 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import org.xml.sax.EntityResolver ; 16 import org.xml.sax.InputSource ; 17 18 36 public class CatalogEntityResolver implements EntityResolver 37 { 38 51 public int debug = 0; 52 53 62 public Catalog catalog = null; 63 64 69 private boolean retryBadSystemIds = false; 70 71 76 public CatalogEntityResolver() 77 { 78 String property = System.getProperty( "xml.catalog.debug" ); 79 80 if( property != null ) 81 { 82 try 83 { 84 debug = Integer.parseInt( property ); 85 } 86 catch( NumberFormatException e ) 87 { 88 debug = 0; 89 } 90 } 91 } 92 93 106 public synchronized void parseCatalog( String fileName ) 107 throws MalformedURLException , IOException 108 { 109 catalog.parseCatalog( fileName ); 110 } 111 112 135 public InputSource resolveEntity( String publicId, String systemId ) 136 { 137 String resolved = null; 138 139 if( systemId != null ) 140 { 141 try 142 { 143 resolved = catalog.resolveSystem( systemId ); 144 } 145 catch( MalformedURLException me ) 146 { 147 debug( 1, "Malformed URL exception trying to resolve", 148 publicId ); 149 resolved = null; 150 } 151 catch( IOException ie ) 152 { 153 debug( 1, "I/O exception trying to resolve", publicId ); 154 resolved = null; 155 } 156 } 157 158 if( resolved == null ) 159 { 160 if( publicId != null ) 161 { 162 try 163 { 164 resolved = catalog.resolvePublic( publicId, systemId ); 165 } 166 catch( MalformedURLException me ) 167 { 168 debug( 1, "Malformed URL exception trying to resolve", 169 publicId ); 170 } 171 catch( IOException ie ) 172 { 173 debug( 1, "I/O exception trying to resolve", publicId ); 174 } 175 } 176 177 if( resolved != null ) 178 { 179 debug( 2, "Resolved", publicId, resolved ); 180 } 181 } 182 else 183 { 184 debug( 2, "Resolved", systemId, resolved ); 185 } 186 187 if( resolved == null && retryBadSystemIds 188 && publicId != null && systemId != null ) 189 { 190 URL systemURL = null; 191 try 192 { 193 systemURL = new URL ( systemId ); 194 } 195 catch( MalformedURLException e ) 196 { 197 try 198 { 199 systemURL = new URL ( "file:///" + systemId ); 200 } 201 catch( MalformedURLException e2 ) 202 { 203 systemURL = null; 204 } 205 } 206 207 if( systemURL != null ) 208 { 209 try 210 { 211 InputStream iStream = systemURL.openStream(); 212 213 217 InputSource iSource = new InputSource ( systemId ); 218 iSource.setPublicId( publicId ); 219 iSource.setByteStream( iStream ); 220 return iSource; 221 } 222 catch( Exception e ) 223 { 224 } 226 } 227 228 debug( 2, "Failed to open", systemId ); 231 debug( 2, "\tAttempting catalog lookup without system identifier." ); 232 return resolveEntity( publicId, null ); 233 } 234 235 if( resolved != null ) 236 { 237 try 238 { 239 InputSource iSource = new InputSource ( resolved ); 240 iSource.setPublicId( publicId ); 241 242 URL url = new URL ( resolved ); 254 InputStream iStream = url.openStream(); 255 iSource.setByteStream( iStream ); 256 257 return iSource; 258 } 259 catch( Exception e ) 260 { 261 debug( 1, "Failed to create InputSource", resolved ); 262 return null; 263 } 264 } 265 return null; 266 } 267 268 278 public void setCatalog( Catalog cat ) 279 { 280 catalog = cat; 281 } 282 283 306 public void setRetry( boolean retry ) 307 { 308 retryBadSystemIds = retry; 309 } 310 311 321 private void debug( int level, String message ) 322 { 323 if( debug >= level ) 324 { 325 System.out.println( message ); 326 } 327 } 328 329 340 private void debug( int level, String message, String spec ) 341 { 342 if( debug >= level ) 343 { 344 System.out.println( message + ": " + spec ); 345 } 346 } 347 348 360 private void debug( int level, String message, String spec1, String spec2 ) 361 { 362 if( debug >= level ) 363 { 364 System.out.println( message + ": " + spec1 ); 365 System.out.println( "\t" + spec2 ); 366 } 367 } 368 } 369 | Popular Tags |