/** * Test whether an XPath is valid. It seems the Xalan has no easy way to check, so this creates a * dummy test document, then tries to evaluate the xpath against it. * * @param xpathString XPath String to validate * @param showDialog weather to show a dialog * @return returns true if valid, valse otherwise. */ public static boolean validXPath(String xpathString, boolean showDialog) { String ret = null; boolean success = true; Document testDoc = null; try { testDoc = XPathUtil.makeDocumentBuilder(false, false, false, false).newDocument(); Element el = testDoc.createElement("root"); // $NON-NLS-1$ testDoc.appendChild(el); XPathUtil.validateXPath(testDoc, xpathString); } catch (IllegalArgumentException e) { log.warn(e.getLocalizedMessage()); success = false; ret = e.getLocalizedMessage(); } catch (ParserConfigurationException e) { success = false; ret = e.getLocalizedMessage(); } catch (TransformerException e) { success = false; ret = e.getLocalizedMessage(); } if (showDialog) { JOptionPane.showMessageDialog( null, (success) ? JMeterUtils.getResString("xpath_assertion_valid") : ret, // $NON-NLS-1$ (success) ? JMeterUtils.getResString("xpath_assertion_valid") : JMeterUtils //$NON-NLS-1$ .getResString("xpath_assertion_failed"), (success) ? JOptionPane.INFORMATION_MESSAGE // $NON-NLS-1$ : JOptionPane.ERROR_MESSAGE); } return success; }
/** * Converts (X)HTML response to DOM object Tree. This version cares of charset of response. * * @param unicodeData * @return */ private Document parseResponse(String unicodeData, XPathExtractor extractor) throws UnsupportedEncodingException, IOException, ParserConfigurationException, SAXException, TidyException { // TODO: validate contentType for reasonable types? // NOTE: responseData encoding is server specific // Therefore we do byte -> unicode -> byte conversion // to ensure UTF-8 encoding as required by XPathUtil // convert unicode String -> UTF-8 bytes byte[] utf8data = unicodeData.getBytes("UTF-8"); // $NON-NLS-1$ ByteArrayInputStream in = new ByteArrayInputStream(utf8data); boolean isXML = JOrphanUtils.isXML(utf8data); // this method assumes UTF-8 input data return XPathUtil.makeDocument( in, false, false, extractor.useNameSpace(), extractor.isTolerant(), extractor.isQuiet(), extractor.showWarnings(), extractor.reportErrors(), isXML, extractor.isDownloadDTDs()); }
private String process(String textToParse, XPathExtractor extractor) { try { Document doc = parseResponse(textToParse, extractor); List<String> matchStrings = new ArrayList<String>(); XPathUtil.putValuesForXPathInList( doc, xpathExpressionField.getText(), matchStrings, extractor.getFragment()); StringBuilder builder = new StringBuilder(); int nbFound = matchStrings.size(); builder.append("Match count: ").append(nbFound).append("\n"); for (int i = 0; i < nbFound; i++) { builder .append("Match[") .append(i + 1) .append("]=") .append(matchStrings.get(i)) .append("\n"); } return builder.toString(); } catch (Exception e) { return "Exception:" + ExceptionUtils.getStackTrace(e); } }