use of javax.xml.xpath.XPathExpression in project spring-boot by spring-projects.
the class Versions method evaluateExpression.
private static String evaluateExpression(String expression) {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile(expression);
String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
return version;
} catch (Exception ex) {
throw new IllegalStateException("Failed to evaluate expression", ex);
}
}
use of javax.xml.xpath.XPathExpression in project perun by CESNET.
the class MUStrategy method parseResponse.
/**
* Parse String response as XML document and retrieve Publications from it.
* @param xml XML response from MU Prezentator
* @return List of Publications
* @throws CabinetException If anything fails
*/
protected List<Publication> parseResponse(String xml) throws CabinetException {
assert xml != null;
List<Publication> result = new ArrayList<Publication>();
//hook for titles with &
xml = xml.replace("&", "&");
//log.debug("RESPONSE: "+xml);
//Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new CabinetException("Error when creating newDocumentBuilder.", ex);
}
Document doc;
try {
doc = builder.parse(new InputSource(new StringReader(xml)));
} catch (SAXParseException ex) {
throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
} catch (SAXException ex) {
throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
} catch (IOException ex) {
throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
}
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression publicationsQuery;
try {
publicationsQuery = xpath.compile("/P/UL/publication");
} catch (XPathExpressionException ex) {
throw new CabinetException("Error when compiling xpath query.", ex);
}
NodeList nodeList;
try {
nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
throw new CabinetException("Error when evaluate xpath query on document.", ex);
}
//Test if there is any nodeset in result
if (nodeList.getLength() == 0) {
//There is no results, return empty subjects
return result;
}
//Iterate through nodes and convert them to Map<String,String>
for (int i = 0; i < nodeList.getLength(); i++) {
Node singleNode = nodeList.item(i);
// remove node from original structure in order to keep access time constant (otherwise is exp.)
singleNode.getParentNode().removeChild(singleNode);
try {
Publication publication = convertNodeToPublication(singleNode);
result.add(publication);
} catch (InternalErrorException ex) {
log.error("Unable to parse Publication:", ex);
}
}
return result;
}
use of javax.xml.xpath.XPathExpression in project perun by CESNET.
the class OBD30Strategy method parseResponse.
/**
* Parse String response as XML document and retrieve Publications from it.
* @param xml XML response from OBD 3.0
* @return List of Publications
* @throws CabinetException If anything fails
*/
protected List<Publication> parseResponse(String xml) throws CabinetException {
assert xml != null;
List<Publication> result = new ArrayList<Publication>();
//hook for titles with &
xml = xml.replace("&", "&");
//log.debug("RESPONSE: "+xml);
//Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new CabinetException("Error when creating newDocumentBuilder.", ex);
}
Document doc;
try {
doc = builder.parse(new InputSource(new StringReader(xml)));
} catch (SAXParseException ex) {
throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
} catch (SAXException ex) {
throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
} catch (IOException ex) {
throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
}
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression publicationsQuery;
try {
publicationsQuery = xpath.compile("/zaznamy/zaznam");
} catch (XPathExpressionException ex) {
throw new CabinetException("Error when compiling xpath query.", ex);
}
NodeList nodeList;
try {
nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
throw new CabinetException("Error when evaluate xpath query on document.", ex);
}
//Test if there is any nodeset in result
if (nodeList.getLength() == 0) {
//There is no results, return empty subjects
return result;
}
//Iterate through nodes and convert them to Map<String,String>
for (int i = 0; i < nodeList.getLength(); i++) {
Node singleNode = nodeList.item(i);
// remove node from original structure in order to keep access time constant (otherwise is exp.)
singleNode.getParentNode().removeChild(singleNode);
try {
Publication publication = convertNodeToPublication(singleNode);
result.add(publication);
} catch (InternalErrorException ex) {
log.error("Unable to parse Publication:", ex);
}
}
return result;
}
use of javax.xml.xpath.XPathExpression in project ORCID-Source by ORCID.
the class IdentityProviderManagerImpl method loadIdentityProviders.
@Override
public void loadIdentityProviders() {
String[] metadataUrls = StringUtils.split(metadataUrlsString);
XPath xpath = createXPath();
XPathExpression entityDescriptorXpath = compileXPath(xpath, "//md:EntityDescriptor");
for (String metadataUrl : metadataUrls) {
Document document = downloadMetadata(metadataUrl);
NodeList nodes = evaluateXPathNodeList(entityDescriptorXpath, document);
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
IdentityProviderEntity incoming = createEntityFromXml(element);
LOGGER.info("Found identity provider: {}", incoming.toShortString());
saveOrUpdateIdentityProvider(incoming);
}
}
}
use of javax.xml.xpath.XPathExpression in project Activiti by Activiti.
the class ProcessDiagramLayoutFactory method fixFlowNodePositionsIfModelFromAdonis.
protected Map<String, DiagramNode> fixFlowNodePositionsIfModelFromAdonis(Document bpmnModel, Map<String, DiagramNode> elementBoundsFromBpmnDi) {
if (isExportedFromAdonis50(bpmnModel)) {
Map<String, DiagramNode> mapOfFixedBounds = new HashMap<String, DiagramNode>();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
xPath.setNamespaceContext(new Bpmn20NamespaceContext());
for (Entry<String, DiagramNode> entry : elementBoundsFromBpmnDi.entrySet()) {
String elementId = entry.getKey();
DiagramNode elementBounds = entry.getValue();
String expression = "local-name(//bpmn:*[@id = '" + elementId + "'])";
try {
XPathExpression xPathExpression = xPath.compile(expression);
String elementLocalName = xPathExpression.evaluate(bpmnModel);
if (!"participant".equals(elementLocalName) && !"lane".equals(elementLocalName) && !"textAnnotation".equals(elementLocalName) && !"group".equals(elementLocalName)) {
elementBounds.setX(elementBounds.getX() - elementBounds.getWidth() / 2);
elementBounds.setY(elementBounds.getY() - elementBounds.getHeight() / 2);
}
} catch (XPathExpressionException e) {
throw new ActivitiException("Error while evaluating the following XPath expression on a BPMN XML document: '" + expression + "'.", e);
}
mapOfFixedBounds.put(elementId, elementBounds);
}
return mapOfFixedBounds;
} else {
return elementBoundsFromBpmnDi;
}
}
Aggregations