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.databinding.source;
019:
020: import java.io.IOException;
021: import java.util.Collection;
022: import java.util.logging.Logger;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.stream.XMLStreamException;
026: import javax.xml.stream.XMLStreamReader;
027: import javax.xml.stream.XMLStreamWriter;
028: import javax.xml.transform.dom.DOMSource;
029: import javax.xml.transform.sax.SAXSource;
030: import javax.xml.transform.stream.StreamSource;
031: import javax.xml.validation.Schema;
032:
033: import org.w3c.dom.Document;
034: import org.xml.sax.InputSource;
035:
036: import org.apache.cxf.common.i18n.Message;
037: import org.apache.cxf.common.logging.LogUtils;
038: import org.apache.cxf.databinding.DataReader;
039: import org.apache.cxf.interceptor.Fault;
040: import org.apache.cxf.io.CachedOutputStream;
041: import org.apache.cxf.message.Attachment;
042: import org.apache.cxf.service.model.MessagePartInfo;
043: import org.apache.cxf.staxutils.StaxUtils;
044:
045: public class XMLStreamDataReader implements DataReader<XMLStreamReader> {
046: private static final Logger LOG = LogUtils
047: .getL7dLogger(XMLStreamDataReader.class);
048:
049: public Object read(MessagePartInfo part, XMLStreamReader input) {
050: return read(null, input, part.getTypeClass());
051: }
052:
053: public Object read(QName name, XMLStreamReader input, Class type) {
054: if (SAXSource.class.isAssignableFrom(type)) {
055: try {
056: CachedOutputStream out = new CachedOutputStream();
057: XMLStreamWriter xsw = StaxUtils
058: .createXMLStreamWriter(out);
059: StaxUtils.copy(input, xsw);
060: xsw.close();
061: out.close();
062:
063: return new SAXSource(new InputSource(out
064: .getInputStream()));
065: } catch (IOException e) {
066: throw new Fault(new Message(
067: "COULD_NOT_READ_XML_STREAM", LOG), e);
068: } catch (XMLStreamException e) {
069: throw new Fault(new Message(
070: "COULD_NOT_READ_XML_STREAM", LOG), e);
071: }
072: } else if (StreamSource.class.isAssignableFrom(type)) {
073: try {
074: CachedOutputStream out = new CachedOutputStream();
075: XMLStreamWriter xsw = StaxUtils
076: .createXMLStreamWriter(out);
077: StaxUtils.copy(input, xsw);
078: xsw.close();
079: out.close();
080:
081: return new StreamSource(out.getInputStream());
082: } catch (IOException e) {
083: throw new Fault(new Message(
084: "COULD_NOT_READ_XML_STREAM", LOG), e);
085: } catch (XMLStreamException e) {
086: throw new Fault(new Message(
087: "COULD_NOT_READ_XML_STREAM", LOG), e);
088: }
089: }
090:
091: return read(input);
092: }
093:
094: public Object read(XMLStreamReader reader) {
095: // Use a DOMSource for now, we should really use a StaxSource/SAXSource though for
096: // performance reasons
097: try {
098: Document document = StaxUtils.read(reader);
099: return new DOMSource(document);
100: } catch (XMLStreamException e) {
101: throw new Fault(new Message("COULD_NOT_READ_XML_STREAM",
102: LOG), e);
103: }
104: }
105:
106: public void setSchema(Schema s) {
107: }
108:
109: public void setAttachments(Collection<Attachment> attachments) {
110: }
111:
112: public void setProperty(String prop, Object value) {
113: }
114: }
|