use of javax.xml.xpath.XPathFactory in project jangaroo-tools by CoreMedia.
the class PomConverter method removeExmlPlugin.
/**
* Replaces exml-maven-plugin configuration by jangaroo-maven-plugin configuration within the given {@code plugins}
* element.
*/
private static void removeExmlPlugin(Node pluginsNode) throws XPathException {
if (pluginsNode == null) {
return;
}
Document document = pluginsNode.getOwnerDocument();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
Node exmlPluginNode = (Node) xPath.evaluate("plugin[artifactId='exml-maven-plugin']", pluginsNode, NODE);
if (exmlPluginNode == null) {
return;
}
Node jangarooPluginNode = (Node) xPath.evaluate("plugin[artifactId='jangaroo-maven-plugin']", pluginsNode, NODE);
Node exmlVersionNode = (Node) xPath.evaluate("version", exmlPluginNode, NODE);
Node exmlExtensionNode = (Node) xPath.evaluate("extensions", exmlPluginNode, NODE);
Node configClassPackageNode = (Node) xPath.evaluate("configuration/configClassPackage", exmlPluginNode, NODE);
if (jangarooPluginNode == null) {
jangarooPluginNode = document.createElement("plugin");
insertChildWithWhitespace(pluginsNode, jangarooPluginNode, exmlPluginNode);
Node jangarooPluginGroupIdNode = document.createElement("groupId");
jangarooPluginGroupIdNode.setTextContent("net.jangaroo");
insertChildWithWhitespace(jangarooPluginNode, jangarooPluginGroupIdNode, null);
Node jangarooPluginArtifactIdNode = document.createElement("artifactId");
jangarooPluginArtifactIdNode.setTextContent("jangaroo-maven-plugin");
insertChildWithWhitespace(jangarooPluginNode, jangarooPluginArtifactIdNode, null);
if (exmlVersionNode != null) {
Node jangarooPluginVersionNode = document.createElement("version");
jangarooPluginVersionNode.setTextContent(exmlVersionNode.getTextContent());
insertChildWithWhitespace(jangarooPluginNode, jangarooPluginVersionNode, null);
}
}
Node jangarooConfigurationNode = (Node) xPath.evaluate("configuration", jangarooPluginNode, NODE);
Node jangarooExtensionsNode = (Node) xPath.evaluate("extensions", jangarooPluginNode, NODE);
removeChildWithWhitespace(pluginsNode, exmlPluginNode);
if (exmlExtensionNode != null) {
if (jangarooExtensionsNode == null) {
insertChildWithWhitespace(jangarooPluginNode, exmlExtensionNode, jangarooConfigurationNode);
} else {
jangarooExtensionsNode.setTextContent(exmlExtensionNode.getTextContent());
}
}
if (configClassPackageNode != null) {
if (jangarooConfigurationNode == null) {
jangarooConfigurationNode = document.createElement("configuration");
insertChildWithWhitespace(jangarooPluginNode, jangarooConfigurationNode, null);
}
Node namespacesNode = document.createElement("namespaces");
insertChildWithWhitespace(jangarooConfigurationNode, namespacesNode, null);
Node namespaceNode = document.createElement("namespace");
insertChildWithWhitespace(namespacesNode, namespaceNode, null);
Node uriNode = document.createElement("uri");
uriNode.setTextContent("exml:" + configClassPackageNode.getTextContent());
insertChildWithWhitespace(namespaceNode, uriNode, null);
}
}
use of javax.xml.xpath.XPathFactory in project spring-boot by spring-projects.
the class GradleIT 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.XPathFactory 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.XPathFactory 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.XPathFactory 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;
}
Aggregations