use of javax.xml.xpath.XPathExpression in project jangaroo-tools by CoreMedia.
the class ASDocScreenScraper method getClassDoc.
private String getClassDoc() throws XPathExpressionException {
XPath xpath = xPathFactory.newXPath();
XPathExpression classDoc = xpath.compile("//*[@id='content']//*[name()='div'][@class='MainContent']");
Node classDocNode = (Node) classDoc.evaluate(document, XPathConstants.NODE);
if (classDocNode != null) {
NodeList classDocNodes = classDocNode.getChildNodes();
return getASDoc(classDocNodes, null);
}
return "";
}
use of javax.xml.xpath.XPathExpression in project perun by CESNET.
the class ExtSourceXML method getValueFromXpath.
/**
* Get xml Node and xpath expression to get value from node by this xpath.
*
* @param node node for getting value from
* @param xpathExpression expression for xpath to looking for value in node
* @return string extracted from node by xpath
* @throws InternalErrorException
*/
protected String getValueFromXpath(Node node, String xpathExpression) throws InternalErrorException {
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr;
try {
expr = xpath.compile(xpathExpression);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query.", ex);
}
String text;
try {
text = (String) expr.evaluate(node, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on node.", ex);
}
return text;
}
use of javax.xml.xpath.XPathExpression in project perun by CESNET.
the class ExtSourceXML method xpathParsing.
/**
* Get query and maxResults.
* Prepare document and xpathExpression by query.
* Get all nodes by xpath from document and parse them one by one.
*
* The way of xml take from "file" or "uri" (configuration file)
*
* @param query xpath query from config file
* @param maxResults never get more than maxResults results (0 mean unlimited)
*
* @return List of results, where result is Map<String,String> like <name, value>
* @throws InternalErrorException
*/
protected List<Map<String, String>> xpathParsing(String query, int maxResults) throws InternalErrorException {
//Prepare result list
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
//Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new InternalErrorException("Error when creating newDocumentBuilder.", ex);
}
Document doc;
try {
if (file != null && !file.isEmpty()) {
doc = builder.parse(file);
} else if (uri != null && !uri.isEmpty()) {
doc = builder.parse(this.createTwoWaySSLConnection(uri));
} else {
throw new InternalErrorException("Document can't be parsed, because there is no way (file or uri) to this document in xpathParser.");
}
} catch (SAXParseException ex) {
throw new InternalErrorException("Error when parsing uri by document builder.", ex);
} catch (SAXException ex) {
throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters.", ex);
} catch (IOException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output.", ex);
}
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression queryExpr;
try {
queryExpr = xpath.compile(query);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query.", ex);
}
//Call query on document node and get back nodesets
NodeList nodeList;
try {
nodeList = (NodeList) queryExpr.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("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 subjects;
}
//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);
Map<String, String> map = convertNodeToMap(singleNode);
if (map != null)
subjects.add(map);
//Reducing results by maxResults
if (maxResults > 0) {
if (subjects.size() >= maxResults)
break;
}
}
this.close();
return subjects;
}
use of javax.xml.xpath.XPathExpression in project perun by CESNET.
the class MuPasswordManagerModule method parseUCO.
/**
* Parse UCO from XML body response and convert it to map of parameters.
*
* @param document XML document to be parsed
* @param requestID unique ID of a request
* @return Map of response params
* @throws InternalErrorException
*/
private Map<String, String> parseUCO(Document document, int requestID) throws InternalErrorException {
Map<String, String> result = new HashMap<>();
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression ucoExpr;
try {
ucoExpr = xpath.compile("//resp/uco/text()");
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
}
try {
String uco = (String) ucoExpr.evaluate(document, XPathConstants.STRING);
result.put("urn:perun:user:attribute-def:def:login-namespace:mu", uco);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on resulting document for request ID: " + requestID, ex);
}
return result;
}
use of javax.xml.xpath.XPathExpression in project perun by CESNET.
the class MuPasswordManagerModule method parseResponse.
/**
* Parse XML response from IS MU to XML document.
*
* @param inputStream Stream to be parsed to Document
* @param requestID ID of request made to IS MU.
* @return XML document for further processing
* @throws InternalErrorException
*/
private Document parseResponse(InputStream inputStream, int requestID) throws InternalErrorException {
//Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new InternalErrorException("Error when creating newDocumentBuilder. Request ID: " + requestID, ex);
}
String response = null;
try {
response = convertStreamToString(inputStream, "UTF-8");
} catch (IOException ex) {
log.error("Unable to convert InputStream to String: {}", ex);
}
log.trace("Request ID: " + requestID + " Response: " + response);
Document doc;
try {
doc = builder.parse(new InputSource(new StringReader(response)));
} catch (SAXParseException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Request ID: " + requestID, ex);
} catch (SAXException ex) {
throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters. Request ID: " + requestID, ex);
} catch (IOException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output. Request ID: " + requestID, ex);
}
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression isErrorExpr;
XPathExpression getErrorTextExpr;
XPathExpression getDbErrorTextExpr;
try {
isErrorExpr = xpath.compile("//resp/stav/text()");
getErrorTextExpr = xpath.compile("//resp/error/text()");
getDbErrorTextExpr = xpath.compile("//resp/dberror/text()");
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
}
// OK or ERROR
String responseStatus;
try {
responseStatus = (String) isErrorExpr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document to resolve response status. Request ID: " + requestID, ex);
}
log.trace("Request ID: " + requestID + " Response status: " + responseStatus);
if ("OK".equals(responseStatus)) {
return doc;
} else {
try {
String error = (String) getErrorTextExpr.evaluate(doc, XPathConstants.STRING);
if (error == null || error.isEmpty()) {
error = (String) getDbErrorTextExpr.evaluate(doc, XPathConstants.STRING);
}
throw new InternalErrorException("IS MU (password manager backend) responded with error to a Request ID: " + requestID + " Error: " + error);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document to resolve error status. Request ID: " + requestID, ex);
}
}
}
Aggregations