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