1 3 19 20 package com.sun.org.apache.xml.internal.resolver.tools; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 import java.util.Locale ; 27 28 import org.xml.sax.Parser ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.Locator ; 31 import org.xml.sax.ErrorHandler ; 32 import org.xml.sax.DTDHandler ; 33 import org.xml.sax.DocumentHandler ; 34 import org.xml.sax.AttributeList ; 35 import org.xml.sax.EntityResolver ; 36 import org.xml.sax.SAXException ; 37 38 import javax.xml.parsers.SAXParserFactory ; 39 import javax.xml.parsers.SAXParser ; 40 41 import com.sun.org.apache.xml.internal.resolver.Catalog; 42 import com.sun.org.apache.xml.internal.resolver.CatalogManager; 43 import com.sun.org.apache.xml.internal.resolver.helpers.FileURL; 44 45 63 public class ResolvingParser 64 implements Parser , DTDHandler , DocumentHandler , EntityResolver { 65 66 public static boolean namespaceAware = true; 67 68 69 public static boolean validating = false; 70 71 75 public static boolean suppressExplanation = false; 76 77 78 private SAXParser saxParser = null; 79 80 81 private Parser parser = null; 82 83 84 private DocumentHandler documentHandler = null; 85 86 87 private DTDHandler dtdHandler = null; 88 89 90 private CatalogManager catalogManager = CatalogManager.getStaticManager(); 91 92 93 private CatalogResolver catalogResolver = null; 94 95 96 private CatalogResolver piCatalogResolver = null; 97 98 99 private boolean allowXMLCatalogPI = false; 100 101 102 private boolean oasisXMLCatalogPI = false; 103 104 105 private URL baseURL = null; 106 107 108 public ResolvingParser() { 109 initParser(); 110 } 111 112 113 public ResolvingParser(CatalogManager manager) { 114 catalogManager = manager; 115 initParser(); 116 } 117 118 119 private void initParser() { 120 catalogResolver = new CatalogResolver(catalogManager); 121 122 SAXParserFactory spf = SAXParserFactory.newInstance(); 123 spf.setNamespaceAware(namespaceAware); 124 spf.setValidating(validating); 125 126 try { 127 saxParser = spf.newSAXParser(); 128 parser = saxParser.getParser(); 129 documentHandler = null; 130 dtdHandler = null; 131 } catch (Exception ex) { 132 ex.printStackTrace(); 133 } 134 } 135 136 137 public Catalog getCatalog() { 138 return catalogResolver.getCatalog(); 139 } 140 141 162 public void parse(InputSource input) 163 throws IOException , 164 SAXException { 165 setupParse(input.getSystemId()); 166 try { 167 parser.parse(input); 168 } catch (InternalError ie) { 169 explain(input.getSystemId()); 170 throw ie; 171 } 172 } 173 174 178 public void parse(String systemId) 179 throws IOException , 180 SAXException { 181 setupParse(systemId); 182 try { 183 parser.parse(systemId); 184 } catch (InternalError ie) { 185 explain(systemId); 186 throw ie; 187 } 188 } 189 190 191 public void setDocumentHandler(DocumentHandler handler) { 192 documentHandler = handler; 193 } 194 195 196 public void setDTDHandler(DTDHandler handler) { 197 dtdHandler = handler; 198 } 199 200 206 public void setEntityResolver(EntityResolver resolver) { 207 } 209 210 211 public void setErrorHandler(ErrorHandler handler) { 212 parser.setErrorHandler(handler); 213 } 214 215 216 public void setLocale(Locale locale) throws SAXException { 217 parser.setLocale(locale); 218 } 219 220 221 public void characters(char[] ch, int start, int length) 222 throws SAXException { 223 if (documentHandler != null) { 224 documentHandler.characters(ch,start,length); 225 } 226 } 227 228 229 public void endDocument() throws SAXException { 230 if (documentHandler != null) { 231 documentHandler.endDocument(); 232 } 233 } 234 235 236 public void endElement(String name) throws SAXException { 237 if (documentHandler != null) { 238 documentHandler.endElement(name); 239 } 240 } 241 242 243 public void ignorableWhitespace(char[] ch, int start, int length) 244 throws SAXException { 245 if (documentHandler != null) { 246 documentHandler.ignorableWhitespace(ch,start,length); 247 } 248 } 249 250 251 public void processingInstruction(String target, String pidata) 252 throws SAXException { 253 254 if (target.equals("oasis-xml-catalog")) { 255 URL catalog = null; 256 String data = pidata; 257 258 int pos = data.indexOf("catalog="); 259 if (pos >= 0) { 260 data = data.substring(pos+8); 261 if (data.length() > 1) { 262 String quote = data.substring(0,1); 263 data = data.substring(1); 264 pos = data.indexOf(quote); 265 if (pos >= 0) { 266 data = data.substring(0, pos); 267 try { 268 if (baseURL != null) { 269 catalog = new URL (baseURL, data); 270 } else { 271 catalog = new URL (data); 272 } 273 } catch (MalformedURLException mue) { 274 } 276 } 277 } 278 } 279 280 if (allowXMLCatalogPI) { 281 if (catalogManager.getAllowOasisXMLCatalogPI()) { 282 catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata); 283 284 if (catalog != null) { 285 try { 286 catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString()); 287 oasisXMLCatalogPI = true; 288 289 if (piCatalogResolver == null) { 290 piCatalogResolver = new CatalogResolver(true); 291 } 292 293 piCatalogResolver.getCatalog().parseCatalog(catalog.toString()); 294 } catch (Exception e) { 295 catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: " 296 + catalog.toString()); 297 } 298 } else { 299 catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata); 300 } 301 } else { 302 catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata); 303 } 304 } else { 305 catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: " 306 + pidata); 307 } 308 } else { 309 if (documentHandler != null) { 310 documentHandler.processingInstruction(target, pidata); 311 } 312 } 313 } 314 315 316 public void setDocumentLocator(Locator locator) { 317 if (documentHandler != null) { 318 documentHandler.setDocumentLocator(locator); 319 } 320 } 321 322 323 public void startDocument() throws SAXException { 324 if (documentHandler != null) { 325 documentHandler.startDocument(); 326 } 327 } 328 329 330 public void startElement(String name, AttributeList atts) 331 throws SAXException { 332 allowXMLCatalogPI = false; 333 if (documentHandler != null) { 334 documentHandler.startElement(name,atts); 335 } 336 } 337 338 339 public void notationDecl (String name, String publicId, String systemId) 340 throws SAXException { 341 allowXMLCatalogPI = false; 342 if (dtdHandler != null) { 343 dtdHandler.notationDecl(name,publicId,systemId); 344 } 345 } 346 347 348 public void unparsedEntityDecl (String name, 349 String publicId, 350 String systemId, 351 String notationName) 352 throws SAXException { 353 allowXMLCatalogPI = false; 354 if (dtdHandler != null) { 355 dtdHandler.unparsedEntityDecl (name, publicId, systemId, notationName); 356 } 357 } 358 359 364 public InputSource resolveEntity (String publicId, String systemId) { 365 allowXMLCatalogPI = false; 366 String resolved = catalogResolver.getResolvedEntity(publicId, systemId); 367 368 if (resolved == null && piCatalogResolver != null) { 369 resolved = piCatalogResolver.getResolvedEntity(publicId, systemId); 370 } 371 372 if (resolved != null) { 373 try { 374 InputSource iSource = new InputSource (resolved); 375 iSource.setPublicId(publicId); 376 377 URL url = new URL (resolved); 389 InputStream iStream = url.openStream(); 390 iSource.setByteStream(iStream); 391 392 return iSource; 393 } catch (Exception e) { 394 catalogManager.debug.message(1, "Failed to create InputSource", resolved); 395 return null; 396 } 397 } else { 398 return null; 399 } 400 } 401 402 403 private void setupParse(String systemId) { 404 allowXMLCatalogPI = true; 405 parser.setEntityResolver(this); 406 parser.setDocumentHandler(this); 407 parser.setDTDHandler(this); 408 409 URL cwd = null; 410 411 try { 412 cwd = FileURL.makeURL("basename"); 413 } catch (MalformedURLException mue) { 414 cwd = null; 415 } 416 417 try { 418 baseURL = new URL (systemId); 419 } catch (MalformedURLException mue) { 420 if (cwd != null) { 421 try { 422 baseURL = new URL (cwd, systemId); 423 } catch (MalformedURLException mue2) { 424 baseURL = null; 426 } 427 } else { 428 baseURL = null; 430 } 431 } 432 } 433 434 435 private void explain(String systemId) { 436 if (!suppressExplanation) { 437 System.out.println("Parser probably encountered bad URI in " + systemId); 438 System.out.println("For example, replace '/some/uri' with 'file:/some/uri'."); 439 } 440 } 441 } 442 443 | Popular Tags |