Search in sources :

Example 26 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project pwm by pwm-project.

the class StoredConfigurationImpl method readLocaleBundleMap.

public Map<String, String> readLocaleBundleMap(final String bundleName, final String keyName) {
    domModifyLock.readLock().lock();
    try {
        final XPathExpression xp = XPathBuilder.xpathForLocaleBundleSetting(bundleName, keyName);
        final Element localeBundleElement = (Element) xp.evaluateFirst(document);
        if (localeBundleElement != null) {
            final Map<String, String> bundleMap = new LinkedHashMap<>();
            for (final Element valueElement : localeBundleElement.getChildren("value")) {
                final String localeStrValue = valueElement.getAttributeValue("locale");
                bundleMap.put(localeStrValue == null ? "" : localeStrValue, valueElement.getText());
            }
            if (!bundleMap.isEmpty()) {
                return bundleMap;
            }
        }
    } finally {
        domModifyLock.readLock().unlock();
    }
    return Collections.emptyMap();
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) Element(org.jdom2.Element) LinkedHashMap(java.util.LinkedHashMap)

Example 27 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project pwm by pwm-project.

the class PwmSettingXml method readSettingXml.

static Element readSettingXml(final PwmSetting setting) {
    final XPathFactory xpfac = XPathFactory.instance();
    final XPathExpression xp = xpfac.compile("/settings/setting[@key=\"" + setting.getKey() + "\"]");
    return (Element) xp.evaluateFirst(readXml());
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) XPathFactory(org.jdom2.xpath.XPathFactory) Element(org.jdom2.Element)

Example 28 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project tutorials by eugenp.

the class JDomParser method getNodeById.

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;
    }
}
Also used : XPathFactory(org.jdom2.xpath.XPathFactory) SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException)

Example 29 with XPathExpression

use of org.jdom2.xpath.XPathExpression 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)

Example 30 with XPathExpression

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

the class MCRDerivate method getUrnMap.

/**
 * Reads all files and urns from the derivate.
 *
 * @return A {@link Map} which contains the files as key and the urns as value. If no URN assigned the map will be empty.
 */
public Map<String, String> getUrnMap() {
    Map<String, String> fileUrnMap = new HashMap<>();
    XPathExpression<Element> filesetPath = XPathFactory.instance().compile("./mycorederivate/derivate/fileset", Filters.element());
    Element result = filesetPath.evaluateFirst(this.createXML());
    if (result == null) {
        return fileUrnMap;
    }
    String urn = result.getAttributeValue("urn");
    if (urn != null) {
        XPathExpression<Element> filePath = XPathFactory.instance().compile("./mycorederivate/derivate/fileset[@urn='" + urn + "']/file", Filters.element());
        List<Element> files = filePath.evaluate(this.createXML());
        for (Element currentFileElement : files) {
            String currentUrn = currentFileElement.getChildText("urn");
            String currentFile = currentFileElement.getAttributeValue("name");
            fileUrnMap.put(currentFile, currentUrn);
        }
    }
    return fileUrnMap;
}
Also used : HashMap(java.util.HashMap) Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)43 Document (org.jdom2.Document)23 XPathExpression (org.jdom2.xpath.XPathExpression)13 IOException (java.io.IOException)9 XPathFactory (org.jdom2.xpath.XPathFactory)9 SAXBuilder (org.jdom2.input.SAXBuilder)7 Attribute (org.jdom2.Attribute)6 JDOMException (org.jdom2.JDOMException)5 Test (org.junit.Test)5 Text (org.jdom2.Text)4 XMLOutputter (org.jdom2.output.XMLOutputter)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 URISyntaxException (java.net.URISyntaxException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)2 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)2