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.adapter;
17
18 import static org.openehealth.ipf.platform.camel.core.util.Exchanges.prepareResult;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.Reader;
23
24 import javax.xml.transform.Source;
25
26 import org.apache.camel.Exchange;
27 import org.openehealth.ipf.commons.core.modules.api.Parser;
28
29 /**
30 * Adapts a {@link Parser}.
31 *
32 * @author Martin Krasser
33 */
34 public class ParserAdapter extends ProcessorAdapter {
35
36 private final Parser parser;
37
38 /**
39 * Creates a new {@link ParserAdapter} and sets the delegate
40 * {@link Parser}.
41 *
42 * @param parser
43 * a parser.
44 */
45 public ParserAdapter(Parser parser) {
46 this.parser = parser;
47 }
48
49 /**
50 * Processes input data and populates the output message. This method
51 * delegates message processing to more specialized <code>doProcess</code>
52 * implementations.
53 *
54 * @param exchange
55 * message exchange where to write processing results.
56 * @param inputData
57 * input data.
58 * @param inputParams
59 * input parameters.
60 * @throws Exception
61 * if a processing error occurs.
62 *
63 * @see #doProcess(InputStream, Object...)
64 * @see #doProcess(Reader, Object...)
65 * @see #doProcess(Source, Object...)
66 * @see #doProcess(String, Object...)
67 */
68 @Override
69 protected void doProcess(Exchange exchange, Object inputData,
70 Object... inputParams) throws Exception {
71
72 if (inputData instanceof InputStream) {
73 prepareResult(exchange).setBody(doProcess((InputStream)inputData, inputParams));
74 } else if (inputData instanceof Reader) {
75 prepareResult(exchange).setBody(doProcess((Reader)inputData, inputParams));
76 } else if (inputData instanceof Source) {
77 prepareResult(exchange).setBody(doProcess((Source)inputData, inputParams));
78 } else if (inputData instanceof String) {
79 prepareResult(exchange).setBody(doProcess((String)inputData, inputParams));
80 } else {
81 throw new IllegalArgumentException(
82 "input data class not supported: " + inputData.getClass());
83 }
84
85 }
86
87 /**
88 * Parses <code>inputData</code> using <code>inputParams</code> and
89 * returns the parsing result. Parsing is delegated to the {@link Parser}
90 * set at construction time.
91 *
92 * @param inputData
93 * input data
94 * @param inputParams
95 * input parameters
96 * @throws IOException
97 * if a system-level problem occurs
98 * @return parsing result
99 */
100 protected Object doProcess(InputStream inputData, Object... inputParams) throws IOException {
101 return parser.parse(inputData, inputParams);
102 }
103
104 /**
105 * Parses <code>inputData</code> using <code>inputParams</code> and
106 * returns the parsing result. Parsing is delegated to the {@link Parser}
107 * set at construction time.
108 *
109 * @param inputData
110 * input data
111 * @param inputParams
112 * input parameters
113 * @throws IOException
114 * if a system-level problem occurs
115 * @return parsing result
116 */
117 protected Object doProcess(Reader inputData, Object... inputParams) throws IOException {
118 return parser.parse(inputData, inputParams);
119 }
120
121 /**
122 * Parses <code>inputData</code> using <code>inputParams</code> and
123 * returns the parsing result. Parsing is delegated to the {@link Parser}
124 * set at construction time.
125 *
126 * @param inputData
127 * input data
128 * @param inputParams
129 * input parameters
130 * @throws IOException
131 * if a system-level problem occurs
132 * @return parsing result
133 */
134 protected Object doProcess(Source inputData, Object... inputParams) throws IOException {
135 return parser.parse(inputData, inputParams);
136 }
137
138 /**
139 * Parses <code>inputData</code> using <code>inputParams</code> and
140 * returns the parsing result. Parsing is delegated to the {@link Parser}
141 * set at construction time.
142 *
143 * @param inputData
144 * input data
145 * @param inputParams
146 * input parameters
147 * @return parsing result
148 */
149 protected Object doProcess(String inputData, Object... inputParams) {
150 return parser.parse(inputData, inputParams);
151 }
152
153 }