source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/Admin.java @ 768

Last change on this file since 768 was 768, checked in by vronk, 14 years ago

terms-matrix; model, datcats to terms; model-matrix-all!

File size: 9.1 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.util.Properties;
10
11import javax.xml.parsers.DocumentBuilder;
12import javax.xml.parsers.DocumentBuilderFactory;
13import javax.xml.parsers.ParserConfigurationException;
14
15import org.w3c.dom.Document;
16import org.xml.sax.SAXException;
17
18import net.sf.json.JSON;
19import net.sf.json.JSONSerializer;
20import net.sf.json.xml.XMLSerializer;
21
22
23import eu.clarin.cmdi.mdservice.model.Query;
24
25/**
26 * Helper class, main-class to run as command-line java-app for testing
27 *
28 * @author vronk
29 *
30 */
31
32public class Admin {
33
34        private static String config_path = "mdservice.properties";
35        private static Properties  config;
36               
37        public static void main(String[] args) {               
38
39               
40                //String dirpath = "C:/apps/ApacheTomcat/webapps/mdservice/schemas";
41               
42                //CMDRegistryProxy cmdreg = new CMDRegistryProxy ();
43               
44                //cmdreg.readCompRegistryFromDir(dirpath);
45               
46                //System.out.println(cmdreg.listComponents());
47
48                testJSON();
49               
50                //loadConfig(config_path);
51               
52                /*
53                //Query q = new Query("dc.title=dino or dinosaur", Query.RECORDSET);
54                //Query q = new Query("( ( Date any 2005 ) or ( Date any 2001 ) ) and ( ( Description any part ) )", Query.RECORDSET);
55                Query q = new Query("( ( Date any 2005 ) or ( Country = \"United States\" ) ) and ( ( Description any part ) )", Query.RECORDSET);
56                //System.out.println(q.toXCQL());
57                System.out.println(q.toXPath());
58                */
59                //Query q = new Query("CMD", "cmdIndex");
60               
61                //try {
62                       
63                        /*
64
65                        Query q = new Query("Title=the", Query.RECORDSET, "olac" );
66                        q.execute();
67                 
68                 InputStream inputStream = q.getResult().getResultStream("htmltable");
69        */       
70                //MDTransformer.getMDTransformer(q.getResult().getResultStream("xml"),"model2html",);
71                /*
72                        MDRepoProxyAction mrpa = new MDRepoProxyAction();
73                        // crp.setCompname("cmdi-corpus-aac2");
74                        mrpa.setQ("Title any the");
75                        mrpa.setCollection("olac");
76                        mrpa.setActionkey("recordset");
77                        mrpa.setFormat("xml");
78                        mrpa.execute();
79                        InputStream inputStream = mrpa.getInputStream();
80                       
81                        Cache.getCache().putInCache("test", inputStream);
82                        */
83                       
84                        /*
85                        DCRProxyAction dcr = new DCRProxyAction();
86                        dcr.setDc("1253");
87                        InputStream     inputStream = MDTransformer.getMDTransformer().transformXML(dcr.getSourceStream(), "dcif2htmldiv");             
88
89                        */
90                        /*
91                        CompRegistryProxyAction crp = new CompRegistryProxyAction ();
92                        // crp.setCompname("cmdi-corpus-aac2");
93                        crp.setFormat("htmllist");
94                        crp.execute();
95                        InputStream inputStream = crp.getInputStream();
96        */
97                       
98                //      InputStream inputStream = MDRepositoryProxy.getMDRepo().getCollections(null);
99                       
100                        //copyStreams(inputStream,System.out);
101                        /*
102                } catch (IOException e1) {
103                        // TODO Auto-generated catch block
104                        e1.printStackTrace();
105                } catch (InterruptedException e1) {
106                        // TODO Auto-generated catch block
107                        e1.printStackTrace();
108                }       catch (Exception e) {
109                        Admin.notifyUser(e.getMessage());
110                }
111        */
112                       
113                //System.out.println(res.xmlString());
114                //System.out.println(res.toFormat("MODEL2VIEW"));
115               
116               
117                //System.out.println(res.xmlString());
118               
119//              MDRepositoryProxy.getMDRepo().queryModel("CMD", System.out);
120               
121                //testParse();
122               
123                /*
124                 * CMDComponent testcomp = new CMDComponent("C:/apps/ApacheTomcat/webapps/mdservice/schemas/component-olac.xml");
125                 
126                System.out.println(testcomp.name());
127                System.out.println("components:");
128                for (Iterator it = testcomp.components().iterator();
129                it.hasNext();) {
130                        CMDComponentType comp = (CMDComponentType)it.next();
131                        System.out.println(comp.getName() + " " + comp.getConceptLink() + "\n");
132                       
133                }
134
135                System.out.println("elements:");
136                for (Iterator it = testcomp.elements().iterator();
137                it.hasNext();) {
138                        CMDElementType comp = (CMDElementType)it.next();
139                        System.out.println(comp.getName() + " " + comp.getConceptLink() + "\n");
140                       
141                }
142                */
143        }
144
145        /*
146        public static void testCache (InputStream instream) {
147                String myKey = "myKey2";
148                String myValue;
149                int myRefreshPeriod = 1000;
150                GeneralCacheAdministrator admin = new GeneralCacheAdministrator();
151                System.out.print(admin.isMemoryCaching());
152                System.out.print(admin.getProperty("cache.path"));
153               
154                try {
155                    // Get from the cache
156                    myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
157                    System.out.println("value from cache:" + myValue);
158                } catch (NeedsRefreshException nre) {
159                    try {
160                        // Get the value (probably from the database)
161        //              myValue = "This is the content retrieved.";
162                        // Store in the cache
163                        admin.putInCache(myKey, instream.toString());
164                        System.out.println("freshfromcache: " );
165                        System.out.println(admin.getFromCache(myKey, myRefreshPeriod));
166                        //copyStreams((InputStream) admin.getFromCache(myKey, myRefreshPeriod),System.out);
167                    } catch (Exception ex) {
168                        // We have the current content if we want fail-over.
169//                      myValue = (String) nre.getCacheContent();
170                        // It is essential that cancelUpdate is called if the
171                        // cached content is not rebuilt
172                        admin.cancelUpdate(myKey);
173                    }
174                //System.out.println(myValue);
175        }
176        */
177        public static void loadConfig() {
178                loadConfig(config_path);
179        }
180
181        public static void loadConfig(String configPath) {
182               
183                System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
184                InputStream config_file;
185                 
186                try {                   
187                        config_file = Admin.class.getClassLoader().getResourceAsStream(configPath);
188                       
189//                   System.out.println(config_file);
190                        if (config_file == null) {
191                             Admin.notifyUser("CONFIGURATION ERROR: Properties file not found!");
192                        } else {
193                               
194                                System.out.println("Reading configuration from: " + Admin.class.getClassLoader().getResource(configPath));
195                                //config_file = new FileInputStream(configPath);
196                                config = new Properties();
197                                config.load(config_file);       
198                        }
199                       
200                } catch (FileNotFoundException e) {
201                        // TODO Auto-generated catch block
202                        e.printStackTrace();
203                } catch (IOException e) {
204                        // TODO Auto-generated catch block
205                        e.printStackTrace();
206                } 
207        }
208       
209       
210        public static Properties getConfig() {
211                if (config==null)  {
212                        loadConfig(config_path);
213                }
214                return config;
215        }
216
217        public static void notifyUser(String msg) {
218                System.out.println(msg);
219        }
220
221
222        public static InputStream loadFile(String path) {
223               
224                InputStream file=null;
225                 
226                try {                   
227                        file = Admin.class.getClassLoader().getResourceAsStream(path);
228                       
229//                   System.out.println(config_file);
230                        if (file == null) {
231                             Admin.notifyUser("File not found!: " + path);
232                        } else {
233                               
234                                System.out.println("Reading in: " + Admin.class.getClassLoader().getResource(path));
235                                //config_file = new FileInputStream(configPath);
236                //              Properties test = new Properties();
237                        //      test.load(file);
238                        }
239                        /*
240                } catch (FileNotFoundException e) {
241                        // TODO Auto-generated catch block
242                        e.printStackTrace();
243                        */
244                }   catch (Exception e) {
245                        // TODO Auto-generated catch block
246                        e.printStackTrace();
247                } 
248               
249                return file;
250        }
251       
252       
253        public static Document getXMLDocument(String path) {
254                Document retdoc=null;
255                try {
256                       
257                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
258                DocumentBuilder docBuilder;
259                        try {
260                                docBuilder = docFactory.newDocumentBuilder();               
261                                try {
262                                        Admin.notifyUser("trying to read from:" + path);
263                                        InputStream xmlstream =  loadFile(path);                                       
264                                        //retdoc= docBuilder.parse(fname);
265                                        retdoc= docBuilder.parse(xmlstream);
266                                        Admin.notifyUser("read in:" + retdoc);
267                                } catch (SAXException e) {
268                                                // TODO Auto-generated catch block
269                                                Admin.notifyUser(e.getMessage());
270                                                e.printStackTrace();
271                                }
272                        } catch (ParserConfigurationException e) {
273                                        // TODO Auto-generated catch block
274                                Admin.notifyUser(e.getMessage());
275                                        e.printStackTrace();
276                        }
277                       
278                 
279            }  catch (IOException ex){
280                //Admin.notifyUser("initCacheCounter:" + ex.toString());
281                ex.printStackTrace();
282            } catch (Exception e) {
283                Admin.notifyUser("Admin.getXMLDocument():" + e.getMessage());
284            }
285            return retdoc;
286        }
287       
288
289       
290public static void copyStreams (InputStream in, OutputStream out) throws IOException, InterruptedException {           
291               
292                byte[] buffer = new byte[1024];
293                int len = in.read(buffer);
294                while (len != -1) {
295                    out.write(buffer, 0, len);
296                    len = in.read(buffer);
297                    if (Thread.interrupted()) {
298                        throw new InterruptedException();
299                    }
300                }
301        }
302       
303public static void writeToFile (String path, InputStream in) { 
304        try
305            {
306                        File f=new File(path);     
307                    OutputStream out=new FileOutputStream(f);
308                    copyStreams(in,out);           
309                    out.close();           
310            }
311            catch (Exception e){
312                Admin.notifyUser("ERROR writeToFile");
313                e.printStackTrace();
314            }
315        }
316
317public static void testJSON() {
318Admin.notifyUser("JSON TEST");
319    String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";
320    Query q = new Query("dc.title=dino or dinosaur", Query.RECORDSET);
321        JSON json = JSONSerializer.toJSON( str); 
322        XMLSerializer xmlSerializer = new XMLSerializer(); 
323        String xml = xmlSerializer.write( json ); 
324        Admin.notifyUser(xml);
325}
326
327}
328
329
Note: See TracBrowser for help on using the repository browser.