home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / xml4j.jar / com / ibm / xml / parser / SAXDriver.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-30  |  6.3 KB  |  271 lines

  1. package com.ibm.xml.parser;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.Reader;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.Locale;
  9. import java.util.Stack;
  10. import org.xml.sax.AttributeList;
  11. import org.xml.sax.DTDHandler;
  12. import org.xml.sax.DocumentHandler;
  13. import org.xml.sax.EntityResolver;
  14. import org.xml.sax.ErrorHandler;
  15. import org.xml.sax.HandlerBase;
  16. import org.xml.sax.InputSource;
  17. import org.xml.sax.Parser;
  18. import org.xml.sax.SAXException;
  19. import org.xml.sax.SAXParseException;
  20. import org.xml.sax.helpers.LocatorImpl;
  21.  
  22. public class SAXDriver implements Parser, AttributeList, ErrorListener, TagHandler, StreamProducer {
  23.    static HandlerBase s_defaulthandler = new HandlerBase();
  24.    com.ibm.xml.parser.Parser parser;
  25.    Locale locale;
  26.    EntityResolver entityHandler;
  27.    DTDHandler dtdHandler;
  28.    DocumentHandler documentHandler;
  29.    ErrorHandler errorHandler;
  30.    TXAttribute[] attributes;
  31.    DTD dtd;
  32.    String currentSystemID;
  33.    Stack stack;
  34.    int depth;
  35.    RuntimeException userException;
  36.    boolean pause;
  37.  
  38.    public void setLocale(Locale var1) throws SAXException {
  39.       this.locale = var1;
  40.       if (this.parser != null) {
  41.          this.parser.setLocale(var1);
  42.       }
  43.  
  44.    }
  45.  
  46.    public void setEntityResolver(EntityResolver var1) {
  47.       this.entityHandler = var1;
  48.    }
  49.  
  50.    public void setDTDHandler(DTDHandler var1) {
  51.       this.dtdHandler = var1;
  52.    }
  53.  
  54.    public void setDocumentHandler(DocumentHandler var1) {
  55.       this.documentHandler = var1;
  56.    }
  57.  
  58.    public void setErrorHandler(ErrorHandler var1) {
  59.       this.errorHandler = var1;
  60.    }
  61.  
  62.    public void parse(InputSource var1) throws SAXException {
  63.       this.depth = 0;
  64.       this.pause = false;
  65.       this.stack = new Stack();
  66.       this.currentSystemID = var1.getSystemId();
  67.       this.documentHandler.startDocument();
  68.  
  69.       try {
  70.          this.parser = new com.ibm.xml.parser.Parser(this.currentSystemID, this, this);
  71.          this.parser.setSAXDriver(this);
  72.          if (this.locale != null) {
  73.             this.parser.setLocale(this.locale);
  74.          }
  75.  
  76.          this.parser.setElementFactory(new SAXDocument(this));
  77.          this.parser.setTagHandler(this);
  78.          this.stack.push(this.currentSystemID);
  79.          if (var1.getByteStream() != null) {
  80.             this.parser.readStream(var1.getByteStream());
  81.          } else if (var1.getCharacterStream() != null) {
  82.             this.parser.readStream(var1.getCharacterStream());
  83.          } else {
  84.             this.parser.readStream(this.getInputStream(this.currentSystemID, (String)null, this.currentSystemID));
  85.          }
  86.  
  87.          if (this.userException == null) {
  88.             this.documentHandler.endDocument();
  89.          }
  90.       } catch (SAXException var9) {
  91.          throw var9;
  92.       } catch (ExceptionWrapper var10) {
  93.          throw var10.wrapped;
  94.       } catch (Exception var11) {
  95.          throw new SAXException(var11);
  96.       } finally {
  97.          this.parser = null;
  98.          this.attributes = null;
  99.          this.dtd = null;
  100.          this.currentSystemID = null;
  101.          this.stack = null;
  102.          this.userException = null;
  103.       }
  104.  
  105.    }
  106.  
  107.    public void parse(String var1) throws SAXException {
  108.       this.parse(new InputSource(var1));
  109.    }
  110.  
  111.    public int getLength() {
  112.       return this.attributes.length;
  113.    }
  114.  
  115.    public String getName(int var1) {
  116.       return this.attributes[var1].getName();
  117.    }
  118.  
  119.    public String getType(int var1) {
  120.       int var2 = this.attributes[var1].getType();
  121.       if (var2 == 0) {
  122.          var2 = 1;
  123.       }
  124.  
  125.       return AttDef.S_TYPESTR[var2];
  126.    }
  127.  
  128.    public String getType(String var1) {
  129.       int var2 = this.searchAttribute(var1);
  130.       return var2 < 0 ? null : this.getType(var2);
  131.    }
  132.  
  133.    public String getValue(int var1) {
  134.       return this.attributes[var1].getValue();
  135.    }
  136.  
  137.    public String getValue(String var1) {
  138.       int var2 = this.searchAttribute(var1);
  139.       return var2 < 0 ? null : this.getValue(var2);
  140.    }
  141.  
  142.    public Source getInputStream(String var1, String var2, String var3) throws IOException, RuntimeException {
  143.       Source var4 = null;
  144.       String var5 = this.currentSystemID;
  145.       if (var3 != null) {
  146.          try {
  147.             this.stack.push(var5);
  148.             this.currentSystemID = var3;
  149.             InputSource var6 = this.entityHandler.resolveEntity(var2, var3);
  150.             if (var6 != null) {
  151.                if (var6.getSystemId() != null) {
  152.                   this.currentSystemID = var3 = var6.getSystemId();
  153.                }
  154.  
  155.                if (var6.getByteStream() != null) {
  156.                   var4 = new Source(var6.getByteStream());
  157.                } else if (var6.getCharacterStream() != null) {
  158.                   var4 = new Source(var6.getCharacterStream());
  159.                }
  160.             }
  161.  
  162.             if (var4 == null) {
  163.                URL var7;
  164.                if (var5 == null) {
  165.                   var7 = new URL(var3);
  166.                } else {
  167.                   var7 = new URL(new URL(var5), var3);
  168.                }
  169.  
  170.                this.currentSystemID = var7.toString();
  171.                var4 = new Source(var7.openStream());
  172.             }
  173.          } catch (MalformedURLException var8) {
  174.             var4 = new Source(new FileInputStream(var3));
  175.          } catch (SAXException var9) {
  176.             throw new ExceptionWrapper(this, var9);
  177.          }
  178.       }
  179.  
  180.       return var4;
  181.    }
  182.  
  183.    public void closeInputStream(Source var1) {
  184.       try {
  185.          this.currentSystemID = (String)this.stack.pop();
  186.       } catch (Exception var2) {
  187.       }
  188.    }
  189.  
  190.    public void loadCatalog(Reader var1) throws IOException {
  191.    }
  192.  
  193.    public int error(String var1, int var2, int var3, Object var4, String var5) throws RuntimeException {
  194.       if (this.userException != null) {
  195.          throw this.userException;
  196.       } else {
  197.          LocatorImpl var6 = new LocatorImpl();
  198.          var6.setSystemId(var1);
  199.          var6.setLineNumber(var2);
  200.          var6.setColumnNumber(var3);
  201.          SAXParseException var7 = new SAXParseException(var5, var6);
  202.          boolean var8 = var4 instanceof String && ((String)var4).startsWith("W_");
  203.          boolean var9 = var4 instanceof String && ((String)var4).startsWith("V_");
  204.  
  205.          try {
  206.             if (var8) {
  207.                this.errorHandler.warning(var7);
  208.             } else if (var9) {
  209.                this.errorHandler.error(var7);
  210.             } else {
  211.                this.errorHandler.fatalError(var7);
  212.             }
  213.  
  214.             return 0;
  215.          } catch (SAXException var11) {
  216.             this.userException = new ExceptionWrapper(this, var11);
  217.             throw this.userException;
  218.          }
  219.       }
  220.    }
  221.  
  222.    public void handleStartTag(TXElement var1, boolean var2) throws RuntimeException {
  223.       ++this.depth;
  224.       if (!this.pause) {
  225.          this.attributes = var1.getAttributeArray();
  226.  
  227.          try {
  228.             this.documentHandler.startElement(var1.getNodeName(), this);
  229.          } catch (SAXException var4) {
  230.             throw new ExceptionWrapper(this, var4);
  231.          }
  232.  
  233.          this.attributes = null;
  234.       }
  235.  
  236.    }
  237.  
  238.    public void handleEndTag(TXElement var1, boolean var2) throws RuntimeException {
  239.       --this.depth;
  240.       if (!this.pause) {
  241.          try {
  242.             this.documentHandler.endElement(var1.getNodeName());
  243.          } catch (SAXException var4) {
  244.             throw new ExceptionWrapper(this, var4);
  245.          }
  246.       }
  247.    }
  248.  
  249.    private int searchAttribute(String var1) {
  250.       for(int var2 = 0; var2 < this.attributes.length; ++var2) {
  251.          if (var1.equals(this.attributes[var2].getName())) {
  252.             return var2;
  253.          }
  254.       }
  255.  
  256.       return -1;
  257.    }
  258.  
  259.    void pauseEvent(boolean var1) {
  260.       this.pause = var1;
  261.    }
  262.  
  263.    public SAXDriver() {
  264.       this.entityHandler = s_defaulthandler;
  265.       this.dtdHandler = s_defaulthandler;
  266.       this.documentHandler = s_defaulthandler;
  267.       this.errorHandler = s_defaulthandler;
  268.       this.pause = false;
  269.    }
  270. }
  271.