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
22package org.kxml2.kdom;
23
24import java.io.*;
25
26import org.xmlpull.v1.*;
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
31public 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
56    public String getName() {
57        return "#document";
58    }
59
60    /** Adds a child at the given index position. Throws
61    an exception when a second root element is added */
62
63    public void addChild(int index, int type, Object child) {
64        if (type == ELEMENT) {
65         //   if (rootIndex != -1)
66           //     throw new RuntimeException("Only one document root element allowed");
67
68            rootIndex = index;
69        }
70        else if (rootIndex >= index)
71            rootIndex++;
72
73        super.addChild(index, type, child);
74    }
75
76    /** reads the document and checks if the last event
77    is END_DOCUMENT. If not, an exception is thrown.
78    The end event is consumed. For parsing partial
79        XML structures, consider using Node.parse (). */
80
81    public void parse(XmlPullParser parser)
82        throws IOException, XmlPullParserException {
83
84        parser.require(XmlPullParser.START_DOCUMENT, null, null);
85        parser.nextToken ();
86
87        encoding = parser.getInputEncoding();
88        standalone = (Boolean)parser.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
116    /** Writes this node to the given XmlWriter. For node and document,
117        this method is identical to writeChildren, except that the
118        stream is flushed automatically. */
119
120    public void write(XmlSerializer writer)
121        throws IOException {
122
123        writer.startDocument(encoding, standalone);
124        writeChildren(writer);
125        writer.endDocument();
126    }
127
128
129}