Java Code Examples for org.dom4j.Element#selectNodes()
The following examples show how to use
org.dom4j.Element#selectNodes() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OutcomesProcessingParser.java From olat with Apache License 2.0 | 6 votes |
/** */ @Override public Object parse(final Element element) { // assert element.getName().equalsIgnoreCase("outcomes_processing"); final OutcomesProcessing outcomesProcessing = new OutcomesProcessing(); final List decvars = element.selectNodes("*/decvar"); if (decvars.size() == 0) { return outcomesProcessing; } final Element decvar = (Element) decvars.get(0); for (final Iterator iter = decvar.attributeIterator(); iter.hasNext();) { final Attribute attr = (Attribute) iter.next(); outcomesProcessing.setField(attr.getName(), attr.getValue()); } return outcomesProcessing; }
Example 2
Source File: JUnitReportGenerator.java From gocd with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { Document doc = new SAXReader().read(new FileInputStream(new File("/home/cruise/sample_junit.xml"))); Element suite = (Element) doc.selectSingleNode("//testsuite"); Element rootElement = doc.getRootElement(); for (int i = 0; i < 50000; i++) { Element copy = suite.createCopy(); setAttr(i, copy, "name"); setAttr(i, copy, "hostname"); List<Element> elements = copy.selectNodes(".//testcase"); for (Element element : elements) { setAttr(i, element, "classname"); setAttr(i, element, "name"); } rootElement.add(copy); } FileUtils.writeStringToFile(new File("/tmp/repo/imagine.xml"), doc.asXML(), UTF_8); }
Example 3
Source File: OutcomesProcessingParser.java From olat with Apache License 2.0 | 6 votes |
/** */ @Override public Object parse(final Element element) { // assert element.getName().equalsIgnoreCase("outcomes_processing"); final OutcomesProcessing outcomesProcessing = new OutcomesProcessing(); final List decvars = element.selectNodes("*/decvar"); if (decvars.size() == 0) { return outcomesProcessing; } final Element decvar = (Element) decvars.get(0); for (final Iterator iter = decvar.attributeIterator(); iter.hasNext();) { final Attribute attr = (Attribute) iter.next(); outcomesProcessing.setField(attr.getName(), attr.getValue()); } return outcomesProcessing; }
Example 4
Source File: RunnableState.java From bulbasaur with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public StateLike parse(Element elem) { require(elem.attributeValue("name") != null, "attribute 'name' in node is required"); stateName = elem.attributeValue("name"); stateAlias = elem.attributeValue("alias"); // 得到所有该节点下的 paths 下的 path List<Element> childPaths = elem.selectNodes(PATH_TAG); for (Element node : childPaths) { require(node.attributeValue("to") != null, "attribute to in path node is required"); String pathTo = node.attributeValue("to"); String expr = null; if (node.attributeValue("expr") != null) { expr = node.attributeValue("expr"); } // 新实例一个path,并加入到paths中 Path pt = new Path(pathTo, expr); // 添加到path 列表中 paths.add(pt); } return this; }
Example 5
Source File: QTIEditHelperEBL.java From olat with Apache License 2.0 | 6 votes |
/** * Fetch choices. * @param response_labels * @return Map of choices. */ public static List<Response> fetchChoices(List response_labels) { List<Response> choices = new ArrayList<Response>(); for (Iterator i = response_labels.iterator(); i.hasNext();) { ChoiceResponse choice = new ChoiceResponse(); Element response_label = (Element) i.next(); choice.setIdent(response_label.attributeValue("ident")); List materials = response_label.selectNodes(".//material"); Material content = new Material(); for (Iterator iter = materials.iterator(); iter.hasNext();) { Element el_material = (Element) iter.next(); Material mat = (Material) parserManager.parse(el_material); content.getElements().addAll(mat.getElements()); } // assure material always has some content if (content.getElements().size() == 0) content.getElements().add(new Mattext("[blank]")); choice.setContent(content); choices.add(choice); } return choices; }
Example 6
Source File: XmlHelper.java From atlas with Apache License 2.0 | 6 votes |
public static void removeStringValue(File file, String key) throws IOException, DocumentException { if (!file.exists()) { return; } Document document = XmlHelper.readXml(file);// Read the XML file Element root = document.getRootElement();// Get the root node List<? extends Node> nodes = root.selectNodes("//string"); for (Node node : nodes) { Element element = (Element)node; String name = element.attributeValue("name"); if (key.equals(name)) { element.getParent().remove(element); break; } } // sLogger.warn("[resxmlediter] add " + key + " to " + file.getAbsolutePath()); XmlHelper.saveDocument(document, file); }
Example 7
Source File: SecurityServiceImpl.java From studio with GNU General Public License v3.0 | 5 votes |
@SuppressWarnings("unchecked") protected void loadRoles(Element root, PermissionsConfigTO config) { if (root.getName().equals(StudioXmlConstants.DOCUMENT_ROLE_MAPPINGS)) { Map<String, List<String>> rolesMap = new HashMap<String, List<String>>(); List<Node> userNodes = root.selectNodes(StudioXmlConstants.DOCUMENT_ELM_USER_NODE); rolesMap = getRoles(userNodes, rolesMap); List<Node> groupNodes = root.selectNodes(StudioXmlConstants.DOCUMENT_ELM_GROUPS_NODE); rolesMap = getRoles(groupNodes, rolesMap); config.setRoles(rolesMap); } }
Example 8
Source File: QTIMetadataParser.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public Object parse(final Element element) { // assert element.getName().equalsIgnoreCase("qtimetadata"); final Metadata meta = new Metadata(); final List metadatafields = element.selectNodes("./qtimetadatafield"); for (final Iterator iter = metadatafields.iterator(); iter.hasNext();) { final Element metadatafield = (Element) iter.next(); final String key = metadatafield.elementText("fieldlabel"); final String value = metadatafield.elementText("fieldentry"); meta.setField(key, value); } return meta; }
Example 9
Source File: SelectionOrderingParser.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public Object parse(final Element element) { // assert element.getName().equalsIgnoreCase("selection_ordering"); final SelectionOrdering selectionOrdering = new SelectionOrdering(); // Set correct selection of items. To select all is the default value (represented // by a non existing 'selection_number' element. Otherwhise the exact number given // in the 'selection_number' element is taken') final List el_selections = element.selectNodes("selection"); if (el_selections.size() > 1) { // error throw new OLATRuntimeException(SelectionOrderingParser.class, "more then one selection element", new IllegalStateException()); } if (el_selections.size() != 0) { final Element el_selection = ((Element) el_selections.get(0)); final Element selection_number = (Element) el_selection.selectSingleNode("selection_number"); if (selection_number != null) { selectionOrdering.setSelectionNumber(Integer.parseInt(selection_number.getText())); } } // else use default value // Set correct order type. Use sequential ordering als default if none defined final Element order = (Element) element.selectSingleNode("//order"); if (order != null) { final String order_type = order.attributeValue(SelectionOrdering.ORDER_TYPE); if (order_type != null && order_type.equals(SelectionOrdering.RANDOM)) { selectionOrdering.setOrderType(SelectionOrdering.RANDOM); // else use default value } } return selectionOrdering; }
Example 10
Source File: QTIHelper.java From olat with Apache License 2.0 | 5 votes |
/** * */ public static Variables declareVariables(final Element el_outcomes) { String varName; final Variables variables = new Variables(); if (el_outcomes == null) { return variables; } final List decvars = el_outcomes.selectNodes("decvar"); /* * <decvar defaultval = "0" varname = "Var_SumofScores" vartype = "Integer" minvalue = "-10" maxvalue = "10" cutvalue = "0"/> <decvar minvalue = "0" maxvalue = * "1" defaultval = "0"/> */ for (final Iterator iter = decvars.iterator(); iter.hasNext();) { final Element decvar = (Element) iter.next(); varName = decvar.attributeValue("varname"); // dtd CDATA 'SCORE' if (varName == null) { varName = "SCORE"; } String varType = decvar.attributeValue("vartype"); if (varType == null) { varType = "Integer"; // default } Variable v = null; if (varType.equals("Integer") || varType.equals("Decimal")) { final String def = decvar.attributeValue("defaultval"); final String min = decvar.attributeValue("minvalue"); final String max = decvar.attributeValue("maxvalue"); final String cut = decvar.attributeValue("cutvalue"); v = new DecimalVariable(varName, max, min, cut, def); variables.setVariable(v); } else { throw new RuntimeException("vartype " + varType + " not supported (declaration)"); } } return variables; }
Example 11
Source File: QTI_resprocessing.java From olat with Apache License 2.0 | 5 votes |
/** * ims qti 1.2.1 <!ELEMENT resprocessing (qticomment? , outcomes , respcondition+)> also <!ELEMENT respcondition (qticomment? , conditionvar , setvar* , * displayfeedback*)> <!ATTLIST respcondition %I_Continue; %I_Title; > mit <!ENTITY % I_Continue " continue (Yes | No ) 'No'"> <!ELEMENT conditionvar (not | and | * or | unanswered | other | varequal | varlt | varlte | vargt | vargte | varsubset | varinside | varsubstring | durequal | durlt | durlte | durgt | durgte)+> * <!ELEMENT outcomes (qticomment? , (decvar , interpretvar*)+)> <!ELEMENT decvar (#PCDATA)> <!ATTLIST decvar %I_VarName; vartype (Integer | String | Decimal | * Scientific | Boolean | Enumerated | Set ) 'Integer' defaultval CDATA #IMPLIED minvalue CDATA #IMPLIED maxvalue CDATA #IMPLIED members CDATA #IMPLIED cutvalue CDATA * #IMPLIED > * * @param n * @param userContext */ public void evalAnswer(final Element n, final ItemContext userContext) { // first initialize all variables as stated in decvar elements userContext.resetVariables(); // now eval all respconditions as long as continue="Yes" final QTI_respcondition respCondition = QTIHelper.getQTI_respcondition(); final List el_respconditions = n.selectNodes("respcondition"); final int size = el_respconditions.size(); boolean cont = true; int i = 0; boolean hasBeenTrue = false; final EvalContext ect = new EvalContext(); ect.setHasBeenTrue(hasBeenTrue); while (cont && i < size) { final Element el_respcond = (Element) el_respconditions.get(i); final String str_continue = el_respcond.attributeValue("continue"); cont = (str_continue == null) ? false : str_continue.equals("Yes"); // default: No // eval conditions and set vars accordingly final boolean res = respCondition.process(el_respcond, userContext, ect); if (!res) { cont = true; // continue flag only effective if previous didn't match } hasBeenTrue = hasBeenTrue || res; ect.setHasBeenTrue(hasBeenTrue); i++; } }
Example 12
Source File: QTIMetadataParser.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public Object parse(final Element element) { // assert element.getName().equalsIgnoreCase("qtimetadata"); final Metadata meta = new Metadata(); final List metadatafields = element.selectNodes("./qtimetadatafield"); for (final Iterator iter = metadatafields.iterator(); iter.hasNext();) { final Element metadatafield = (Element) iter.next(); final String key = metadatafield.elementText("fieldlabel"); final String value = metadatafield.elementText("fieldentry"); meta.setField(key, value); } return meta; }
Example 13
Source File: QTI_resprocessing.java From olat with Apache License 2.0 | 5 votes |
/** * ims qti 1.2.1 <!ELEMENT resprocessing (qticomment? , outcomes , respcondition+)> also <!ELEMENT respcondition (qticomment? , conditionvar , setvar* , * displayfeedback*)> <!ATTLIST respcondition %I_Continue; %I_Title; > mit <!ENTITY % I_Continue " continue (Yes | No ) 'No'"> <!ELEMENT conditionvar (not | and | * or | unanswered | other | varequal | varlt | varlte | vargt | vargte | varsubset | varinside | varsubstring | durequal | durlt | durlte | durgt | durgte)+> * <!ELEMENT outcomes (qticomment? , (decvar , interpretvar*)+)> <!ELEMENT decvar (#PCDATA)> <!ATTLIST decvar %I_VarName; vartype (Integer | String | Decimal | * Scientific | Boolean | Enumerated | Set ) 'Integer' defaultval CDATA #IMPLIED minvalue CDATA #IMPLIED maxvalue CDATA #IMPLIED members CDATA #IMPLIED cutvalue CDATA * #IMPLIED > * * @param n * @param userContext */ public void evalAnswer(final Element n, final ItemContext userContext) { // first initialize all variables as stated in decvar elements userContext.resetVariables(); // now eval all respconditions as long as continue="Yes" final QTI_respcondition respCondition = QTIHelper.getQTI_respcondition(); final List el_respconditions = n.selectNodes("respcondition"); final int size = el_respconditions.size(); boolean cont = true; int i = 0; boolean hasBeenTrue = false; final EvalContext ect = new EvalContext(); ect.setHasBeenTrue(hasBeenTrue); while (cont && i < size) { final Element el_respcond = (Element) el_respconditions.get(i); final String str_continue = el_respcond.attributeValue("continue"); cont = (str_continue == null) ? false : str_continue.equals("Yes"); // default: No // eval conditions and set vars accordingly final boolean res = respCondition.process(el_respcond, userContext, ect); if (!res) { cont = true; // continue flag only effective if previous didn't match } hasBeenTrue = hasBeenTrue || res; ect.setHasBeenTrue(hasBeenTrue); i++; } }
Example 14
Source File: ConfigurationServiceImpl.java From studio with GNU General Public License v3.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public Map<String, List<String>> geRoleMappings(String siteId) throws ConfigurationException { // TODO: Refactor this to use Apache's Commons Configuration Map<String, List<String>> roleMappings = new HashMap<>(); String roleMappingsConfigPath = getSiteRoleMappingsConfigPath(siteId); Document document; try { document = contentService.getContentAsDocument(siteId, roleMappingsConfigPath); if (document != null) { Element root = document.getRootElement(); if (root.getName().equals(DOCUMENT_ROLE_MAPPINGS)) { List<Node> groupNodes = root.selectNodes(DOCUMENT_ELM_GROUPS_NODE); for (Node node : groupNodes) { String name = node.valueOf(DOCUMENT_ATTR_PERMISSIONS_NAME); if (StringUtils.isNotEmpty(name)) { List<Node> roleNodes = node.selectNodes(DOCUMENT_ELM_PERMISSION_ROLE); List<String> roles = new ArrayList<>(); for (Node roleNode : roleNodes) { roles.add(roleNode.getText()); } roleMappings.put(name, roles); } } } } } catch (DocumentException e) { throw new ConfigurationException("Error while reading role mappings file for site " + siteId + " @ " + roleMappingsConfigPath); } return roleMappings; }
Example 15
Source File: ItemParser.java From olat with Apache License 2.0 | 5 votes |
private String getMaterialAsString(final Element el_root) { final StringBuilder result = new StringBuilder(); final List materials = el_root.selectNodes(".//mattext"); for (final Iterator iter = materials.iterator(); iter.hasNext();) { final Element el_mattext = (Element) iter.next(); result.append(el_mattext.getTextTrim() + "\n"); } return result.toString(); }
Example 16
Source File: FeedbackParser.java From olat with Apache License 2.0 | 4 votes |
/** */ @Override public Object parse(final Element element) { // assert element.getName().equalsIgnoreCase("sectionfeedback") // || element.getName().equalsIgnoreCase("itemfeedback") // || element.getName().equalsIgnoreCase("assessmentfeedback"); final List materialsXML = element.selectNodes(".//material"); if (materialsXML.size() == 0) { return null; } final Feedback feedback = new Feedback(); // attributes Attribute tmp = element.attribute("ident"); if (tmp != null) { feedback.setIdent(tmp.getValue()); } tmp = element.attribute("title"); if (tmp != null) { feedback.setTitle(tmp.getValue()); } tmp = element.attribute("view"); if (tmp != null) { feedback.setView(tmp.getValue()); } // get type if (element.element("solution") != null) { return null; } else if (element.element("hint") != null) { return null; } // parse Material // MATERIAL final List materials = new ArrayList(); for (final Iterator i = materialsXML.iterator(); i.hasNext();) { materials.add(parserManager.parse((Element) i.next())); } feedback.setMaterials(materials); return feedback; }
Example 17
Source File: TestFileResourceValidator.java From olat with Apache License 2.0 | 4 votes |
@Override public boolean validate(final File unzippedDir) { // with VFS FIXME:pb:c: remove casts to LocalFileImpl and LocalFolderImpl if // no longer needed. final VFSContainer vfsUnzippedRoot = new LocalFolderImpl(unzippedDir); final VFSItem vfsQTI = vfsUnzippedRoot.resolve("qti.xml"); // getDocument(..) ensures that InputStream is closed in every case. final Document doc = QTIHelper.getDocument((LocalFileImpl) vfsQTI); // if doc is null an error loading the document occured if (doc == null) { return false; } // check if this is marked as test final List metas = doc.selectNodes("questestinterop/assessment/qtimetadata/qtimetadatafield"); for (final Iterator iter = metas.iterator(); iter.hasNext();) { final Element el_metafield = (Element) iter.next(); final Element el_label = (Element) el_metafield.selectSingleNode("fieldlabel"); final String label = el_label.getText(); if (label.equals(AssessmentInstance.QMD_LABEL_TYPE)) { // type meta final Element el_entry = (Element) el_metafield.selectSingleNode("fieldentry"); final String entry = el_entry.getText(); if (!(entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_SELF) || entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_ASSESS))) { return false; } } } // check if at least one section with one item final List sectionItems = doc.selectNodes("questestinterop/assessment/section/item"); if (sectionItems.size() == 0) { return false; } for (final Iterator iter = sectionItems.iterator(); iter.hasNext();) { final Element it = (Element) iter.next(); final List sv = it.selectNodes("resprocessing/outcomes/decvar[@varname='SCORE']"); // the QTIv1.2 system relies on the SCORE variable of items if (sv.size() != 1) { return false; } } return true; }
Example 18
Source File: TestFileResourceValidator.java From olat with Apache License 2.0 | 4 votes |
@Override public boolean validate(final File unzippedDir) { // with VFS FIXME:pb:c: remove casts to LocalFileImpl and LocalFolderImpl if // no longer needed. final VFSContainer vfsUnzippedRoot = new LocalFolderImpl(unzippedDir); final VFSItem vfsQTI = vfsUnzippedRoot.resolve("qti.xml"); // getDocument(..) ensures that InputStream is closed in every case. final Document doc = QTIHelper.getDocument((LocalFileImpl) vfsQTI); // if doc is null an error loading the document occured if (doc == null) { return false; } // check if this is marked as test final List metas = doc.selectNodes("questestinterop/assessment/qtimetadata/qtimetadatafield"); for (final Iterator iter = metas.iterator(); iter.hasNext();) { final Element el_metafield = (Element) iter.next(); final Element el_label = (Element) el_metafield.selectSingleNode("fieldlabel"); final String label = el_label.getText(); if (label.equals(AssessmentInstance.QMD_LABEL_TYPE)) { // type meta final Element el_entry = (Element) el_metafield.selectSingleNode("fieldentry"); final String entry = el_entry.getText(); if (!(entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_SELF) || entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_ASSESS))) { return false; } } } // check if at least one section with one item final List sectionItems = doc.selectNodes("questestinterop/assessment/section/item"); if (sectionItems.size() == 0) { return false; } for (final Iterator iter = sectionItems.iterator(); iter.hasNext();) { final Element it = (Element) iter.next(); final List sv = it.selectNodes("resprocessing/outcomes/decvar[@varname='SCORE']"); // the QTIv1.2 system relies on the SCORE variable of items if (sv.size() != 1) { return false; } } return true; }
Example 19
Source File: ItemWithResponseStr.java From olat with Apache License 2.0 | 4 votes |
/** * Constructor for ItemWithResponseLid. * * @param el_item */ public ItemWithResponseStr(final Element el_item) { // CELFI#107 this.itemTitle = el_item.attributeValue("title"); this.itemIdent = el_item.attributeValue("ident"); final Element decvar = (Element) el_item.selectSingleNode(".//outcomes/decvar"); if (decvar != null) { this.itemMinValue = decvar.attributeValue("minvalue"); this.itemMaxValue = decvar.attributeValue("maxvalue"); this.itemCutValue = decvar.attributeValue("cutvalue"); } final List el_presentationElements = el_item.selectNodes(".//presentation//mattext | .//presentation//response_str"); int i = 1; boolean lastWasMattext = false; for (final Iterator itPresentations = el_presentationElements.iterator(); itPresentations.hasNext();) { final Element el_presentation = (Element) itPresentations.next(); final String el_qname = el_presentation.getQualifiedName(); if (el_qname.equalsIgnoreCase("mattext")) { this.quetionText += el_presentation.getTextTrim(); lastWasMattext = true; } else { responseStrIdents.add(el_presentation.attributeValue("ident")); final Element render_fib = el_presentation.element("render_fib"); if (render_fib != null) { isEssay = (render_fib.attributeValue("rows") == null) ? false : true; responseColumnHeaders.add((isEssay ? "A" : "B") + i); // A -> Area, B -> Blank final Element responseValue = (Element) el_item.selectSingleNode(".//varequal[@respident='" + el_presentation.attributeValue("ident") + "']"); if (responseValue != null) { responseLabelMaterials.add(responseValue.getTextTrim()); if (lastWasMattext) { this.quetionText += " [" + responseValue.getTextTrim() + "] "; lastWasMattext = false; } } else { responseLabelMaterials.add(""); } } else { responseColumnHeaders.add("unknownType"); responseLabelMaterials.add(""); } i++; } } // CELFI#107 END }
Example 20
Source File: ItemWithResponseLid.java From olat with Apache License 2.0 | 4 votes |
/** * Constructor for ItemWithResponseLid. * * @param el_item */ public ItemWithResponseLid(final Element el_item) { this.itemTitle = el_item.attributeValue("title"); this.itemIdent = el_item.attributeValue("ident"); final List<Element> responseLids = el_item.selectNodes(".//response_lid"); // Question text // CELFI#107 final Node temp = el_item.selectSingleNode(".//presentation/material/mattext"); if (temp != null) { this.questionText = ((Element) temp).getTextTrim(); } int i = 1; for (final Iterator<Element> itresponseLid = responseLids.iterator(); itresponseLid.hasNext();) { final Element el_responseLid = itresponseLid.next(); isSingle = el_responseLid.attributeValue("rcardinality").equals("Single"); final List<Element> labels = el_responseLid.selectNodes(".//response_label"); final Element decvar = (Element) el_item.selectSingleNode(".//outcomes/decvar"); if (decvar != null) { this.itemMinValue = decvar.attributeValue("minvalue"); this.itemMaxValue = decvar.attributeValue("maxvalue"); this.itemCutValue = decvar.attributeValue("cutvalue"); } for (final Iterator<Element> itlabel = labels.iterator(); itlabel.hasNext();) { final Element el_label = itlabel.next(); final String sIdent = el_label.attributeValue("ident"); responseLabelIdents.add(sIdent); final List<Element> materials = el_label.selectNodes(".//mattext"); final StringBuilder mat = new StringBuilder(); for (final Iterator<Element> itmaterial = materials.iterator(); itmaterial.hasNext();) { final Element el_material = itmaterial.next(); mat.append(el_material.getText()); } responseLabelMaterials.add(mat.length() == 0 ? "IDENT: " + sIdent : mat.toString()); responseColumnHeaders.add((isSingle ? "R" : "C") + i); // R -> Radio button, C -> Check box i++; } } }