@Override protected DocumentBuilder initialValue() { DocumentBuilderFactory instance = DocumentBuilderFactory.newInstance(); instance.setIgnoringComments(true); instance.setValidating(false); try { return instance.newDocumentBuilder(); } catch (ParserConfigurationException e) { log(Level.FATAL, e, "No Document builder available"); return null; } } };
public long findUptime(Document d, String upTimePath) { long uptime = 0; if(upTimePath == null) { log(Level.ERROR, "No xpath for the uptime for " + this); return 0; } try { XPath xpather = localXpath.get(); Node upTimeNode = (Node) xpather.evaluate(upTimePath, d, XPathConstants.NODE); if(upTimeNode != null) { log(Level.TRACE, "Will parse uptime: %s", upTimeNode.getTextContent()); String dateString = upTimeNode.getTextContent(); uptime = jrds.Util.parseStringNumber(dateString, 0L); } log(Level.ALL, "uptime is %d", uptime); } catch (XPathExpressionException e) { log(Level.ERROR, e, "Uptime not found"); } return uptime; }
public Document getDocument(InputSource stream) { DocumentBuilder dbuilder = localDocumentBuilder.get(); Document d = null; log(Level.TRACE, "%s %s %s started %s@%s", stream, dbuilder, isStarted(), getClass().getName(), Integer.toHexString(hashCode())); try { dbuilder.reset(); d = dbuilder.parse(stream); log(Level.TRACE, "just parsed a %s", d.getDocumentElement().getTagName()); } catch (SAXException e) { log(Level.ERROR, e, "Invalid XML: %s", e.getMessage()); } catch (IOException e) { log(Level.ERROR, e, "IO Exception getting values: %s", e.getMessage()); } return d; }
public Map<XPathExpression, Number> fileFromXpaths(Document d, Set<XPathExpression> xpaths) { Map<XPathExpression, Number> values = new HashMap<>(xpaths.size()); for(XPathExpression xpath: xpaths) { try { log(Level.TRACE, "Will search the xpath \"%s\"", xpath); Node n = (Node) xpath.evaluate(d, XPathConstants.NODE); Double value; if(n != null) { value = jrds.Util.parseStringNumber(n.getTextContent(), Double.NaN); values.put(xpath, value); } } catch (XPathExpressionException e) { log(Level.ERROR, "Invalid XPATH : " + xpath + " for " + this); } catch (NumberFormatException e) { log(Level.WARN, e, "value read from %s not parsable", xpath); } } log(Level.TRACE, "Values found: %s", values); return values; }
public long findUptimeByDate(Document d, String startTimePath, String currentTimePath, DateFormat pattern) { XPath xpather = localXpath.get(); try { Node startTimeNode = (Node) xpather.evaluate(startTimePath, d, XPathConstants.NODE); String startTimeString = startTimeNode.getTextContent(); Date startTime = pattern.parse(startTimeString); Node currentTimeNode = (Node) xpather.evaluate(currentTimePath, d, XPathConstants.NODE); String currentTimeString = currentTimeNode.getTextContent(); Date currentTime = pattern.parse(currentTimeString); return (currentTime.getTime() - startTime.getTime()) / 1000; } catch (XPathExpressionException e) { log(Level.ERROR, e, "Time not found"); } catch (ParseException e) { log(Level.ERROR, e, "Date not parsed with pattern " + ((SimpleDateFormat) pattern).toPattern()); } return 0; }