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: */
019: package org.apache.axis2.databinding;
020:
021: import org.apache.axiom.om.OMDataSource;
022: import org.apache.axiom.om.OMOutputFormat;
023: import org.apache.axiom.om.util.StAXUtils;
024: import org.apache.axis2.databinding.utils.writer.OMElementStreamWriter;
025: import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter;
026: import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLSerializer;
027: import org.apache.axis2.databinding.utils.writer.MTOMAwareOMBuilder;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.stream.XMLStreamException;
031: import javax.xml.stream.XMLStreamReader;
032: import javax.xml.stream.XMLStreamWriter;
033: import java.io.OutputStream;
034: import java.io.Writer;
035:
036: public abstract class ADBDataSource implements OMDataSource {
037: protected QName parentQName;
038: private ADBBean bean;
039:
040: /**
041: * Constructor taking in an ADBBean
042: *
043: * @param bean
044: */
045: protected ADBDataSource(ADBBean bean, QName parentQName) {
046: this .bean = bean;
047: this .parentQName = parentQName;
048: }
049:
050: /**
051: * @param output
052: * @param format
053: * @throws XMLStreamException
054: * @see OMDataSource#serialize(java.io.OutputStream, org.apache.axiom.om.OMOutputFormat)
055: */
056: public void serialize(OutputStream output, OMOutputFormat format)
057: throws XMLStreamException {
058: XMLStreamWriter xmlStreamWriter = StAXUtils
059: .createXMLStreamWriter(output);
060: serialize(xmlStreamWriter);
061: xmlStreamWriter.flush();
062: }
063:
064: /**
065: * @param writer
066: * @param format
067: * @throws XMLStreamException
068: * @see OMDataSource#serialize(java.io.Writer, org.apache.axiom.om.OMOutputFormat)
069: */
070: public void serialize(Writer writer, OMOutputFormat format)
071: throws XMLStreamException {
072: serialize(StAXUtils.createXMLStreamWriter(writer));
073: }
074:
075: /**
076: * This needs to be generated inside the ADB bean
077: *
078: * @param xmlWriter
079: * @throws XMLStreamException
080: * @see OMDataSource#serialize(javax.xml.stream.XMLStreamWriter)
081: */
082: public void serialize(XMLStreamWriter xmlWriter)
083: throws XMLStreamException {
084: MTOMAwareXMLStreamWriter mtomAwareXMLStreamWriter = new MTOMAwareXMLSerializer(
085: xmlWriter);
086: serialize(mtomAwareXMLStreamWriter);
087: }
088:
089: public abstract void serialize(MTOMAwareXMLStreamWriter xmlWriter)
090: throws XMLStreamException;
091:
092: /**
093: * @throws XMLStreamException
094: * @see org.apache.axiom.om.OMDataSource#getReader()
095: */
096: public XMLStreamReader getReader() throws XMLStreamException {
097: // since only ADBBeans related to elements can be serialized
098: // we are safe in passing null here.
099: MTOMAwareOMBuilder mtomAwareOMBuilder = new MTOMAwareOMBuilder();
100: serialize(mtomAwareOMBuilder);
101: return mtomAwareOMBuilder.getOMElement().getXMLStreamReader();
102: }
103:
104: }
|