home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / pc / JAVA / MSJAVX86.EXE / xmldso.cab / XML4IE3.cab / com / ms / xml / om / Document.class (.txt) next >
Encoding:
Java Class File  |  1997-10-10  |  6.5 KB  |  358 lines

  1. package com.ms.xml.om;
  2.  
  3. import com.ms.xml.parser.DTD;
  4. import com.ms.xml.parser.ParseException;
  5. import com.ms.xml.parser.Parser;
  6. import com.ms.xml.util.Attribute;
  7. import com.ms.xml.util.Name;
  8. import com.ms.xml.util.XMLOutputStream;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16. import java.util.Enumeration;
  17. import java.util.Vector;
  18.  
  19. public class Document extends ElementImpl implements ElementFactory {
  20.    URL URLdoc;
  21.    Element DTDnode;
  22.    Element root;
  23.    Element XML;
  24.    protected DTD dtd;
  25.    String outputEncoding;
  26.    protected ElementFactory factory;
  27.    Parser parser;
  28.    Vector children;
  29.    int outputStyle;
  30.    static Name nameVERSION = Name.create("VERSION");
  31.    static Name nameENCODING = Name.create("ENCODING");
  32.    static Name nameDOCTYPE = Name.create("DOCTYPE");
  33.    static Name nameNAME = Name.create("NAME");
  34.    static Name nameURL = Name.create("URL");
  35.    static Name namePUBLICID = Name.create("PUBLICID");
  36.    static Name nameXML = Name.create("XML");
  37.    static Name nameENTITY = Name.create("ENTITY");
  38.    static String defaultEncoding = "UTF-8";
  39.    static String defaultVersion = "1.0";
  40.  
  41.    public final String getId() {
  42.       if (this.DTDnode == null) {
  43.          return null;
  44.       } else {
  45.          Object var1 = this.DTDnode.getAttribute(namePUBLICID);
  46.          return var1 != null ? var1.toString() : null;
  47.       }
  48.    }
  49.  
  50.    public Document() {
  51.       this.outputStyle = XMLOutputStream.PRETTY;
  52.       this.factory = new ElementFactoryImpl();
  53.       this.dtd = new DTD();
  54.    }
  55.  
  56.    public Document(ElementFactory var1) {
  57.       this.outputStyle = XMLOutputStream.PRETTY;
  58.       this.factory = var1;
  59.       this.dtd = new DTD();
  60.    }
  61.  
  62.    public final String getCharset() {
  63.       return this.getEncoding();
  64.    }
  65.  
  66.    public final void setCharset(String var1) {
  67.       this.setEncoding(var1);
  68.    }
  69.  
  70.    private final Element getXML() {
  71.       return this.XML;
  72.    }
  73.  
  74.    public void setOutputStyle(int var1) {
  75.       this.outputStyle = var1;
  76.    }
  77.  
  78.    public int getOutputStyle() {
  79.       return this.outputStyle;
  80.    }
  81.  
  82.    public final String getDTDURL() {
  83.       if (this.DTDnode == null) {
  84.          return null;
  85.       } else {
  86.          Object var1 = this.DTDnode.getAttribute(nameURL);
  87.          return var1 != null ? var1.toString() : null;
  88.       }
  89.    }
  90.  
  91.    public final String getVersion() {
  92.       if (this.getXML() != null) {
  93.          Object var1 = this.getXML().getAttribute(nameVERSION);
  94.          if (var1 != null) {
  95.             return var1.toString();
  96.          }
  97.       }
  98.  
  99.       return defaultVersion;
  100.    }
  101.  
  102.    public final void setVersion(String var1) {
  103.       if (this.getXML() != null) {
  104.          this.getXML().setAttribute(nameVERSION, var1);
  105.       }
  106.  
  107.    }
  108.  
  109.    public void load(String var1) throws ParseException {
  110.       URL var2;
  111.       try {
  112.          var2 = new URL(var1);
  113.       } catch (MalformedURLException var5) {
  114.          throw new ParseException("MalformedURLException: " + var5);
  115.       }
  116.  
  117.       this.load(var2);
  118.    }
  119.  
  120.    public void load(URL var1) throws ParseException {
  121.       this.clear();
  122.       this.URLdoc = var1;
  123.       this.parser = new Parser();
  124.       this.parser.parse(var1, this, this.dtd, this);
  125.    }
  126.  
  127.    public void load(InputStream var1) throws ParseException {
  128.       this.clear();
  129.       this.parser = new Parser();
  130.       this.parser.parse(var1, this, this.dtd, this);
  131.    }
  132.  
  133.    public void reportError(ParseException var1, OutputStream var2) {
  134.       if (this.parser != null) {
  135.          this.parser.report(var1, var2);
  136.       }
  137.  
  138.    }
  139.  
  140.    public XMLOutputStream createOutputStream(OutputStream var1) throws IOException {
  141.       XMLOutputStream var2 = null;
  142.       if (this.outputEncoding == null && this.parser != null) {
  143.          var2 = this.parser.createOutputStream(var1);
  144.       }
  145.  
  146.       if (var2 == null) {
  147.          Element var3 = this.getXML();
  148.          var2 = new XMLOutputStream(var1);
  149.          if (var3 != null) {
  150.             var3.setAttribute(nameENCODING, this.outputEncoding);
  151.             var2.setEncoding(this.outputEncoding, true, true);
  152.          }
  153.       }
  154.  
  155.       var2.setOutputStyle(this.outputStyle);
  156.       var2.dtd = this.getDTD();
  157.       return var2;
  158.    }
  159.  
  160.    public DTD getDTD() {
  161.       return this.dtd;
  162.    }
  163.  
  164.    public void save(XMLOutputStream var1) throws IOException {
  165.       var1.dtd = this.dtd;
  166.       if (this.getXML() != null) {
  167.          var1.writeChars("<?XML");
  168.          Enumeration var2 = this.getXML().getAttributes();
  169.  
  170.          while(var2.hasMoreElements()) {
  171.             Attribute var3 = (Attribute)var2.nextElement();
  172.             var1.writeChars(" " + var3.getName() + "=");
  173.             var1.writeQuotedString(var3.getValue().toString());
  174.          }
  175.  
  176.          var1.writeChars("?>");
  177.          var1.writeNewLine();
  178.       }
  179.  
  180.       ElementEnumeration var6 = new ElementEnumeration(this);
  181.  
  182.       while(var6.hasMoreElements()) {
  183.          Element var7 = (Element)var6.nextElement();
  184.          if (var7.getType() == 4) {
  185.             var1.writeChars("<!DOCTYPE " + this.getDocType());
  186.             if (this.getDTDURL() != null) {
  187.                if (this.getId() != null) {
  188.                   var1.writeChars(" PUBLIC ");
  189.                   var1.writeQuotedString(this.getId());
  190.                   var1.write(32);
  191.                } else {
  192.                   var1.writeChars(" SYSTEM ");
  193.                }
  194.  
  195.                var1.writeQuotedString(this.getDTDURL());
  196.             }
  197.  
  198.             if (var7.numElements() > 0) {
  199.                var1.writeChars(" [");
  200.                var1.writeNewLine();
  201.                var1.addIndent(1);
  202.                var1.savingDTD = true;
  203.                ElementEnumeration var4 = new ElementEnumeration(var7);
  204.  
  205.                while(var4.hasMoreElements()) {
  206.                   Element var5 = (Element)var4.nextElement();
  207.                   var5.save(var1);
  208.                }
  209.  
  210.                var1.savingDTD = false;
  211.                var1.addIndent(-1);
  212.                var1.write(93);
  213.             }
  214.  
  215.             var1.write(62);
  216.             var1.writeNewLine();
  217.          } else if (var7.getType() != 5 || var7.getTagName() != nameXML) {
  218.             var7.save(var1);
  219.          }
  220.       }
  221.  
  222.    }
  223.  
  224.    public void addChild(Element var1, Element var2) {
  225.       if (var1.getType() == 0) {
  226.          this.root = var1;
  227.       } else if (var1.getType() == 5 && var1.getTagName() == nameXML) {
  228.          this.XML = var1;
  229.       } else if (var1.getType() == 4) {
  230.          this.DTDnode = var1;
  231.       }
  232.  
  233.       super.addChild(var1, var2);
  234.    }
  235.  
  236.    public int getType() {
  237.       return 3;
  238.    }
  239.  
  240.    public final String getEncoding() {
  241.       if (this.outputEncoding != null) {
  242.          return this.outputEncoding;
  243.       } else {
  244.          if (this.getXML() != null) {
  245.             Object var1 = this.getXML().getAttribute(nameENCODING);
  246.             if (var1 != null) {
  247.                return var1.toString();
  248.             }
  249.          }
  250.  
  251.          return defaultEncoding;
  252.       }
  253.    }
  254.  
  255.    public final Element createElement(int var1, Name var2) {
  256.       return this.factory.createElement(var1, var2);
  257.    }
  258.  
  259.    public Element getParent() {
  260.       return null;
  261.    }
  262.  
  263.    public final void setEncoding(String var1) {
  264.       this.outputEncoding = var1;
  265.    }
  266.  
  267.    public long getFileModifiedDate() {
  268.       if (this.URLdoc != null) {
  269.          if (this.URLdoc.getProtocol().equals("file")) {
  270.             File var3 = new File(this.URLdoc.getFile());
  271.             return var3.lastModified();
  272.          } else {
  273.             try {
  274.                URLConnection var1 = this.URLdoc.openConnection();
  275.                var1.connect();
  276.                return var1.getLastModified();
  277.             } catch (IOException var2) {
  278.                return 0L;
  279.             }
  280.          }
  281.       } else {
  282.          return 0L;
  283.       }
  284.    }
  285.  
  286.    public final Element createElement(int var1, String var2) {
  287.       return this.factory.createElement(var1, Name.create(var2.toUpperCase()));
  288.    }
  289.  
  290.    public final Element createElement(int var1) {
  291.       return this.factory.createElement(var1, (Name)null);
  292.    }
  293.  
  294.    public final String getDocType() {
  295.       if (this.DTDnode == null) {
  296.          return null;
  297.       } else {
  298.          Object var1 = this.DTDnode.getAttribute(nameNAME);
  299.          return var1 != null ? var1.toString() : null;
  300.       }
  301.    }
  302.  
  303.    public void removeChild(Element var1) {
  304.       super.removeChild(var1);
  305.       if (this.root == var1) {
  306.          this.root = null;
  307.       } else if (this.XML == var1) {
  308.          this.XML = null;
  309.       } else {
  310.          if (this.XML == this.DTDnode) {
  311.             this.DTDnode = null;
  312.          }
  313.  
  314.       }
  315.    }
  316.  
  317.    public final Element getRoot() {
  318.       return this.root;
  319.    }
  320.  
  321.    public void parsed(Element var1) {
  322.       this.factory.parsed(var1);
  323.    }
  324.  
  325.    public final String getURL() {
  326.       return this.URLdoc.toString();
  327.    }
  328.  
  329.    public void setURL(String var1) throws ParseException {
  330.       this.load(var1);
  331.    }
  332.  
  333.    public final Enumeration elementDeclarations() {
  334.       return this.dtd.elementDeclarations();
  335.    }
  336.  
  337.    public void clear() {
  338.       this.DTDnode = null;
  339.       this.dtd.clear();
  340.       this.root = null;
  341.       this.XML = null;
  342.       this.URLdoc = null;
  343.       this.outputEncoding = null;
  344.       super.clear();
  345.    }
  346.  
  347.    public String getText() {
  348.       return this.root != null ? this.root.getText() : null;
  349.    }
  350.  
  351.    public void setText(String var1) {
  352.       if (this.root != null) {
  353.          this.root.setText(var1);
  354.       }
  355.  
  356.    }
  357. }
  358.