use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRLayoutUtilities method getPersonalNavigation.
public static org.w3c.dom.Document getPersonalNavigation() throws JDOMException, XPathExpressionException {
Document navi = getNavi();
DOMOutputter accessCleaner = new DOMOutputter(new AccessCleaningDOMOutputProcessor());
org.w3c.dom.Document personalNavi = accessCleaner.output(navi);
XPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
NodeList emptyGroups = (NodeList) xpath.evaluate("/navigation/menu/group[not(item)]", personalNavi, XPathConstants.NODESET);
for (int i = 0; i < emptyGroups.getLength(); ++i) {
org.w3c.dom.Element group = (org.w3c.dom.Element) emptyGroups.item(i);
group.getParentNode().removeChild(group);
}
NodeList emptyMenu = (NodeList) xpath.evaluate("/navigation/menu[not(item or group)]", personalNavi, XPathConstants.NODESET);
for (int i = 0; i < emptyMenu.getLength(); ++i) {
org.w3c.dom.Element menu = (org.w3c.dom.Element) emptyMenu.item(i);
menu.getParentNode().removeChild(menu);
}
NodeList emptyNodes = (NodeList) xpath.evaluate("//text()[normalize-space(.) = '']", personalNavi, XPathConstants.NODESET);
for (int i = 0; i < emptyNodes.getLength(); ++i) {
Node emptyTextNode = emptyNodes.item(i);
emptyTextNode.getParentNode().removeChild(emptyTextNode);
}
personalNavi.normalizeDocument();
if (LOGGER.isDebugEnabled()) {
try {
String encoding = "UTF-8";
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
transformer.transform(new DOMSource(personalNavi), new StreamResult(bout));
LOGGER.debug("personal navigation: {}", bout.toString(encoding));
} catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException | UnsupportedEncodingException e) {
LOGGER.warn("Error while getting debug information.", e);
}
}
return personalNavi;
}
use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRDefaultAltoChangeApplier method applyChange.
@Override
public void applyChange(MCRAltoChangeSet changeSet) {
String derivateID = changeSet.getDerivateID();
changeSet.getWordChanges().stream().forEach(change -> {
List<MCRAltoWordChange> list = fileChangeMap.computeIfAbsent(change.getFile(), (k) -> new ArrayList<>());
list.add(change);
});
fileChangeMap.keySet().forEach(file -> {
LOGGER.info("Open file {} to apply changes!", file);
MCRPath altoFilePath = MCRPath.getPath(derivateID, file);
if (!Files.exists(altoFilePath)) {
LOGGER.warn("Could not find file {} which was referenced by alto change!", altoFilePath);
throw new MCRException(new IOException("Alto-File " + altoFilePath + " does not exist"));
}
Document altoDocument = readALTO(altoFilePath);
List<MCRAltoWordChange> wordChangesInThisFile = fileChangeMap.get(file);
wordChangesInThisFile.stream().forEach(wordChange -> {
String xpath = String.format(Locale.ROOT, "//alto:String[number(@HPOS)=number('%d') and number(@VPOS)=number('%d')]", wordChange.getHpos(), wordChange.getVpos());
List<Element> wordToChange = XPathFactory.instance().compile(xpath, Filters.element(), null, MCRConstants.ALTO_NAMESPACE).evaluate(altoDocument);
if (wordToChange.size() != 1) {
LOGGER.warn("Found {} words to change.", wordToChange.size());
}
wordToChange.forEach(word -> {
word.setAttribute("CONTENT", wordChange.getTo());
word.setAttribute("WC", "1");
});
});
storeALTO(altoFilePath, altoDocument);
});
}
use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRXEditorTransformer method createEmptyDocumentFromXPath.
private void createEmptyDocumentFromXPath(String xPath) throws JaxenException, JDOMException {
Element root = createRootElement(xPath);
editorSession.setEditedXML(new Document(root));
editorSession.setBreakpoint("Starting with empty XML document");
}
use of org.jdom2.xpath.XPath 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.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRSubselectReturnTarget method bindsFirstOrMoreThanOneElement.
private boolean bindsFirstOrMoreThanOneElement(String xPath, MCREditorSession session) throws JaxenException, JDOMException {
MCRBinding binding = new MCRBinding(xPath, false, session.getRootBinding());
boolean result = (binding.getBoundNode() instanceof Element) && !xPath.endsWith("]");
binding.detach();
return result;
}
Aggregations