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: */package org.apache.cxf.aegis.type.xml;
019:
020: import javax.xml.stream.FactoryConfigurationError;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamReader;
023: import javax.xml.stream.XMLStreamWriter;
024: import javax.xml.transform.Source;
025: import javax.xml.transform.dom.DOMSource;
026: import javax.xml.transform.sax.SAXSource;
027: import javax.xml.transform.stream.StreamSource;
028:
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031:
032: import org.xml.sax.SAXException;
033: import org.xml.sax.XMLReader;
034: import org.xml.sax.helpers.XMLReaderFactory;
035:
036: import javanet.staxutils.ContentHandlerToXMLStreamWriter;
037:
038: import org.apache.cxf.aegis.Context;
039: import org.apache.cxf.aegis.DatabindingException;
040: import org.apache.cxf.aegis.type.Type;
041: import org.apache.cxf.aegis.xml.MessageReader;
042: import org.apache.cxf.aegis.xml.MessageWriter;
043: import org.apache.cxf.aegis.xml.stax.ElementWriter;
044: import org.apache.cxf.staxutils.StaxUtils;
045:
046: /**
047: * Reads and writes <code>javax.xml.transform.Source</code> types.
048: * <p>
049: * The XML stream is converted DOMSource and sent off.
050: *
051: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
052: * @see javanet.staxutils.StAXSource
053: * @see javax.xml.stream.XMLInputFactory
054: * @see org.apache.cxf.aegis.util.STAXUtils
055: */
056: public class SourceType extends Type {
057: public SourceType() {
058: setTypeClass(Source.class);
059: setWriteOuter(false);
060: }
061:
062: @Override
063: public Object readObject(MessageReader mreader, Context context)
064: throws DatabindingException {
065: DocumentType dt = (DocumentType) getTypeMapping().getType(
066: Document.class);
067:
068: return new DOMSource((Document) dt.readObject(mreader, context));
069: }
070:
071: @Override
072: public void writeObject(Object object, MessageWriter writer,
073: Context context) throws DatabindingException {
074: try {
075: if (object == null) {
076: return;
077: }
078:
079: write((Source) object, ((ElementWriter) writer)
080: .getXMLStreamWriter());
081: } catch (XMLStreamException e) {
082: throw new DatabindingException("Could not write xml.", e);
083: }
084: }
085:
086: protected void write(Source object, XMLStreamWriter writer)
087: throws FactoryConfigurationError, XMLStreamException,
088: DatabindingException {
089: if (object == null) {
090: return;
091: }
092:
093: if (object instanceof DOMSource) {
094: DOMSource ds = (DOMSource) object;
095:
096: Element element = null;
097: if (ds.getNode() instanceof Element) {
098: element = (Element) ds.getNode();
099: } else if (ds.getNode() instanceof Document) {
100: element = ((Document) ds.getNode())
101: .getDocumentElement();
102: } else {
103: throw new DatabindingException("Node type "
104: + ds.getNode().getClass()
105: + " was not understood.");
106: }
107:
108: StaxUtils.writeElement(element, writer, false);
109: } else if (object instanceof SAXSource) {
110: SAXSource source = (SAXSource) object;
111:
112: try {
113: XMLReader xmlReader = source.getXMLReader();
114: if (xmlReader == null) {
115: xmlReader = createXMLReader();
116: }
117:
118: xmlReader
119: .setContentHandler(new FilteringContentHandlerToXMLStreamWriter(
120: writer));
121:
122: xmlReader.parse(source.getInputSource());
123: } catch (Exception e) {
124: throw new DatabindingException("Could not send xml.", e);
125: }
126: } else if (object instanceof StreamSource) {
127: StreamSource ss = (StreamSource) object;
128: XMLStreamReader reader = StaxUtils.createXMLStreamReader(ss
129: .getInputStream(), null);
130: StaxUtils.copy(reader, writer);
131: }
132: }
133:
134: protected XMLReader createXMLReader() throws SAXException {
135: // In JDK 1.4, the xml reader factory does not look for META-INF
136: // services
137: // If the org.xml.sax.driver system property is not defined, and
138: // exception will be thrown.
139: // In these cases, default to xerces parser
140: try {
141: return XMLReaderFactory.createXMLReader();
142: } catch (Exception e) {
143: return XMLReaderFactory
144: .createXMLReader("org.apache.xerces.parsers.SAXParser");
145: }
146: }
147:
148: class FilteringContentHandlerToXMLStreamWriter extends
149: ContentHandlerToXMLStreamWriter {
150: public FilteringContentHandlerToXMLStreamWriter(
151: XMLStreamWriter xmlStreamWriter) {
152: super (xmlStreamWriter);
153: }
154:
155: @Override
156: public void startDocument() throws SAXException {
157: }
158:
159: @Override
160: public void endDocument() throws SAXException {
161: }
162: }
163: }
|