org.apache.xml.resolver_1.2.0.v201005080400

    }
      }
      else if (localName.equals("nextCatalog"))
      {
        if (checkAttributes(atts, "catalog"))
        {
          entryType = Catalog.CATALOG;
          entryArgs.add(atts.getValue("catalog"));
          
          debug.message(4, "nextCatalog", atts.getValue("catalog"));
        }
      }
      else if (localName.equals("public"))
      {
        if (checkAttributes(atts, "publicId", "uri"))
        {
          entryType = Catalog.PUBLIC;
          entryArgs.add(atts.getValue("publicId"));
          entryArgs.add(atts.getValue("uri"));
          
          debug.message(4, "public", PublicId.normalize(atts.getValue("publicId")), atts.getValue("uri"));
        }
      }
      else if (localName.equals("system"))
      {
        if (checkAttributes(atts, "systemId", "uri"))
        {
          entryType = Catalog.SYSTEM;
          entryArgs.add(atts.getValue("systemId"));
          entryArgs.add(atts.getValue("uri"));
          
          debug.message(4, "system", atts.getValue("systemId"), atts.getValue("uri"));
        }
      }
      else if (localName.equals("uri"))
      {
        if (checkAttributes(atts, "name", "uri"))
        {
          entryType = Catalog.URI;
          entryArgs.add(atts.getValue("name"));
          entryArgs.add(atts.getValue("uri"));
          
          debug.message(4, "uri", atts.getValue("name"), atts.getValue("uri"));
        }
      }
      else if (!localName.equals("catalog")) {
        if (!localName.equals("group")) {
          debug.message(1, "Invalid catalog entry type", localName);
        }
      }
      if (entryType >= 0) {
        try
        {
          CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
          catalog.addEntry(ce);
        }
        catch (CatalogException cex)
        {
          if (cex.getExceptionType() == 3) {
            debug.message(1, "Invalid catalog entry type", localName);
          } else if (cex.getExceptionType() == 2) {
            debug.message(1, "Invalid catalog entry", localName);
          }
        }
      }
    }
    if ((namespaceURI != null) && ("urn:oasis:names:tc:entity:xmlns:tr9401:catalog".equals(namespaceURI)) && (!inExtension))
    {
      if (atts.getValue("xml:base") != null)
      {
        String baseURI = atts.getValue("xml:base");
        entryType = Catalog.BASE;
        entryArgs.add(baseURI);
        baseURIStack.push(baseURI);
        
        debug.message(4, "xml:base", baseURI);
        try
        {
          CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
          catalog.addEntry(ce);
        }
        catch (CatalogException cex)
        {
          if (cex.getExceptionType() == 3) {
            debug.message(1, "Invalid catalog entry type", localName);
          } else if (cex.getExceptionType() == 2) {
            debug.message(1, "Invalid catalog entry (base)", localName);
          }
        }
        entryType = -1;
        entryArgs = new Vector();
      }
      else
      {
        baseURIStack.push(baseURIStack.peek());
      }
      if (localName.equals("doctype"))
      {
        entryType = Catalog.DOCTYPE;
        entryArgs.add(atts.getValue("name"));
        entryArgs.add(atts.getValue("uri"));
      }
      else if (localName.equals("document"))
      {
        entryType = Catalog.DOCUMENT;
        entryArgs.add(atts.getValue("uri"));
      }
      else if (localName.equals("dtddecl"))
      {
        entryType = Catalog.DTDDECL;
        entryArgs.add(atts.getValue("publicId"));
        entryArgs.add(atts.getValue("uri"));
      }
      else if (localName.equals("entity"))
      {
        entryType = Catalog.ENTITY;
        entryArgs.add(atts.getValue("name"));
        entryArgs.add(atts.getValue("uri"));
      }
      else if (localName.equals("linktype"))
      {
        entryType = Catalog.LINKTYPE;
        entryArgs.add(atts.getValue("name"));
        entryArgs.add(atts.getValue("uri"));
      }
      else if (localName.equals("notation"))
      {
        entryType = Catalog.NOTATION;
        entryArgs.add(atts.getValue("name"));
        entryArgs.add(atts.getValue("uri"));
      }
      else if (localName.equals("sgmldecl"))
      {
        entryType = Catalog.SGMLDECL;
        entryArgs.add(atts.getValue("uri"));
      }
      else
      {
        debug.message(1, "Invalid catalog entry type", localName);
      }
      if (entryType >= 0) {
        try
        {
          CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
          catalog.addEntry(ce);
        }
        catch (CatalogException cex)
        {
          if (cex.getExceptionType() == 3) {
            debug.message(1, "Invalid catalog entry type", localName);
          } else if (cex.getExceptionType() == 2) {
            debug.message(1, "Invalid catalog entry", localName);
          }
        }
      }
    }
  }
  
  public boolean checkAttributes(Attributes atts, String attName)
  {
    if (atts.getValue(attName) == null)
    {
      debug.message(1, "Error: required attribute " + attName + " missing.");
      return false;
    }
    return true;
  }
  
  public boolean checkAttributes(Attributes atts, String attName1, String attName2)
  {
    return (checkAttributes(atts, attName1)) && (checkAttributes(atts, attName2));
  }
  
  public void endElement(String namespaceURI, String localName, String qName)
    throws SAXException
  {
    int entryType = -1;
    Vector entryArgs = new Vector();
    
    boolean inExtension = inExtensionNamespace();
    if ((namespaceURI != null) && (!inExtension) && (("urn:oasis:names:tc:entity:xmlns:xml:catalog".equals(namespaceURI)) || ("urn:oasis:names:tc:entity:xmlns:tr9401:catalog".equals(namespaceURI))))
    {
      String popURI = (String)baseURIStack.pop();
      String baseURI = (String)baseURIStack.peek();
      if (!baseURI.equals(popURI))
      {
        entryType = Catalog.BASE;
        entryArgs.add(baseURI);
        
        debug.message(4, "(reset) xml:base", baseURI);
        try
        {
          CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
          catalog.addEntry(ce);
        }
        catch (CatalogException cex)
        {
          if (cex.getExceptionType() == 3) {
            debug.message(1, "Invalid catalog entry type", localName);
          } else if (cex.getExceptionType() == 2) {
            debug.message(1, "Invalid catalog entry (rbase)", localName);
          }
        }
      }
    }
    if ((namespaceURI != null) && ("urn:oasis:names:tc:entity:xmlns:xml:catalog".equals(namespaceURI)) && (!inExtension)) {
      if ((localName.equals("catalog")) || (localName.equals("group")))
      {
        String popOverride = (String)overrideStack.pop();
        String override = (String)overrideStack.peek();
        if (!override.equals(popOverride))
        {
          entryType = Catalog.OVERRIDE;
          entryArgs.add(override);
          overrideStack.push(override);
          
          debug.message(4, "(reset) override", override);
          try
          {
            CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
            catalog.addEntry(ce);
          }
          catch (CatalogException cex)
          {
            if (cex.getExceptionType() == 3) {
              debug.message(1, "Invalid catalog entry type", localName);
            } else if (cex.getExceptionType() == 2) {
              debug.message(1, "Invalid catalog entry (roverride)", localName);
            }
          }
        }
      }
    }
    namespaceStack.pop();
  }
  
  public void characters(char[] ch, int start, int length)
    throws SAXException
  {}
  
  public void ignorableWhitespace(char[] ch, int start, int length)
    throws SAXException
  {}
  
  public void processingInstruction(String target, String data)
    throws SAXException
  {}
  
  public void skippedEntity(String name)
    throws SAXException
  {}
  
  public void startPrefixMapping(String prefix, String uri)
    throws SAXException
  {}
  
  public void endPrefixMapping(String prefix)
    throws SAXException
  {}
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.OASISXMLCatalogReader
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.readers;

