Search in sources :

Example 1 with XPathExpression

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

the class XmlUtil method parse.

/**
 * Parses the given stream and returns the requested nodes. If there's an error accessing or parsing the stream, an
 * empty list is returned.
 *
 * @param xmlStream stream to parse.
 * @param requestedNodes requestd nodes on the xml stream.
 * @return the requested nodes of the XML stream.
 */
public static List<Element> parse(InputStream xmlStream, String requestedNodes) {
    if (xmlStream != null && StringUtils.isNotEmpty(requestedNodes)) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(xmlStream);
            XPathFactory xpfac = XPathFactory.instance();
            XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
            return xp.evaluate(doc);
        } catch (IOException ioe) {
            log.error("Couldn't load all " + xmlStream + " resources", ioe);
        } catch (JDOMException jdome) {
            log.error("error parsing " + xmlStream + " resources", jdome);
        }
    }
    return Collections.<Element>emptyList();
}
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 2 with XPathExpression

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

the class StoredConfigurationImpl method readConfigProperty.

@Override
public String readConfigProperty(final ConfigurationProperty propertyName) {
    final XPathExpression xp = XPathBuilder.xpathForConfigProperty(propertyName);
    final Element propertyElement = (Element) xp.evaluateFirst(document);
    return propertyElement == null ? null : propertyElement.getText();
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) Element(org.jdom2.Element)

Example 3 with XPathExpression

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

the class StoredConfigurationImpl method writeConfigProperty.

@Override
public void writeConfigProperty(final ConfigurationProperty propertyName, final String value) {
    domModifyLock.writeLock().lock();
    try {
        final XPathExpression xp = XPathBuilder.xpathForConfigProperty(propertyName);
        final List<Element> propertyElements = xp.evaluate(document);
        for (final Element propertyElement : propertyElements) {
            propertyElement.detach();
        }
        final Element propertyElement = new Element(XML_ELEMENT_PROPERTY);
        propertyElement.setAttribute(new Attribute(XML_ATTRIBUTE_KEY, propertyName.getKey()));
        propertyElement.setContent(new Text(value));
        if (null == XPathBuilder.xpathForConfigProperties().evaluateFirst(document)) {
            final Element configProperties = new Element(XML_ELEMENT_PROPERTIES);
            configProperties.setAttribute(new Attribute(XML_ATTRIBUTE_TYPE, XML_ATTRIBUTE_VALUE_CONFIG));
            document.getRootElement().addContent(configProperties);
        }
        final XPathExpression xp2 = XPathBuilder.xpathForConfigProperties();
        final Element propertiesElement = (Element) xp2.evaluateFirst(document);
        propertyElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now()));
        propertiesElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now()));
        propertiesElement.addContent(propertyElement);
    } finally {
        domModifyLock.writeLock().unlock();
    }
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Text(org.jdom2.Text)

Example 4 with XPathExpression

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

the class StoredConfigurationImpl method readTemplateValue.

private static PwmSettingTemplate readTemplateValue(final Document document, final PwmSetting pwmSetting) {
    final XPathExpression xp = XPathBuilder.xpathForSetting(pwmSetting, null);
    final Element settingElement = (Element) xp.evaluateFirst(document);
    if (settingElement != null) {
        try {
            final String strValue = (String) ValueFactory.fromXmlValues(pwmSetting, settingElement, null).toNativeObject();
            return JavaHelper.readEnumFromString(PwmSettingTemplate.class, null, strValue);
        } catch (IllegalStateException e) {
            LOGGER.error("error reading template", e);
        }
    }
    return null;
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) Element(org.jdom2.Element)

Example 5 with XPathExpression

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

the class StoredConfigurationImpl method readSetting.

public StoredValue readSetting(final PwmSetting setting, final String profileID) {
    if (profileID == null && setting.getCategory().hasProfiles()) {
        final IllegalArgumentException e = new IllegalArgumentException("reading of setting " + setting.getKey() + " requires a non-null profileID");
        LOGGER.error("error", e);
        throw e;
    }
    if (profileID != null && !setting.getCategory().hasProfiles()) {
        throw new IllegalStateException("cannot read setting key " + setting.getKey() + " with non-null profileID");
    }
    domModifyLock.readLock().lock();
    try {
        final XPathExpression xp = XPathBuilder.xpathForSetting(setting, profileID);
        final Element settingElement = (Element) xp.evaluateFirst(document);
        if (settingElement == null) {
            return defaultValue(setting, getTemplateSet());
        }
        if (settingElement.getChild(XML_ELEMENT_DEFAULT) != null) {
            return defaultValue(setting, getTemplateSet());
        }
        try {
            return ValueFactory.fromXmlValues(setting, settingElement, getKey());
        } catch (PwmException e) {
            final String errorMsg = "unexpected error reading setting '" + setting.getKey() + "' profile '" + profileID + "', error: " + e.getMessage();
            throw new IllegalStateException(errorMsg);
        }
    } finally {
        domModifyLock.readLock().unlock();
    }
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) PwmException(password.pwm.error.PwmException) 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