use of org.omegat.filters3.Attribute 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.omegat.filters3.Attribute in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testNestedChanges.
@Test
public void testNestedChanges() {
Element root = new Element("root");
Document doc = new Document(root);
MCRChangeTracker tracker = new MCRChangeTracker();
Element title = new Element("title");
root.addContent(title);
tracker.track(MCRAddedElement.added(title));
Attribute id = new Attribute("type", "main");
title.setAttribute(id);
tracker.track(MCRAddedAttribute.added(id));
Element part = new Element("part");
title.addContent(part);
tracker.track(MCRAddedElement.added(part));
tracker.track(MCRRemoveElement.remove(part));
tracker.track(MCRRemoveAttribute.remove(id));
tracker.track(MCRRemoveElement.remove(title));
tracker.undoChanges(doc);
}
use of org.omegat.filters3.Attribute in project mycore by MyCoRe-Org.
the class MCRBinding method bind.
private void bind(String xPath, boolean buildIfNotExists, String initialValue) throws JaxenException {
this.xPath = xPath;
Map<String, Object> variables = buildXPathVariables();
XPathExpression<Object> xPathExpr = XPathFactory.instance().compile(xPath, Filters.fpassthrough(), variables, MCRConstants.getStandardNamespaces());
boundNodes.addAll(xPathExpr.evaluate(parent.getBoundNodes()));
for (Object boundNode : boundNodes) if (!(boundNode instanceof Element || boundNode instanceof Attribute || boundNode instanceof Document))
throw new RuntimeException("XPath MUST only bind either element, attribute or document nodes: " + xPath);
LOGGER.debug("Bind to {} selected {} node(s)", xPath, boundNodes.size());
if (boundNodes.isEmpty() && buildIfNotExists) {
MCRNodeBuilder builder = new MCRNodeBuilder(variables);
Object built = builder.buildNode(xPath, initialValue, (Parent) (parent.getBoundNode()));
LOGGER.debug("Bind to {} generated node {}", xPath, MCRXPathBuilder.buildXPath(built));
boundNodes.add(built);
trackNodeCreated(builder.getFirstNodeBuilt());
}
}
use of org.omegat.filters3.Attribute in project mycore by MyCoRe-Org.
the class MCRSetAttributeValue method undo.
public void undo(MCRChangeData data) {
Attribute attribute = data.getAttribute();
data.getContext().removeAttribute(attribute.getName(), attribute.getNamespace());
data.getContext().setAttribute(attribute);
}
use of org.omegat.filters3.Attribute in project mycore by MyCoRe-Org.
the class MCRAddedAttribute method undo.
public void undo(MCRChangeData data) {
Attribute attribute = data.getAttribute();
data.getContext().removeAttribute(attribute.getName(), attribute.getNamespace());
}
Aggregations