001: /*
002: * Copyright 2004,2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.axis2.databinding.utils.writer;
017:
018: import junit.framework.TestCase;
019:
020: import javax.xml.stream.XMLStreamException;
021: import javax.xml.stream.XMLStreamWriter;
022: import javax.xml.stream.XMLStreamReader;
023: import javax.xml.namespace.QName;
024:
025: import org.apache.axiom.om.OMElement;
026: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
027: import org.apache.axis2.databinding.utils.ConverterUtil;
028: import org.apache.axis2.databinding.utils.BeanUtil;
029:
030: public class OMElementStreamWriterTest extends TestCase {
031:
032: private int prefixNum;
033:
034: public void testGetOMElement() {
035:
036: OMElementStreamWriter omElementStreamWriter = new OMElementStreamWriter();
037:
038: try {
039:
040: writeStartElement(null, "test1", omElementStreamWriter);
041: writeStartElement("http://apach.temp.uri", "test2",
042: omElementStreamWriter);
043: writeStartElement("http://apach.temp.uri1", "test3",
044: omElementStreamWriter);
045: omElementStreamWriter.writeCharacters("test string3");
046: writeAttribute("http://apach.temp.uri", "attribute1",
047: "attrubteValue1", omElementStreamWriter);
048: writeQNames(
049: new QName[] {
050: new QName("http://axis2.apach.org1",
051: "testQName"),
052: new QName("http://axis2.apach.org2",
053: "testQName2") },
054: omElementStreamWriter);
055: omElementStreamWriter.writeEndElement();
056: writeStartElement("http://apach.temp.uri1", "test4",
057: omElementStreamWriter);
058: writeAttribute(null, "attribute1", "attrubteValue1",
059: omElementStreamWriter);
060: writeAttribute("ns1", "http://apach.temp.uri",
061: "attribute1", "attrubteValue1",
062: omElementStreamWriter);
063: omElementStreamWriter.writeCharacters("test string4");
064: omElementStreamWriter.writeEndElement();
065: writeStartElement("http://temp.new.org",
066: "testattributeElement", omElementStreamWriter);
067: writeQNameAttribute(null, "testQname", new QName(
068: "http://temp.new.org", "testQName"),
069: omElementStreamWriter);
070: writeQName(
071: new QName("http://axis2.apach.org", "testQName"),
072: omElementStreamWriter);
073: omElementStreamWriter.writeEndElement();
074: omElementStreamWriter.writeEndElement();
075: omElementStreamWriter.writeEndElement();
076:
077: OMElement omElement = omElementStreamWriter.getOMElement();
078: System.out.println("OM String ==> " + omElement.toString());
079:
080: XMLStreamReader xmlReader = omElement.getXMLStreamReader();
081: StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(xmlReader);
082: OMElement result = stAXOMBuilder.getDocumentElement();
083:
084: System.out.println("OM result ==> " + result.toString());
085: } catch (XMLStreamException e) {
086: fail();
087: }
088:
089: }
090:
091: private String generatePrefix(String string) {
092: return "ns" + ++prefixNum;
093: }
094:
095: private void writeStartElement(String namespace, String localName,
096: XMLStreamWriter xmlWriter) throws XMLStreamException {
097:
098: if ((namespace != null) && !namespace.equals("")) {
099: java.lang.String prefix = xmlWriter.getPrefix(namespace);
100:
101: if (prefix == null) {
102: prefix = "ns" + ++prefixNum;
103: xmlWriter.writeStartElement(prefix, localName,
104: namespace);
105: xmlWriter.writeNamespace(prefix, namespace);
106: xmlWriter.setPrefix(prefix, namespace);
107: } else {
108: xmlWriter.writeStartElement(namespace, localName);
109: }
110: } else {
111: xmlWriter.writeStartElement(localName);
112: }
113: }
114:
115: private void writeAttribute(String prefix, String namespace,
116: String attName, String attValue, XMLStreamWriter xmlWriter)
117: throws XMLStreamException {
118: if (xmlWriter.getPrefix(namespace) == null) {
119: xmlWriter.writeNamespace(prefix, namespace);
120: xmlWriter.setPrefix(prefix, namespace);
121:
122: }
123: xmlWriter.writeAttribute(namespace, attName, attValue);
124: }
125:
126: private void writeAttribute(String namespace, String attName,
127: String attValue, XMLStreamWriter xmlWriter)
128: throws XMLStreamException {
129: if ((namespace == null) || namespace.equals("")) {
130: xmlWriter.writeAttribute(attName, attValue);
131: } else {
132: registerPrefix(xmlWriter, namespace);
133: xmlWriter.writeAttribute(namespace, attName, attValue);
134: }
135: }
136:
137: private void writeQNameAttribute(String namespace, String attName,
138: QName qname, XMLStreamWriter xmlWriter)
139: throws javax.xml.stream.XMLStreamException {
140:
141: String attributeNamespace = qname.getNamespaceURI();
142: String attributePrefix = xmlWriter
143: .getPrefix(attributeNamespace);
144: if (attributePrefix == null) {
145: attributePrefix = registerPrefix(xmlWriter,
146: attributeNamespace);
147: }
148: java.lang.String attributeValue;
149: if (attributePrefix.trim().length() > 0) {
150: attributeValue = attributePrefix + ":"
151: + qname.getLocalPart();
152: } else {
153: attributeValue = qname.getLocalPart();
154: }
155:
156: if ((namespace != null) && namespace.equals("")) {
157: xmlWriter.writeAttribute(attName, attributeValue);
158: } else {
159: if (namespace != null) {
160: registerPrefix(xmlWriter, namespace);
161: }
162: xmlWriter
163: .writeAttribute(namespace, attName, attributeValue);
164: }
165: }
166:
167: private void writeQName(QName qname, XMLStreamWriter xmlWriter)
168: throws javax.xml.stream.XMLStreamException {
169: String namespaceURI = qname.getNamespaceURI();
170: if (namespaceURI != null) {
171: String prefix = xmlWriter.getPrefix(namespaceURI);
172: if (prefix == null) {
173: prefix = generatePrefix(namespaceURI);
174: xmlWriter.writeNamespace(prefix, namespaceURI);
175: xmlWriter.setPrefix(prefix, namespaceURI);
176: }
177:
178: if (prefix.trim().length() > 0) {
179: xmlWriter.writeCharacters(prefix + ":"
180: + ConverterUtil.convertToString(qname));
181: } else {
182: // i.e this is the default namespace
183: xmlWriter.writeCharacters(ConverterUtil
184: .convertToString(qname));
185: }
186:
187: } else {
188: xmlWriter.writeCharacters(ConverterUtil
189: .convertToString(qname));
190: }
191: }
192:
193: private void writeQNames(QName[] qnames, XMLStreamWriter xmlWriter)
194: throws XMLStreamException {
195:
196: if (qnames != null) {
197: // we have to store this data until last moment since it is not possible to write any
198: // namespace data after writing the charactor data
199: StringBuffer stringToWrite = new StringBuffer();
200: String namespaceURI = null;
201: String prefix = null;
202:
203: for (int i = 0; i < qnames.length; i++) {
204: if (i > 0) {
205: stringToWrite.append(" ");
206: }
207: namespaceURI = qnames[i].getNamespaceURI();
208: if (namespaceURI != null) {
209: prefix = xmlWriter.getPrefix(namespaceURI);
210: if ((prefix == null) || (prefix.length() == 0)) {
211: prefix = generatePrefix(namespaceURI);
212: xmlWriter.writeNamespace(prefix, namespaceURI);
213: xmlWriter.setPrefix(prefix, namespaceURI);
214: }
215:
216: if (prefix.trim().length() > 0) {
217: stringToWrite
218: .append(prefix)
219: .append(":")
220: .append(
221: ConverterUtil
222: .convertToString(qnames[i]));
223: } else {
224: stringToWrite.append(ConverterUtil
225: .convertToString(qnames[i]));
226: }
227: } else {
228: stringToWrite.append(ConverterUtil
229: .convertToString(qnames[i]));
230: }
231: }
232: xmlWriter.writeCharacters(stringToWrite.toString());
233: }
234:
235: }
236:
237: private String registerPrefix(XMLStreamWriter xmlWriter,
238: String namespace) throws XMLStreamException {
239: String prefix = xmlWriter.getPrefix(namespace);
240:
241: if (prefix == null) {
242: prefix = generatePrefix(namespace);
243: while (xmlWriter.getNamespaceContext().getNamespaceURI(
244: prefix) != null) {
245: prefix = BeanUtil.getUniquePrefix();
246: }
247: xmlWriter.writeNamespace(prefix, namespace);
248: xmlWriter.setPrefix(prefix, namespace);
249: }
250: return prefix;
251: }
252:
253: }
|