001: package net.sf.saxon.dom;
002:
003: import net.sf.saxon.event.Emitter;
004: import net.sf.saxon.om.NamespaceConstant;
005: import net.sf.saxon.trans.DynamicError;
006: import net.sf.saxon.trans.XPathException;
007: import org.w3c.dom.*;
008:
009: /**
010: * DOMEmitter is an Emitter that attaches the result tree to a specified Node in a DOM Document
011: */
012:
013: public class DOMEmitter extends Emitter {
014: protected Node currentNode;
015: protected Document document;
016: private boolean canNormalize = true;
017:
018: /**
019: * Start of the document.
020: */
021:
022: public void open() {
023: }
024:
025: /**
026: * End of the document.
027: */
028:
029: public void close() {
030: }
031:
032: /**
033: * Start of a document node.
034: */
035:
036: public void startDocument(int properties) throws XPathException {
037: }
038:
039: /**
040: * Notify the end of a document node
041: */
042:
043: public void endDocument() throws XPathException {
044: }
045:
046: /**
047: * Start of an element.
048: */
049:
050: public void startElement(int nameCode, int typeCode,
051: int locationId, int properties) throws XPathException {
052: String qname = namePool.getDisplayName(nameCode);
053: String uri = namePool.getURI(nameCode);
054: try {
055: Element element = document.createElementNS(uri, qname);
056: currentNode.appendChild(element);
057: currentNode = element;
058: } catch (DOMException err) {
059: throw new DynamicError(err);
060: }
061: }
062:
063: public void namespace(int namespaceCode, int properties)
064: throws XPathException {
065: try {
066: String prefix = namePool
067: .getPrefixFromNamespaceCode(namespaceCode);
068: String uri = namePool
069: .getURIFromNamespaceCode(namespaceCode);
070: Element element = (Element) currentNode;
071: if (!(uri.equals(NamespaceConstant.XML))) {
072: if (prefix.equals("")) {
073: element.setAttributeNS(
074: "http://www.w3.org/2000/xmlns/", "xmlns",
075: uri);
076: } else {
077: element.setAttributeNS(
078: "http://www.w3.org/2000/xmlns/", "xmlns:"
079: + prefix, uri);
080:
081: }
082: }
083: } catch (DOMException err) {
084: throw new DynamicError(err);
085: }
086: }
087:
088: public void attribute(int nameCode, int typeCode,
089: CharSequence value, int locationId, int properties)
090: throws XPathException {
091: String qname = namePool.getDisplayName(nameCode);
092: String uri = namePool.getURI(nameCode);
093: try {
094: Element element = (Element) currentNode;
095: element.setAttributeNS(uri, qname, value.toString());
096: } catch (DOMException err) {
097: throw new DynamicError(err);
098: }
099: }
100:
101: public void startContent() throws XPathException {
102: }
103:
104: /**
105: * End of an element.
106: */
107:
108: public void endElement() throws XPathException {
109: if (canNormalize) {
110: try {
111: currentNode.normalize();
112: } catch (Throwable err) {
113: canNormalize = false;
114: } // in case it's a Level 1 DOM
115: }
116:
117: currentNode = currentNode.getParentNode();
118:
119: }
120:
121: /**
122: * Character data.
123: */
124:
125: public void characters(CharSequence chars, int locationId,
126: int properties) throws XPathException {
127: try {
128: Text text = document.createTextNode(chars.toString());
129: currentNode.appendChild(text);
130: } catch (DOMException err) {
131: throw new DynamicError(err);
132: }
133: }
134:
135: /**
136: * Handle a processing instruction.
137: */
138:
139: public void processingInstruction(String target, CharSequence data,
140: int locationId, int properties) throws XPathException {
141: try {
142: ProcessingInstruction pi = document
143: .createProcessingInstruction(target, data
144: .toString());
145: currentNode.appendChild(pi);
146: } catch (DOMException err) {
147: throw new DynamicError(err);
148: }
149: }
150:
151: /**
152: * Handle a comment.
153: */
154:
155: public void comment(CharSequence chars, int locationId,
156: int properties) throws XPathException {
157: try {
158: Comment comment = document.createComment(chars.toString());
159: currentNode.appendChild(comment);
160: } catch (DOMException err) {
161: throw new DynamicError(err);
162: }
163: }
164:
165: /**
166: * Set output destination
167: */
168:
169: public void setNode(Node node) {
170: if (node == null) {
171: return;
172: }
173: currentNode = node;
174: if (node.getNodeType() == Node.DOCUMENT_NODE) {
175: document = (Document) node;
176: } else {
177: document = currentNode.getOwnerDocument();
178: }
179: }
180:
181: }
182:
183: //
184: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
185: // you may not use this file except in compliance with the License. You may obtain a copy of the
186: // License at http://www.mozilla.org/MPL/
187: //
188: // Software distributed under the License is distributed on an "AS IS" basis,
189: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
190: // See the License for the specific language governing rights and limitations under the License.
191: //
192: // The Original Code is: all this file.
193: //
194: // The Initial Developer of the Original Code is Michael H. Kay.
195: //
196: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
197: //
198: // Contributor(s): none.
199: //
|