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.xml.stax;
019:
020: import java.io.OutputStream;
021:
022: import javax.xml.namespace.QName;
023: import javax.xml.stream.XMLOutputFactory;
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.XMLStreamWriter;
026:
027: import org.apache.cxf.aegis.DatabindingException;
028: import org.apache.cxf.aegis.util.NamespaceHelper;
029: import org.apache.cxf.aegis.xml.AbstractMessageWriter;
030: import org.apache.cxf.aegis.xml.MessageWriter;
031:
032: /**
033: * LiteralWriter
034: *
035: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
036: */
037: public class ElementWriter extends AbstractMessageWriter implements
038: MessageWriter {
039: private XMLStreamWriter writer;
040:
041: private String namespace;
042:
043: private String name;
044:
045: private String prefix;
046:
047: /**
048: * Create a LiteralWriter but without writing an element name.
049: *
050: * @param writer
051: */
052: public ElementWriter(XMLStreamWriter writer) {
053: this .writer = writer;
054: }
055:
056: public ElementWriter(XMLStreamWriter writer, String name,
057: String namespace) {
058: this (writer, name, namespace, null);
059: }
060:
061: public ElementWriter(XMLStreamWriter streamWriter, QName name) {
062: this (streamWriter, name.getLocalPart(), name.getNamespaceURI());
063: }
064:
065: public ElementWriter(XMLStreamWriter writer, String name,
066: String namespace, String prefix) {
067: this .writer = writer;
068: this .namespace = namespace;
069: this .name = name;
070: this .prefix = prefix;
071:
072: try {
073: writeStartElement();
074: } catch (XMLStreamException e) {
075: throw new DatabindingException("Error writing document.", e);
076: }
077: }
078:
079: /**
080: * @param os
081: * @throws XMLStreamException
082: */
083: public ElementWriter(OutputStream os, String name, String namespace)
084: throws XMLStreamException {
085: XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
086: this .writer = ofactory.createXMLStreamWriter(os);
087:
088: this .namespace = namespace;
089: this .name = name;
090:
091: try {
092: writeStartElement();
093: } catch (XMLStreamException e) {
094: throw new DatabindingException("Error writing document.", e);
095: }
096: }
097:
098: private void writeStartElement() throws XMLStreamException {
099: if (namespace != null) {
100: boolean declare = false;
101:
102: String decPrefix = writer.getNamespaceContext().getPrefix(
103: namespace);
104:
105: // If the user didn't specify a prefix, create one
106: if (prefix == null && decPrefix == null) {
107: declare = true;
108: prefix = NamespaceHelper.getUniquePrefix(writer);
109: } else if (prefix == null) {
110: prefix = decPrefix;
111: } else if (!prefix.equals(decPrefix)) {
112: declare = true;
113: }
114:
115: writer.writeStartElement(prefix, name, namespace);
116:
117: if (declare) {
118: writer.setPrefix(prefix, namespace);
119: writer.writeNamespace(prefix, namespace);
120: }
121: } else {
122: writer.writeStartElement(name);
123: }
124: }
125:
126: /**
127: * @see org.apache.cxf.aegis.xml.MessageWriter#writeValue(java.lang.Object)
128: */
129: public void writeValue(Object value) {
130: try {
131: if (value != null) {
132: writer.writeCharacters(value.toString());
133: }
134: } catch (XMLStreamException e) {
135: throw new DatabindingException("Error writing document.", e);
136: }
137: }
138:
139: /**
140: * @see org.apache.cxf.aegis.xml.MessageWriter#getWriter(java.lang.String)
141: */
142: public MessageWriter getElementWriter(String nm) {
143: return new ElementWriter(writer, nm, namespace);
144: }
145:
146: public MessageWriter getElementWriter(String nm, String ns) {
147: return new ElementWriter(writer, nm, ns);
148: }
149:
150: public MessageWriter getElementWriter(QName qname) {
151: return new ElementWriter(writer, qname.getLocalPart(), qname
152: .getNamespaceURI(), qname.getPrefix());
153: }
154:
155: public String getNamespace() {
156: return namespace;
157: }
158:
159: public void close() {
160: try {
161: writer.writeEndElement();
162: } catch (XMLStreamException e) {
163: throw new DatabindingException("Error writing document.", e);
164: }
165: }
166:
167: public void flush() throws XMLStreamException {
168: writer.flush();
169: }
170:
171: public XMLStreamWriter getXMLStreamWriter() {
172: return writer;
173: }
174:
175: public MessageWriter getAttributeWriter(String nm) {
176: return new AttributeWriter(writer, nm, namespace);
177: }
178:
179: public MessageWriter getAttributeWriter(String nm, String ns) {
180: return new AttributeWriter(writer, nm, ns);
181: }
182:
183: public MessageWriter getAttributeWriter(QName qname) {
184: return new AttributeWriter(writer, qname.getLocalPart(), qname
185: .getNamespaceURI());
186: }
187:
188: public String getPrefixForNamespace(String ns) {
189: try {
190: String pfx = writer.getPrefix(ns);
191:
192: if (pfx == null) {
193: pfx = NamespaceHelper.getUniquePrefix(writer);
194:
195: writer.setPrefix(pfx, ns);
196: writer.writeNamespace(pfx, ns);
197: }
198:
199: return prefix;
200: } catch (XMLStreamException e) {
201: throw new DatabindingException("Error writing document.", e);
202: }
203: }
204:
205: public String getPrefixForNamespace(String ns, String hint) {
206: try {
207: String pfx = writer.getPrefix(ns);
208:
209: if (pfx == null) {
210: String ns2 = writer.getNamespaceContext()
211: .getNamespaceURI(hint);
212: if (ns2 == null) {
213: pfx = hint;
214: } else if (ns2.equals(ns)) {
215: return pfx;
216: } else {
217: pfx = NamespaceHelper.getUniquePrefix(writer);
218: }
219:
220: writer.setPrefix(pfx, ns);
221: writer.writeNamespace(pfx, ns);
222: }
223:
224: return pfx;
225: } catch (XMLStreamException e) {
226: throw new DatabindingException("Error writing document.", e);
227: }
228: }
229: }
|