Search in sources :

Example 11 with MCRNodeBuilder

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());
    }
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) MCRRemoveAttribute(org.mycore.frontend.xeditor.tracker.MCRRemoveAttribute) MCRAddedAttribute(org.mycore.frontend.xeditor.tracker.MCRAddedAttribute) Attribute(org.jdom2.Attribute) MCRAddedElement(org.mycore.frontend.xeditor.tracker.MCRAddedElement) MCRRemoveElement(org.mycore.frontend.xeditor.tracker.MCRRemoveElement) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 12 with MCRNodeBuilder

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));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) HashMap(java.util.HashMap) Document(org.jdom2.Document) Test(org.junit.Test)

Example 13 with MCRNodeBuilder

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"));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) HashMap(java.util.HashMap) Document(org.jdom2.Document) Test(org.junit.Test)

Example 14 with MCRNodeBuilder

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"));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) Test(org.junit.Test)

Example 15 with MCRNodeBuilder

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);
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) HashMap(java.util.HashMap) Element(org.jdom2.Element) MCRXPathEvaluator(org.mycore.common.xml.MCRXPathEvaluator) Document(org.jdom2.Document) Before(org.junit.Before)

Aggregations

MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)27 Document (org.jdom2.Document)24 Test (org.junit.Test)20 Element (org.jdom2.Element)19 MCRBinding (org.mycore.frontend.xeditor.MCRBinding)8 Attribute (org.jdom2.Attribute)6 MCRRepeatBinding (org.mycore.frontend.xeditor.MCRRepeatBinding)6 HashMap (java.util.HashMap)4 MCRAddedElement (org.mycore.frontend.xeditor.tracker.MCRAddedElement)2 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 MCRException (org.mycore.common.MCRException)1 MCRXPathEvaluator (org.mycore.common.xml.MCRXPathEvaluator)1 MCRBase (org.mycore.datamodel.metadata.MCRBase)1 MCRObject (org.mycore.datamodel.metadata.MCRObject)1 MCREditorSession (org.mycore.frontend.xeditor.MCREditorSession)1 MCRAddedAttribute (org.mycore.frontend.xeditor.tracker.MCRAddedAttribute)1 MCRRemoveAttribute (org.mycore.frontend.xeditor.tracker.MCRRemoveAttribute)1 MCRRemoveElement (org.mycore.frontend.xeditor.tracker.MCRRemoveElement)1 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)1