Code example for Parser

Methods: parse, setDTDHandler, setDocumentHandler, setEntityResolver

0
   */ 
  public void parse(String systemId)
    throws IOException, 
	   SAXException { 
    setupParse(systemId);
    try { 
      parser.parse(systemId);
    } catch (InternalError ie) {
      explain(systemId);
      throw ie;
    } 
  } 
 
  /** SAX Parser API. */ 
  public void setDocumentHandler(DocumentHandler handler) {
    documentHandler = handler;
  } 
 
  /** SAX Parser API. */ 
  public void setDTDHandler(DTDHandler handler) {
    dtdHandler = handler;
  } 
 
  /** 
   * SAX Parser API. 
   * 
   * <p>The purpose of this class is to implement an entity resolver. 
   * Attempting to set a different one is pointless (and ignored).</p> 
   */ 
  public void setEntityResolver(EntityResolver resolver) {
    // nop 
  } 
 
  /** SAX Parser API. */ 
  public void setErrorHandler(ErrorHandler handler) {
    parser.setErrorHandler(handler);
  } 
 
  /** SAX Parser API. */ 
  public void setLocale(Locale locale) throws SAXException {
    parser.setLocale(locale);
  } 
 
  /** SAX DocumentHandler API. */ 
  public void characters(char[] ch, int start, int length)
    throws SAXException { 
    if (documentHandler != null) {
      documentHandler.characters(ch,start,length);
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void endDocument() throws SAXException { 
    if (documentHandler != null) {
      documentHandler.endDocument();
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void endElement(String name) throws SAXException {
    if (documentHandler != null) {
      documentHandler.endElement(name);
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void ignorableWhitespace(char[] ch, int start, int length)
    throws SAXException { 
    if (documentHandler != null) {
      documentHandler.ignorableWhitespace(ch,start,length);
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void processingInstruction(String target, String pidata)
    throws SAXException { 
 
    if (target.equals("oasis-xml-catalog")) {
      URL catalog = null;
      String data = pidata;
 
      int pos = data.indexOf("catalog=");
      if (pos >= 0) {
	data = data.substring(pos+8);
	if (data.length() > 1) {
	  String quote = data.substring(0,1);
	  data = data.substring(1);
	  pos = data.indexOf(quote);
	  if (pos >= 0) {
	    data = data.substring(0, pos);
	    try { 
	      if (baseURL != null) {
		catalog = new URL(baseURL, data);
	      } else { 
		catalog = new URL(data);
	      } 
	    } catch (MalformedURLException mue) {
	      // nevermind 
	    } 
	  } 
	} 
      } 
 
      if (allowXMLCatalogPI) {
	if (catalogManager.getAllowOasisXMLCatalogPI()) {
	  catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
 
	  if (catalog != null) {
	    try { 
	      catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());
	      oasisXMLCatalogPI = true;
 
	      if (piCatalogResolver == null) {
		piCatalogResolver = new CatalogResolver(true);
	      } 
 
	      piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
	    } catch (Exception e) {
	      catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
			    + catalog.toString());
	    } 
	  } else { 
	    catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
	  } 
	} else { 
	  catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
	} 
      } else { 
	catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
		      + pidata);
      } 
    } else { 
      if (documentHandler != null) {
	documentHandler.processingInstruction(target, pidata);
      } 
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void setDocumentLocator(Locator locator) {
    if (documentHandler != null) {
      documentHandler.setDocumentLocator(locator);
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void startDocument() throws SAXException { 
    if (documentHandler != null) {
      documentHandler.startDocument();
    } 
  } 
 
  /** SAX DocumentHandler API. */ 
  public void startElement(String name, AttributeList atts)
    throws SAXException { 
    allowXMLCatalogPI = false;
    if (documentHandler != null) {
      documentHandler.startElement(name,atts);
    } 
  } 
 
  /** SAX DTDHandler API. */ 
  public void notationDecl (String name, String publicId, String systemId)
    throws SAXException { 
    allowXMLCatalogPI = false;
    if (dtdHandler != null) {
      dtdHandler.notationDecl(name,publicId,systemId);
    } 
  } 
 
  /** SAX DTDHandler API. */ 
  public void unparsedEntityDecl (String name,
				  String publicId,
				  String systemId,
				  String notationName) 
    throws SAXException { 
    allowXMLCatalogPI = false;
    if (dtdHandler != null) {
      dtdHandler.unparsedEntityDecl (name, publicId, systemId, notationName);
    } 
  } 
 
  /** 
   * Implements the <code>resolveEntity</code> method 
   * for the SAX interface, using an underlying CatalogResolver 
   * to do the real work. 
   */ 
  public InputSource resolveEntity (String publicId, String systemId) {
    allowXMLCatalogPI = false;
    String resolved = catalogResolver.getResolvedEntity(publicId, systemId);
 
    if (resolved == null && piCatalogResolver != null) {
      resolved = piCatalogResolver.getResolvedEntity(publicId, systemId);
    } 
 
    if (resolved != null) {
      try { 
	InputSource iSource = new InputSource(resolved);
	iSource.setPublicId(publicId);
 
	// Ideally this method would not attempt to open the 
	// InputStream, but there is a bug (in Xerces, at least) 
	// that causes the parser to mistakenly open the wrong 
	// system identifier if the returned InputSource does 
	// not have a byteStream. 
	// 
	// It could be argued that we still shouldn't do this here, 
	// but since the purpose of calling the entityResolver is 
	// almost certainly to open the input stream, it seems to 
	// do little harm. 
	// 
	URL url = new URL(resolved);
	InputStream iStream = url.openStream();
	iSource.setByteStream(iStream);
 
	return iSource;
      } catch (Exception e) {
	catalogManager.debug.message(1,
				     "Failed to create InputSource (" 
				     + e.toString()
				     + ")", resolved);
	return null; 
      } 
    } else { 
      return null; 
    } 
  } 
 
  /** Setup for parsing. */ 
  private void setupParse(String systemId) {
    allowXMLCatalogPI = true;
    parser.setEntityResolver(this);
    parser.setDocumentHandler(this);
    parser.setDTDHandler(this);
 
    URL cwd = null;
 
    try {