Search in sources :

Example 16 with MCRNodeBuilder

use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.

the class MCRSwapInsertTargetTest method testCloneInsert.

@Test
public void testCloneInsert() throws JaxenException, JDOMException {
    String x = "mods:mods[mods:name[@type='personal']='p1'][mods:name[@type='personal'][2]='p2'][mods:name[@type='corporate']='c1']";
    Element template = new MCRNodeBuilder().buildElement(x, null, null);
    Document doc = new Document(template);
    MCRBinding root = new MCRBinding(doc);
    MCRRepeatBinding repeat = new MCRRepeatBinding("mods:mods/mods:name[@type='personal']", root, 3, 10, "clone");
    assertEquals("mods:name[(@type = \"personal\")]", repeat.getElementNameWithPredicates());
    assertEquals(3, repeat.getBoundNodes().size());
    assertEquals("p1", ((Element) (repeat.getBoundNodes().get(0))).getText());
    assertEquals("p2", ((Element) (repeat.getBoundNodes().get(1))).getText());
    assertEquals("p2", ((Element) (repeat.getBoundNodes().get(2))).getText());
    assertEquals("name", ((Element) (repeat.getBoundNodes().get(2))).getName());
    assertEquals("personal", ((Element) (repeat.getBoundNodes().get(2))).getAttributeValue("type"));
    repeat.insert(1);
    assertEquals(4, repeat.getBoundNodes().size());
    assertEquals("p1", ((Element) (repeat.getBoundNodes().get(0))).getText());
    assertEquals("p1", ((Element) (repeat.getBoundNodes().get(1))).getText());
    assertEquals("name", ((Element) (repeat.getBoundNodes().get(1))).getName());
    assertEquals("personal", ((Element) (repeat.getBoundNodes().get(1))).getAttributeValue("type"));
    assertEquals("p2", ((Element) (repeat.getBoundNodes().get(2))).getText());
    repeat = new MCRRepeatBinding("mods:mods/mods:name[@type='corporate']", root, 1, 10, "build");
    assertEquals(1, repeat.getBoundNodes().size());
    assertEquals("mods:name[(@type = \"corporate\")]", repeat.getElementNameWithPredicates());
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) MCRRepeatBinding(org.mycore.frontend.xeditor.MCRRepeatBinding) Element(org.jdom2.Element) Document(org.jdom2.Document) MCRBinding(org.mycore.frontend.xeditor.MCRBinding) Test(org.junit.Test)

Example 17 with MCRNodeBuilder

use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.

the class MCRSwapInsertTargetTest method testSwap.

@Test
public void testSwap() throws JaxenException, JDOMException {
    Element template = new MCRNodeBuilder().buildElement("parent[name='a'][note][foo][name='b'][note[2]]", null, null);
    Document doc = new Document(template);
    MCRBinding root = new MCRBinding(doc);
    MCRRepeatBinding repeat = new MCRRepeatBinding("parent/name", root, 2, 0, "build");
    assertEquals(2, repeat.getBoundNodes().size());
    assertEquals("a", doc.getRootElement().getChildren().get(0).getText());
    assertEquals("b", doc.getRootElement().getChildren().get(3).getText());
    assertEquals("a", ((Element) (repeat.getBoundNodes().get(0))).getText());
    assertEquals("b", ((Element) (repeat.getBoundNodes().get(1))).getText());
    repeat.bindRepeatPosition();
    String swapParam = MCRSwapTarget.getSwapParameter(repeat, MCRSwapTarget.MOVE_DOWN);
    new MCRSwapTarget().handle(swapParam, root);
    assertEquals("b", doc.getRootElement().getChildren().get(0).getText());
    assertEquals("a", doc.getRootElement().getChildren().get(3).getText());
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) MCRRepeatBinding(org.mycore.frontend.xeditor.MCRRepeatBinding) Element(org.jdom2.Element) Document(org.jdom2.Document) MCRBinding(org.mycore.frontend.xeditor.MCRBinding) Test(org.junit.Test)

Example 18 with MCRNodeBuilder

use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.

the class MCRChangeTrackerTest method testSetAttributeValue.

@Test
public void testSetAttributeValue() throws JaxenException {
    Document doc = new Document(new MCRNodeBuilder().buildElement("document[@id='foo']", null, null));
    MCRChangeTracker tracker = new MCRChangeTracker();
    Attribute id = doc.getRootElement().getAttribute("id");
    tracker.track(MCRSetAttributeValue.setValue(id, "bar"));
    assertEquals("bar", id.getValue());
    tracker.undoChanges(doc);
    assertEquals("foo", doc.getRootElement().getAttributeValue("id"));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Attribute(org.jdom2.Attribute) Document(org.jdom2.Document) Test(org.junit.Test)

Example 19 with MCRNodeBuilder

use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.

the class MCRChangeTrackerTest method testSwapElements.

@Test
public void testSwapElements() throws JaxenException {
    Element root = new MCRNodeBuilder().buildElement("parent[name='a'][note][foo][name[2]='b'][note[2]]", null, null);
    Document doc = new Document(root);
    assertEquals("a", root.getChildren().get(0).getText());
    assertEquals("b", root.getChildren().get(3).getText());
    MCRChangeTracker tracker = new MCRChangeTracker();
    tracker.track(MCRSwapElements.swap(root, 0, 3));
    assertEquals("b", root.getChildren().get(0).getText());
    assertEquals("a", root.getChildren().get(3).getText());
    tracker.undoChanges(doc);
    assertEquals("a", root.getChildren().get(0).getText());
    assertEquals("b", root.getChildren().get(3).getText());
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) Test(org.junit.Test)

Example 20 with MCRNodeBuilder

use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.

the class MCRChangeTrackerTest method testRemoveElement.

@Test
public void testRemoveElement() throws JaxenException {
    String template = "document[title][title[2][@type='main'][subTitle]][title[3]]";
    Document doc = new Document(new MCRNodeBuilder().buildElement(template, null, null));
    MCRChangeTracker tracker = new MCRChangeTracker();
    Element title = doc.getRootElement().getChildren().get(1);
    tracker.track(MCRRemoveElement.remove(title));
    assertEquals(2, doc.getRootElement().getChildren().size());
    assertFalse(doc.getRootElement().getChildren().contains(title));
    tracker.undoChanges(doc);
    assertEquals(3, doc.getRootElement().getChildren().size());
    assertEquals("main", doc.getRootElement().getChildren().get(1).getAttributeValue("type"));
    assertNotNull(doc.getRootElement().getChildren().get(1).getChild("subTitle"));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) Test(org.junit.Test)

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