/* * Created on Feb 24, 2005 */ package fr.lifl.stc.stan.analyser; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import fr.lifl.stc.stan.util.Utils; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * @author yann, dorina */ public class Config { private static String configFile = "config.xml"; private static boolean verbose = false; // private static String dict = "dicotests_jits_worstePublic.txt"; private static String dict = "dict.txt"; private static String inputFile = null; /** * dictionary that contains hand-annotated methods */ //private static String initDict = "dicoJITSnatives.txt"; private static String initDict = null; private static String handAnnotationsFile = null; private static String policy = null; //output for log files private static String path = ""; private static boolean html = false; private static String html_rep = "html/"; private static boolean annotate = false; private static String annotation_rep = "annotated/"; private static boolean statistics = false; private static String statistics_file = "stats-dump.txt"; /** * variables for DSL */ private static String dslFile = "policies"; /** * if true, the global signatures of abstract methods * will be the worst possible. * * if false, the global signature of abstract methods * is initialized to null and it will be computed during analysis */ //private static boolean worst_sign = false; private static boolean worst_sign = false; /** * implicit flow */ private static boolean implicit_flow = false; private static boolean implicit_flow_booleans_only = false; /** * */ public Config() { super(); } public static void setDictionary(String d) { dict = d; } public static String getDictionary() { return path + dict; } public static void setInitialDictionary(String d) { initDict = d; } public static String getInitialDictionary() { return (initDict==null)?null:(path + initDict); } public static void setPolicy(String d) { policy = d; } public static String getPolicy() { return (policy==null)?null:(path + policy); } public static String getDSLFile() { return (dslFile==null)?null:(path + dslFile); } public static void setHandAnnotationsFile(String d) { handAnnotationsFile = d; } public static String getHandAnnotationsFile() { return (handAnnotationsFile==null)?null:(path + handAnnotationsFile); } public static void setEnableImplicitFlow(boolean b) { implicit_flow = b; } public static boolean implicitFlowEnabled() { return implicit_flow ; } public static boolean implicitFlowBooleansOnlyEnabled(){ return implicit_flow_booleans_only; } public static void setEnableImplicitFlowBooleansOnly(boolean b){ implicit_flow_booleans_only = b; } public static void setEnableHtml(boolean b) { html = b; } public static boolean htmlEnabled() { return html; } public static void setHtmlRep(String rep) { //html_rep = rep; if(htmlEnabled()) html_rep = Utils.createRep(path+rep); else html_rep = path+rep; } public static String htmlRep() { return html_rep; } public static void setEnableStatistics(boolean b) { statistics = b; } public static boolean statisticsEnabled() { return statistics; } public static void setStatisticsFile(String file) { statistics_file = file; } public static String statisticsFile() { return path+statistics_file; } public static void setPath(String rep) { path = Utils.createRep(rep); //try { //File f= new File(path); /*}catch(IOException e){ System.out.println(e.toString()); }*/ //System.out.println("FILE = "+f); } public static String getPath() { return path; } public static void setAnnotationRep(String rep) { if(annotateEnabled()) annotation_rep = Utils.createRep(path+rep); else annotation_rep = path+rep; } public static String annotationRep() { return annotation_rep; } public static boolean annotateEnabled(){ return annotate; } public static void setEnableAnnotate(boolean b){ annotate = b; } public static boolean worstSignatureEnabled(){ return worst_sign; } public static void setEnableWorstSignature(boolean b){ worst_sign = b; } public static void setEnableVerbose(boolean v){ verbose = v; } public static boolean verboseEnable(){ return verbose; } public static void setInputFile(String s) { inputFile = s; } public static void setDSLFile(String s) { dslFile = s; } /** * * @return */ public static String[] getInputFiles() { if(inputFile == null) return null; String[] result = new String[0]; try{ RandomAccessFile r = new RandomAccessFile(inputFile, "r"); String s; while ((s=r.readLine())!=null){ String[] res = s.split("\\s"); String[] aux = new String[res.length + result.length]; System.arraycopy(result, 0, aux, 0, result.length); System.arraycopy(res, 0, aux, result.length, res.length); result = aux; //System.out.println("[[[[[[[[[adding : "+s+"]]]]]]"); } r.close(); }catch(IOException e){ } return result; } public static void loadXMLConfigFile (String configName){ try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File(configFile)); // normalize text representation doc.getDocumentElement ().normalize (); //System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfConfigs = doc.getElementsByTagName("config"); for(int s=0; s0) { Element dictionaryElement = (Element)dictionaryList.item(0); NodeList textFNList = dictionaryElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setDictionary(value); //System.out.println("Dictionary : " + value); } } } //read initial dictionary NodeList initDictionaryList = firstConfigElement.getElementsByTagName("initial-dictionary"); if(initDictionaryList!=null && initDictionaryList.getLength()>0) { Element initDictionaryElement = (Element)initDictionaryList.item(0); NodeList textFNList = initDictionaryElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setInitialDictionary(value); //System.out.println("Initial Dictionary : " + value); } } } //read policy NodeList policyList = firstConfigElement.getElementsByTagName("policy"); if(policyList!=null && policyList.getLength()>0) { Element policyElement = (Element)policyList.item(0); NodeList textFNList = policyElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setPolicy(value); //System.out.println("Policy : " + value); } } } //read hand-annotation NodeList handAnnList = firstConfigElement.getElementsByTagName("hand-annotations-file"); if(handAnnList!=null && handAnnList.getLength()>0) { Element handAnnElement = (Element)handAnnList.item(0); NodeList textFNList = handAnnElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setHandAnnotationsFile(value); //System.out.println("Policy : " + value); } } } //read base-rep NodeList pathList = firstConfigElement.getElementsByTagName("path"); if(pathList!=null && pathList.getLength()>0) { Element pathElement = (Element)pathList.item(0); NodeList textFNList = pathElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setPath(value); //System.out.println("Initial Dictionary : " + value); } } } //read annotate NodeList annotateList = firstConfigElement.getElementsByTagName("annotate"); if(annotateList!=null && annotateList.getLength()>0) { Config.setEnableAnnotate(true); //System.out.println("is annotate enabled "); } //read annotateRep NodeList annotateRepList = firstConfigElement.getElementsByTagName("annotate-rep"); if(annotateRepList!=null && annotateRepList.getLength()>0) { Element annotateRepElement = (Element)annotateRepList.item(0); NodeList textFNList = annotateRepElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setAnnotationRep(value); //System.out.println("Annotate-rep : " + value); } } } //read statistics NodeList statisticsList = firstConfigElement.getElementsByTagName("statistics"); if(statisticsList!=null && statisticsList.getLength()>0) { Config.setEnableStatistics(true); //System.out.println("is statistics enabled "); } //read statistics-file NodeList statisticsRepList = firstConfigElement.getElementsByTagName("statistics-file"); if(statisticsRepList!=null && statisticsRepList.getLength()>0) { Element statisticsRepElement = (Element)statisticsRepList.item(0); NodeList textFNList = statisticsRepElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setStatisticsFile(value); //System.out.println("statistics-rep : " + value); } } } //read html NodeList htmlList = firstConfigElement.getElementsByTagName("html"); if(htmlList!=null && htmlList.getLength()>0) { Config.setEnableHtml(true); //System.out.println("is Html enabled "); } //read htmlRep NodeList htmlRepList = firstConfigElement.getElementsByTagName("html-rep"); if(htmlRepList!=null && htmlRepList.getLength()>0) { Element htmlRepElement = (Element)htmlRepList.item(0); NodeList textFNList = htmlRepElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setHtmlRep(value); //System.out.println("html-rep : " + value); } } } //read worstSignature NodeList worstSignatureList = firstConfigElement.getElementsByTagName("worst-signature"); if(worstSignatureList!=null && worstSignatureList.getLength()>0) { Config.setEnableWorstSignature(true); //System.out.println("is worstSignature enabled "); } //read verbose NodeList verboseList = firstConfigElement.getElementsByTagName("verbose"); if(verboseList!=null && verboseList.getLength()>0) { Config.setEnableVerbose(true); //System.out.println("is verbose enabled "); } //read inputFile NodeList inputFileList = firstConfigElement.getElementsByTagName("file"); if(inputFileList!=null && inputFileList.getLength()>0) { Element inputFileElement = (Element)inputFileList.item(0); NodeList textFNList = inputFileElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setInputFile(value); //System.out.println("inputFile : " + value); } } } //read DSL policies NodeList dslpolicyList = firstConfigElement.getElementsByTagName("dslPolicies"); if(dslpolicyList!=null && dslpolicyList.getLength()>0) { Element dslpolicyElement = (Element)dslpolicyList.item(0); NodeList textFNList = dslpolicyElement.getChildNodes(); if(textFNList!=null && textFNList.getLength()>0 && ((Node)textFNList.item(0)).getNodeValue() != null ) { value = ((Node)textFNList.item(0)).getNodeValue().trim(); if(!value.equals("")) { Config.setDSLFile(value); } } } break; }//end of if clause } } }//end of for loop with s var }catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); }catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); }catch (Throwable t) { t.printStackTrace (); } }//end of method }