package eu.clarin.cmdi.mdservice.action; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; import net.sf.json.JSON; import net.sf.json.JSONSerializer; import net.sf.json.xml.XMLSerializer; import eu.clarin.cmdi.mdservice.model.Query; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; /** * Helper class, main-class to run as command-line java-app for testing * * @author vronk * */ public class Admin { private static String config_path = "mdservice.properties"; private static Properties config; public static Logger log = Logger.getLogger(Admin.class); public static String FATAL = "fatal"; public static String ERROR = "error"; public static String WARN = "warn"; public static String INFO = "info"; public static String DEBUG = "debug"; public static String TRACE = "trace"; public static void main(String[] args) { //log = Logger.getLogger(Admin.class); //DOMConfigurator.configure("log4j.xml"); //log.debug("WELCOME LOG4J"); //String dirpath = "C:/apps/ApacheTomcat/webapps/mdservice/schemas"; //CMDRegistryProxy cmdreg = new CMDRegistryProxy (); //cmdreg.readCompRegistryFromDir(dirpath); //System.out.println(cmdreg.listComponents()); testJSON(); //loadConfig(config_path); /* //Query q = new Query("dc.title=dino or dinosaur", Query.RECORDSET); //Query q = new Query("( ( Date any 2005 ) or ( Date any 2001 ) ) and ( ( Description any part ) )", Query.RECORDSET); Query q = new Query("( ( Date any 2005 ) or ( Country = \"United States\" ) ) and ( ( Description any part ) )", Query.RECORDSET); //System.out.println(q.toXCQL()); System.out.println(q.toXPath()); */ //Query q = new Query("CMD", "cmdIndex"); //try { /* Query q = new Query("Title=the", Query.RECORDSET, "olac" ); q.execute(); InputStream inputStream = q.getResult().getResultStream("htmltable"); */ //MDTransformer.getMDTransformer(q.getResult().getResultStream("xml"),"model2html",); /* MDRepoProxyAction mrpa = new MDRepoProxyAction(); // crp.setCompname("cmdi-corpus-aac2"); mrpa.setQ("Title any the"); mrpa.setCollection("olac"); mrpa.setActionkey("recordset"); mrpa.setFormat("xml"); mrpa.execute(); InputStream inputStream = mrpa.getInputStream(); Cache.getCache().putInCache("test", inputStream); */ /* DCRProxyAction dcr = new DCRProxyAction(); dcr.setDc("1253"); InputStream inputStream = MDTransformer.getMDTransformer().transformXML(dcr.getSourceStream(), "dcif2htmldiv"); */ /* CompRegistryProxyAction crp = new CompRegistryProxyAction (); // crp.setCompname("cmdi-corpus-aac2"); crp.setFormat("htmllist"); crp.execute(); InputStream inputStream = crp.getInputStream(); */ // InputStream inputStream = MDRepositoryProxy.getMDRepo().getCollections(null); //copyStreams(inputStream,System.out); /* } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (Exception e) { Admin.notifyUser(e.getMessage()); } */ //System.out.println(res.xmlString()); //System.out.println(res.toFormat("MODEL2VIEW")); //System.out.println(res.xmlString()); // MDRepositoryProxy.getMDRepo().queryModel("CMD", System.out); //testParse(); /* * CMDComponent testcomp = new CMDComponent("C:/apps/ApacheTomcat/webapps/mdservice/schemas/component-olac.xml"); System.out.println(testcomp.name()); System.out.println("components:"); for (Iterator it = testcomp.components().iterator(); it.hasNext();) { CMDComponentType comp = (CMDComponentType)it.next(); System.out.println(comp.getName() + " " + comp.getConceptLink() + "\n"); } System.out.println("elements:"); for (Iterator it = testcomp.elements().iterator(); it.hasNext();) { CMDElementType comp = (CMDElementType)it.next(); System.out.println(comp.getName() + " " + comp.getConceptLink() + "\n"); } */ } /* public static void testCache (InputStream instream) { String myKey = "myKey2"; String myValue; int myRefreshPeriod = 1000; GeneralCacheAdministrator admin = new GeneralCacheAdministrator(); System.out.print(admin.isMemoryCaching()); System.out.print(admin.getProperty("cache.path")); try { // Get from the cache myValue = (String) admin.getFromCache(myKey, myRefreshPeriod); System.out.println("value from cache:" + myValue); } catch (NeedsRefreshException nre) { try { // Get the value (probably from the database) // myValue = "This is the content retrieved."; // Store in the cache admin.putInCache(myKey, instream.toString()); System.out.println("freshfromcache: " ); System.out.println(admin.getFromCache(myKey, myRefreshPeriod)); //copyStreams((InputStream) admin.getFromCache(myKey, myRefreshPeriod),System.out); } catch (Exception ex) { // We have the current content if we want fail-over. // myValue = (String) nre.getCacheContent(); // It is essential that cancelUpdate is called if the // cached content is not rebuilt admin.cancelUpdate(myKey); } //System.out.println(myValue); } */ public static void loadConfig() { loadConfig(config_path); } public static void loadConfig(String configPath) { System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); InputStream config_file; try { config_file = Admin.class.getClassLoader().getResourceAsStream(configPath); // System.out.println(config_file); if (config_file == null) { Admin.notifyUser("CONFIGURATION ERROR: Properties file not found!"); } else { System.out.println("Reading configuration from: " + Admin.class.getClassLoader().getResource(configPath)); //config_file = new FileInputStream(configPath); config = new Properties(); config.load(config_file); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Properties getConfig() { if (config==null) { loadConfig(config_path); } return config; } public static void notifyUser(String msg ) { notifyUser(msg, Admin.DEBUG); } public static void notifyUser(String msg, String type ) { //Logger log = Logger.getLogger("Admin.class"); if (msg.length() == 0){ return; } if (type.equals(Admin.FATAL)) { log.fatal(msg); } if (type.equals(Admin.ERROR)) { log.error(msg); } if (type.equals(Admin.WARN)) { log.warn(msg); } if (type.equals(Admin.INFO)) { log.info(msg); } if (type.equals(Admin.DEBUG)) { log.debug(msg); } if (type.equals(Admin.TRACE)) { log.trace(msg); } //System.out.println(msg); } public static InputStream loadFile(String path) { InputStream file=null; try { file = Admin.class.getClassLoader().getResourceAsStream(path); // System.out.println(config_file); if (file == null) { Admin.notifyUser("File not found!: " + path); } else { System.out.println("Reading in: " + Admin.class.getClassLoader().getResource(path)); //config_file = new FileInputStream(configPath); // Properties test = new Properties(); // test.load(file); } /* } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); */ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return file; } public static Document getXMLDocument(String path) { Document retdoc=null; try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; try { docBuilder = docFactory.newDocumentBuilder(); try { Admin.notifyUser("trying to read from:" + path); InputStream xmlstream = loadFile(path); //retdoc= docBuilder.parse(fname); retdoc= docBuilder.parse(xmlstream); Admin.notifyUser("read in:" + retdoc); } catch (SAXException e) { // TODO Auto-generated catch block Admin.notifyUser(e.getMessage()); e.printStackTrace(); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block Admin.notifyUser(e.getMessage()); e.printStackTrace(); } } catch (IOException ex){ //Admin.notifyUser("initCacheCounter:" + ex.toString()); ex.printStackTrace(); } catch (Exception e) { Admin.notifyUser("Admin.getXMLDocument():" + e.getMessage()); } return retdoc; } public static void copyStreams (InputStream in, OutputStream out) throws IOException, InterruptedException { byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len != -1) { out.write(buffer, 0, len); len = in.read(buffer); if (Thread.interrupted()) { throw new InterruptedException(); } } } public static void writeToFile (String path, InputStream in) { try { File f=new File(path); OutputStream out=new FileOutputStream(f); copyStreams(in,out); out.close(); } catch (Exception e){ Admin.notifyUser("ERROR writeToFile"); e.printStackTrace(); } } public static void testJSON() { Admin.notifyUser("JSON TEST"); String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; Query q = new Query("dc.title=dino or dinosaur", Query.RECORDSET); JSON json = JSONSerializer.toJSON( str); XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write( json ); Admin.notifyUser(xml); } }