Created
February 9, 2016 17:01
-
-
Save BDF/9bcb82761b169398fcbe to your computer and use it in GitHub Desktop.
Code using net.sf.saxon.functions.DeepEqual.deepEquals to compare the equality of two XdmNodes. Very saxon specific. Is this the deepEquals method theadsafe?
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
import net.sf.saxon.Configuration; | |
import net.sf.saxon.expr.XPathContext; | |
import net.sf.saxon.expr.sort.CodepointCollator; | |
import net.sf.saxon.expr.sort.GenericAtomicComparer; | |
import net.sf.saxon.functions.DeepEqual; | |
import net.sf.saxon.lib.UnfailingErrorListener; | |
import net.sf.saxon.s9api.Processor; | |
import net.sf.saxon.s9api.XdmNode; | |
import net.sf.saxon.trans.XPathException; | |
import javax.xml.transform.TransformerException; | |
public class DeepEqualsWrapper { | |
private final Processor proc; | |
// Singleton processor created within a spring context. | |
public DeepEqualsWrapper(Processor proc) { | |
proc = this.proc; | |
} | |
private class LocalErrorListener implements UnfailingErrorListener { | |
private final StringBuilder errors; | |
LocalErrorListener() { | |
errors = new StringBuilder(); | |
} | |
@Override | |
public void warning(TransformerException e) { | |
errors.append(e.getMessage()); | |
} | |
@Override | |
public void error(TransformerException e) { | |
errors.append(e.getMessage()); | |
} | |
@Override | |
public void fatalError(TransformerException e) { | |
errors.append(e.getMessage()); | |
} | |
public String getErrors() { | |
return errors.toString(); | |
} | |
} | |
// XdmNodes are created by the same Processor that has been passed into this class. | |
public String getNodeDifferences(XdmNode node1, XdmNode node2) throws XPathException { | |
Configuration config = new Configuration(); | |
LocalErrorListener errorListener = new LocalErrorListener(); | |
config.setErrorListener(errorListener); | |
XPathContext context = config.getConversionContext().newContext(); | |
GenericAtomicComparer comparer = new GenericAtomicComparer(CodepointCollator.getInstance(), context); | |
boolean isEqual = DeepEqual.deepEquals( | |
node1.getUnderlyingNode(), | |
node2.getUnderlyingNode(), | |
comparer, | |
config, | |
DeepEqual.INCLUDE_PREFIXES | DeepEqual.EXCLUDE_WHITESPACE_TEXT_NODES | DeepEqual.INCLUDE_PROCESSING_INSTRUCTIONS); | |
String differences; | |
if (isEqual) { | |
differences = ""; | |
} else { | |
differences = errorListener.getErrors(); | |
} | |
return differences; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment