use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testRemoveAttribute.
@Test
public void testRemoveAttribute() 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(MCRRemoveAttribute.remove(id));
assertNull(doc.getRootElement().getAttribute("id"));
tracker.undoChanges(doc);
assertEquals("foo", doc.getRootElement().getAttributeValue("id"));
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testRemoveChangeTracking.
@Test
public void testRemoveChangeTracking() throws JaxenException, JDOMException {
String template = "document[titles[title][title[2]]][authors/author[first='John'][last='Doe']]";
Document doc = new Document(new MCRNodeBuilder().buildElement(template, null, null));
MCRChangeTracker tracker = new MCRChangeTracker();
Element titles = (Element) (new MCRBinding("document/titles", true, new MCRBinding(doc)).getBoundNode());
Element title = new Element("title").setAttribute("type", "alternative");
titles.addContent(2, title);
tracker.track(MCRAddedElement.added(title));
Attribute lang = new Attribute("lang", "de");
doc.getRootElement().setAttribute(lang);
tracker.track(MCRAddedAttribute.added(lang));
Element author = (Element) (new MCRBinding("document/authors/author", true, new MCRBinding(doc)).getBoundNode());
tracker.track(MCRRemoveElement.remove(author));
doc = MCRChangeTracker.removeChangeTracking(doc);
assertFalse(doc.getDescendants(Filters.processinginstruction()).iterator().hasNext());
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testCompleteUndo.
@Test
public void testCompleteUndo() throws JaxenException, JDOMException {
String template = "document[titles[title][title[2]]][authors/author[first='John'][last='Doe']]";
Document doc = new Document(new MCRNodeBuilder().buildElement(template, null, null));
Document before = doc.clone();
MCRChangeTracker tracker = new MCRChangeTracker();
Element titles = (Element) (new MCRBinding("document/titles", true, new MCRBinding(doc)).getBoundNode());
Element title = new Element("title").setAttribute("type", "alternative");
titles.addContent(2, title);
tracker.track(MCRAddedElement.added(title));
Attribute lang = new Attribute("lang", "de");
doc.getRootElement().setAttribute(lang);
tracker.track(MCRAddedAttribute.added(lang));
Element author = (Element) (new MCRBinding("document/authors/author", true, new MCRBinding(doc)).getBoundNode());
tracker.track(MCRRemoveElement.remove(author));
tracker.undoChanges(doc);
assertTrue(MCRXMLHelper.deepEqual(before, doc));
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRXEditorValidatorTest method buildSession.
private MCREditorSession buildSession(String template) throws JaxenException, JDOMException {
MCREditorSession session = new MCREditorSession();
Document editedXML = new Document(new MCRNodeBuilder().buildElement(template, null, null));
session.setEditedXML(editedXML);
return session;
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRBindingTest method testGroupByReferencedID.
@Test
public void testGroupByReferencedID() throws JDOMException, JaxenException {
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);
MCRBinding rootBinding = new MCRBinding(new Document(document));
MCRBinding documentBinding = new MCRBinding("document", true, rootBinding);
MCRBinding id = new MCRBinding("name[@id][1]/@id", null, "id", documentBinding);
assertEquals("/document/name/@id", id.getAbsoluteXPath());
assertEquals("n1", id.getValue());
binding = new MCRBinding("note[@href=concat('#',$id)]", true, documentBinding);
Element note = (Element) (binding.getBoundNode());
assertEquals("note", note.getName());
assertEquals("#n1", note.getAttributeValue("href"));
binding = new MCRBinding("location[@href=concat('#',$id)]", true, documentBinding);
Element location = (Element) (binding.getBoundNode());
assertEquals("location", location.getName());
assertEquals("#n1", location.getAttributeValue("href"));
id = new MCRBinding("name[@id][2]/@id", null, "id", documentBinding);
assertEquals("/document/name[2]/@id", id.getAbsoluteXPath());
assertEquals("n2", id.getValue());
binding = new MCRBinding("note[@href=concat('#',$id)]", true, documentBinding);
note = (Element) (binding.getBoundNode());
assertEquals("note", note.getName());
assertEquals("#n2", note.getAttributeValue("href"));
binding = new MCRBinding("location[@href=concat('#',$id)]", true, documentBinding);
location = (Element) (binding.getBoundNode());
assertEquals("location", location.getName());
assertEquals("#n2", location.getAttributeValue("href"));
}
Aggregations