home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / parser.jar / com / sun / xml / tree / XmlDocument.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-02-23  |  11.9 KB  |  567 lines

  1. package com.sun.xml.tree;
  2.  
  3. import com.sun.xml.parser.Parser;
  4. import com.sun.xml.parser.Resolver;
  5. import com.sun.xml.parser.ValidatingParser;
  6. import com.sun.xml.util.MessageCatalog;
  7. import com.sun.xml.util.XmlNames;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.io.OutputStreamWriter;
  12. import java.io.Writer;
  13. import java.util.Locale;
  14. import org.w3c.dom.Attr;
  15. import org.w3c.dom.CDATASection;
  16. import org.w3c.dom.Comment;
  17. import org.w3c.dom.DOMException;
  18. import org.w3c.dom.DOMImplementation;
  19. import org.w3c.dom.Document;
  20. import org.w3c.dom.DocumentFragment;
  21. import org.w3c.dom.DocumentType;
  22. import org.w3c.dom.Element;
  23. import org.w3c.dom.EntityReference;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import org.w3c.dom.ProcessingInstruction;
  27. import org.w3c.dom.Text;
  28. import org.xml.sax.InputSource;
  29. import org.xml.sax.SAXException;
  30.  
  31. public class XmlDocument extends ParentNode implements DocumentEx, DOMImplementation {
  32.    static String eol;
  33.    static final MessageCatalog catalog;
  34.    private Locale locale = Locale.getDefault();
  35.    private String systemId;
  36.    private ElementFactory factory;
  37.    int mutationCount;
  38.    boolean replaceRootElement;
  39.    // $FF: synthetic field
  40.    static Class class$com$sun$xml$tree$XmlDocument$Catalog;
  41.  
  42.    static {
  43.       String var0;
  44.       try {
  45.          var0 = System.getProperty("line.separator", "\n");
  46.       } catch (SecurityException var1) {
  47.          var0 = "\n";
  48.       }
  49.  
  50.       eol = var0;
  51.       catalog = new Catalog();
  52.    }
  53.  
  54.    public Node appendChild(Node var1) throws DOMException {
  55.       if (var1 instanceof Element && this.getDocument() != null) {
  56.          throw new DomEx((short)3);
  57.       } else if (var1 instanceof DocumentType && this.getDoctype() != null) {
  58.          throw new DomEx((short)3);
  59.       } else {
  60.          return super.appendChild(var1);
  61.       }
  62.    }
  63.  
  64.    public final void changeNodeOwner(Node var1) throws DOMException {
  65.       if (var1.getOwnerDocument() != this) {
  66.          if (!(var1 instanceof NodeBase)) {
  67.             throw new DomEx((short)4);
  68.          } else {
  69.             switch (var1.getNodeType()) {
  70.                case 6:
  71.                case 9:
  72.                case 10:
  73.                case 12:
  74.                   throw new DomEx((short)3);
  75.                case 7:
  76.                case 8:
  77.                case 11:
  78.                default:
  79.                   if (var1 instanceof AttributeNode) {
  80.                      AttributeNode var4 = (AttributeNode)var1;
  81.                      ElementNode var5 = var4.getNameScope();
  82.                      if (var5 != null && ((NodeBase)var5).getOwnerDocument() != this) {
  83.                         throw new DomEx((short)3);
  84.                      }
  85.                   }
  86.  
  87.                   NodeBase var3 = (NodeBase)var1.getParentNode();
  88.                   if (var3 != null) {
  89.                      var3.removeChild(var1);
  90.                   }
  91.  
  92.                   TreeWalker var2 = new TreeWalker(var1);
  93.                   var3 = (NodeBase)var2.getCurrent();
  94.  
  95.                   for(; var3 != null; var3 = (NodeBase)var2.getNext()) {
  96.                      var3.setOwnerDocument(this);
  97.                      if (var3 instanceof ElementNode) {
  98.                         NamedNodeMap var8 = var3.getAttributes();
  99.                         int var9 = var8.getLength();
  100.  
  101.                         for(int var6 = 0; var6 < var9; ++var6) {
  102.                            this.changeNodeOwner(var8.item(var6));
  103.                         }
  104.                      }
  105.                   }
  106.  
  107.             }
  108.          }
  109.       }
  110.    }
  111.  
  112.    void checkChildType(int var1) throws DOMException {
  113.       switch (var1) {
  114.          case 1:
  115.          case 7:
  116.          case 8:
  117.          case 10:
  118.             return;
  119.          case 2:
  120.          case 3:
  121.          case 4:
  122.          case 5:
  123.          case 6:
  124.          case 9:
  125.          default:
  126.             throw new DomEx((short)3);
  127.       }
  128.    }
  129.  
  130.    public Locale chooseLocale(String[] var1) {
  131.       Locale var2 = catalog.chooseLocale(var1);
  132.       if (var2 != null) {
  133.          this.setLocale(var2);
  134.       }
  135.  
  136.       return var2;
  137.    }
  138.  
  139.    // $FF: synthetic method
  140.    static Class class$(String var0) {
  141.       try {
  142.          return Class.forName(var0);
  143.       } catch (ClassNotFoundException var2) {
  144.          throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  145.       }
  146.    }
  147.  
  148.    public Node cloneNode(boolean var1) {
  149.       XmlDocument var2 = new XmlDocument();
  150.       var2.systemId = this.systemId;
  151.       Node var3;
  152.       if (var1) {
  153.          for(int var4 = 0; (var3 = ((ParentNode)this).item(var4)) != null; ++var4) {
  154.             if (!(var3 instanceof DocumentType)) {
  155.                var3 = var3.cloneNode(true);
  156.                var2.changeNodeOwner(var3);
  157.                var2.appendChild(var3);
  158.             }
  159.          }
  160.       }
  161.  
  162.       return var2;
  163.    }
  164.  
  165.    public Attr createAttribute(String var1) throws DOMException {
  166.       if (!XmlNames.isName(var1)) {
  167.          throw new DomEx((short)5);
  168.       } else {
  169.          AttributeNode var2 = new AttributeNode(var1, (String)null, true, (String)null);
  170.          ((NodeBase)var2).setOwnerDocument(this);
  171.          return var2;
  172.       }
  173.    }
  174.  
  175.    public CDATASection createCDATASection(String var1) {
  176.       CDataNode var2 = new CDataNode();
  177.       if (var1 != null) {
  178.          ((DataNode)var2).setText(var1.toCharArray());
  179.       }
  180.  
  181.       ((NodeBase)var2).setOwnerDocument(this);
  182.       return var2;
  183.    }
  184.  
  185.    public Comment createComment(String var1) {
  186.       CommentNode var2 = new CommentNode(var1);
  187.       ((NodeBase)var2).setOwnerDocument(this);
  188.       return var2;
  189.    }
  190.  
  191.    Doctype createDoctype(String var1) {
  192.       Doctype var2 = new Doctype(var1);
  193.       var2.setOwnerDocument(this);
  194.       return var2;
  195.    }
  196.  
  197.    public DocumentFragment createDocumentFragment() {
  198.       DocFragNode var1 = new DocFragNode();
  199.       ((NodeBase)var1).setOwnerDocument(this);
  200.       return var1;
  201.    }
  202.  
  203.    public final Element createElement(String var1) throws DOMException {
  204.       return this.createElementEx(var1);
  205.    }
  206.  
  207.    public final ElementEx createElementEx(String var1) throws DOMException {
  208.       if (!XmlNames.isName(var1)) {
  209.          throw new DomEx((short)5);
  210.       } else {
  211.          ElementNode var2;
  212.          if (this.factory != null) {
  213.             var2 = (ElementNode)this.factory.createElementEx(var1);
  214.          } else {
  215.             var2 = new ElementNode();
  216.          }
  217.  
  218.          var2.setTag(var1);
  219.          ((NodeBase)var2).setOwnerDocument(this);
  220.          return var2;
  221.       }
  222.    }
  223.  
  224.    public final ElementEx createElementEx(String var1, String var2) throws DOMException {
  225.       if (!XmlNames.isName(var2)) {
  226.          throw new DomEx((short)5);
  227.       } else {
  228.          ElementNode var3;
  229.          if (this.factory != null) {
  230.             var3 = (ElementNode)this.factory.createElementEx(var1, var2);
  231.          } else {
  232.             var3 = new ElementNode();
  233.          }
  234.  
  235.          var3.setTag(var2);
  236.          ((NodeBase)var3).setOwnerDocument(this);
  237.          return var3;
  238.       }
  239.    }
  240.  
  241.    public EntityReference createEntityReference(String var1) throws DOMException {
  242.       if (!XmlNames.isName(var1)) {
  243.          throw new DomEx((short)5);
  244.       } else {
  245.          EntityRefNode var2 = new EntityRefNode(var1);
  246.          ((NodeBase)var2).setOwnerDocument(this);
  247.          return var2;
  248.       }
  249.    }
  250.  
  251.    public ProcessingInstruction createProcessingInstruction(String var1, String var2) throws DOMException {
  252.       if (!XmlNames.isName(var1)) {
  253.          throw new DomEx((short)5);
  254.       } else {
  255.          PINode var3 = new PINode(var1, var2);
  256.          ((NodeBase)var3).setOwnerDocument(this);
  257.          return var3;
  258.       }
  259.    }
  260.  
  261.    public Text createTextNode(String var1) {
  262.       TextNode var2 = new TextNode();
  263.       ((NodeBase)var2).setOwnerDocument(this);
  264.       if (var1 != null) {
  265.          ((DataNode)var2).setText(var1.toCharArray());
  266.       }
  267.  
  268.       return var2;
  269.    }
  270.  
  271.    public XmlWriteContext createWriteContext(Writer var1) {
  272.       return new ExtWriteContext(this, var1);
  273.    }
  274.  
  275.    public XmlWriteContext createWriteContext(Writer var1, int var2) {
  276.       return new ExtWriteContext(this, var1, var2);
  277.    }
  278.  
  279.    public static XmlDocument createXmlDocument(InputStream var0, boolean var1) throws IOException, SAXException {
  280.       return createXmlDocument(new InputSource(var0), var1);
  281.    }
  282.  
  283.    public static XmlDocument createXmlDocument(String var0) throws IOException, SAXException {
  284.       return createXmlDocument(new InputSource(var0), false);
  285.    }
  286.  
  287.    public static XmlDocument createXmlDocument(String var0, boolean var1) throws IOException, SAXException {
  288.       return createXmlDocument(new InputSource(var0), var1);
  289.    }
  290.  
  291.    public static XmlDocument createXmlDocument(InputSource var0, boolean var1) throws IOException, SAXException {
  292.       Object var2;
  293.       XmlDocumentBuilder var3;
  294.       try {
  295.          if (var1) {
  296.             var2 = new ValidatingParser(true);
  297.          } else {
  298.             var2 = new Parser();
  299.          }
  300.  
  301.          ((Parser)var2).setEntityResolver(new Resolver());
  302.          var3 = new XmlDocumentBuilder();
  303.          var3.setDisableNamespaces(true);
  304.       } catch (Exception var5) {
  305.          throw new SAXException(var5);
  306.       }
  307.  
  308.       var3.setParser((org.xml.sax.Parser)var2);
  309.       ((Parser)var2).parse(var0);
  310.       return var3.getDocument();
  311.    }
  312.  
  313.    public final DocumentType getDoctype() {
  314.       int var1 = 0;
  315.  
  316.       while(true) {
  317.          Node var2 = ((ParentNode)this).item(var1);
  318.          if (var2 == null) {
  319.             return null;
  320.          }
  321.  
  322.          if (var2 instanceof DocumentType) {
  323.             return (DocumentType)var2;
  324.          }
  325.  
  326.          ++var1;
  327.       }
  328.    }
  329.  
  330.    ElementNode getDocument() {
  331.       int var1 = 0;
  332.  
  333.       while(true) {
  334.          Node var2 = ((ParentNode)this).item(var1);
  335.          if (var2 == null) {
  336.             return null;
  337.          }
  338.  
  339.          if (var2 instanceof ElementNode) {
  340.             return (ElementNode)var2;
  341.          }
  342.  
  343.          ++var1;
  344.       }
  345.    }
  346.  
  347.    public Element getDocumentElement() {
  348.       return this.getDocument();
  349.    }
  350.  
  351.    public ElementEx getElementExById(String var1) {
  352.       if (var1 == null) {
  353.          throw new IllegalArgumentException(((NodeBase)this).getMessage("XD-000"));
  354.       } else {
  355.          TreeWalker var2 = new TreeWalker(this);
  356.  
  357.          ElementEx var3;
  358.          while((var3 = (ElementEx)var2.getNextElement((String)null)) != null) {
  359.             String var4 = var3.getIdAttributeName();
  360.             if (var4 != null) {
  361.                String var5 = var3.getAttribute(var4);
  362.                if (var5.equals(var1)) {
  363.                   return var3;
  364.                }
  365.             }
  366.          }
  367.  
  368.          return null;
  369.       }
  370.    }
  371.  
  372.    public final ElementFactory getElementFactory() {
  373.       return this.factory;
  374.    }
  375.  
  376.    public DOMImplementation getImplementation() {
  377.       return this;
  378.    }
  379.  
  380.    public Locale getLocale() {
  381.       return this.locale;
  382.    }
  383.  
  384.    public final String getNodeName() {
  385.       return "#document";
  386.    }
  387.  
  388.    public final short getNodeType() {
  389.       return 9;
  390.    }
  391.  
  392.    public Document getOwnerDoc() {
  393.       return null;
  394.    }
  395.  
  396.    public final String getSystemId() {
  397.       return this.systemId;
  398.    }
  399.  
  400.    public boolean hasFeature(String var1, String var2) {
  401.       if (!"XML".equalsIgnoreCase(var1)) {
  402.          return false;
  403.       } else {
  404.          return var2 != null && "1.0".equals(var2);
  405.       }
  406.    }
  407.  
  408.    public Node insertBefore(Node var1, Node var2) throws DOMException {
  409.       if (!this.replaceRootElement && var1 instanceof Element && this.getDocument() != null) {
  410.          throw new DomEx((short)3);
  411.       } else if (!this.replaceRootElement && var1 instanceof DocumentType && this.getDoctype() != null) {
  412.          throw new DomEx((short)3);
  413.       } else {
  414.          return super.insertBefore(var1, var2);
  415.       }
  416.    }
  417.  
  418.    static String java2std(String var0) {
  419.       if (var0 == null) {
  420.          return null;
  421.       } else if (var0.startsWith("ISO8859_")) {
  422.          return "ISO-8859-" + var0.substring(8);
  423.       } else if (var0.startsWith("8859_")) {
  424.          return "ISO-8859-" + var0.substring(5);
  425.       } else if (!"ASCII7".equalsIgnoreCase(var0) && !"ASCII".equalsIgnoreCase(var0)) {
  426.          if ("UTF8".equalsIgnoreCase(var0)) {
  427.             return "UTF-8";
  428.          } else if (var0.startsWith("Unicode")) {
  429.             return "UTF-16";
  430.          } else if ("SJIS".equalsIgnoreCase(var0)) {
  431.             return "Shift_JIS";
  432.          } else if ("JIS".equalsIgnoreCase(var0)) {
  433.             return "ISO-2022-JP";
  434.          } else {
  435.             return "EUCJIS".equalsIgnoreCase(var0) ? "EUC-JP" : var0;
  436.          }
  437.       } else {
  438.          return "US-ASCII";
  439.       }
  440.    }
  441.  
  442.    TextNode newText(char[] var1, int var2, int var3) throws SAXException {
  443.       TextNode var4 = (TextNode)this.createTextNode((String)null);
  444.       char[] var5 = new char[var3];
  445.       System.arraycopy(var1, var2, var5, 0, var3);
  446.       ((DataNode)var4).setText(var5);
  447.       return var4;
  448.    }
  449.  
  450.    public Node replaceChild(Node var1, Node var2) throws DOMException {
  451.       if (var1 instanceof DocumentFragment) {
  452.          int var3 = 0;
  453.          int var4 = 0;
  454.          this.replaceRootElement = false;
  455.          ParentNode var5 = (ParentNode)var1;
  456.  
  457.          Node var6;
  458.          for(int var7 = 0; (var6 = var5.item(var7)) != null; ++var7) {
  459.             if (var6 instanceof Element) {
  460.                ++var3;
  461.             } else if (var6 instanceof DocumentType) {
  462.                ++var4;
  463.             }
  464.          }
  465.  
  466.          if (var3 > 1 || var4 > 1) {
  467.             throw new DomEx((short)3);
  468.          }
  469.  
  470.          this.replaceRootElement = true;
  471.       }
  472.  
  473.       return super.replaceChild(var1, var2);
  474.    }
  475.  
  476.    public DocumentType setDoctype(String var1, String var2, String var3) {
  477.       Doctype var4 = (Doctype)this.getDoctype();
  478.       if (var4 != null) {
  479.          var4.setPrintInfo(var1, var2, var3);
  480.       } else {
  481.          var4 = new Doctype(var1, var2, var3);
  482.          var4.setOwnerDocument(this);
  483.          this.insertBefore(var4, ((ParentNode)this).getFirstChild());
  484.       }
  485.  
  486.       return var4;
  487.    }
  488.  
  489.    public final void setElementFactory(ElementFactory var1) {
  490.       this.factory = var1;
  491.    }
  492.  
  493.    public void setLocale(Locale var1) {
  494.       if (var1 == null) {
  495.          var1 = Locale.getDefault();
  496.       }
  497.  
  498.       this.locale = var1;
  499.    }
  500.  
  501.    public final void setSystemId(String var1) {
  502.       this.systemId = var1;
  503.    }
  504.  
  505.    public void write(OutputStream var1) throws IOException {
  506.       OutputStreamWriter var2 = new OutputStreamWriter(var1, "UTF8");
  507.       this.write(var2, "UTF-8");
  508.    }
  509.  
  510.    public void write(Writer var1) throws IOException {
  511.       String var2 = null;
  512.       if (var1 instanceof OutputStreamWriter) {
  513.          var2 = java2std(((OutputStreamWriter)var1).getEncoding());
  514.       }
  515.  
  516.       this.write(var1, var2);
  517.    }
  518.  
  519.    public void write(Writer var1, String var2) throws IOException {
  520.       var1.write("<?xml version=\"1.0\"");
  521.       if (var2 != null) {
  522.          var1.write(" encoding=\"");
  523.          var1.write(var2);
  524.          var1.write(34);
  525.       }
  526.  
  527.       var1.write("?>");
  528.       var1.write(eol);
  529.       var1.write(eol);
  530.       this.writeChildrenXml(this.createWriteContext(var1, 0));
  531.       var1.write(eol);
  532.       var1.flush();
  533.    }
  534.  
  535.    public void writeChildrenXml(XmlWriteContext var1) throws IOException {
  536.       int var2 = ((ParentNode)this).getLength();
  537.       Writer var3 = var1.getWriter();
  538.       if (var2 != 0) {
  539.          for(int var4 = 0; var4 < var2; ++var4) {
  540.             ((NodeBase)((ParentNode)this).item(var4)).writeXml(var1);
  541.             var3.write(eol);
  542.          }
  543.  
  544.       }
  545.    }
  546.  
  547.    public void writeXml(XmlWriteContext var1) throws IOException {
  548.       Writer var2 = var1.getWriter();
  549.       String var3 = null;
  550.       if (var2 instanceof OutputStreamWriter) {
  551.          var3 = java2std(((OutputStreamWriter)var2).getEncoding());
  552.       }
  553.  
  554.       var2.write("<?xml version=\"1.0\"");
  555.       if (var3 != null) {
  556.          var2.write(" encoding=\"");
  557.          var2.write(var3);
  558.          var2.write(34);
  559.       }
  560.  
  561.       var2.write("?>");
  562.       var2.write(eol);
  563.       var2.write(eol);
  564.       this.writeChildrenXml(var1);
  565.    }
  566. }
  567.