001: /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
002: *
003: * Permission is hereby granted, free of charge, to any person obtaining a copy
004: * of this software and associated documentation files (the "Software"), to deal
005: * in the Software without restriction, including without limitation the rights
006: * to use, copy, modify, merge, publish, distribute, sublicense, and/or
007: * sell copies of the Software, and to permit persons to whom the Software is
008: * furnished to do so, subject to the following conditions:
009: *
010: * The above copyright notice and this permission notice shall be included in
011: * all copies or substantial portions of the Software.
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
014: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
015: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
016: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
017: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
018: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
019: * IN THE SOFTWARE. */
020:
021: package org.kxml2.kdom;
022:
023: import java.io.*;
024:
025: import org.xmlpull.v1.*;
026:
027: /** The document consists of some legacy events and a single root
028: element. This class basically adds some consistency checks to
029: Node. */
030:
031: public class Document extends Node {
032:
033: protected int rootIndex = -1;
034: String encoding;
035: Boolean standalone;
036:
037: /** returns "#document" */
038:
039: public String getEncoding() {
040: return encoding;
041: }
042:
043: public void setEncoding(String enc) {
044: this .encoding = enc;
045: }
046:
047: public void setStandalone(Boolean standalone) {
048: this .standalone = standalone;
049: }
050:
051: public Boolean getStandalone() {
052: return standalone;
053: }
054:
055: public String getName() {
056: return "#document";
057: }
058:
059: /** Adds a child at the given index position. Throws
060: an exception when a second root element is added */
061:
062: public void addChild(int index, int type, Object child) {
063: if (type == ELEMENT) {
064: // if (rootIndex != -1)
065: // throw new RuntimeException("Only one document root element allowed");
066:
067: rootIndex = index;
068: } else if (rootIndex >= index)
069: rootIndex++;
070:
071: super .addChild(index, type, child);
072: }
073:
074: /** reads the document and checks if the last event
075: is END_DOCUMENT. If not, an exception is thrown.
076: The end event is consumed. For parsing partial
077: XML structures, consider using Node.parse (). */
078:
079: public void parse(XmlPullParser parser) throws IOException,
080: XmlPullParserException {
081:
082: parser.require(XmlPullParser.START_DOCUMENT, null, null);
083: parser.nextToken();
084:
085: encoding = parser.getInputEncoding();
086: standalone = (Boolean) parser
087: .getProperty("http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone");
088:
089: super .parse(parser);
090:
091: if (parser.getEventType() != XmlPullParser.END_DOCUMENT)
092: throw new RuntimeException("Document end expected!");
093:
094: }
095:
096: public void removeChild(int index) {
097: if (index == rootIndex)
098: rootIndex = -1;
099: else if (index < rootIndex)
100: rootIndex--;
101:
102: super .removeChild(index);
103: }
104:
105: /** returns the root element of this document. */
106:
107: public Element getRootElement() {
108: if (rootIndex == -1)
109: throw new RuntimeException("Document has no root element!");
110:
111: return (Element) getChild(rootIndex);
112: }
113:
114: /** Writes this node to the given XmlWriter. For node and document,
115: this method is identical to writeChildren, except that the
116: stream is flushed automatically. */
117:
118: public void write(XmlSerializer writer) throws IOException {
119:
120: writer.startDocument(encoding, standalone);
121: writeChildren(writer);
122: writer.endDocument();
123: }
124:
125: }
|