001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.staxutils;
019:
020: import java.util.Collections;
021: import java.util.Map;
022: import java.util.Stack;
023:
024: import javax.xml.namespace.NamespaceContext;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.parsers.ParserConfigurationException;
028: import javax.xml.stream.XMLStreamException;
029: import javax.xml.stream.XMLStreamWriter;
030:
031: import org.w3c.dom.Attr;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034:
035: public class W3CDOMStreamWriter implements XMLStreamWriter {
036: static final String XML_NS = "http://www.w3.org/2000/xmlns/";
037: private Stack<Element> stack = new Stack<Element>();
038: private Document document;
039: private Element currentNode;
040: private NamespaceContext context;
041: private Map properties = Collections.EMPTY_MAP;
042:
043: public W3CDOMStreamWriter() throws ParserConfigurationException {
044: DocumentBuilderFactory factory = DocumentBuilderFactory
045: .newInstance();
046: factory.setNamespaceAware(true);
047: document = factory.newDocumentBuilder().newDocument();
048: }
049:
050: public W3CDOMStreamWriter(DocumentBuilder builder) {
051: document = builder.newDocument();
052: }
053:
054: public W3CDOMStreamWriter(Document document) {
055: this .document = document;
056: }
057:
058: public W3CDOMStreamWriter(Element e) {
059: this .document = e.getOwnerDocument();
060:
061: currentNode = e;
062:
063: W3CNamespaceContext newContext = new W3CNamespaceContext();
064: newContext.setElement(currentNode);
065: this .context = newContext;
066: }
067:
068: public void setProperties(Map properties) {
069: this .properties = properties;
070: }
071:
072: public Document getDocument() {
073: return document;
074: }
075:
076: public void writeStartElement(String local)
077: throws XMLStreamException {
078: newChild(document.createElement(local));
079: }
080:
081: private void newChild(Element element) {
082: if (currentNode != null) {
083: stack.push(currentNode);
084: currentNode.appendChild(element);
085: } else {
086: document.appendChild(element);
087: }
088:
089: W3CNamespaceContext newContext = new W3CNamespaceContext();
090: newContext.setElement(element);
091: this .context = newContext;
092:
093: currentNode = element;
094: }
095:
096: public void writeStartElement(String namespace, String local)
097: throws XMLStreamException {
098: newChild(document.createElementNS(namespace, local));
099: }
100:
101: public void writeStartElement(String prefix, String local,
102: String namespace) throws XMLStreamException {
103: if (prefix == null || prefix.equals("")) {
104: writeStartElement(namespace, local);
105: } else {
106: newChild(document.createElementNS(namespace, prefix + ":"
107: + local));
108: }
109: }
110:
111: public void writeEmptyElement(String namespace, String local)
112: throws XMLStreamException {
113: writeStartElement(namespace, local);
114: }
115:
116: public void writeEmptyElement(String prefix, String namespace,
117: String local) throws XMLStreamException {
118: writeStartElement(prefix, namespace, local);
119: }
120:
121: public void writeEmptyElement(String local)
122: throws XMLStreamException {
123: writeStartElement(local);
124: }
125:
126: public void writeEndElement() throws XMLStreamException {
127: if (stack.size() > 0) {
128: currentNode = (Element) stack.pop();
129: } else {
130: currentNode = null;
131: }
132: ((W3CNamespaceContext) context).setElement(currentNode);
133: }
134:
135: public void writeEndDocument() throws XMLStreamException {
136: }
137:
138: public void writeAttribute(String local, String value)
139: throws XMLStreamException {
140: Attr a = document.createAttribute(local);
141: a.setValue(value);
142: currentNode.setAttributeNode(a);
143: }
144:
145: public void writeAttribute(String prefix, String namespace,
146: String local, String value) throws XMLStreamException {
147: if (prefix.length() > 0) {
148: local = prefix + ":" + local;
149: }
150:
151: Attr a = document.createAttributeNS(namespace, local);
152: a.setValue(value);
153: currentNode.setAttributeNodeNS(a);
154: }
155:
156: public void writeAttribute(String namespace, String local,
157: String value) throws XMLStreamException {
158: Attr a = document.createAttributeNS(namespace, local);
159: a.setValue(value);
160: currentNode.setAttributeNodeNS(a);
161: }
162:
163: public void writeNamespace(String prefix, String namespace)
164: throws XMLStreamException {
165: if (prefix.length() == 0) {
166: writeDefaultNamespace(namespace);
167: } else {
168: currentNode.setAttributeNS(XML_NS, "xmlns:" + prefix,
169: namespace);
170: }
171: }
172:
173: public void writeDefaultNamespace(String namespace)
174: throws XMLStreamException {
175: currentNode.setAttributeNS(XML_NS, "xmlns", namespace);
176: }
177:
178: public void writeComment(String value) throws XMLStreamException {
179: currentNode.appendChild(document.createComment(value));
180: }
181:
182: public void writeProcessingInstruction(String target)
183: throws XMLStreamException {
184: currentNode.appendChild(document.createProcessingInstruction(
185: target, null));
186: }
187:
188: public void writeProcessingInstruction(String target, String data)
189: throws XMLStreamException {
190: currentNode.appendChild(document.createProcessingInstruction(
191: target, data));
192: }
193:
194: public void writeCData(String data) throws XMLStreamException {
195: currentNode.appendChild(document.createCDATASection(data));
196: }
197:
198: public void writeDTD(String arg0) throws XMLStreamException {
199: throw new UnsupportedOperationException();
200: }
201:
202: public void writeEntityRef(String ref) throws XMLStreamException {
203: currentNode.appendChild(document.createEntityReference(ref));
204: }
205:
206: public void writeStartDocument() throws XMLStreamException {
207: }
208:
209: public void writeStartDocument(String version)
210: throws XMLStreamException {
211: writeStartDocument();
212: }
213:
214: public void writeStartDocument(String encoding, String version)
215: throws XMLStreamException {
216: writeStartDocument();
217: }
218:
219: public void writeCharacters(String text) throws XMLStreamException {
220: currentNode.appendChild(document.createTextNode(text));
221: }
222:
223: public void writeCharacters(char[] text, int start, int len)
224: throws XMLStreamException {
225: writeCharacters(new String(text, start, len));
226: }
227:
228: public String getPrefix(String uri) throws XMLStreamException {
229: return context == null ? null : context.getPrefix(uri);
230: }
231:
232: public void setPrefix(String arg0, String arg1)
233: throws XMLStreamException {
234: }
235:
236: public void setDefaultNamespace(String arg0)
237: throws XMLStreamException {
238: }
239:
240: public void setNamespaceContext(NamespaceContext ctx)
241: throws XMLStreamException {
242: this .context = ctx;
243: }
244:
245: public NamespaceContext getNamespaceContext() {
246: return context;
247: }
248:
249: public Object getProperty(String prop)
250: throws IllegalArgumentException {
251: return properties.get(prop);
252: }
253:
254: public void close() throws XMLStreamException {
255: }
256:
257: public void flush() throws XMLStreamException {
258: }
259:
260: }
|