import org.apache.xml.resolver.Catalog;
import org.xml.sax.ContentHandler;
import org.xml.sax.DocumentHandler;

public abstract interface SAXCatalogParser
  extends ContentHandler, DocumentHandler
{
  public abstract void setCatalog(Catalog paramCatalog);
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.SAXCatalogParser
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.readers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Hashtable;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogException;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.helpers.Debug;
import org.xml.sax.AttributeList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DocumentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;

public class SAXCatalogReader
  implements CatalogReader, ContentHandler, DocumentHandler
{
  protected SAXParserFactory parserFactory = null;
  protected String parserClass = null;
  protected Hashtable namespaceMap = new Hashtable();
  private SAXCatalogParser saxParser = null;
  private boolean abandonHope = false;
  private ClassLoader loader = null;
  private Catalog catalog;
  
  public void setParserFactory(SAXParserFactory parserFactory)
  {
    this.parserFactory = parserFactory;
  }
  
  public void setParserClass(String parserClass)
  {
    this.parserClass = parserClass;
  }
  
  public SAXParserFactory getParserFactory()
  {
    return parserFactory;
  }
  
  public String getParserClass()
  {
    return parserClass;
  }
  
  public void setClassLoader(ClassLoader loader)
  {
    this.loader = loader;
  }
  
  protected Debug debug = getStaticManagerdebug;
  
  public SAXCatalogReader()
  {
    parserFactory = null;
    parserClass = null;
  }
  
  public SAXCatalogReader(SAXParserFactory parserFactory)
  {
    this.parserFactory = parserFactory;
  }
  
  public SAXCatalogReader(String parserClass)
  {
    this.parserClass = parserClass;
  }
  
  public void setCatalogParser(String namespaceURI, String rootElement, String parserClass)
  {
    namespaceURI = namespaceURI != null ? namespaceURI.trim() : "";
    namespaceMap.put("{" + namespaceURI + "}" + rootElement, parserClass);
  }
  
  public String getCatalogParser(String namespaceURI, String rootElement)
  {
    namespaceURI = namespaceURI != null ? namespaceURI.trim() : "";
    return (String)namespaceMap.get("{" + namespaceURI + "}" + rootElement);
  }
  
  public void readCatalog(Catalog catalog, String fileUrl)
    throws MalformedURLException, IOException, CatalogException
  {
    URL url = null;
    try
    {
      url = new URL(fileUrl);
    }
    catch (MalformedURLException e)
    {
      url = new URL("file:///" + fileUrl);
    }
    debug = getCatalogManagerdebug;
    try
    {
      URLConnection urlCon = url.openConnection();
      readCatalog(catalog, urlCon.getInputStream());
    }
    catch (FileNotFoundException e)
    {
      getCatalogManagerdebug.message(1, "Failed to load catalog, file not found", url.toString());
    }
  }
  
  public void readCatalog(Catalog catalog, InputStream is)
    throws IOException, CatalogException
  {
    if ((parserFactory == null) && (parserClass == null))
    {
      debug.message(1, "Cannot read SAX catalog without a parser");
      throw new CatalogException(6);
    }
    debug = getCatalogManagerdebug;
    EntityResolver bResolver = catalog.getCatalogManager().getBootstrapResolver();
    
    this.catalog = catalog;
    try
    {
      if (parserFactory != null)
      {
        SAXParser parser = parserFactory.newSAXParser();
        SAXParserHandler spHandler = new SAXParserHandler();
        spHandler.setContentHandler(this);
        if (bResolver != null) {
          spHandler.setEntityResolver(bResolver);
        }
        parser.parse(new InputSource(is), spHandler);
      }
      else
      {
        Parser parser = (Parser)Class.forName(parserClass, true, loader != null ? loader : getClass().getClassLoader()).newInstance();
        parser.setDocumentHandler(this);
        if (bResolver != null) {
          parser.setEntityResolver(bResolver);
        }
        parser.parse(new InputSource(is));
      }
    }
    catch (ClassNotFoundException cnfe)
    {
      throw new CatalogException(6);
    }
    catch (IllegalAccessException iae)
    {
      throw new CatalogException(6);
    }
    catch (InstantiationException ie)
    {
      throw new CatalogException(6);
    }
    catch (ParserConfigurationException pce)
    {
      throw new CatalogException(5);
    }
    catch (SAXException se)
    {
      Exception e = se.getException();
      
      UnknownHostException uhe = new UnknownHostException();
      FileNotFoundException fnfe = new FileNotFoundException();
      if (e != null)
      {
        if (e.getClass() == uhe.getClass()) {
          throw new CatalogException(7, e.toString());
        }
        if (e.getClass() == fnfe.getClass()) {
          throw new CatalogException(7, e.toString());
        }
      }
      throw new CatalogException(se);
    }
  }
  
  public void setDocumentLocator(Locator locator)
  {
    if (saxParser != null) {
      saxParser.setDocumentLocator(locator);
    }
  }
  
  public void startDocument()
    throws SAXException
  {
    saxParser = null;
    abandonHope = false;
  }
  
  public void endDocument()
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.endDocument();
    }
  }
  
  public void startElement(String name, AttributeList atts)
    throws SAXException
  {
    if (abandonHope) {
      return;
    }
    if (saxParser == null)
    {
      String prefix = "";
      if (name.indexOf(':') > 0) {
        prefix = name.substring(0, name.indexOf(':'));
      }
      String localName = name;
      if (localName.indexOf(':') > 0) {
        localName = localName.substring(localName.indexOf(':') + 1);
      }
      String namespaceURI = null;
      if (prefix.equals("")) {
        namespaceURI = atts.getValue("xmlns");
      } else {
        namespaceURI = atts.getValue("xmlns:" + prefix);
      }
      String saxParserClass = getCatalogParser(namespaceURI, localName);
      if (saxParserClass == null)
      {
        abandonHope = true;
        if (namespaceURI == null) {
          debug.message(2, "No Catalog parser for " + name);
        } else {
          debug.message(2, "No Catalog parser for {" + namespaceURI + "}" + name);
        }
        return;
      }
      try
      {
        saxParser = ((SAXCatalogParser)Class.forName(saxParserClass, true, loader != null ? loader : getClass().getClassLoader()).newInstance());
        
        saxParser.setCatalog(catalog);
        saxParser.startDocument();
        saxParser.startElement(name, atts);
      }
      catch (ClassNotFoundException cnfe)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, cnfe.toString());
      }
      catch (InstantiationException ie)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, ie.toString());
      }
      catch (IllegalAccessException iae)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, iae.toString());
      }
      catch (ClassCastException cce)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, cce.toString());
      }
    }
    else
    {
      saxParser.startElement(name, atts);
    }
  }
  
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    throws SAXException
  {
    if (abandonHope) {
      return;
    }
    if (saxParser == null)
    {
      String saxParserClass = getCatalogParser(namespaceURI, localName);
      if (saxParserClass == null)
      {
        abandonHope = true;
        if (namespaceURI == null) {
          debug.message(2, "No Catalog parser for " + localName);
        } else {
          debug.message(2, "No Catalog parser for {" + namespaceURI + "}" + localName);
        }
        return;
      }
      try
      {
        saxParser = ((SAXCatalogParser)Class.forName(saxParserClass, true, loader != null ? loader : getClass().getClassLoader()).newInstance());
        
        saxParser.setCatalog(catalog);
        saxParser.startDocument();
        saxParser.startElement(namespaceURI, localName, qName, atts);
      }
      catch (ClassNotFoundException cnfe)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, cnfe.toString());
      }
      catch (InstantiationException ie)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, ie.toString());
      }
      catch (IllegalAccessException iae)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, iae.toString());
      }
      catch (ClassCastException cce)
      {
        saxParser = null;
        abandonHope = true;
        debug.message(2, cce.toString());
      }
    }
    else
    {
      saxParser.startElement(namespaceURI, localName, qName, atts);
    }
  }
  
  public void endElement(String name)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.endElement(name);
    }
  }
  
  public void endElement(String namespaceURI, String localName, String qName)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.endElement(namespaceURI, localName, qName);
    }
  }
  
  public void characters(char[] ch, int start, int length)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.characters(ch, start, length);
    }
  }
  
  public void ignorableWhitespace(char[] ch, int start, int length)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.ignorableWhitespace(ch, start, length);
    }
  }
  
  public void processingInstruction(String target, String data)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.processingInstruction(target, data);
    }
  }
  
  public void startPrefixMapping(String prefix, String uri)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.startPrefixMapping(prefix, uri);
    }
  }
  
  public void endPrefixMapping(String prefix)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.endPrefixMapping(prefix);
    }
  }
  
  public void skippedEntity(String name)
    throws SAXException
  {
    if (saxParser != null) {
      saxParser.skippedEntity(name);
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.SAXCatalogReader
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.readers;

import java.io.IOException;
import java.io.PrintStream;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParserHandler
  extends DefaultHandler
{
  private EntityResolver er = null;
  private ContentHandler ch = null;
  
  public void setEntityResolver(EntityResolver er)
  {
    this.er = er;
  }
  
  public void setContentHandler(ContentHandler ch)
  {
    this.ch = ch;
  }
  
  public InputSource resolveEntity(String publicId, String systemId)
    throws SAXException
  {
    if (er != null) {
      try
      {
        return er.resolveEntity(publicId, systemId);
      }
      catch (IOException e)
      {
        System.out.println("resolveEntity threw IOException!");
        return null;
      }
    }
    return null;
  }
  
  public void characters(char[] ch, int start, int length)
    throws SAXException
  {
    if (this.ch != null) {
      this.ch.characters(ch, start, length);
    }
  }
  
  public void endDocument()
    throws SAXException
  {
    if (ch != null) {
      ch.endDocument();
    }
  }
  
  public void endElement(String namespaceURI, String localName, String qName)
    throws SAXException
  {
    if (ch != null) {
      ch.endElement(namespaceURI, localName, qName);
    }
  }
  
  public void endPrefixMapping(String prefix)
    throws SAXException
  {
    if (ch != null) {
      ch.endPrefixMapping(prefix);
    }
  }
  
  public void ignorableWhitespace(char[] ch, int start, int length)
    throws SAXException
  {
    if (this.ch != null) {
      this.ch.ignorableWhitespace(ch, start, length);
    }
  }
  
  public void processingInstruction(String target, String data)
    throws SAXException
  {
    if (ch != null) {
      ch.processingInstruction(target, data);
    }
  }
  
  public void setDocumentLocator(Locator locator)
  {
    if (ch != null) {
      ch.setDocumentLocator(locator);
    }
  }
  
  public void skippedEntity(String name)
    throws SAXException
  {
    if (ch != null) {
      ch.skippedEntity(name);
    }
  }
  
  public void startDocument()
    throws SAXException
  {
    if (ch != null) {
      ch.startDocument();
    }
  }
  
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    throws SAXException
  {
    if (ch != null) {
      ch.startElement(namespaceURI, localName, qName, atts);
    }
  }
  
  public void startPrefixMapping(String prefix, String uri)
    throws SAXException
  {
    if (ch != null) {
      ch.startPrefixMapping(prefix, uri);
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.SAXParserHandler
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.readers;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Vector;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogEntry;
import org.apache.xml.resolver.CatalogException;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.helpers.Debug;

public class TR9401CatalogReader
  extends TextCatalogReader
{
  public void readCatalog(Catalog catalog, InputStream is)
    throws MalformedURLException, IOException
  {
    catfile = is;
    if (catfile == null) {
      return;
    }
    Vector unknownEntry = null;
    try
    {
      for (;;)
      {
        String token = nextToken();
        if (token == null)
        {
          if (unknownEntry != null)
          {
            catalog.unknownEntry(unknownEntry);
            unknownEntry = null;
          }
          catfile.close();
          catfile = null;
          return;
        }
        String entryToken = null;
        if (caseSensitive) {
          entryToken = token;
        } else {
          entryToken = token.toUpperCase();
        }
        if (entryToken.equals("DELEGATE")) {
          entryToken = "DELEGATE_PUBLIC";
        }
        try
        {
          int type = CatalogEntry.getEntryType(entryToken);
          int numArgs = CatalogEntry.getEntryArgCount(type);
          Vector args = new Vector();
          if (unknownEntry != null)
          {
            catalog.unknownEntry(unknownEntry);
            unknownEntry = null;
          }
          for (int count = 0; count < numArgs; count++) {
            args.addElement(nextToken());
          }
          catalog.addEntry(new CatalogEntry(entryToken, args));
        }
        catch (CatalogException cex)
        {
          if (cex.getExceptionType() == 3)
          {
            if (unknownEntry == null) {
              unknownEntry = new Vector();
            }
            unknownEntry.addElement(token);
          }
          else if (cex.getExceptionType() == 2)
          {
            getCatalogManagerdebug.message(1, "Invalid catalog entry", token);
            unknownEntry = null;
          }
          else if (cex.getExceptionType() == 8)
          {
            getCatalogManagerdebug.message(1, cex.getMessage());
          }
        }
      }
    }
    catch (CatalogException cex2)
    {
      if (cex2.getExceptionType() == 8) {
        getCatalogManagerdebug.message(1, cex2.getMessage());
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.TR9401CatalogReader
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.readers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Stack;
import java.util.Vector;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogEntry;
import org.apache.xml.resolver.CatalogException;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.helpers.Debug;

public class TextCatalogReader
  implements CatalogReader
{
  protected InputStream catfile = null;
  protected int[] stack = new int[3];
  protected Stack tokenStack = new Stack();
  protected int top = -1;
  protected boolean caseSensitive = false;
  
  public void setCaseSensitive(boolean isCaseSensitive)
  {
    caseSensitive = isCaseSensitive;
  }
  
  public boolean getCaseSensitive()
  {
    return caseSensitive;
  }
  
  public void readCatalog(Catalog catalog, String fileUrl)
    throws MalformedURLException, IOException
  {
    URL catURL = null;
    try
    {
      catURL = new URL(fileUrl);
    }
    catch (MalformedURLException e)
    {
      catURL = new URL("file:///" + fileUrl);
    }
    URLConnection urlCon = catURL.openConnection();
    try
    {
      readCatalog(catalog, urlCon.getInputStream());
    }
    catch (FileNotFoundException e)
    {
      getCatalogManagerdebug.message(1, "Failed to load catalog, file not found", catURL.toString());
    }
  }
  
  public void readCatalog(Catalog catalog, InputStream is)
    throws MalformedURLException, IOException
  {
    catfile = is;
    if (catfile == null) {
      return;
    }
    Vector unknownEntry = null;
    try
    {
      for (;;)
      {
        String token = nextToken();
        if (token == null)
        {
          if (unknownEntry != null)
          {
            catalog.unknownEntry(unknownEntry);
            unknownEntry = null;
          }
          catfile.close();
          catfile = null;
          return;
        }
        String entryToken = null;
        if (caseSensitive) {
          entryToken = token;
        } else {
          entryToken = token.toUpperCase();
        }
        try
        {
          int type = CatalogEntry.getEntryType(entryToken);
          int numArgs = CatalogEntry.getEntryArgCount(type);
          Vector args = new Vector();
          if (unknownEntry != null)
          {
            catalog.unknownEntry(unknownEntry);
            unknownEntry = null;
          }
          for (int count = 0; count < numArgs; count++) {
            args.addElement(nextToken());
          }
          catalog.addEntry(new CatalogEntry(entryToken, args));
        }
        catch (CatalogException cex)
        {
          if (cex.getExceptionType() == 3)
          {
            if (unknownEntry == null) {
              unknownEntry = new Vector();
            }
            unknownEntry.addElement(token);
          }
          else if (cex.getExceptionType() == 2)
          {
            getCatalogManagerdebug.message(1, "Invalid catalog entry", token);
            unknownEntry = null;
          }
          else if (cex.getExceptionType() == 8)
          {
            getCatalogManagerdebug.message(1, cex.getMessage());
          }
        }
      }
    }
    catch (CatalogException cex2)
    {
      if (cex2.getExceptionType() == 8) {
        getCatalogManagerdebug.message(1, cex2.getMessage());
      }
    }
  }
  
  protected void finalize()
  {
    if (catfile != null) {
      try
      {
        catfile.close();
      }
      catch (IOException e) {}
    }
    catfile = null;
  }
  
  protected String nextToken()
    throws IOException, CatalogException
  {
    String token = "";
    if (!tokenStack.empty()) {
      return (String)tokenStack.pop();
    }
    int nextch;
    do
    {
      ch = catfile.read();
      while (ch <= 32)
      {
        ch = catfile.read();
        if (ch < 0) {
          return null;
        }
      }
      nextch = catfile.read();
      if (nextch < 0) {
        return null;
      }
      if ((ch != 45) || (nextch != 45)) {
        break;
      }
      ch = 32;
      nextch = nextChar();
      while (((ch != 45) || (nextch != 45)) && (nextch > 0))
      {
        ch = nextch;
        nextch = nextChar();
      }
    } while (nextch >= 0);
    throw new CatalogException(8, "Unterminated comment in catalog file; EOF treated as end-of-comment.");
    
    stack[(++top)] = nextch;
    stack[(++top)] = ch;
    
    int ch = nextChar();
    if ((ch == 34) || (ch == 39))
    {
      quote = ch;
      while ((ch = nextChar()) != quote)
      {
        chararr = new char[1];
        chararr[0] = ((char)ch);
        s = new String(chararr);
        token = token.concat(s);
      }
      return token;
    }
    while (ch > 32)
    {
      int quote;
      char[] chararr;
      String s;
      nextch = nextChar();
      if ((ch == 45) && (nextch == 45))
      {
        stack[(++top)] = ch;
        stack[(++top)] = nextch;
        return token;
      }
      char[] chararr = new char[1];
      chararr[0] = ((char)ch);
      String s = new String(chararr);
      token = token.concat(s);
      ch = nextch;
    }
    return token;
  }
  
  protected int nextChar()
    throws IOException
  {
    if (top < 0) {
      return catfile.read();
    }
    return stack[(top--)];
  }
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.TextCatalogReader
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.readers;

import java.util.Vector;
import javax.xml.parsers.SAXParserFactory;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogEntry;
import org.apache.xml.resolver.CatalogException;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.helpers.Debug;
import org.apache.xml.resolver.helpers.PublicId;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class XCatalogReader
  extends SAXCatalogReader
  implements SAXCatalogParser
{
  protected Catalog catalog = null;
  
  public void setCatalog(Catalog catalog)
  {
    this.catalog = catalog;
    debug = getCatalogManagerdebug;
  }
  
  public Catalog getCatalog()
  {
    return catalog;
  }
  
  public XCatalogReader() {}
  
  public XCatalogReader(SAXParserFactory parserFactory, Catalog catalog)
  {
    super(parserFactory);
    setCatalog(catalog);
  }
  
  public void setDocumentLocator(Locator locator) {}
  
  public void startDocument()
    throws SAXException
  {}
  
  public void endDocument()
    throws SAXException
  {}
  
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    throws SAXException
  {
    int entryType = -1;
    Vector entryArgs = new Vector();
    if (localName.equals("Base"))
    {
      entryType = Catalog.BASE;
      entryArgs.add(atts.getValue("HRef"));
      
      debug.message(4, "Base", atts.getValue("HRef"));
    }
    else if (localName.equals("Delegate"))
    {
      entryType = Catalog.DELEGATE_PUBLIC;
      entryArgs.add(atts.getValue("PublicID"));
      entryArgs.add(atts.getValue("HRef"));
      
      debug.message(4, "Delegate", PublicId.normalize(atts.getValue("PublicID")), atts.getValue("HRef"));
    }
    else if (localName.equals("Extend"))
    {
      entryType = Catalog.CATALOG;
      entryArgs.add(atts.getValue("HRef"));
      
      debug.message(4, "Extend", atts.getValue("HRef"));
    }
    else if (localName.equals("Map"))
    {
      entryType = Catalog.PUBLIC;
      entryArgs.add(atts.getValue("PublicID"));
      entryArgs.add(atts.getValue("HRef"));
      
      debug.message(4, "Map", PublicId.normalize(atts.getValue("PublicID")), atts.getValue("HRef"));
    }
    else if (localName.equals("Remap"))
    {
      entryType = Catalog.SYSTEM;
      entryArgs.add(atts.getValue("SystemID"));
      entryArgs.add(atts.getValue("HRef"));
      
      debug.message(4, "Remap", atts.getValue("SystemID"), atts.getValue("HRef"));
    }
    else if (!localName.equals("XCatalog"))
    {
      debug.message(1, "Invalid catalog entry type", localName);
    }
    if (entryType >= 0) {
      try
      {
        CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
        catalog.addEntry(ce);
      }
      catch (CatalogException cex)
      {
        if (cex.getExceptionType() == 3) {
          debug.message(1, "Invalid catalog entry type", localName);
        } else if (cex.getExceptionType() == 2) {
          debug.message(1, "Invalid catalog entry", localName);
        }
      }
    }
  }
  
  public void endElement(String namespaceURI, String localName, String qName)
    throws SAXException
  {}
  
  public void characters(char[] ch, int start, int length)
    throws SAXException
  {}
  
  public void ignorableWhitespace(char[] ch, int start, int length)
    throws SAXException
  {}
  
  public void processingInstruction(String target, String data)
    throws SAXException
  {}
  
  public void skippedEntity(String name)
    throws SAXException
  {}
  
  public void startPrefixMapping(String prefix, String uri)
    throws SAXException
  {}
  
  public void endPrefixMapping(String prefix)
    throws SAXException
  {}
}

/* Location:
 * Qualified Name:     org.apache.xml.resolver.readers.XCatalogReader
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package org.apache.xml.resolver.tools;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.helpers.Debug;
import org.apache.xml.resolver.helpers.FileURL;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class CatalogResolver
  implements EntityResolver, URIResolver
{
  public boolean namespaceAware = true;
  public boolean validating = false;
  private Catalog catalog = null;
  private CatalogManager catalogManager = CatalogManager.getStaticManager();
  
  public CatalogResolver()
  {
    initializeCatalogs(false);
  }
  
  public CatalogResolver(boolean privateCatalog)
  {
    initializeCatalogs(privateCatalog);
  }
  
  public CatalogResolver(CatalogManager manager)
  {
    catalogManager = manager;
    initializeCatalogs(!catalogManager.getUseStaticCatalog());
  }
  
  private void initializeCatalogs(boolean privateCatalog)
  {
    catalog = catalogManager.getCatalog();
  }
  
  public Catalog getCatalog()
  {
    return catalog;
  }
  
  public String getResolvedEntity(String publicId, String systemId)
  {
    String resolved = null;
    if (catalog == null)
    {
      catalogManager.debug.message(1, "Catalog resolution attempted with null catalog; ignored");
      return null;
    }
    if (systemId != null) {
      try
      {
        resolved = catalog.resolveSystem(systemId);
      }
      catch (MalformedURLException me)
      {
        catalogManager.debug.message(1, "Malformed URL exception trying to resolve", publicId);
        
        resolved = null;
      }
      catch (IOException ie)
      {
        catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
        resolved = null;
      }
    }
    if (resolved == null)
    {
      if (publicId != null) {
        try
        {
          resolved = catalog.resolvePublic(publicId, systemId);
        }
        catch (MalformedURLException me)
        {
          catalogManager.debug.message(1, "Malformed URL exception trying to resolve", publicId);
        }
        catch (IOException ie)
        {
          catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
        }
      }
      if (resolved != null) {
        catalogManager.debug.message(2, "Resolved public", publicId, resolved);
      }
    }
    else
    {
      catalogManager.debug.message(2, "Resolved system", systemId, resolved);
    }
    return resolved;
  }
  
  public InputSource resolveEntity(String publicId, String systemId)
  {
    String resolved = getResolvedEntity(publicId, systemId);
    if (resolved != null) {
      try
      {
        InputSource iSource = new InputSource(resolved);
        iSource.setPublicId(publicId);
        
        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;
      }
    }
    return null;
  }
  
  public Source resolve(String href, String base)
    throws TransformerException
  {
    String uri = href;
    String fragment = null;
    int hashPos = href.indexOf("#");
    if (hashPos >= 0)
    {
      uri = href.substring(0, hashPos);
      fragment = href.substring(hashPos + 1);
    }
    String result = null;
    try
    {
      result = catalog.resolveURI(href);
    }
    catch (Exception e) {}
    if (result == null) {
      try
      {
        URL url = null;
        if (base == null)
        {
          url = new URL(uri);
          result = url.toString();
        }
        else
        {
          URL baseURL = new URL(base);
          url = href.length() == 0 ? baseURL : new URL(baseURL, uri);
          result = url.toString();
        }
      }
      catch (MalformedURLException mue)
      {
        String absBase = makeAbsolute(base);
        if (!absBase.equals(base)) {
          return resolve(href, absBase);
        }
        throw new TransformerException("Malformed URL " + href + "(base " + base + ")", mue);
      }
    }
    catalogManager.debug.message(2, "Resolved URI", href, result);
    
    SAXSource source = new SAXSource();
    source.setInputSource(new InputSource(result));
    setEntityResolver(source);
    return source;
  }
  
  private void setEntityResolver(SAXSource source)
    throws TransformerException
  {
    XMLReader reader = source.getXMLReader();
    if (reader == null)
    {
      SAXParserFactory spFactory = SAXParserFactory.newInstance();
      spFactory.setNamespaceAware(true);
      try
      {
        reader = spFactory.newSAXParser().getXMLReader();
      }
      catch (ParserConfigurationException ex)
      {
        throw new TransformerException(ex);
      }
      catch (SAXException ex)
      {
        throw new TransformerException(ex);
      }
    }
    reader.setEntityResolver(this);
    source.setXMLReader(reader);
  }
  
  private String makeAbsolute(String uri)
  {
    if (uri == null) {
      uri = "";
   
1 2 3 4 5

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd