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();
}
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();
}
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();
}
}
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;
}
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();
}
}
Aggregations