Search in sources :

Example 21 with MCRNodeBuilder

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

Example 22 with MCRNodeBuilder

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

Example 23 with MCRNodeBuilder

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

Example 24 with MCRNodeBuilder

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;
}
Also used : MCREditorSession(org.mycore.frontend.xeditor.MCREditorSession) MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Document(org.jdom2.Document)

Example 25 with MCRNodeBuilder

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