Search in sources :

Example 16 with XPath

use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.

the class MCRSimpleModelXMLConverterTest method testToXML.

@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);
    });
}
Also used : XPathFactory(org.jdom2.xpath.XPathFactory) XMLOutputter(org.jdom2.output.XMLOutputter) Document(org.jdom2.Document) Test(org.junit.Test)

Example 17 with XPath

use of org.jdom2.xpath.XPath in project jspwiki by apache.

the class WebContainerAuthorizer method getRoles.

/**
 * Protected method that extracts the roles from JSPWiki's web application
 * deployment descriptor. Each Role is constructed by using the String
 * representation of the Role, for example
 * <code>new Role("Administrator")</code>.
 * @param webxml the web application deployment descriptor
 * @return an array of Role objects
 * @throws JDOMException if elements cannot be parsed correctly
 */
protected Role[] getRoles(Document webxml) throws JDOMException {
    Set<Role> roles = new HashSet<Role>();
    Element root = webxml.getRootElement();
    // Get roles referred to by constraints
    String selector = "//j:web-app/j:security-constraint/j:auth-constraint/j:role-name";
    XPath xpath = XPath.newInstance(selector);
    xpath.addNamespace("j", J2EE_SCHEMA_25_NAMESPACE);
    List<?> nodes = xpath.selectNodes(root);
    for (Iterator<?> it = nodes.iterator(); it.hasNext(); ) {
        String role = ((Element) it.next()).getTextTrim();
        roles.add(new Role(role));
    }
    // Get all defined roles
    selector = "//j:web-app/j:security-role/j:role-name";
    xpath = XPath.newInstance(selector);
    xpath.addNamespace("j", J2EE_SCHEMA_25_NAMESPACE);
    nodes = xpath.selectNodes(root);
    for (Iterator<?> it = nodes.iterator(); it.hasNext(); ) {
        String role = ((Element) it.next()).getTextTrim();
        roles.add(new Role(role));
    }
    return roles.toArray(new Role[roles.size()]);
}
Also used : XPath(org.jdom2.xpath.XPath) Element(org.jdom2.Element) HashSet(java.util.HashSet)

Example 18 with XPath

use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.

the class MCRXEditorTransformer method createRootElement.

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);
}
Also used : LocationPath(org.jaxen.expr.LocationPath) BaseXPath(org.jaxen.BaseXPath) Element(org.jdom2.Element) NameStep(org.jaxen.expr.NameStep) DocumentNavigator(org.jaxen.dom.DocumentNavigator) Namespace(org.jdom2.Namespace)

Example 19 with XPath

use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.

the class MCRSubselectReturnTarget method getBaseXPathForSubselect.

private String getBaseXPathForSubselect(MCREditorSession session) throws JaxenException, JDOMException {
    Document doc = session.getEditedXML();
    MCRChangeData change = session.getChangeTracker().findLastChange(doc);
    String text = change.getText();
    String xPath = text.substring(text.lastIndexOf(" ") + 1).trim();
    return bindsFirstOrMoreThanOneElement(xPath, session) ? xPath + "[1]" : xPath;
}
Also used : MCRChangeData(org.mycore.frontend.xeditor.tracker.MCRChangeData) Document(org.jdom2.Document)

Example 20 with XPath

use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.

the class MCRLayoutUtilities method getAncestorLabels.

/**
 * Returns all labels of the ancestor axis for the given item within
 * navigation.xml
 *
 * @param item a navigation item
 * @return Label as String, like "labelRoot &gt; labelChild &gt;
 *         labelChildOfChild"
 */
public static String getAncestorLabels(Element item) {
    StringBuilder label = new StringBuilder();
    String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage().trim();
    XPathExpression<Element> xpath;
    Element ic = null;
    xpath = XPATH_FACTORY.compile("//.[@href='" + getWebpageID(item) + "']", Filters.element());
    ic = xpath.evaluateFirst(getNavi());
    while (ic.getName().equals("item")) {
        ic = ic.getParentElement();
        String webpageID = getWebpageID(ic);
        Element labelEl = null;
        xpath = XPATH_FACTORY.compile("//.[@href='" + webpageID + "']/label[@xml:lang='" + lang + "']", Filters.element());
        labelEl = xpath.evaluateFirst(getNavi());
        if (labelEl != null) {
            if (label.length() == 0) {
                label = new StringBuilder(labelEl.getTextTrim());
            } else {
                label.insert(0, labelEl.getTextTrim() + " > ");
            }
        }
    }
    return label.toString();
}
Also used : Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)24 Document (org.jdom2.Document)11 IOException (java.io.IOException)4 Attribute (org.jdom2.Attribute)4 JDOMException (org.jdom2.JDOMException)4 MCRException (org.mycore.common.MCRException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URISyntaxException (java.net.URISyntaxException)3 Text (org.jdom2.Text)3 XPath (org.jdom2.xpath.XPath)3 XPathFactory (org.jdom2.xpath.XPathFactory)3 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)3 MCRObject (org.mycore.datamodel.metadata.MCRObject)3 XmlException (gov.cms.qpp.conversion.xml.XmlException)2 List (java.util.List)2 TransformerException (javax.xml.transform.TransformerException)2 XPath (javax.xml.xpath.XPath)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRPath (org.mycore.datamodel.niofs.MCRPath)2 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)2