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

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