MCRIdentifierType(String typeID, String xPath) { this.typeID = typeID; this.xPath = xPath; this.xPathExpr = XPathFactory.instance().compile(xPath, Filters.element(), null, MCRConstants.getStandardNamespaces()); }
public void register(XPathFunctionContext context) { context.registerFunction(MCRConstants.getStandardNamespace(prefix).getURI(), localName, function); }
public void addNamespace(String prefix, String uri) { MCRConstants.registerNamespace(Namespace.getNamespace(prefix, uri)); }
MCRCleaningRule(String xPathExprNodesToInspect, String xPathExprRelevancyTest) { this.xPathExprNodesToInspect = xPathExprNodesToInspect; this.xPathNodesToInspect = XPathFactory.instance().compile(xPathExprNodesToInspect, Filters.fpassthrough(), null, MCRConstants.getStandardNamespaces()); this.xPathRelevancyTest = XPathFactory.instance().compile(xPathExprRelevancyTest, Filters.fpassthrough(), null, MCRConstants.getStandardNamespaces()); }
private void addNamespacesFrom(Element element) { MCRConstants.registerNamespace(element.getNamespace()); for (Namespace ns : element.getAdditionalNamespaces()) { MCRConstants.registerNamespace(ns); } for (Element child : element.getChildren()) { addNamespacesFrom(child); } }
private Element createRootElement(String xPath) throws JaxenException { BaseXPath baseXPath = new BaseXPath(xPath, new DocumentNavigator()); LocationPath lp = (LocationPath) (baseXPath.getRootExpr()); NameStep nameStep = (NameStep) (lp.getSteps().get(0)); String prefix = nameStep.getPrefix(); Namespace ns = prefix.isEmpty() ? Namespace.NO_NAMESPACE : MCRConstants.getStandardNamespace(prefix); return new Element(nameStep.getLocalName(), ns); }
private static Element getHostGenre(Element mods) { XPathExpression<Element> expr = XPathFactory.instance().compile("mods:relatedItem[@type='host']/mods:genre", Filters.element(), null, MCRConstants.getStandardNamespaces()); return expr.evaluateFirst(mods); } }
Element getRelatedItemIfExists(Element parent) { XPathExpression<Element> xPath = XPathFactory.instance().compile(xPathOfRelatedItem, Filters.element(), null, MCRConstants.getStandardNamespaces()); Element fixedParent = xPath.evaluateFirst(parent); return fixedParent != null ? fixedParent : parent; } }
@Override public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException { String xpath = getProperties().get("Xpath"); Document xml = obj.createXML(); XPathFactory xpfac = XPathFactory.instance(); XPathExpression<Element> xp = xpfac .compile(xpath, Filters.element(), null, MCRConstants.getStandardNamespaces()); List<Element> evaluate = xp.evaluate(xml); if (evaluate.size() > 1) { throw new MCRPersistentIdentifierException( "Got " + evaluate.size() + " matches for " + obj.getId() + " with xpath " + xpath + ""); } if (evaluate.size() == 0) { return Optional.empty(); } Element identifierElement = evaluate.listIterator().next(); String identifierString = identifierElement.getTextNormalize(); Optional<MCRPersistentIdentifier> parsedIdentifierOptional = MCRPIManager.getInstance() .getParserForType(getProperties().get("Type")).parse(identifierString); return parsedIdentifierOptional.map(MCRPersistentIdentifier.class::cast); }
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 TypeNotPresentException( "XPath MUST only bind either element, attribute or document nodes: " + xPath, null); } } 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()); } }
MCREnricher(String configID) { xPath2FindNestedObjects = XPathFactory.instance().compile(XPATH_HOST_SERIES, Filters.element(), null, MCRConstants.getStandardNamespaces()); dsConfig = MCRConfiguration.instance().getString("MCR.MODS.EnrichmentResolver.DataSources." + configID); idPool = new MCRIdentifierPool(); prepareDataSourceCalls(dsConfig); }