01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.databinding.source;
19:
20: import java.io.IOException;
21: import java.util.Collection;
22: import java.util.logging.Logger;
23:
24: import javax.activation.DataSource;
25: import javax.xml.stream.XMLStreamException;
26: import javax.xml.stream.XMLStreamReader;
27: import javax.xml.stream.XMLStreamWriter;
28: import javax.xml.transform.Source;
29: import javax.xml.transform.dom.DOMSource;
30: import javax.xml.validation.Schema;
31:
32: import org.apache.cxf.common.i18n.Message;
33: import org.apache.cxf.common.logging.LogUtils;
34: import org.apache.cxf.databinding.DataWriter;
35: import org.apache.cxf.interceptor.Fault;
36: import org.apache.cxf.message.Attachment;
37: import org.apache.cxf.service.model.MessagePartInfo;
38: import org.apache.cxf.staxutils.StaxUtils;
39:
40: public class XMLStreamDataWriter implements DataWriter<XMLStreamWriter> {
41: private static final Logger LOG = LogUtils
42: .getL7dLogger(XMLStreamDataWriter.class);
43:
44: public void write(Object obj, MessagePartInfo part,
45: XMLStreamWriter output) {
46: write(obj, output);
47: }
48:
49: public void write(Object obj, XMLStreamWriter writer) {
50: try {
51: XMLStreamReader reader = null;
52: if (obj instanceof DataSource) {
53: DataSource ds = (DataSource) obj;
54: reader = StaxUtils.createXMLStreamReader(ds
55: .getInputStream());
56: } else {
57: Source s = (Source) obj;
58: if (s instanceof DOMSource
59: && ((DOMSource) s).getNode() == null) {
60: return;
61: }
62:
63: reader = StaxUtils.createXMLStreamReader(s);
64: }
65: StaxUtils.copy(reader, writer);
66: reader.close();
67: } catch (XMLStreamException e) {
68: throw new Fault(new Message("COULD_NOT_READ_XML_STREAM",
69: LOG), e);
70: } catch (IOException e) {
71: throw new Fault(new Message("COULD_NOT_READ_XML_STREAM",
72: LOG), e);
73: }
74: }
75:
76: public void setSchema(Schema s) {
77: }
78:
79: public void setAttachments(Collection<Attachment> attachments) {
80:
81: }
82: }
|