Java Code Examples for org.jdom2.xpath.XPathFactory#instance()

The following examples show how to use org.jdom2.xpath.XPathFactory#instance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MCRSimpleModelXMLConverterTest.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testToXML() throws Exception {
    Document document = MCRSimpleModelXMLConverter.toXML(metsSimpleModel);

    XPathFactory xPathFactory = XPathFactory.instance();
    String documentAsString = new XMLOutputter(Format.getPrettyFormat()).outputString(document);

    Arrays.asList(PATHS_TO_CHECK.split(";")).stream()
        .map((String xpath) -> xPathFactory.compile(xpath, Filters.fboolean(), Collections.emptyMap(),
            Namespace.getNamespace("mets", "http://www.loc.gov/METS/")))
        .forEachOrdered(xPath -> {
            Boolean evaluate = xPath.evaluateFirst(document);
            Assert.assertTrue(
                String.format("The xpath : %s is not true! %s %s", xPath, System.lineSeparator(), documentAsString),
                evaluate);
        });
}
 
Example 2
Source File: MCRPIXPathMetadataService.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: MCRPIXPathMetadataService.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void removeIdentifier(MCRPersistentIdentifier identifier, MCRBase obj, String additional) {
    String xpath = getProperties().get("Xpath");
    Document xml = obj.createXML();
    XPathFactory xPathFactory = XPathFactory.instance();
    XPathExpression<Element> xp = xPathFactory.compile(xpath, Filters.element());
    List<Element> elements = xp.evaluate(xml);

    elements.stream()
        .filter(element -> element.getTextTrim().equals(identifier.asString()))
        .forEach(Element::detach);
}
 
Example 4
Source File: XPathExtractor.java    From web-data-extractor with Apache License 2.0 5 votes vote down vote up
private XPathExpression createXpathExpression() {
    XPathFactory xpfac = XPathFactory.instance();
    XPathExpression xp = null;
    if (namespaces.isEmpty()) {
        xp = xpfac.compile(xpath, Filters.fpassthrough());
    } else {
        xp = xpfac.compile(xpath, Filters.fpassthrough(), new LinkedHashMap<String, Object>(), namespaces.toArray(new Namespace[namespaces.size()]));
    }
    return xp;
}
 
Example 5
Source File: UniqueOnCancelValidator.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(Element element, ConfigElementImplementationRegistry registry) throws Exception {
    XPathFactory xPathFactory = XPathFactory.instance();
    List<String> tasks = ConfigUtil.allTasks(registry);
    for (String task : tasks) {
        List<Element> taskNodes = xPathFactory.compile("//" + task, Filters.element()).evaluate(element);
        for (Element taskNode : taskNodes) {
            List<Element> list = xPathFactory.compile("oncancel", Filters.element()).evaluate(taskNode);
            if (list.size() > 1) {
                throw new Exception("Task [" + task + "] should not contain more than 1 oncancel task");
            }
        }
    }
}
 
Example 6
Source File: JDomParser.java    From tutorials with MIT License 5 votes vote down vote up
public Element getNodeById(String id) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document document = (Document) builder.build(file);
        String filter = "//*[@tutId='" + id + "']";
        XPathFactory xFactory = XPathFactory.instance();
        XPathExpression<Element> expr = xFactory.compile(filter, Filters.element());
        List<Element> node = expr.evaluate(document);

        return node.get(0);
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 7
Source File: MCRPIXPathPredicate.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public MCRPIXPathPredicate(String propertyPrefix) {
    super(propertyPrefix);
    final String xPath = requireProperty("XPath");
    XPathFactory factory = XPathFactory.instance();
    expr = factory.compile(xPath, Filters.fpassthrough(), null, MCRConstants.getStandardNamespaces());
}
 
Example 8
Source File: HolidayEndpoint.java    From spring-boot-samples with Apache License 2.0 4 votes vote down vote up
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService){
	
	this.humanResourceService = humanResourceService;
	
	Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
	XPathFactory xPathFactory = XPathFactory.instance();
	
	startDateExpression =xPathFactory.compile("//hr:StartDate", Filters.element(), null,namespace);
	
	endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null,namespace);
	
	nameExpression = xPathFactory.compile(
			"concat(//hr:FirstName,' ',//hr:LastName)",
			Filters.fstring(), null,namespace);		
}