Search in sources :

Example 6 with MCRNodeBuilder

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

the class MCRSwapInsertTargetTest method testBuildInsert.

@Test
public void testBuildInsert() 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, "build");
    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("", ((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("", ((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 7 with MCRNodeBuilder

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

the class MCRChangeTrackerTest method testAddElement.

@Test
public void testAddElement() throws JaxenException {
    Document doc = new Document(new MCRNodeBuilder().buildElement("document[title][title[2]]", null, null));
    MCRChangeTracker tracker = new MCRChangeTracker();
    Element title = new Element("title");
    doc.getRootElement().getChildren().add(1, title);
    assertEquals(3, doc.getRootElement().getChildren().size());
    assertTrue(doc.getRootElement().getChildren().contains(title));
    tracker.track(MCRAddedElement.added(title));
    tracker.undoChanges(doc);
    assertEquals(2, doc.getRootElement().getChildren().size());
    assertFalse(doc.getRootElement().getChildren().contains(title));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) Test(org.junit.Test)

Example 8 with MCRNodeBuilder

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

the class MCRChangeTrackerTest method testSetElementText.

@Test
public void testSetElementText() throws JaxenException {
    Document doc = new Document(new MCRNodeBuilder().buildElement("document[@id='foo'][titles/title][author]", null, null));
    MCRChangeTracker tracker = new MCRChangeTracker();
    tracker.track(MCRSetElementText.setText(doc.getRootElement(), "text"));
    assertEquals("text", doc.getRootElement().getText());
    assertEquals("foo", doc.getRootElement().getAttributeValue("id"));
    tracker.undoChanges(doc);
    assertEquals("foo", doc.getRootElement().getAttributeValue("id"));
    assertEquals(2, doc.getRootElement().getChildren().size());
    assertEquals("titles", doc.getRootElement().getChildren().get(0).getName());
    assertEquals("author", doc.getRootElement().getChildren().get(1).getName());
    assertEquals("", doc.getRootElement().getText());
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Document(org.jdom2.Document) Test(org.junit.Test)

Example 9 with MCRNodeBuilder

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

the class MCRChangeTrackerTest method testAddAttribute.

@Test
public void testAddAttribute() throws JaxenException {
    Document doc = new Document(new MCRNodeBuilder().buildElement("document[title]", null, null));
    MCRChangeTracker tracker = new MCRChangeTracker();
    Attribute id = new Attribute("id", "foo");
    doc.getRootElement().setAttribute(id);
    tracker.track(MCRAddedAttribute.added(id));
    tracker.undoChanges(doc);
    assertNull(doc.getRootElement().getAttribute("id"));
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Attribute(org.jdom2.Attribute) Document(org.jdom2.Document) Test(org.junit.Test)

Example 10 with MCRNodeBuilder

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

the class MCRRepeatBinding method insert.

public void insert(int pos) throws JaxenException {
    if ("build".equals(method)) {
        Element parentElement = getParentElement();
        Element precedingElement = (Element) (getBoundNodes().get(pos - 1));
        int posOfPrecedingInParent = parentElement.indexOf(precedingElement);
        int targetPos = posOfPrecedingInParent + 1;
        String pathToBuild = getElementNameWithPredicates();
        Element newElement = (Element) (new MCRNodeBuilder().buildNode(pathToBuild, null, null));
        parentElement.addContent(targetPos, newElement);
        boundNodes.add(pos, newElement);
        track(MCRAddedElement.added(newElement));
    } else
        cloneBoundElement(pos - 1);
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) MCRAddedElement(org.mycore.frontend.xeditor.tracker.MCRAddedElement) Element(org.jdom2.Element)

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