View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.ws.cxf.databinding.plainxml;
17  
18  import org.apache.cxf.databinding.DataWriter;
19  import org.apache.cxf.message.Attachment;
20  import org.apache.cxf.service.model.MessagePartInfo;
21  import org.apache.cxf.staxutils.StaxUtils;
22  
23  import javax.xml.stream.XMLStreamWriter;
24  import javax.xml.validation.Schema;
25  import java.io.Reader;
26  import java.io.StringReader;
27  import java.util.Collection;
28  
29  /**
30   * A special writer for some transactions (e.g. HL7 v3) whose
31   * expected input data is String representation of an XML element.
32   *
33   * @author Dmytro Rud
34   */
35  public class PlainXmlWriter implements DataWriter<XMLStreamWriter> {
36  
37      @Override
38      public void write(Object obj, MessagePartInfo part, XMLStreamWriter writer) {
39          try {
40              String s = (String) obj;
41              Reader reader = new StringReader(s);
42              StaxUtils.copy(StaxUtils.createXMLStreamReader(reader), writer);
43          } catch (Exception e) {
44              throw new RuntimeException(e);
45          }
46      }
47  
48  
49      @Override
50      public void write(Object obj, XMLStreamWriter writer) {
51          write(obj, null, writer);
52      }
53  
54      @Override
55      public void setAttachments(Collection<Attachment> attachments) {
56          // nop
57      }
58  
59      @Override
60      public void setProperty(String key, Object value) {
61          // nop
62      }
63  
64      @Override
65      public void setSchema(Schema s) {
66          // nop
67      }
68  }