use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.
the class XmlUnitService method initInputTranslator.
/**
* Initializes {@link #inputTranslator} by two {@link InputTranslator}
* <ul>
* <li>One for handle the <code>url</code> prefix</li>
* <li>One for handle without prefix</li>
* </ul>
*/
private void initInputTranslator() {
inputTranslator = new InputTranslatorManager<Document>();
// Add handling on the "url" prefix, to get URL input
inputTranslator.addTranslator(new AInputTranslator<Document>("url") {
@Override
public Document translate(String input) throws InputTranslatorException {
try {
URL urlInput = new URL(InputTranslatorUtil.getValue(input));
return XmlUtil.fromURL(urlInput);
} catch (MalformedURLException e) {
throw new InputTranslatorException(e);
} catch (XmlUtilException e) {
throw new InputTranslatorException(e);
}
}
});
// Add handling for raw XML input
inputTranslator.addTranslator(new AInputTranslator<Document>(null) {
@Override
public Document translate(String input) throws InputTranslatorException {
try {
return XmlUtil.fromString(input);
} catch (XmlUtilException e) {
throw new InputTranslatorException(e);
}
}
});
}
use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.
the class XmlUnitService method getFromXml.
@Override
public String getFromXml(final String xmlToParse, final String xpath) {
if (xpath == null) {
LOG.warn("Null argument");
return DEFAULT_GET_FROM_XML_VALUE;
}
try {
final Document document = StringUtil.isURL(xmlToParse) ? XmlUtil.fromURL(new URL(xmlToParse)) : XmlUtil.fromString(xmlToParse);
final String result = XmlUtil.evaluateString(document, xpath);
// Not that in case of multiple values then send the first one
return result != null && result.length() > 0 ? result : DEFAULT_GET_FROM_XML_VALUE;
} catch (XmlUtilException e) {
LOG.warn("Unable to get from xml", e);
} catch (MalformedURLException e) {
LOG.warn("Unable to get from xml", e);
}
return DEFAULT_GET_FROM_XML_VALUE;
}
use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.
the class XmlUnitService method isElementEquals.
@Override
public boolean isElementEquals(String lastSOAPResponse, String xpath, String expectedElement) {
if (lastSOAPResponse == null || xpath == null || expectedElement == null) {
LOG.warn("Null argument");
return false;
}
try {
NodeList candidates = XmlUtil.evaluate(lastSOAPResponse, xpath);
LOG.debug(candidates.toString());
for (Document candidate : XmlUtil.fromNodeList(candidates)) {
if (Differences.fromString(getDifferencesFromXml(XmlUtil.toString(candidate), expectedElement)).isEmpty()) {
return true;
}
}
} catch (XmlUtilException xue) {
LOG.warn("Unable to check if element equality", xue);
} catch (DifferencesException de) {
LOG.warn("Unable to check if element equality", de);
}
return false;
}
use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.
the class Differences method toDocument.
/**
* Returns a {@link Document} representation of this {@link Differences} by following the {@link Differences} format.
*
* @return a {@link Document} representation of this {@link Differences}
* @throws DifferencesException
* if an error occurred
*/
public Document toDocument() throws DifferencesException {
// Creates the result document which will contain difference list
Document resultDoc = null;
try {
resultDoc = XmlUtil.newDocument();
} catch (XmlUtilException e) {
throw new DifferencesException(e);
}
// Creates the root node
Element resultRoot = resultDoc.createElement(DIFFERENCES_NODE);
resultDoc.appendChild(resultRoot);
// Appends differences to the root node
for (Difference diff : differences) {
Element element = resultDoc.createElement(DIFFERENCE_NODE);
element.appendChild(resultDoc.createTextNode(diff.getDiff()));
resultRoot.appendChild(element);
}
// Returns the result document
return resultDoc;
}
use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.
the class Differences method mkString.
/**
* Returns a {@link String} representation of this {@link Differences} by following the {@link Differences} format.
*
* <p>
* If this {@link Differences} is empty then returned the {@link #EMPTY_DIFFERENCES_STRING} value
* </p>
*
* <p>
* In case of error, then returned <code>null</code>
* </p>
*/
public String mkString() {
try {
Document doc = toDocument();
XPath path = XPathFactory.newInstance().newXPath();
NodeList list = (NodeList) path.compile(DIFFERENCE_COMMON_XPATH).evaluate(doc, XPathConstants.NODESET);
return list.getLength() == 0 ? EMPTY_DIFFERENCES_STRING : XmlUtil.toString(doc);
} catch (XmlUtilException e) {
return null;
} catch (DifferencesException e) {
return null;
} catch (XPathExpressionException e) {
return null;
}
}
Aggregations