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());
}
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));
}
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());
}
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"));
}
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);
}
Aggregations