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.transform.TransformerException;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.sax.SAXSource;
028: import javax.xml.transform.stream.StreamSource;
029: import javax.xml.validation.Schema;
030:
031: import org.w3c.dom.Node;
032:
033: import org.xml.sax.InputSource;
034:
035: import org.apache.cxf.common.i18n.Message;
036: import org.apache.cxf.common.logging.LogUtils;
037: import org.apache.cxf.databinding.DataReader;
038: import org.apache.cxf.helpers.DOMUtils;
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:
044: public class NodeDataReader implements DataReader<Node> {
045: private static final Logger LOG = LogUtils
046: .getL7dLogger(NodeDataReader.class);
047:
048: public Object read(MessagePartInfo part, Node input) {
049: return read(input);
050: }
051:
052: public Object read(QName name, Node input, Class type) {
053: if (SAXSource.class.isAssignableFrom(type)) {
054: try {
055: CachedOutputStream out = new CachedOutputStream();
056: DOMUtils.writeXml(input, out);
057:
058: return new SAXSource(new InputSource(out
059: .getInputStream()));
060: } catch (IOException e) {
061: throw new Fault(new Message(
062: "COULD_NOT_READ_XML_STREAM", LOG), e);
063: } catch (TransformerException e) {
064: throw new Fault(new Message(
065: "COULD_NOT_READ_XML_STREAM", LOG), e);
066: }
067: } else if (StreamSource.class.isAssignableFrom(type)) {
068: try {
069: CachedOutputStream out = new CachedOutputStream();
070: DOMUtils.writeXml(input, out);
071: out.close();
072:
073: return new StreamSource(out.getInputStream());
074: } catch (IOException e) {
075: throw new Fault(new Message(
076: "COULD_NOT_READ_XML_STREAM", LOG), e);
077: } catch (TransformerException e) {
078: throw new Fault(new Message(
079: "COULD_NOT_READ_XML_STREAM", LOG), e);
080: }
081: }
082: return read(input);
083: }
084:
085: public Object read(Node n) {
086: return new DOMSource(n);
087: }
088:
089: public void setSchema(Schema s) {
090: // TODO Auto-generated method stub
091:
092: }
093:
094: public void setAttachments(Collection<Attachment> attachments) {
095: // TODO Auto-generated method stub
096:
097: }
098:
099: public void setProperty(String prop, Object value) {
100: // TODO Auto-generated method stub
101:
102: }
103:
104: }
|