use of nl.nn.adapterframework.xml.NamespaceRemovingFilter in project iaf by ibissource.
the class MatchUtils method xmlPretty.
public static String xmlPretty(String xml, boolean removeNamespaces) {
XmlWriter xmlWriter = new XmlWriter();
xmlWriter.setIncludeComments(false);
ContentHandler contentHandler = new PrettyPrintFilter(xmlWriter);
if (removeNamespaces) {
contentHandler = new NamespaceRemovingFilter(contentHandler);
}
try {
XmlUtils.parseXml(xml, contentHandler);
return xmlWriter.toString();
} catch (IOException | SAXException e) {
throw new RuntimeException("ERROR: could not prettify [" + xml + "]", e);
}
}
use of nl.nn.adapterframework.xml.NamespaceRemovingFilter in project iaf by ibissource.
the class ForEachChildElementPipe method createHandler.
protected void createHandler(HandlerRecord result, ThreadConnector threadConnector, PipeLineSession session, ItemCallback callback) throws TransformerConfigurationException {
result.itemHandler = new ItemCallbackCallingHandler(callback);
result.inputHandler = result.itemHandler;
if (getXmlDebugger() != null && (StringUtils.isNotEmpty(getContainerElement()) || StringUtils.isNotEmpty(getTargetElement()) || getExtractElementsTp() != null)) {
String containerElementString = StringUtils.isNotEmpty(getContainerElement()) ? "filter to containerElement '" + getContainerElement() + "'" : null;
String targetElementString = StringUtils.isNotEmpty(getTargetElement()) ? "filter to targetElement '" + getTargetElement() + "'" : null;
String xpathString = getExtractElementsTp() != null ? "filter XPath '" + getElementXPathExpression() + "'" : null;
String label = "XML after preprocessing: " + Misc.concat(", ", containerElementString, targetElementString, xpathString);
result.inputHandler = getXmlDebugger().inspectXml(session, label, result.inputHandler);
}
if (isRemoveNamespaces()) {
result.inputHandler = new NamespaceRemovingFilter(result.inputHandler);
}
if (getExtractElementsTp() != null) {
if (log.isDebugEnabled())
log.debug("transforming input to obtain list of elements using xpath [" + getElementXPathExpression() + "]");
TransformerFilter transformerFilter = getExtractElementsTp().getTransformerFilter(threadConnector, result.inputHandler);
result.inputHandler = transformerFilter;
result.transformerErrorListener = (TransformerErrorListener) transformerFilter.getErrorListener();
result.errorMessage = "Could not process list of elements using xpath [" + getElementXPathExpression() + "]";
}
if (StringUtils.isNotEmpty(getTargetElement())) {
result.inputHandler = new NodeSetFilter(XmlUtils.getNamespaceMap(getNamespaceDefs()), getTargetElement(), true, true, result.inputHandler);
}
if (StringUtils.isNotEmpty(getContainerElement())) {
result.inputHandler = new NodeSetFilter(XmlUtils.getNamespaceMap(getNamespaceDefs()), getContainerElement(), false, true, result.inputHandler);
}
result.inputHandler = new StopSensor(result.itemHandler, result.inputHandler);
result.inputHandler = new ExceptionCatchingFilter(result.inputHandler) {
@Override
protected void handleException(Exception e) throws SAXException {
if (e instanceof SaxTimeoutException) {
throw (SaxTimeoutException) e;
}
if (result.itemHandler.isStopRequested()) {
if (e instanceof SaxAbortException) {
throw (SaxAbortException) e;
}
throw new SaxAbortException(e);
}
// For improved diagnosability of error situations, rethrow the original exception, where applicable.
if (result.transformerErrorListener != null) {
TransformerException tex = result.transformerErrorListener.getFatalTransformerException();
if (tex != null) {
throw new SaxException(result.errorMessage, tex);
}
IOException iox = result.transformerErrorListener.getFatalIOException();
if (iox != null) {
throw new SaxException(result.errorMessage, iox);
}
}
throw new SaxException(result.errorMessage, e);
}
};
}
use of nl.nn.adapterframework.xml.NamespaceRemovingFilter in project iaf by ibissource.
the class Json2XmlValidator method alignJson.
protected PipeRunResult alignJson(String messageToValidate, PipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
ValidationContext context;
ValidatorHandler validatorHandler;
try {
context = validator.createValidationContext(session, getJsonRootValidations(responseMode), getInvalidRootNamespaces());
validatorHandler = validator.getValidatorHandler(session, context);
} catch (ConfigurationException e) {
throw new PipeRunException(this, "Cannot create ValidationContext", e);
}
String resultEvent;
String out = null;
try {
Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
if (StringUtils.isNotEmpty(getTargetNamespace())) {
aligner.setTargetNamespace(getTargetNamespace());
}
aligner.setDeepSearch(isDeepSearch());
aligner.setErrorHandler(context.getErrorHandler());
aligner.setFailOnWildcards(isFailOnWildcards());
aligner.setIgnoreUndeclaredElements(isIgnoreUndeclaredElements());
ParameterList parameterList = getParameterList();
if (parameterList != null) {
Map<String, Object> parametervalues = null;
parametervalues = parameterList.getValues(new Message(messageToValidate), session).getValueMap();
// remove parameters with null values, to support optional request parameters
for (Iterator<String> it = parametervalues.keySet().iterator(); it.hasNext(); ) {
String key = it.next();
if (parametervalues.get(key) == null) {
it.remove();
}
}
aligner.setOverrideValues(parametervalues);
}
JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
// cannot build filter chain as usual backwardly, because it ends differently.
// This will be fixed once an OutputStream can be provided to Xml2Json
XMLFilterImpl sourceFilter = aligner;
if (StringUtils.isNotEmpty(getRootElementSessionKey())) {
XMLFilterImpl storeRootFilter = new RootElementToSessionKeyFilter(session, getRootElementSessionKey(), getRootNamespaceSessionKey(), null);
aligner.setContentHandler(storeRootFilter);
sourceFilter = storeRootFilter;
}
if (getOutputFormat(session, responseMode) == DocumentFormat.JSON) {
Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
sourceFilter.setContentHandler(xml2json);
aligner.startParse(jsonStructure);
out = xml2json.toString();
} else {
XmlWriter xmlWriter = new XmlWriter();
xmlWriter.setIncludeXmlDeclaration(true);
ContentHandler handler = xmlWriter;
if (isProduceNamespaceLessXml()) {
handler = new NamespaceRemovingFilter(handler);
}
sourceFilter.setContentHandler(handler);
aligner.startParse(jsonStructure);
out = xmlWriter.toString();
}
} catch (Exception e) {
resultEvent = validator.finalizeValidation(context, session, e);
}
resultEvent = validator.finalizeValidation(context, session, null);
PipeForward forward = determineForward(resultEvent, session, responseMode);
PipeRunResult result = new PipeRunResult(forward, out);
return result;
}
Aggregations