use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRBinding method bind.
private void bind(String xPath, boolean buildIfNotExists, String initialValue) throws JaxenException {
this.xPath = xPath;
Map<String, Object> variables = buildXPathVariables();
XPathExpression<Object> xPathExpr = XPathFactory.instance().compile(xPath, Filters.fpassthrough(), variables, MCRConstants.getStandardNamespaces());
boundNodes.addAll(xPathExpr.evaluate(parent.getBoundNodes()));
for (Object boundNode : boundNodes) if (!(boundNode instanceof Element || boundNode instanceof Attribute || boundNode instanceof Document))
throw new RuntimeException("XPath MUST only bind either element, attribute or document nodes: " + xPath);
LOGGER.debug("Bind to {} selected {} node(s)", xPath, boundNodes.size());
if (boundNodes.isEmpty() && buildIfNotExists) {
MCRNodeBuilder builder = new MCRNodeBuilder(variables);
Object built = builder.buildNode(xPath, initialValue, (Parent) (parent.getBoundNode()));
LOGGER.debug("Bind to {} generated node {}", xPath, MCRXPathBuilder.buildXPath(built));
boundNodes.add(built);
trackNodeCreated(builder.getFirstNodeBuilt());
}
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCREditorSubmissionTest method testSubmitTextfields.
@Test
public void testSubmitTextfields() throws JaxenException, JDOMException, IOException {
String template = "document[title='Titel'][author[@firstName='John'][@lastName='Doe']]";
MCREditorSession session = new MCREditorSession();
session.setEditedXML(new Document(new MCRNodeBuilder().buildElement(template, null, null)));
Map<String, String[]> submittedValues = new HashMap<>();
submittedValues.put("/document/title", new String[] { "Title" });
submittedValues.put("/document/author/@firstName", new String[] { "Jim" });
submittedValues.put("/document/author/@lastName", new String[] { "" });
session.getSubmission().setSubmittedValues(submittedValues);
session.getSubmission().emptyNotResubmittedNodes();
template = "document[title='Title'][author[@firstName='Jim'][@lastName='']]";
Document expected = new Document(new MCRNodeBuilder().buildElement(template, null, null));
Document result = session.getEditedXML();
result = MCRChangeTracker.removeChangeTracking(result);
assertTrue(MCRXMLHelper.deepEqual(expected, result));
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCREditorSubmissionTest method testSubmitCheckbox.
@Test
public void testSubmitCheckbox() throws JaxenException, JDOMException, IOException {
String template = "document[@archive='false']";
MCREditorSession session = new MCREditorSession();
session.setEditedXML(new Document(new MCRNodeBuilder().buildElement(template, null, null)));
session.getSubmission().setXPaths2CheckResubmission("@archive");
session.getSubmission().emptyNotResubmittedNodes();
assertEquals("", session.getEditedXML().getRootElement().getAttributeValue("archive"));
session.getSubmission().setXPaths2CheckResubmission("@archive");
Map<String, String[]> submittedValues = new HashMap<>();
submittedValues.put("/document/@archive", new String[] { "true" });
session.getSubmission().setSubmittedValues(submittedValues);
session.getSubmission().emptyNotResubmittedNodes();
assertEquals("true", session.getEditedXML().getRootElement().getAttributeValue("archive"));
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRRepeatBindingTest method testRepeatBindingWithComplexPredicate.
@Test
public void testRepeatBindingWithComplexPredicate() throws JaxenException, JDOMException {
Element template = new MCRNodeBuilder().buildElement("conditions[condition/@type='bingo'][condition[2]/@type='bongo']", null, null);
Document doc = new Document(template);
MCRBinding root = new MCRBinding(doc);
MCRBinding conditions = new MCRBinding("conditions", true, root);
MCRRepeatBinding repeat = new MCRRepeatBinding("condition[contains(' foo bar ',concat(' ',@type,' '))]", conditions, 3, 5, "build");
assertEquals(3, repeat.getBoundNodes().size());
MCRBinding binding = repeat.bindRepeatPosition();
assertEquals(1, binding.getBoundNodes().size());
assertEquals("/conditions/condition[3]", binding.getAbsoluteXPath());
((Element) (binding.getBoundNode())).setAttribute("value", "a");
binding = repeat.bindRepeatPosition();
assertEquals(1, binding.getBoundNodes().size());
assertEquals("/conditions/condition[4]", binding.getAbsoluteXPath());
((Element) (binding.getBoundNode())).setAttribute("value", "b");
binding = repeat.bindRepeatPosition();
assertEquals(1, binding.getBoundNodes().size());
assertEquals("/conditions/condition[5]", binding.getAbsoluteXPath());
((Element) (binding.getBoundNode())).setAttribute("value", "c");
repeat.removeBoundNode(0);
assertEquals(4, doc.getRootElement().getChildren().size());
assertEquals(2, repeat.getBoundNodes().size());
assertEquals("b", ((Element) (repeat.getBoundNodes().get(0))).getAttributeValue("value"));
assertEquals("c", ((Element) (repeat.getBoundNodes().get(1))).getAttributeValue("value"));
repeat.cloneBoundElement(0);
assertEquals(5, doc.getRootElement().getChildren().size());
assertEquals(3, repeat.getBoundNodes().size());
assertEquals("b", ((Element) (repeat.getBoundNodes().get(0))).getAttributeValue("value"));
assertEquals("b", ((Element) (repeat.getBoundNodes().get(1))).getAttributeValue("value"));
assertEquals("c", ((Element) (repeat.getBoundNodes().get(2))).getAttributeValue("value"));
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRJaxenXPathFactoryTest method setUp.
@Before
@Override
public void setUp() throws Exception {
super.setUp();
String builder = "document[name/@id='n1'][note/@href='#n1'][location/@href='#n1'][name[@id='n2']][location[@href='#n2']]";
Element document = new MCRNodeBuilder().buildElement(builder, null, null);
new Document(document);
Map<String, Object> variables = new HashMap<>();
variables.put("CurrentUser", "gast");
evaluator = new MCRXPathEvaluator(variables, document);
}
Aggregations