1 | package eu.clarin.cmdi.smc; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.net.URL; |
---|
7 | import java.net.URLConnection; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | import java.util.Properties; |
---|
12 | //import org.apache.commons.configuration.Configuration; |
---|
13 | //import org.apache.commons.lang.exception.NestableException; |
---|
14 | //import org.apache.commons.configuration.ConfigurationException; |
---|
15 | //import org.apache.commons.configuration.PropertiesConfiguration; |
---|
16 | // |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | import javax.xml.transform.TransformerException; |
---|
21 | |
---|
22 | import org.apache.log4j.Logger; |
---|
23 | |
---|
24 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
25 | import eu.clarin.cmdi.mdservice.internal.NoStylesheetException; |
---|
26 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
27 | |
---|
28 | /** |
---|
29 | * some of the generic stuff (getStream, getParams, addParam ...) taken from mdservice.GenericAction |
---|
30 | * @author m |
---|
31 | * |
---|
32 | */ |
---|
33 | public class SMC { |
---|
34 | private static String appname="smc"; |
---|
35 | |
---|
36 | public SMC () { |
---|
37 | //Utils.loadConfig("smc.properties"); |
---|
38 | |
---|
39 | this.configure(); |
---|
40 | this.addParam("cache_dir", Utils.getConfig("cache.dir")); |
---|
41 | //this.addParam("cache_dir", config.getString("cache.dir")); |
---|
42 | |
---|
43 | } |
---|
44 | private Map<String,String[]> params; |
---|
45 | |
---|
46 | public static Logger log = Logger.getLogger("SMC"); |
---|
47 | |
---|
48 | |
---|
49 | public static void main(String[] args) { |
---|
50 | |
---|
51 | SMC smc = new SMC(); |
---|
52 | |
---|
53 | //smc.init(); |
---|
54 | |
---|
55 | InputStream is = smc.listTermsets(""); |
---|
56 | String output_path = Utils.getConfig("cache.dir") + smc.getParam("data_key") + "_.xml" ; |
---|
57 | File f = Utils.write2File(output_path, is); |
---|
58 | log.debug("result stored in: " + f.getAbsolutePath()); |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | public void configure(){ |
---|
63 | Utils.loadConfig(appname, "smc.properties", this.getClass().getClassLoader()); |
---|
64 | } |
---|
65 | public void configure(String configPath) { |
---|
66 | try { |
---|
67 | Utils.loadConfig(appname, configPath, this.getClass().getClassLoader()); |
---|
68 | |
---|
69 | //config = new PropertiesConfiguration("smc.properties"); |
---|
70 | } catch (Exception e) { |
---|
71 | // TODO Auto-generated catch block |
---|
72 | e.printStackTrace(); |
---|
73 | } |
---|
74 | } |
---|
75 | /** |
---|
76 | * load data from registries based on configuration |
---|
77 | * transform and store as local xml in cache.dir |
---|
78 | */ |
---|
79 | public void init () { |
---|
80 | |
---|
81 | InputStream is =null; |
---|
82 | is = Utils.load2Stream(Utils.getConfig("termsets.config.file"),this.getClass().getClassLoader()); |
---|
83 | //is = Utils.load2Stream(config.getString("termsets.config.file")); |
---|
84 | |
---|
85 | |
---|
86 | MDTransformer transformer = new MDTransformer(); |
---|
87 | transformer.configure(Utils.getAppConfig(appname), this.getClass().getClassLoader()); |
---|
88 | |
---|
89 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
90 | // TODO: WHY?? |
---|
91 | //transformer.setSrcFile(Utils.getConfig("termsets.config.file")); |
---|
92 | addParam("data_key", "dcr-cmd-map"); |
---|
93 | |
---|
94 | // this is necessary for the transformer (in MDUTILS-library) to search for the resources (config and xsls) in the correct context) |
---|
95 | //transformer.configure(Utils.getConfig(), this.getClass().getClassLoader()); |
---|
96 | |
---|
97 | transformer.setParams(getParams()); |
---|
98 | transformer.setTranskey("init"); |
---|
99 | // here the transformation is invoked |
---|
100 | InputStream resultStream; |
---|
101 | try { |
---|
102 | resultStream = transformer.transformXML(is); |
---|
103 | |
---|
104 | // store the result in the cache |
---|
105 | String output_path = Utils.getConfig("cache.dir") + getParam("data_key") + ".xml" ; |
---|
106 | //String output_path = config.getString("cache.dir") + getParam("data_key") + ".xml" ; |
---|
107 | File f = Utils.write2File(output_path, resultStream); |
---|
108 | log.debug("SMC.init(): result stored in: " + f.getAbsolutePath()); |
---|
109 | |
---|
110 | } catch (IOException e1) { |
---|
111 | log.debug(Utils.errorMessage(e1)); |
---|
112 | } catch (InterruptedException e1) { |
---|
113 | log.debug(Utils.errorMessage(e1)); |
---|
114 | } catch (TransformerException e1) { |
---|
115 | log.debug(Utils.errorMessage(e1)); |
---|
116 | } catch (NoStylesheetException e1) { |
---|
117 | log.debug(Utils.errorMessage(e1)); |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Gets the local parameter map. |
---|
124 | * |
---|
125 | * @return |
---|
126 | */ |
---|
127 | private Map<String,String[]> getParams() { |
---|
128 | if (params == null) { |
---|
129 | params = new HashMap<String,String[]>(); |
---|
130 | } |
---|
131 | return params; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Add parameter into local parameter map. |
---|
136 | * |
---|
137 | * @param key - parameter key |
---|
138 | * @param value - parameter value |
---|
139 | */ |
---|
140 | public void addParam(String key, String value){ |
---|
141 | String[] sarr = new String[1]; |
---|
142 | sarr[0] = value; |
---|
143 | getParams().put(key, sarr); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * This is for simplified access to the the values of the request-parameters |
---|
148 | * They are stored in the parameters-map as a String-array, |
---|
149 | * but in most(?) situations, we expect just a simple string. |
---|
150 | * @param key |
---|
151 | * @return |
---|
152 | */ |
---|
153 | private String getParam(String key) { |
---|
154 | String v = ""; |
---|
155 | if (!(getParams().get(key)==null)) v=(String)getParams().get(key)[0]; |
---|
156 | return v; |
---|
157 | } |
---|
158 | |
---|
159 | private static InputStream getStream(String uri, String rep) throws IOException |
---|
160 | { |
---|
161 | |
---|
162 | URL url = new URL(uri); |
---|
163 | URLConnection urlConnection = url.openConnection(); |
---|
164 | |
---|
165 | if (rep.equals("rdf")) { |
---|
166 | urlConnection.setRequestProperty("Accept", "application/rdf+xml"); |
---|
167 | } |
---|
168 | if (rep.equals("dcif")) { |
---|
169 | urlConnection.setRequestProperty("Accept", "application/dcif+xml"); |
---|
170 | } |
---|
171 | //urlConnection.setRequestProperty("Accept-Language", getLang()); |
---|
172 | |
---|
173 | InputStream resultStream = urlConnection.getInputStream(); |
---|
174 | |
---|
175 | return resultStream; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | private static void test() { |
---|
181 | //String uri = "http://www.isocat.org/rest/profile/5"; |
---|
182 | String uri = "http://www.isocat.org/rest/user/guest/search?keywords=chinese"; |
---|
183 | String output_path = "C:/Users/m/3lingua/clarin/CMDI/SMC/output/isocat_search.dcif.xml"; |
---|
184 | |
---|
185 | try { |
---|
186 | File f = Utils.write2File(output_path, getStream(uri,"dcif")); |
---|
187 | System.out.print(f.getAbsolutePath()); |
---|
188 | } catch (IOException e) { |
---|
189 | // TODO Auto-generated catch block |
---|
190 | e.printStackTrace(); |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * list termsets |
---|
197 | * @param context internal key(?) of the context set to start from; '*' or 'top' or '' for default top-level list |
---|
198 | * @return stream with XML listing the available termsets |
---|
199 | */ |
---|
200 | public InputStream listTermsets(String context) { |
---|
201 | InputStream is =null; |
---|
202 | is = Utils.load2Stream(Utils.getConfig("termsets.config.file").trim(), this.getClass().getClassLoader()); |
---|
203 | return is; |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * list terms of a set |
---|
208 | * @param context |
---|
209 | * @return |
---|
210 | */ |
---|
211 | public InputStream listTerms(String context) { |
---|
212 | |
---|
213 | InputStream is =null; |
---|
214 | is = Utils.load2Stream(Utils.getConfig("termsets.config.file"),this.getClass().getClassLoader()); |
---|
215 | |
---|
216 | MDTransformer transformer = new MDTransformer(); |
---|
217 | // this is necessary for the transformer (in MDUTILS-library) to search for the resources (config and xsls) in the correct context) |
---|
218 | transformer.configure(Utils.getAppConfig(appname), this.getClass().getClassLoader()); |
---|
219 | |
---|
220 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
221 | // TODO: WHY?? |
---|
222 | //transformer.setSrcFile(Utils.getConfig("termsets.config.file")); |
---|
223 | |
---|
224 | addParam("set", context); |
---|
225 | |
---|
226 | transformer.setParams(getParams()); |
---|
227 | transformer.setTranskey("op"); |
---|
228 | |
---|
229 | InputStream resultStream=null; |
---|
230 | try { |
---|
231 | resultStream = transformer.transformXML(is); |
---|
232 | |
---|
233 | } catch (IOException e1) { |
---|
234 | log.debug(Utils.errorMessage(e1)); |
---|
235 | } catch (InterruptedException e1) { |
---|
236 | log.debug(Utils.errorMessage(e1)); |
---|
237 | } catch (TransformerException e1) { |
---|
238 | log.debug(Utils.errorMessage(e1)); |
---|
239 | } catch (NoStylesheetException e1) { |
---|
240 | log.debug(Utils.errorMessage(e1)); |
---|
241 | } |
---|
242 | |
---|
243 | return resultStream; |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * map from source term to target-terms |
---|
248 | * @param term |
---|
249 | * @return |
---|
250 | */ |
---|
251 | public InputStream map(String term) { |
---|
252 | |
---|
253 | InputStream is =null; |
---|
254 | is = Utils.load2Stream(Utils.getConfig("termsets.config.file"),this.getClass().getClassLoader()); |
---|
255 | |
---|
256 | MDTransformer transformer = new MDTransformer(); |
---|
257 | // this is necessary for the transformer (in MDUTILS-library) to search for the resources (config and xsls) in the correct context) |
---|
258 | transformer.configure(Utils.getAppConfig(appname), this.getClass().getClassLoader()); |
---|
259 | |
---|
260 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
261 | // TODO: WHY?? |
---|
262 | //transformer.setSrcFile(Utils.getConfig("termsets.config.file")); |
---|
263 | |
---|
264 | addParam("term", term); |
---|
265 | |
---|
266 | transformer.setParams(getParams()); |
---|
267 | transformer.setTranskey("op"); |
---|
268 | |
---|
269 | InputStream resultStream=null; |
---|
270 | try { |
---|
271 | resultStream = transformer.transformXML(is); |
---|
272 | |
---|
273 | } catch (IOException e1) { |
---|
274 | log.debug(Utils.errorMessage(e1)); |
---|
275 | } catch (InterruptedException e1) { |
---|
276 | log.debug(Utils.errorMessage(e1)); |
---|
277 | } catch (TransformerException e1) { |
---|
278 | log.debug(Utils.errorMessage(e1)); |
---|
279 | } catch (NoStylesheetException e1) { |
---|
280 | log.debug(Utils.errorMessage(e1)); |
---|
281 | } |
---|
282 | |
---|
283 | return resultStream; |
---|
284 | } |
---|
285 | |
---|
286 | } |
---|