View Javadoc
1   /*
2    * Copyright 2008 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.platform.camel.core.support.transform.min;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.io.Reader;
22  import java.io.Writer;
23  import java.nio.charset.Charset;
24  import java.nio.charset.StandardCharsets;
25  
26  import javax.xml.transform.Result;
27  import javax.xml.transform.Source;
28  import javax.xml.transform.stream.StreamResult;
29  import javax.xml.transform.stream.StreamSource;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.openehealth.ipf.commons.core.modules.api.Converter;
33  
34  /**
35   * @author Martin Krasser
36   */
37  public class TestConverter extends Converter<String, String> {
38  
39      public String parse(String message, Object... params) {
40          return "string: " + message;
41      }
42  
43      public String parse(Reader reader, Object... params) throws IOException {
44          return "reader: " + IOUtils.toString(reader);
45      }
46  
47      public String parse(InputStream message, Object... params) throws IOException {
48          return "stream: " + IOUtils.toString(message, Charset.defaultCharset());
49      }
50  
51      public String parse(Source source, Object... params) throws IOException {
52          return "source: " + toString(source);
53      }
54  
55      public Result render(String model, Result result, Object... params) throws IOException {
56          StreamResult r = (StreamResult)result;
57          IOUtils.write(model, r.getWriter());
58          return r;
59      }
60  
61      public OutputStream render(String model, OutputStream result, Object... params) throws IOException {
62          IOUtils.write(model, result, StandardCharsets.UTF_8);
63          return result;
64      }
65  
66      public Writer render(String model, Writer result, Object... params) throws IOException {
67          IOUtils.write(model, result);
68          return result;
69      }
70  
71      public String render(String model, Object... params) {
72          return model;
73      }
74  
75      public String zap(String object, Object... params) {
76          return object;
77      }
78  
79      private static String toString(Source source) throws IOException {
80          StreamSource s = (StreamSource)source;
81          return IOUtils.toString(s.getReader());
82      }
83      
84  }