List of usage examples for org.dom4j Node selectNodes
List<Node> selectNodes(String xpathExpression);
selectNodes
evaluates an XPath expression and returns the result as a List
of Node
instances or String
instances depending on the XPath expression.
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
private LearningObjectiveReportSettings deserializeXMLToLearningObjectiveReportSettings(InputStream xmlStream) throws ParseException, DocumentException { LearningObjectiveReportSettings result = null; SAXReader reader = new SAXReader(); Document doc = reader.read(xmlStream); String lElem = "//loi:LearningObjectiveReportSettings"; doc.getRootElement().setQName(new QName(doc.getRootElement().getQName().getName(), new Namespace("loi", doc.getRootElement().getNamespaceURI()))); Element root = doc.getRootElement(); if (root.getName().equals("LearningObjectiveReportSettings")) { result = new LearningObjectiveReportSettings(); Node node = root.selectSingleNode(lElem + "/loi:AchievementLevelOrder"); if (node.hasContent()) { try { result.setAchievementLevelOrder(AchievementLevelOrder.valueOf(node.getStringValue())); } catch (IllegalArgumentException iea) { result.setAchievementLevelOrder(AchievementLevelOrder.HighestToLowest); }/*ww w . j ava2 s.c o m*/ } node = root.selectSingleNode(lElem + "/loi:UseMastery"); if (node.hasContent()) { result.setUseMastery(Boolean.parseBoolean(node.getStringValue())); } node = root.selectSingleNode(lElem + "/loi:LearningObjectiveMasteryRecurrenceType"); if (node.hasContent()) { try { result.setLearningObjectiveMasteryRecurrenceType( LearningObjectiveMasteryRecurrenceType.valueOf(node.getStringValue())); } catch (IllegalArgumentException iea) { result.setLearningObjectiveMasteryRecurrenceType(LearningObjectiveMasteryRecurrenceType.None); } } node = root.selectSingleNode(lElem + "/loi:Statuses"); if (node.hasContent()) { result.setStatuses(getLearningObjectiveAssessmentStatusesFromXml( node.selectNodes("loi:LearningObjectiveAssessmentStatus"))); } node = root.selectSingleNode(lElem + "/loi:ClientMasterySettings"); if (node.hasContent()) { result.setClientMasterySettings( getClientMasterySettingsHashMapFromXml(node.selectNodes("loi:MasterySettingsItem"))); } node = root.selectSingleNode(lElem + "/loi:ShowReportStatusForStudents"); if (node.hasContent()) { result.setShowReportStatusForStudents(Boolean.parseBoolean(node.getStringValue())); } node = root.selectSingleNode(lElem + "/loi:ConnectAssessmentCriteriaToScale"); if (node.hasContent()) { result.setConnectAssessmentCriteriaToScale(Boolean.parseBoolean(node.getStringValue())); } } return result; }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private void processRoot() { //root node parsing, root element is optional Node rootNode = doc.selectSingleNode("/log4j:configuration/root"); if (rootNode != null) { String rootContent = ""; Node priority = rootNode.selectSingleNode("./priority | ./level"); rootContent += priority.valueOf("@value").toUpperCase(); //appenders refernces List<Node> appendersRefs = rootNode.selectNodes("./appender-ref"); for (Node appender : appendersRefs) { rootContent += ", " + appender.valueOf("@ref"); }/*from ww w . j av a2 s.co m*/ String rootName = "log4j.rootLogger"; log4jProperties.setProperty(rootName, rootContent); //root params List<Node> rootParams = rootNode.selectNodes("./param"); if (!rootParams.isEmpty()) { for (Node param : rootParams) { log4jProperties.setProperty(rootName + "." + param.valueOf("@name"), param.valueOf("@value")); } } } }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private void processAppenders() { //appender nodes, optional List<Node> appenderNodes = doc.selectNodes("/log4j:configuration/appender"); if (!appenderNodes.isEmpty()) { String appenderName = null; for (Node appender : appenderNodes) { appenderName = "log4j.appender." + appender.valueOf("@name"); log4jProperties.setProperty(appenderName, appender.valueOf("@class")); //errorHandler, optional 0-1 Node errorHandler = appender.selectSingleNode("./errorHandler"); if (errorHandler != null) { String errorHandlerName = appenderName + ".errorhandler"; log4jProperties.setProperty(errorHandlerName, errorHandler.valueOf("@class")); //EH params, optional List<Node> eHParams = errorHandler.selectNodes("./param"); if (!eHParams.isEmpty()) { for (Node param : eHParams) { log4jProperties.setProperty(errorHandlerName + "." + param.valueOf("@name"), param.valueOf("@value")); }//from ww w . jav a 2 s . com } //EH root-ref, 0-1 Node rootRef = errorHandler.selectSingleNode("./root-ref"); if (rootRef != null) { log4jProperties.setProperty(errorHandlerName + ".root-ref", "true"); } //EH logger-ref, optional List<Node> eHLoggerRefs = errorHandler.selectNodes("./logger-ref"); if (!eHLoggerRefs.isEmpty()) { String loggersRefValue = eHLoggerRefs.get(0).valueOf("@ref"); for (int i = 1; i < eHLoggerRefs.size(); i++) { loggersRefValue += ", " + eHLoggerRefs.get(i).valueOf("@ref"); } log4jProperties.setProperty(errorHandlerName + ".logger-ref", loggersRefValue); } //EH appender-ref, 0-1 Node appenderRef = errorHandler.selectSingleNode("./appender-ref"); if (appenderRef != null) { log4jProperties.setProperty(errorHandlerName + ".appender-ref", appenderRef.valueOf("@ref")); } } //appender params, optional List<Node> appenderParams = appender.selectNodes("./param"); if (!appenderParams.isEmpty()) { for (Node param : appenderParams) { log4jProperties.setProperty(appenderName + "." + param.valueOf("@name"), param.valueOf("@value")); } } //appender layout, 0-1 Node appenderLayout = appender.selectSingleNode("./layout"); if (appenderLayout != null) { String appenderLayoutName = appenderName + ".layout"; log4jProperties.setProperty(appenderLayoutName, appenderLayout.valueOf("@class")); //AL params, optional List<Node> appenderLayoutParams = appenderLayout.selectNodes("./param"); if (!appenderLayoutParams.isEmpty()) { for (Node param : appenderLayoutParams) { log4jProperties.setProperty(appenderLayoutName + "." + param.valueOf("@name"), param.valueOf("@value")); } } } //appender filter, optional List<Node> appenderFilters = appender.selectNodes("./filter"); if (!appenderFilters.isEmpty()) { String appenderFilterName = ""; int filterCount = 0; for (Node filter : appenderFilters) { filterCount++; appenderFilterName = appenderName + ".filter." + filterCount; log4jProperties.setProperty(appenderFilterName, filter.valueOf("@class")); //filter params, optional List<Node> filterParams = filter.selectNodes("./param"); if (!filterParams.isEmpty()) { for (Node param : filterParams) { log4jProperties.setProperty(appenderFilterName + "." + param.valueOf("@name"), param.valueOf("@value")); } } } } //appender appender-refs, optional List<Node> appenderAppenderRefs = appender.selectNodes("./appender-ref"); if (!appenderAppenderRefs.isEmpty()) { String appenderRefsValue = appenderAppenderRefs.get(0).valueOf("@ref"); for (int i = 1; i < appenderAppenderRefs.size(); i++) { appenderRefsValue += ", " + appenderAppenderRefs.get(i).valueOf("@ref"); } log4jProperties.setProperty(appenderName + ".appender-ref", appenderRefsValue); } } } }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private void processCategories() { //category, optional List<Node> categoryNodes = doc.selectNodes("/log4j:configuration/category"); if (!categoryNodes.isEmpty()) { for (Node category : categoryNodes) { String categoryContent = ""; //(priority | level)? Node categoryPriority = category.selectSingleNode("./priority"); Node categoryLevel = category.selectSingleNode("./level"); if (categoryPriority != null) { categoryContent += categoryPriority.valueOf("@value").toUpperCase(); } else if (categoryLevel != null) { categoryContent += categoryLevel.valueOf("@value").toUpperCase(); }//from www . j a va 2s . co m //appender-refs, optional List<Node> categoryAppenderRefs = category.selectNodes("./appender-ref"); if (!categoryAppenderRefs.isEmpty()) { for (Node appenderRef : categoryAppenderRefs) { categoryContent += ", " + appenderRef.valueOf("@ref"); } } String categoryName = "log4j.category." + category.valueOf("@name"); log4jProperties.setProperty(categoryName, categoryContent); //params, optional List<Node> categoryParams = category.selectNodes("./param"); if (!categoryParams.isEmpty()) { for (Node param : categoryParams) { log4jProperties.setProperty(categoryName + "." + param.valueOf("@name"), param.valueOf("@value")); } } //category additivity if (category.valueOf("@additivity").equals("false")) { log4jProperties.setProperty("log4j.additivity." + category.valueOf("@name"), "false"); } } } }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private void processLoggers() { //logger, optional List<Node> loggerNodes = doc.selectNodes("/log4j:configuration/logger"); if (!loggerNodes.isEmpty()) { for (Node logger : loggerNodes) { String loggerContent = ""; //level, 0-1 Node loggerLevel = logger.selectSingleNode("./level"); if (loggerLevel != null) { loggerContent += loggerLevel.valueOf("@value").toUpperCase(); }//from w w w .j a v a 2 s .c om //appender-refs, optional List<Node> loggerAppenderRefs = logger.selectNodes("./appender-ref"); if (!loggerAppenderRefs.isEmpty()) { for (Node appenderRef : loggerAppenderRefs) { loggerContent += ", " + appenderRef.valueOf("@ref"); } } String loggerName = "log4j.logger." + logger.valueOf("@name"); log4jProperties.setProperty(loggerName, loggerContent); //logger additivity if (logger.valueOf("@additivity").equals("false")) { log4jProperties.setProperty("log4j.additivity." + logger.valueOf("@name"), "false"); } } } }
From source file:main.FrevoMain.java
License:Open Source License
/** * Loads FREVO properties from the configuration XML file. If the XML file * is not present then a default configuration will be loaded creating a new * configuration file.//from www . ja va 2 s.c o m */ //private public static void loadProperties() { try { Document doc = null; // Access XML file File documentFile = new File(FREVO_INSTALL_DIRECTORY + "/frevo_config.xml"); doc = SafeSAX.read(documentFile, false); // load keywords problemKeywordList = new ArrayList<KeywordCategory>(); methodKeywordList = new ArrayList<KeywordCategory>(); representationKeywordList = new ArrayList<KeywordCategory>(); rankingKeywordList = new ArrayList<KeywordCategory>(); Node keywordcategories = doc.selectSingleNode("/frevo/tags"); // Collect problem keywords List<?> items = keywordcategories.selectNodes(".//problemtag"); Iterator<?> it = items.iterator(); while (it.hasNext()) { Element el = (Element) it.next(); String name = el.valueOf("./@name"); String keywords = el.valueOf("./@tags"); String imagename = el.valueOf("./@image"); ArrayList<String> keywordlist = new ArrayList<String>(); Scanner sc = new Scanner(keywords); sc.useDelimiter(","); while (sc.hasNext()) keywordlist.add(sc.next()); problemKeywordList.add(new KeywordCategory(name, keywordlist, imagename)); sc.close(); } // Collect method keywords items = keywordcategories.selectNodes(".//methodtag"); it = items.iterator(); while (it.hasNext()) { Element el = (Element) it.next(); String name = el.valueOf("./@name"); String keywords = el.valueOf("./@tags"); String imagename = el.valueOf("./@image"); ArrayList<String> keywordlist = new ArrayList<String>(); Scanner sc = new Scanner(keywords); sc.useDelimiter(","); while (sc.hasNext()) keywordlist.add(sc.next()); methodKeywordList.add(new KeywordCategory(name, keywordlist, imagename)); sc.close(); } // representation keywords items = keywordcategories.selectNodes(".//representationtag"); it = items.iterator(); while (it.hasNext()) { Element el = (Element) it.next(); String name = el.valueOf("./@name"); String keywords = el.valueOf("./@tags"); String imagename = el.valueOf("./@image"); ArrayList<String> keywordlist = new ArrayList<String>(); Scanner sc = new Scanner(keywords); sc.useDelimiter(","); while (sc.hasNext()) keywordlist.add(sc.next()); representationKeywordList.add(new KeywordCategory(name, keywordlist, imagename)); sc.close(); } // ranking keywords items = keywordcategories.selectNodes(".//rankingtag"); it = items.iterator(); while (it.hasNext()) { Element el = (Element) it.next(); String name = el.valueOf("./@name"); String keywords = el.valueOf("./@tags"); String imagename = el.valueOf("./@image"); ArrayList<String> keywordlist = new ArrayList<String>(); Scanner sc = new Scanner(keywords); sc.useDelimiter(","); while (sc.hasNext()) keywordlist.add(sc.next()); rankingKeywordList.add(new KeywordCategory(name, keywordlist, imagename)); sc.close(); } // load preferred window size Node windowsizes = doc.selectSingleNode("/frevo/window-sizes"); items = windowsizes.selectNodes(".//window"); it = items.iterator(); while (it.hasNext()) { Element el = (Element) it.next(); String name = el.valueOf("./@name"); if (name.equals("main")) { String width = el.valueOf("./@width"); String height = el.valueOf("./@height"); mainWindowParameters[0] = Integer.parseInt(width); mainWindowParameters[1] = Integer.parseInt(height); } else if (name.equals("componentbrowser")) { String width = el.valueOf("./@width"); String height = el.valueOf("./@height"); String topsplit = el.valueOf("./@topsplit"); String bigsplit = el.valueOf("./@bigsplit"); componentBrowserParameters[0] = Integer.parseInt(width); componentBrowserParameters[1] = Integer.parseInt(height); componentBrowserParameters[2] = Integer.parseInt(topsplit); componentBrowserParameters[3] = Integer.parseInt(bigsplit); } } } catch (Exception e) { System.err.println("No config file found, using defaults"); } }
From source file:main.FrevoMain.java
License:Open Source License
/** * Loads the give session file to FREVO. In case of error the components * will not be selected in FREVO.//from ww w . ja va 2 s .co m * * @param loadFile * The session file to load. */ public static Document loadSession(File loadFile) { // loading session Document doc = null; // indicate if error occurs boolean error = false; // load XML tree from file doc = SafeSAX.read(loadFile, true); ProblemXMLData problem = null; ComponentXMLData method = null; ComponentXMLData representation = null; ComponentXMLData ranking = null; try { // Load configuration section Node nd = doc.selectSingleNode("/frevo/sessionconfig"); if (nd == null) { System.err.println("Warning: No config keywords found!"); } else { // load configuration List<?> npops = nd.selectNodes(".//configentry"); Iterator<?> it = npops.iterator(); // load all keys and values while (it.hasNext()) { Element el = (Element) it.next(); String key = el.valueOf("./@key"); String value = el.valueOf("./@value"); if (key.equals("CustomName")) { FrevoMain.customName = value; } else if (key.equals("NumberofRuns")) { FrevoMain.setNumberOfSimulationRuns(Integer.parseInt(value)); } else if (key.equals("StartingSeed")) { FrevoMain.setInitialSeed(Long.parseLong(value)); } } } // Load problem Node problemnode = doc.selectSingleNode("/frevo/problem"); // select appropriate component String classname = problemnode.valueOf("./@class"); problem = (ProblemXMLData) getComponent(ComponentType.FREVO_PROBLEM, classname); if (problem == null) { error = true; System.err.println("ERROR: No such problem component found: " + classname); } if (!error) { // load configuration List<?> pentries = problemnode.selectNodes(".//problementry"); Iterator<?> it = pentries.iterator(); // set problem properties while (it.hasNext()) { Element el = (Element) it.next(); String key = el.valueOf("./@key"); String value = el.valueOf("./@value"); XMLFieldEntry pe = problem.getProperties().get(key); if (pe != null) pe.setValue(value); } } // Load method Node methodnode = doc.selectSingleNode("/frevo/method"); // select appropriate component classname = methodnode.valueOf("./@class"); method = getComponent(ComponentType.FREVO_METHOD, classname); if (method == null) { if (classname.equals("")) { System.err.println("ERROR: No class entry found to load the method component!"); error = true; } else { error = true; System.err.println("ERROR: No such method component found: " + classname); } } else { // load configuration List<?> mentries = methodnode.selectNodes(".//methodentry"); Iterator<?> mit = mentries.iterator(); while (mit.hasNext()) { Element el = (Element) mit.next(); String key = el.valueOf("./@key"); String value = el.valueOf("./@value"); XMLFieldEntry pe = method.getProperties().get(key); if (pe != null) pe.setValue(value); } } // Load representation Node representationnode = doc.selectSingleNode("/frevo/representation"); // select appropriate component classname = representationnode.valueOf("./@class"); representation = getComponent(ComponentType.FREVO_REPRESENTATION, classname); if (representation == null) { if (classname.equals("")) { System.err.println("ERROR: No class entry found to load the representation component!"); error = true; } else { error = true; System.err.println("ERROR: No such representation component found: " + classname); } } else { // load configuration List<?> rentries = representationnode.selectNodes(".//representationentry"); Iterator<?> rit = rentries.iterator(); while (rit.hasNext()) { Element el = (Element) rit.next(); String key = el.valueOf("./@key"); String value = el.valueOf("./@value"); XMLFieldEntry pe = representation.getProperties().get(key); if (pe != null) pe.setValue(value); } // load core representation if (representation.getComponentType() == ComponentType.FREVO_BULKREPRESENTATION) { String corerepresentationclassname = representation.getProperties() .get("core_representation_component").getValue(); ComponentXMLData coreRepresentation = FrevoMain.getComponent(ComponentType.FREVO_REPRESENTATION, corerepresentationclassname); if (coreRepresentation == null) { System.err.println( "ERROR: Could not load specified representation: " + corerepresentationclassname); } else { rentries = representationnode.selectNodes(".//corerepresentationentry"); rit = rentries.iterator(); while (rit.hasNext()) { Element el = (Element) rit.next(); String key = el.valueOf("./@key"); String value = el.valueOf("./@value"); XMLFieldEntry pe = coreRepresentation.getProperties().get(key); if (pe != null) pe.setValue(value); } } } } // Load ranking Node rankingnode = doc.selectSingleNode("/frevo/ranking"); // select appropriate component classname = rankingnode.valueOf("./@class"); ranking = getComponent(ComponentType.FREVO_RANKING, classname); if (ranking == null) { if (classname.equals("")) { System.err.println("ERROR: No class entry found to load the ranking component!"); error = true; } else { System.err.println("ERROR: No such ranking component found: " + classname); error = true; } } else { // load configuration List<?> raentries = rankingnode.selectNodes(".//rankingentry"); Iterator<?> rait = raentries.iterator(); while (rait.hasNext()) { Element el = (Element) rait.next(); String key = el.valueOf("./@key"); String value = el.valueOf("./@value"); XMLFieldEntry pe = ranking.getProperties().get(key); if (pe != null) pe.setValue(value); } } // adjust GUI if (isFrevoWithGraphics()) { mainWindow.updateAdvancedLabels(); } } catch (OutOfMemoryError mem) { System.err.println("ERROR: Could not import! (Out of memory)"); error = true; } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException\n" + e.getMessage()); error = true; } if (error) { System.err.println("Error while importing!"); } else { SELECTED_PROBLEM = problem; SELECTED_METHOD = method; SELECTED_REPRESENTATION = representation; SELECTED_RANKING = ranking; } return doc; }
From source file:main.FrevoMain.java
License:Open Source License
/** * Returns the representation at the given rank loaded from an results XML * file./*from ww w .j a v a 2 s .com*/ * * @param loadfile * The XML results file to be loaded. * @param rank * The position of the requested representation in the XML * results file. * @return A representation at the given position from the XML results file. */ public static AbstractRepresentation getRepresentation(File loadfile, int rank) { // load session loadSession(loadfile); ComponentXMLData representation = SELECTED_REPRESENTATION; AbstractRepresentation net2h = null; Document doc = SafeSAX.read(loadfile, true); try { Node dpopulations = doc.selectSingleNode("/frevo/populations"); List<?> npops = dpopulations.selectNodes(".//population"); Iterator<?> it = npops.iterator(); npops.get(rank); while (it.hasNext()) { Node nd = (Node) it.next(); List<?> npops1 = nd.selectNodes("./*"); Iterator<?> it1 = npops1.iterator(); Node net = (Node) it1.next(); net2h = representation.getNewRepresentationInstance(0, 0, null); net2h.loadFromXML(net); return net2h.clone(); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:main.FrevoMain.java
License:Open Source License
/** * Saves FREVO settings to the configuration file. Initiates file writing to * store data./*w w w . jav a2s. c om*/ */ public static void saveSettings() { Document doc; File configfile = new File(FREVO_INSTALL_DIRECTORY + "/frevo_config.xml"); if (configfile.exists()) { // load data if it already exists doc = SafeSAX.read(configfile, true); Node windowsizes = doc.selectSingleNode("/frevo/window-sizes"); List<?> items = windowsizes.selectNodes(".//window"); Iterator<?> it = items.iterator(); while (it.hasNext()) { Element el = (Element) it.next(); String name = el.valueOf("./@name"); // save main data if (name.equals("main")) { el.addAttribute("width", Integer.toString(FrevoMain.mainWindowParameters[0])); el.addAttribute("height", Integer.toString(FrevoMain.mainWindowParameters[1])); } // save component browser data if (name.equals("componentbrowser")) { el.addAttribute("width", Integer.toString(FrevoMain.componentBrowserParameters[0])); el.addAttribute("height", Integer.toString(FrevoMain.componentBrowserParameters[1])); el.addAttribute("topsplit", Integer.toString(FrevoMain.componentBrowserParameters[2])); el.addAttribute("bigsplit", Integer.toString(FrevoMain.componentBrowserParameters[3])); } } } else { try { configfile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } // file does not exist doc = DocumentHelper.createDocument(); doc.addDocType("frevo", null, "config.dtd"); Element dfrevo = doc.addElement("frevo"); dfrevo.addElement("tags"); Element windowsizes = dfrevo.addElement("window-sizes"); Element mainwindow = windowsizes.addElement("window"); mainwindow.addAttribute("name", "main"); mainwindow.addAttribute("width", Integer.toString(FrevoMain.mainWindowParameters[0])); mainwindow.addAttribute("height", Integer.toString(FrevoMain.mainWindowParameters[1])); Element compwindow = windowsizes.addElement("window"); compwindow.addAttribute("name", "componentbrowser"); compwindow.addAttribute("width", Integer.toString(FrevoMain.componentBrowserParameters[0])); compwindow.addAttribute("height", Integer.toString(FrevoMain.componentBrowserParameters[1])); compwindow.addAttribute("topsplit", Integer.toString(FrevoMain.componentBrowserParameters[2])); compwindow.addAttribute("bigsplit", Integer.toString(FrevoMain.componentBrowserParameters[3])); } // write to file try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setLineSeparator(System.getProperty("line.separator")); configfile.createNewFile(); FileWriter out = new FileWriter(configfile); BufferedWriter bw = new BufferedWriter(out); XMLWriter wr = new XMLWriter(bw, format); wr.write(doc); wr.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } }
From source file:neat.NEAT.java
License:Open Source License
public NEAT(File xmlfile, int popid) { // loads properties super(xmlfile); // get population root node Document doc = SafeSAX.read(xmlfile, true); Node dpopulations = doc.selectSingleNode("/frevo/population"); List<?> nets = dpopulations.selectNodes(".//NEAT"); // Node pop = (Node) npops.get(popid); //List<?> nets = pop.selectNodes("./*"); // return the one with the highest fitness int bestID = 0; Node net = (Node) nets.get(bestID); String fitnessString = net.valueOf("./@fitness"); double bestfitness = Double.parseDouble(fitnessString); for (int i = 1; i < nets.size(); i++) { net = (Node) nets.get(i); fitnessString = net.valueOf("./@fitness"); double fitness = Double.parseDouble(fitnessString); if (fitness > bestfitness) { bestID = i;//www. ja v a 2s . c om bestfitness = fitness; } } loadFromXML((Node) nets.get(bestID)); }