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:
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.XMLStreamException;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029: import java.io.OutputStream;
030: import java.io.Writer;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033:
034: public abstract class ADBHelperDataSource implements OMDataSource {
035:
036: protected QName parentQName;
037: protected Object bean;
038: protected String helperClassName;
039:
040: /**
041: * Constructor taking in an ADBBean
042: *
043: * @param bean
044: */
045: protected ADBHelperDataSource(Object bean, QName parentQName,
046: String helperClassName) {
047: this .bean = bean;
048: this .parentQName = parentQName;
049: this .helperClassName = helperClassName;
050: }
051:
052: /**
053: * @param output
054: * @param format
055: * @throws javax.xml.stream.XMLStreamException
056: *
057: * @see OMDataSource#serialize(java.io.OutputStream, org.apache.axiom.om.OMOutputFormat)
058: */
059: public void serialize(OutputStream output, OMOutputFormat format)
060: throws XMLStreamException {
061: serialize(StAXUtils.createXMLStreamWriter(output));
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 abstract void serialize(XMLStreamWriter xmlWriter)
083: throws XMLStreamException;
084:
085: /**
086: * @throws XMLStreamException
087: * @see org.apache.axiom.om.OMDataSource#getReader()
088: */
089: public XMLStreamReader getReader() throws XMLStreamException {
090: // since only ADBBeans related to elements can be serialized
091: try {
092: Class helperClass = Class.forName(helperClassName);
093: Method method = helperClass.getMethod("getPullParser",
094: new Class[] { Object.class, QName.class });
095: return (XMLStreamReader) method.invoke(null, new Object[] {
096: bean, parentQName });
097: } catch (ClassNotFoundException e) {
098: throw new XMLStreamException(e);
099: } catch (NoSuchMethodException e) {
100: throw new XMLStreamException(e);
101: } catch (IllegalAccessException e) {
102: throw new XMLStreamException(e);
103: } catch (InvocationTargetException e) {
104: throw new XMLStreamException(e);
105: }
106:
107: }
108:
109: }
|