Home | History | Annotate | Download | only in kdom
      1 /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
      2  *
      3  * Permission is hereby granted, free of charge, to any person obtaining a copy
      4  * of this software and associated documentation files (the "Software"), to deal
      5  * in the Software without restriction, including without limitation the rights
      6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or
      7  * sell copies of the Software, and to permit persons to whom the Software is
      8  * furnished to do so, subject to the following conditions:
      9  *
     10  * The  above copyright notice and this permission notice shall be included in
     11  * all copies or substantial portions of the Software.
     12  *
     13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     19  * IN THE SOFTWARE. */
     20 
     21 package org.ksoap2.kdom;
     22 
     23 import java.io.*;
     24 
     25 import org.xmlpull.v1.*;
     26 
     27 /** The document consists of some legacy events and a single root
     28     element. This class basically adds some consistency checks to
     29     Node. */
     30 
     31 public class Document extends Node {
     32 
     33     protected int rootIndex = -1;
     34     String encoding;
     35     Boolean standalone;
     36 
     37     /** returns "#document" */
     38 
     39     public String getEncoding() {
     40         return encoding;
     41     }
     42 
     43     public void setEncoding(String enc) {
     44         this.encoding = enc;
     45     }
     46 
     47     public void setStandalone(Boolean standalone) {
     48         this.standalone = standalone;
     49     }
     50 
     51     public Boolean getStandalone() {
     52         return standalone;
     53     }
     54 
     55     public String getName() {
     56         return "#document";
     57     }
     58 
     59     /** Adds a child at the given index position. Throws
     60     an exception when a second root element is added */
     61 
     62     public void addChild(int index, int type, Object child) {
     63         if (type == ELEMENT) {
     64             //   if (rootIndex != -1)
     65             //     throw new RuntimeException("Only one document root element allowed");
     66 
     67             rootIndex = index;
     68         }
     69         else if (rootIndex >= index)
     70             rootIndex++;
     71 
     72         super.addChild(index, type, child);
     73     }
     74 
     75     /** reads the document and checks if the last event
     76     is END_DOCUMENT. If not, an exception is thrown.
     77     The end event is consumed. For parsing partial
     78         XML structures, consider using Node.parse (). */
     79 
     80     public void parse(XmlPullParser parser)
     81             throws IOException, XmlPullParserException {
     82 
     83         parser.require(XmlPullParser.START_DOCUMENT, null, null);
     84         parser.nextToken();
     85 
     86         encoding = parser.getInputEncoding();
     87         standalone = (Boolean) parser
     88                 .getProperty("http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone");
     89 
     90         super.parse(parser);
     91 
     92         if (parser.getEventType() != XmlPullParser.END_DOCUMENT)
     93             throw new RuntimeException("Document end expected!");
     94 
     95     }
     96 
     97     public void removeChild(int index) {
     98         if (index == rootIndex)
     99             rootIndex = -1;
    100         else if (index < rootIndex)
    101             rootIndex--;
    102 
    103         super.removeChild(index);
    104     }
    105 
    106     /** returns the root element of this document. */
    107 
    108     public Element getRootElement() {
    109         if (rootIndex == -1)
    110             throw new RuntimeException("Document has no root element!");
    111 
    112         return (Element) getChild(rootIndex);
    113     }
    114 
    115     /** Writes this node to the given XmlWriter. For node and document,
    116         this method is identical to writeChildren, except that the
    117         stream is flushed automatically. */
    118 
    119     public void write(XmlSerializer writer)
    120             throws IOException {
    121 
    122         writer.startDocument(encoding, standalone);
    123         writeChildren(writer);
    124         writer.endDocument();
    125     }
    126 
    127 }
    128