source: SMC/trunk/SMC/src/eu/clarin/cmdi/smc/SMC.java @ 1671

Last change on this file since 1671 was 1671, checked in by vronk, 12 years ago

implementing isocat-languages as termsets

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