use of javax.xml.xpath.XPathFactory in project lucene-solr by apache.
the class SimplePostTool method getNodesFromXP.
//
// Utility methods for XPath handing
//
/**
* Gets all nodes matching an XPath
*/
public static NodeList getNodesFromXP(Node n, String xpath) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression expr = xp.compile(xpath);
return (NodeList) expr.evaluate(n, XPathConstants.NODESET);
}
use of javax.xml.xpath.XPathFactory in project lucene-solr by apache.
the class EnumField method init.
/**
* {@inheritDoc}
*/
@Override
protected void init(IndexSchema schema, Map<String, String> args) {
super.init(schema, args);
enumsConfigFile = args.get(PARAM_ENUMS_CONFIG);
if (enumsConfigFile == null) {
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enums config file was configured.");
}
enumName = args.get(PARAM_ENUM_NAME);
if (enumName == null) {
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enum name was configured.");
}
InputStream is = null;
try {
is = schema.getResourceLoader().openResource(enumsConfigFile);
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
final Document doc = dbf.newDocumentBuilder().parse(is);
final XPathFactory xpathFactory = XPathFactory.newInstance();
final XPath xpath = xpathFactory.newXPath();
final String xpathStr = String.format(Locale.ROOT, "/enumsConfig/enum[@name='%s']", enumName);
final NodeList nodes = (NodeList) xpath.evaluate(xpathStr, doc, XPathConstants.NODESET);
final int nodesLength = nodes.getLength();
if (nodesLength == 0) {
String exceptionMessage = String.format(Locale.ENGLISH, "No enum configuration found for enum '%s' in %s.", enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
}
if (nodesLength > 1) {
if (log.isWarnEnabled())
log.warn("More than one enum configuration found for enum '{}' in {}. The last one was taken.", enumName, enumsConfigFile);
}
final Node enumNode = nodes.item(nodesLength - 1);
final NodeList valueNodes = (NodeList) xpath.evaluate("value", enumNode, XPathConstants.NODESET);
for (int i = 0; i < valueNodes.getLength(); i++) {
final Node valueNode = valueNodes.item(i);
final String valueStr = valueNode.getTextContent();
if ((valueStr == null) || (valueStr.length() == 0)) {
final String exceptionMessage = String.format(Locale.ENGLISH, "A value was defined with an no value in enum '%s' in %s.", enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
}
if (enumStringToIntMap.containsKey(valueStr)) {
final String exceptionMessage = String.format(Locale.ENGLISH, "A duplicated definition was found for value '%s' in enum '%s' in %s.", valueStr, enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
}
enumIntToStringMap.put(i, valueStr);
enumStringToIntMap.put(valueStr, i);
}
} catch (ParserConfigurationException | XPathExpressionException | SAXException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing enums config.", e);
}
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error while opening enums config.", e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if ((enumStringToIntMap.size() == 0) || (enumIntToStringMap.size() == 0)) {
String exceptionMessage = String.format(Locale.ENGLISH, "Invalid configuration was defined for enum '%s' in %s.", enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
}
args.remove(PARAM_ENUMS_CONFIG);
args.remove(PARAM_ENUM_NAME);
}
use of javax.xml.xpath.XPathFactory in project gerrit by GerritCodeReview.
the class HtmlDomUtil method compact.
private static void compact(Document doc) {
try {
String expr = "//text()[normalize-space(.) = '']";
XPathFactory xp = XPathFactory.newInstance();
XPathExpression e = xp.newXPath().compile(expr);
NodeList empty = (NodeList) e.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < empty.getLength(); i++) {
Node node = empty.item(i);
node.getParentNode().removeChild(node);
}
} catch (XPathExpressionException e) {
// Don't do the whitespace removal.
}
}
use of javax.xml.xpath.XPathFactory in project uPortal by Jasig.
the class XmlUtilitiesImplTest method testGetUniqueXPath.
@Test
public void testGetUniqueXPath() throws Exception {
final Document testDoc = loadTestDocument();
final XPathFactory xPathFactory = XPathFactory.newInstance();
final XPath xPath = xPathFactory.newXPath();
final XPathExpression xPathExpression = xPath.compile("//*[@ID='11']");
final Node node = (Node) xPathExpression.evaluate(testDoc, XPathConstants.NODE);
final XmlUtilitiesImpl xmlUtilities = new XmlUtilitiesImpl();
final String nodePath = xmlUtilities.getUniqueXPath(node);
assertEquals("/layout/folder[2]/folder[3]", nodePath);
}
use of javax.xml.xpath.XPathFactory in project oxTrust by GluuFederation.
the class Shibboleth3ConfService method isFederationMetadata.
public boolean isFederationMetadata(String spMetaDataFN) {
if (spMetaDataFN == null) {
return false;
}
File spMetaDataFile = new File(getSpMetadataFilePath(spMetaDataFN));
InputStream is = null;
InputStreamReader isr = null;
Document xmlDocument = null;
try {
is = FileUtils.openInputStream(spMetaDataFile);
isr = new InputStreamReader(is, "UTF-8");
try {
xmlDocument = xmlService.getXmlDocument(new InputSource(isr));
} catch (Exception ex) {
log.error("Failed to parse metadata file '{}'", ex, spMetaDataFile.getAbsolutePath());
ex.printStackTrace();
}
} catch (IOException ex) {
log.error("Failed to read metadata file '{}'", ex, spMetaDataFile.getAbsolutePath());
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(isr);
IOUtils.closeQuietly(is);
}
if (xmlDocument == null) {
return false;
}
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String federationTag = null;
try {
federationTag = xPath.compile("count(/EntitiesDescriptor)").evaluate(xmlDocument);
} catch (XPathExpressionException ex) {
log.error("Failed to find IDP metadata file in relaying party file '{}'", ex, spMetaDataFile.getAbsolutePath());
ex.printStackTrace();
}
return Integer.parseInt(federationTag) > 0;
}
Aggregations