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.commons.core.modules.api;
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  
24  import javax.xml.transform.Result;
25  import javax.xml.transform.Source;
26  
27  /**
28   * Abstract class that serializes parsing, translating and rendering steps.
29   * 
30   * @author Christian Ohr
31   * 
32   * @param <S>
33   *            output type of parse(), input type translate()
34   * @param <T>
35   *            output type of translate() input type of render()
36   */
37  public abstract class Converter<S, T> implements Parser<S>, Renderer<T>, Transmogrifier<S, T> {
38  
39      /**
40       * Parses a message and renders the internal model into a different external
41       * representation.
42       * 
43       * @param message message to be converted
44       * @return converted message
45       * @throws ParseException
46       * @throws RenderException
47       */
48      public final String convert(String message, Object... params) {
49          S parsed = parse(message, params);
50          T translated = zap(parsed, params);
51          return render(translated, params);
52      }
53  
54      /**
55       * Parses a message and renders the internal model into a different external
56       * representation.
57       */
58      public final OutputStream convert(InputStream in, OutputStream out, Object... params) throws IOException {
59          S parsed = parse(in, params);
60          T translated = zap(parsed, params);
61          return render(translated, out, params);
62      }
63  
64      /**
65       * Parses a message and renders the internal model into a different external
66       * representation.
67       */
68      public final Result convert(Source source, Result result, Object... params) throws IOException {
69          S parsed = parse(source, params);
70          T translated = zap(parsed, params);
71          return render(translated, result, params);
72      }
73  
74      /**
75       * Parses a message and renders the internal model into a different external
76       * representation.
77       */
78      public final Writer convert(Reader reader, Writer writer, Object... params) throws IOException {
79          S parsed = parse(reader, params);
80          T translated = zap(parsed, params);
81          return render(translated, writer, params);
82      }
83  
84  }