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.stax;
019:
020: import java.util.Collection;
021: import java.util.logging.Logger;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.XMLStreamReader;
026: import javax.xml.stream.XMLStreamWriter;
027: import javax.xml.validation.Schema;
028:
029: import org.w3c.dom.Node;
030:
031: import org.apache.cxf.common.i18n.Message;
032: import org.apache.cxf.common.logging.LogUtils;
033: import org.apache.cxf.databinding.DataBinding;
034: import org.apache.cxf.databinding.DataReader;
035: import org.apache.cxf.databinding.DataWriter;
036: import org.apache.cxf.interceptor.Fault;
037: import org.apache.cxf.message.Attachment;
038: import org.apache.cxf.service.Service;
039: import org.apache.cxf.service.model.MessagePartInfo;
040: import org.apache.cxf.staxutils.StaxUtils;
041:
042: /**
043: * A simple databinding implementation which reads and writes Source objects.
044: * This will not work with the standard databinding interceptors.
045: */
046: public class StaxDataBinding implements DataBinding {
047:
048: private XMLStreamDataReader xsrReader;
049: private XMLStreamDataWriter xswWriter;
050:
051: public StaxDataBinding() {
052: super ();
053: this .xsrReader = new XMLStreamDataReader();
054: this .xswWriter = new XMLStreamDataWriter();
055: }
056:
057: public void initialize(Service service) {
058: // do nothing
059: }
060:
061: @SuppressWarnings("unchecked")
062: public <T> DataReader<T> createReader(Class<T> cls) {
063: if (cls == XMLStreamReader.class) {
064: return (DataReader<T>) xsrReader;
065: } else {
066: throw new UnsupportedOperationException("The type "
067: + cls.getName() + " is not supported.");
068: }
069: }
070:
071: public Class<?>[] getSupportedReaderFormats() {
072: return new Class[] { XMLStreamReader.class };
073: }
074:
075: @SuppressWarnings("unchecked")
076: public <T> DataWriter<T> createWriter(Class<T> cls) {
077: if (cls == XMLStreamWriter.class) {
078: return (DataWriter<T>) xswWriter;
079: } else {
080: throw new UnsupportedOperationException("The type "
081: + cls.getName() + " is not supported.");
082: }
083: }
084:
085: public Class<?>[] getSupportedWriterFormats() {
086: return new Class[] { XMLStreamWriter.class, Node.class };
087: }
088:
089: public static class XMLStreamDataReader implements
090: DataReader<XMLStreamReader> {
091:
092: public Object read(MessagePartInfo part, XMLStreamReader input) {
093: return read(null, input, part.getTypeClass());
094: }
095:
096: public Object read(QName name, XMLStreamReader input, Class type) {
097: return input;
098: }
099:
100: public Object read(XMLStreamReader reader) {
101: return reader;
102: }
103:
104: public void setSchema(Schema s) {
105: }
106:
107: public void setAttachments(Collection<Attachment> attachments) {
108: }
109:
110: public void setProperty(String prop, Object value) {
111: }
112: }
113:
114: public static class XMLStreamDataWriter implements
115: DataWriter<XMLStreamWriter> {
116: private static final Logger LOG = LogUtils
117: .getL7dLogger(XMLStreamDataWriter.class);
118:
119: public void write(Object obj, MessagePartInfo part,
120: XMLStreamWriter output) {
121: write(obj, output);
122: }
123:
124: public void write(Object obj, XMLStreamWriter writer) {
125: try {
126: if (obj instanceof XMLStreamReader) {
127: StaxUtils.copy((XMLStreamReader) obj, writer);
128: } else if (obj instanceof XMLStreamWriterCallback) {
129: ((XMLStreamWriterCallback) obj).write(writer);
130: } else {
131: throw new UnsupportedOperationException(
132: "Data types of " + obj.getClass()
133: + " are not supported.");
134: }
135: } catch (XMLStreamException e) {
136: throw new Fault(new Message(
137: "COULD_NOT_READ_XML_STREAM", LOG), e);
138: }
139: }
140:
141: public void setSchema(Schema s) {
142: }
143:
144: public void setAttachments(Collection<Attachment> attachments) {
145:
146: }
147: }
148: }
|