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.aegis.util.stax;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Stack;
023:
024: import javax.xml.namespace.NamespaceContext;
025: import javax.xml.stream.XMLStreamException;
026: import javax.xml.stream.XMLStreamWriter;
027:
028: import org.apache.cxf.aegis.util.NamespaceHelper;
029: import org.jdom.Attribute;
030: import org.jdom.CDATA;
031: import org.jdom.Comment;
032: import org.jdom.Document;
033: import org.jdom.Element;
034: import org.jdom.EntityRef;
035: import org.jdom.Namespace;
036:
037: public class JDOMStreamWriter implements XMLStreamWriter {
038: private Stack<Element> stack = new Stack<Element>();
039:
040: private Document document;
041:
042: private Element currentNode;
043:
044: private NamespaceContext context;
045:
046: private Map properties = new HashMap();
047:
048: public JDOMStreamWriter() {
049: }
050:
051: public JDOMStreamWriter(Element e) {
052: newChild(e);
053: }
054:
055: public void close() throws XMLStreamException {
056: }
057:
058: public void flush() throws XMLStreamException {
059: }
060:
061: public void writeStartElement(String local)
062: throws XMLStreamException {
063: newChild(new Element(local));
064: }
065:
066: private void newChild(Element element) {
067: if (currentNode != null) {
068: stack.push(currentNode);
069: currentNode.addContent(element);
070: } else {
071: if (document != null) {
072: document.setRootElement(element);
073: }
074: }
075:
076: JDOMNamespaceContext ctx = new JDOMNamespaceContext();
077: ctx.setElement(element);
078: this .context = ctx;
079:
080: currentNode = element;
081: }
082:
083: public void writeStartElement(String namespace, String local)
084: throws XMLStreamException {
085: newChild(new Element(local, namespace));
086: }
087:
088: public void writeStartElement(String prefix, String local,
089: String namespace) throws XMLStreamException {
090: if (prefix == null || prefix.equals("")) {
091: writeStartElement(namespace, local);
092: } else {
093: newChild(new Element(local, prefix, namespace));
094: }
095: }
096:
097: public void writeEmptyElement(String namespace, String local)
098: throws XMLStreamException {
099: writeStartElement(namespace, local);
100: }
101:
102: public void writeEmptyElement(String prefix, String namespace,
103: String local) throws XMLStreamException {
104: writeStartElement(prefix, namespace, local);
105: }
106:
107: public void writeEmptyElement(String local)
108: throws XMLStreamException {
109: writeStartElement(local);
110: }
111:
112: public void writeEndElement() throws XMLStreamException {
113: currentNode = stack.pop();
114: }
115:
116: public void writeEndDocument() throws XMLStreamException {
117: }
118:
119: public void writeAttribute(String local, String value)
120: throws XMLStreamException {
121: currentNode.setAttribute(new Attribute(local, value));
122: }
123:
124: public void writeAttribute(String prefix, String namespace,
125: String local, String value) throws XMLStreamException {
126: currentNode.setAttribute(new Attribute(local, value, Namespace
127: .getNamespace(prefix, namespace)));
128: }
129:
130: public void writeAttribute(String namespace, String local,
131: String value) throws XMLStreamException {
132: currentNode.setAttribute(new Attribute(local, value, Namespace
133: .getNamespace(namespace)));
134: }
135:
136: public void writeNamespace(String prefix, String namespace)
137: throws XMLStreamException {
138: Namespace decNS = currentNode.getNamespace(prefix);
139:
140: if (decNS == null || !decNS.getURI().equals(namespace)) {
141: currentNode.addNamespaceDeclaration(Namespace.getNamespace(
142: prefix, namespace));
143: }
144: }
145:
146: public void writeDefaultNamespace(String namespace)
147: throws XMLStreamException {
148: currentNode.addNamespaceDeclaration(Namespace.getNamespace("",
149: namespace));
150: }
151:
152: public void writeComment(String value) throws XMLStreamException {
153: currentNode.addContent(new Comment(value));
154: }
155:
156: public void writeProcessingInstruction(String arg0)
157: throws XMLStreamException {
158: }
159:
160: public void writeProcessingInstruction(String arg0, String arg1)
161: throws XMLStreamException {
162: }
163:
164: public void writeCData(String data) throws XMLStreamException {
165: currentNode.addContent(new CDATA(data));
166: }
167:
168: public void writeDTD(String arg0) throws XMLStreamException {
169:
170: }
171:
172: public void writeEntityRef(String ref) throws XMLStreamException {
173: currentNode.addContent(new EntityRef(ref));
174: }
175:
176: public void writeStartDocument() throws XMLStreamException {
177: document = new Document(new Element("root"));
178: }
179:
180: public void writeStartDocument(String version)
181: throws XMLStreamException {
182: writeStartDocument();
183:
184: // TODO: set encoding/version
185: }
186:
187: public void writeStartDocument(String encoding, String version)
188: throws XMLStreamException {
189: writeStartDocument();
190:
191: // TODO: set encoding/version
192: }
193:
194: public void writeCharacters(String text) throws XMLStreamException {
195: currentNode.addContent(text);
196: }
197:
198: public void writeCharacters(char[] text, int start, int len)
199: throws XMLStreamException {
200: // TODO Auto-generated method stub
201: currentNode.addContent(new String(text, start, len));
202: }
203:
204: public String getPrefix(String uri) throws XMLStreamException {
205: return NamespaceHelper.getPrefix(currentNode, uri);
206: }
207:
208: public void setPrefix(String arg0, String arg1)
209: throws XMLStreamException {
210: }
211:
212: public void setDefaultNamespace(String arg0)
213: throws XMLStreamException {
214: }
215:
216: public void setNamespaceContext(NamespaceContext ctx)
217: throws XMLStreamException {
218: this .context = ctx;
219: }
220:
221: public NamespaceContext getNamespaceContext() {
222: return context;
223: }
224:
225: public Object getProperty(String prop)
226: throws IllegalArgumentException {
227: return properties.get(prop);
228: }
229:
230: public Document getDocument() {
231: return document;
232: }
233: }
|