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 org.apache.axiom.om.*;
019:
020: import javax.xml.stream.XMLStreamWriter;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.namespace.NamespaceContext;
023: import javax.activation.DataHandler;
024: import java.util.Stack;
025: import java.util.Map;
026: import java.util.HashMap;
027:
028: public class OMElementStreamWriter implements XMLStreamWriter {
029:
030: // this is the om Element we are going to create
031: private OMElement rootElement;
032: private OMFactory omFactory;
033:
034: private OMElement currentOMElement;
035:
036: private Stack omElementStack;
037:
038: // this map contains the namespace key and OMNamespace
039: private Map namespaceOMNamesapceMap;
040:
041: private int prefixNum;
042:
043: private OMStreamNamespaceContext omStreamNamespaceContext;
044:
045: public OMElementStreamWriter() {
046: omFactory = OMAbstractFactory.getOMFactory();
047: omElementStack = new Stack();
048: currentOMElement = null;
049: omStreamNamespaceContext = new OMStreamNamespaceContext();
050: namespaceOMNamesapceMap = new HashMap();
051: prefixNum = 0;
052:
053: }
054:
055: // serailizer must have finish serializing when we call this method.
056: public OMElement getOMElement() throws XMLStreamException {
057: if (!omElementStack.isEmpty()) {
058: throw new XMLStreamException("This is an invalid Xml ");
059: }
060: return rootElement;
061: }
062:
063: private OMNamespace getOMNamespace(String namespace, String prefix)
064: throws XMLStreamException {
065: OMNamespace omNamespace = null;
066: if (namespace != null) {
067: if (namespaceOMNamesapceMap.containsKey(namespace)) {
068: omNamespace = (OMNamespace) namespaceOMNamesapceMap
069: .get(namespace);
070: } else {
071: if (prefix == null) {
072: prefix = "ns" + ++prefixNum;
073: } else if (this .omStreamNamespaceContext
074: .getNamespaceURI(prefix) != null) {
075: throw new XMLStreamException("the prefix ==> "
076: + prefix
077: + " Already exists for namespace ==> "
078: + namespace);
079: }
080: omNamespace = omFactory.createOMNamespace(namespace,
081: prefix);
082: this .omStreamNamespaceContext.registerNamespace(
083: namespace, prefix);
084: namespaceOMNamesapceMap.put(namespace, omNamespace);
085: }
086: }
087: return omNamespace;
088: }
089:
090: public void writeStartElement(String localName)
091: throws XMLStreamException {
092: writeStartElement(null, localName, null);
093: }
094:
095: public void writeStartElement(String namespace, String localName)
096: throws XMLStreamException {
097: writeStartElement(null, localName, namespace);
098: }
099:
100: public void writeStartElement(String prefix, String localName,
101: String namespace) throws XMLStreamException {
102: OMNamespace omNamespace = getOMNamespace(namespace, prefix);
103: currentOMElement = omFactory.createOMElement(localName,
104: omNamespace);
105: if (!omElementStack.isEmpty()) {
106: // we always keep the parent at the top of the stack
107: OMElement parent = (OMElement) omElementStack.peek();
108: parent.addChild(currentOMElement);
109: } else {
110: // i.e this must be an start root element
111: rootElement = currentOMElement;
112: }
113: // set this as the top element
114: omElementStack.push(currentOMElement);
115: }
116:
117: public void writeEmptyElement(String namespaceURI, String localName)
118: throws XMLStreamException {
119: writeEmptyElement(null, localName, namespaceURI);
120: }
121:
122: public void writeEmptyElement(String prefix, String localName,
123: String namespaceURI) throws XMLStreamException {
124: writeStartElement(prefix, localName, namespaceURI);
125: writeEndElement();
126: }
127:
128: public void writeEmptyElement(String localName)
129: throws XMLStreamException {
130: writeEmptyElement(null, localName, null);
131: }
132:
133: public void writeEndElement() throws XMLStreamException {
134: omElementStack.pop();
135: }
136:
137: public void writeEndDocument() throws XMLStreamException {
138: // nothing to do
139: }
140:
141: public void close() throws XMLStreamException {
142: // nothing to do
143: }
144:
145: public void flush() throws XMLStreamException {
146: // nothing to do
147:
148: }
149:
150: public void setDataHandler(DataHandler dataHandler) {
151: OMText omText = omFactory.createOMText(dataHandler, true);
152: currentOMElement.addChild(omText);
153: }
154:
155: public void writeAttribute(String attributeName,
156: String attributeValue) throws XMLStreamException {
157: writeAttribute(null, null, attributeName, attributeValue);
158: }
159:
160: public void writeAttribute(String prefix, String namespace,
161: String attributeName, String attributeValue)
162: throws XMLStreamException {
163: currentOMElement.addAttribute(attributeName, attributeValue,
164: getOMNamespace(namespace, prefix));
165: }
166:
167: public void writeAttribute(String namespace, String attributeName,
168: String attributeValue) throws XMLStreamException {
169: writeAttribute(null, namespace, attributeName, attributeValue);
170: }
171:
172: public void writeNamespace(String prefix, String namespaceURI)
173: throws XMLStreamException {
174: if (namespaceURI != null) {
175: OMNamespace omNamespace = getOMNamespace(namespaceURI,
176: prefix);
177: currentOMElement.declareNamespace(omNamespace);
178: }
179: }
180:
181: public void writeDefaultNamespace(String namespace)
182: throws XMLStreamException {
183: rootElement.declareDefaultNamespace(namespace);
184: getOMNamespace(namespace, "");
185: }
186:
187: public void writeComment(String string) throws XMLStreamException {
188: omFactory.createOMComment(currentOMElement, string);
189: }
190:
191: public void writeProcessingInstruction(String string)
192: throws XMLStreamException {
193: throw new UnsupportedOperationException(
194: "this method has not yet been implemented");
195: }
196:
197: public void writeProcessingInstruction(String string, String string1)
198: throws XMLStreamException {
199: throw new UnsupportedOperationException(
200: "this method has not yet been implemented");
201: }
202:
203: public void writeCData(String string) throws XMLStreamException {
204: throw new UnsupportedOperationException(
205: "this method has not yet been implemented");
206: }
207:
208: public void writeDTD(String string) throws XMLStreamException {
209: throw new UnsupportedOperationException(
210: "this method has not yet been implemented");
211: }
212:
213: public void writeEntityRef(String string) throws XMLStreamException {
214: throw new UnsupportedOperationException(
215: "this method has not yet been implemented");
216: }
217:
218: public void writeStartDocument() throws XMLStreamException {
219: // nothing to do
220: }
221:
222: public void writeStartDocument(String string)
223: throws XMLStreamException {
224: // nothing to do
225: }
226:
227: public void writeStartDocument(String string, String string1)
228: throws XMLStreamException {
229: // nothing to do
230: }
231:
232: public void writeCharacters(String string)
233: throws XMLStreamException {
234: currentOMElement.setText(string);
235: }
236:
237: public void writeCharacters(char[] chars, int i, int i1)
238: throws XMLStreamException {
239: writeCharacters(new String(chars, i, i1));
240: }
241:
242: public String getPrefix(String namespace) throws XMLStreamException {
243: return this .omStreamNamespaceContext.getPrefix(namespace);
244: }
245:
246: public void setPrefix(String prefix, String uri)
247: throws XMLStreamException {
248: // this method will add the namespace correctly.
249: getOMNamespace(uri, prefix);
250: }
251:
252: public void setDefaultNamespace(String namespace)
253: throws XMLStreamException {
254: rootElement.declareDefaultNamespace(namespace);
255: getOMNamespace(namespace, "");
256: }
257:
258: public void setNamespaceContext(NamespaceContext namespaceContext)
259: throws XMLStreamException {
260: throw new UnsupportedOperationException(
261: "this method has not yet been implemented");
262: }
263:
264: public NamespaceContext getNamespaceContext() {
265: return this .omStreamNamespaceContext;
266: }
267:
268: public Object getProperty(String string)
269: throws IllegalArgumentException {
270: throw new UnsupportedOperationException(
271: "this method has not yet been implemented");
272: }
273: }
|