Skip to content

Commit

Permalink
Fix #1250 Make streaming XML input debugable (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit van Brakel authored Nov 10, 2020
1 parent a7ae659 commit 8c108e0
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
23 changes: 21 additions & 2 deletions core/src/main/java/nl/nn/adapterframework/senders/XsltSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import nl.nn.adapterframework.xml.PrettyPrintFilter;
import nl.nn.adapterframework.xml.SkipEmptyTagsFilter;
import nl.nn.adapterframework.xml.TransformerFilter;
import nl.nn.adapterframework.xml.XmlTap;
import nl.nn.adapterframework.xml.XmlWriter;

/**
Expand Down Expand Up @@ -80,6 +81,7 @@ public class XsltSender extends StreamingSenderBase implements IThreadCreator {
private boolean skipEmptyTags=false;
private int xsltVersion=0; // set to 0 for auto detect.
private boolean namespaceAware=XmlUtils.isNamespaceAwareByDefault();
private boolean debugInput = false;

private TransformerPool transformerPool;

Expand Down Expand Up @@ -281,8 +283,17 @@ public PipeRunResult sendMessage(Message message, IPipeLineSession session, IFor
try {
try (MessageOutputStream target=MessageOutputStream.getTargetStream(this, session, next)) {
ContentHandler handler = createHandler(message, session, target);
InputSource source = message.asInputSource();
if (isDebugInput() && log.isDebugEnabled()) {
handler = new XmlTap(handler) {
@Override
public void endDocument() throws SAXException {
super.endDocument();
log.debug(getLogPrefix()+" xml input ["+getWriter()+"]");
}
};
}
XMLReader reader = getXmlReader(handler);
InputSource source = message.asInputSource();
reader.parse(source);
return target.getPipeRunResult();
}
Expand Down Expand Up @@ -393,7 +404,15 @@ public boolean isNamespaceAware() {
return namespaceAware;
}

@IbisDoc({"13", "when set <code>true</code> xslt processor 2.0 (net.sf.saxon) will be used, otherwise xslt processor 1.0 (org.apache.xalan)", "false"})
@IbisDoc({"13", "when set <code>true</code> the input is written to the log file, at DEBUG level", "false"})
public void setDebugInput(boolean debugInput) {
this.debugInput = debugInput;
}
public boolean isDebugInput() {
return debugInput;
}

@IbisDoc({"14", "when set <code>true</code> xslt processor 2.0 (net.sf.saxon) will be used, otherwise xslt processor 1.0 (org.apache.xalan)", "false"})
/**
* @deprecated Please remove setting of xslt2, it will be auto detected. Or use xsltVersion.
*/
Expand Down
35 changes: 35 additions & 0 deletions core/src/main/java/nl/nn/adapterframework/xml/XmlTap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2020 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nl.nn.adapterframework.xml;

import org.xml.sax.ContentHandler;

public class XmlTap extends XmlTee {

public XmlTap() {
super();
setSecondContentHandler(new XmlWriter());
}

public XmlTap(ContentHandler handler) {
super(handler, new XmlWriter());
}

public XmlWriter getWriter() {
return (XmlWriter)getSecondContentHandler();
}

}
127 changes: 127 additions & 0 deletions core/src/main/java/nl/nn/adapterframework/xml/XmlTee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2020 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nl.nn.adapterframework.xml;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

public class XmlTee extends FullXmlFilter {

private ContentHandler handler=null;

public XmlTee() {
super();
}

public XmlTee(ContentHandler handler, ContentHandler secondHandler) {
super(handler);
this.handler = secondHandler;
}

public void setSecondContentHandler(ContentHandler handler) {
this.handler = handler;
}
public ContentHandler getSecondContentHandler() {
return handler;
}

@Override
public void startDocument() throws SAXException {
if (handler!=null) handler.startDocument();
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
if (handler!=null) handler.endDocument();
super.endDocument();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (handler!=null) handler.startElement(uri, localName, qName, atts);
super.startElement(uri, localName, qName, atts);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (handler!=null) handler.endElement(uri, localName, qName);
super.endElement(uri, localName, qName);
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (handler!=null) handler.characters(ch, start, length);
super.characters(ch, start, length);
}

@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
if (handler!=null) handler.startPrefixMapping(prefix, uri);
super.startPrefixMapping(prefix, uri);
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
if (handler!=null) handler.endPrefixMapping(prefix);
super.endPrefixMapping(prefix);
}

@Override
public void comment(char[] ch, int start, int length) throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).comment(ch, start, length);
super.comment(ch, start, length);
}

@Override
public void startCDATA() throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).startCDATA();
super.startCDATA();
}
@Override
public void endCDATA() throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).endCDATA();
super.endCDATA();
}

@Override
public void startDTD(String name, String publicId, String systemId) throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).startDTD(name, publicId, systemId);
super.startDTD(name, publicId, systemId);
}
@Override
public void endDTD() throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).endDTD();
super.endDTD();
}

@Override
public void startEntity(String name) throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).startEntity(name);
super.startEntity(name);
}
@Override
public void endEntity(String name) throws SAXException {
if (handler!=null && handler instanceof LexicalHandler) ((LexicalHandler)handler).endEntity(name);
super.endEntity(name);
}

@Override
public void processingInstruction(String target, String data) throws SAXException {
if (handler!=null) handler.processingInstruction(target, data);
super.processingInstruction(target, data);
}

}
24 changes: 24 additions & 0 deletions core/src/test/java/nl/nn/adapterframework/xml/XmlTapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.nn.adapterframework.xml;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import nl.nn.adapterframework.testutil.TestFileUtils;
import nl.nn.adapterframework.util.XmlUtils;

public class XmlTapTest {


@Test
public void testBasic() throws Exception {
String input = TestFileUtils.getTestFile("/Xslt/AnyXml/in.xml");
String expected = input;
XmlWriter xmlWriter = new XmlWriter();
XmlTap xmlTap = new XmlTap(xmlWriter);
XmlUtils.parseXml(input, xmlTap);
assertEquals(expected,xmlWriter.toString());
assertEquals(expected,xmlTap.getWriter().toString());
}

}

0 comments on commit 8c108e0

Please sign in to comment.