Created
June 28, 2016 18:54
-
-
Save shapiy/a8b3943a495180752f3d9902f8d9ce03 to your computer and use it in GitHub Desktop.
XSLT SAXParser which inserts user trace calls in xsl:template instructions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rocks.xml.saxontrace; | |
import org.apache.xerces.parsers.SAXParser; | |
import org.apache.xerces.util.AugmentationsImpl; | |
import org.apache.xerces.util.XMLAttributesImpl; | |
import org.apache.xerces.xni.Augmentations; | |
import org.apache.xerces.xni.QName; | |
import org.apache.xerces.xni.XMLAttributes; | |
import org.apache.xerces.xni.XNIException; | |
public class TraceXmlReader extends SAXParser { | |
private static final QName TEMPLATE_NAME = new QName("xsl", "template", "xsl:template", "http://www.w3.org/1999/XSL/Transform"); | |
private static final QName SEQUENCE_NAME = new QName("xsl", "sequence", "xsl:sequence", "http://www.w3.org/1999/XSL/Transform"); | |
private static final QName SELECT_NAME = new QName(null, "select", "value", "http://www.w3.org/1999/XSL/Transform"); | |
@Override | |
public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { | |
super.startElement(element, attributes, augs); | |
if (isMatchTemplate(element, attributes)) { | |
sequenceWithTraceCall(attributes); | |
} | |
} | |
private boolean isMatchTemplate(QName element, XMLAttributes attributes) { | |
return TEMPLATE_NAME.equals(element) && attributes.getIndex("match") != -1; | |
} | |
/* | |
* Inserts xsl:sequence instruction with a trace call. | |
* | |
* The trace info contains the match pattern. For instance, | |
* <xsl:sequence select="trace(., 'attribute() | node()')"/> | |
*/ | |
private void sequenceWithTraceCall(XMLAttributes attributes) { | |
XMLAttributesImpl sequenceAttributes = new XMLAttributesImpl(); | |
String selectValue = String.format("trace((), '%s')", attributes.getValue("match")); | |
sequenceAttributes.addAttribute(SELECT_NAME, "CDATA", selectValue); | |
super.startElement(SEQUENCE_NAME, sequenceAttributes, new AugmentationsImpl()); | |
super.endElement(SEQUENCE_NAME, new AugmentationsImpl()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment