001: package com.rimfaxe.xml.xmlreader;
002:
003: /** This class returns the DOM Document created by parsing XML.
004:
005: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
006: This file is part of Sparta, an XML Parser, DOM, and XPath library.
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public License
009: as published by the Free Software Foundation; either version 2.1 of
010: the License, or (at your option) any later version. This library
011: is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: FITNESS FOR A PARTICULAR PURPOSE.</small></blockquote>
014: @see <a "href="doc-files/LGPL.txt">GNU Lesser General Public License</a>
015: @version $Date: 2002/10/30 16:40:27 $ $Revision: 1.2 $
016: @author Sergio Marti
017: */
018:
019: class BuildDocument implements DocumentSource, ParseHandler {
020:
021: public BuildDocument() {
022: this (null);
023: }
024:
025: public BuildDocument(ParseLog log) {
026: log_ = (log == null) ? DEFAULT_LOG : log;
027: }
028:
029: public void setParseSource(ParseSource ps) {
030: parseSource_ = ps;
031: doc_.setSystemId(ps.toString());
032: }
033:
034: public ParseSource getParseSource() {
035: return parseSource_;
036: }
037:
038: public String toString() {
039: if (parseSource_ != null)
040: return "BuildDoc: " + parseSource_.toString();
041: else
042: return null;
043: }
044:
045: public String getSystemId() {
046: if (parseSource_ != null)
047: return parseSource_.getSystemId();
048: else
049: return null;
050: }
051:
052: public int getLineNumber() {
053: if (parseSource_ != null)
054: return parseSource_.getLineNumber();
055: else
056: return -1;
057: }
058:
059: /** The parsed document. */
060: public Document getDocument() {
061: return doc_;
062: }
063:
064: public void startDocument() {
065: }
066:
067: public void endDocument() {
068: /* DEBUG
069: if (currentElement_ != null)
070: log_.warning("EndDocument: currentElement is not null",
071: getSystemId(), getLineNumber());
072: */
073: }
074:
075: public void startElement(Element element) {
076: if (currentElement_ == null) {
077: doc_.setDocumentElement(element);
078: } else {
079: currentElement_.appendChild(element);
080: }
081: currentElement_ = element;
082: }
083:
084: public void endElement(Element element) {
085: /* DEBUG
086: if (isCENull())
087: return;
088: if (element != currentElement_) {
089: log_.warning("EndElement (" + element.getTagName() +
090: ") does not match currentElement (" +
091: currentElement_.getTagName() + ")", getSystemId(),
092: getLineNumber());
093: return;
094: }
095: */
096:
097: currentElement_ = (Element) currentElement_.getParentNode();
098: }
099:
100: public void characters(char[] buf, int offset, int len) {
101: /* DEBUG
102: if (isCENull())
103: return;
104: */
105:
106: Element element = currentElement_;
107: if (element.getLastChild() instanceof Text) {
108: Text text = (Text) element.getLastChild();
109: text.appendData(buf, offset, len);
110: } else {
111: Text text = new Text(new String(buf, offset, len));
112: element.appendChildNoChecking(text);
113: }
114: }
115:
116: private boolean isCENull() {
117: if (currentElement_ == null) {
118: log_.warning("currentElement is null", getSystemId(),
119: getLineNumber());
120: return true;
121: } else
122: return false;
123: }
124:
125: private final ParseLog log_;
126:
127: private Element currentElement_ = null;
128: private final Document doc_ = new Document();
129: private ParseSource parseSource_ = null;
130: }
131:
132: // $Log: BuildDocument.java,v $
133: // Revision 1.2 2002/10/30 16:40:27 eobrain
134: // appendChild no longer throws DOMException
135: //
136: // Revision 1.1.1.1 2002/08/19 05:04:02 eobrain
137: // import from HP Labs internal CVS
138: //
139: // Revision 1.6 2002/08/18 04:30:54 eob
140: // Make class package-private so as not to clutter up the javadoc.
141: //
142: // Revision 1.5 2002/08/17 19:04:36 eob
143: // Add copyright and other formatting and commenting in preparation for
144: // release to SourceForge.
145: //
146: // Revision 1.4 2002/08/15 22:51:37 eob
147: // Sparta node constructors no longer needs document
148: //
149: // Revision 1.3 2002/08/05 20:04:31 sermarti
150: //
151: // Revision 1.2 2002/08/01 23:29:17 sermarti
152: // Much faster Sparta parsing.
153: // Has debug features enabled by default. Currently toggled
154: // in ParseCharStream.java and recompiled.
155: //
156: // Revision 1.1 2002/07/25 21:10:15 sermarti
157: // Adding files that mysteriously weren't added from Sparta before.
|