Package org.jibx.extras
Class DocumentComparator
- java.lang.Object
-
- org.jibx.extras.DocumentComparator
-
public class DocumentComparator extends java.lang.ObjectXML document comparator. This uses XMLPull parsers to read a pair of documents in parallel, comparing the streams of components seen from the two documents. The comparison ignores differences in whitespace separating elements, but in non-schema mode treats whitespace as significant within elements with only character data content.- Author:
- Dennis M. Sosnoski
-
-
Field Summary
Fields Modifier and Type Field Description protected java.io.PrintStreamm_differencePrintPrint stream for reporting differences.protected XmlPullParserm_parserAParser for first document.protected XmlPullParserm_parserBParser for second document.protected booleanm_schemaCompareCompare using schema data value adjustments flag.
-
Constructor Summary
Constructors Constructor Description DocumentComparator(java.io.PrintStream print)Constructor.DocumentComparator(java.io.PrintStream print, boolean schema)Constructor with schema adjustments flag specified.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static java.lang.StringbuildName(java.lang.String ns, java.lang.String name)Build name string.protected static java.lang.StringbuildPositionString(XmlPullParser parser)Build parse input position description.booleancompare(java.io.Reader rdra, java.io.Reader rdrb)Compares a pair of documents by reading them in parallel from a pair of parsers.protected booleanequalValues(java.lang.String texta, java.lang.String textb)Check for equal values.private static booleanisSchemaLocation(java.lang.String name, java.lang.String ns)Check if a name/namespace pair matches a schema namespace location attribute.protected booleanmatchAttributes()Verifies that the attributes on the current start tags match.protected booleanmatchNames()Verifies that the current start or end tag names match.protected booleanmatchText(java.lang.String texta, java.lang.String textb, java.lang.String lead)Check if two text strings match, ignoring leading and trailing spaces.private static java.lang.StringnormalizeDecimal(java.lang.String text)Normalize a decimal value for comparison.protected voidprintError(java.lang.String msg)Prints error description text.
-
-
-
Field Detail
-
m_parserA
protected XmlPullParser m_parserA
Parser for first document.
-
m_parserB
protected XmlPullParser m_parserB
Parser for second document.
-
m_differencePrint
protected java.io.PrintStream m_differencePrint
Print stream for reporting differences.
-
m_schemaCompare
protected boolean m_schemaCompare
Compare using schema data value adjustments flag.
-
-
Constructor Detail
-
DocumentComparator
public DocumentComparator(java.io.PrintStream print, boolean schema) throws XmlPullParserExceptionConstructor with schema adjustments flag specified. Builds the actual parsers and sets up for comparisons.- Parameters:
print- print stream for reporting differencesschema- use schema adjustments in comparisons flag- Throws:
XmlPullParserException- on error creating parsers
-
DocumentComparator
public DocumentComparator(java.io.PrintStream print) throws XmlPullParserExceptionConstructor. Builds the actual parser.- Parameters:
print- print stream for reporting differences- Throws:
XmlPullParserException- on error creating parsers
-
-
Method Detail
-
buildPositionString
protected static java.lang.String buildPositionString(XmlPullParser parser)
Build parse input position description.- Parameters:
parser- for which to build description- Returns:
- text description of current parse position
-
buildName
protected static java.lang.String buildName(java.lang.String ns, java.lang.String name)Build name string.- Parameters:
ns- namespace URIname- local name- Returns:
- printable names string
-
printError
protected void printError(java.lang.String msg)
Prints error description text. The generated text include position information from both documents.- Parameters:
msg- error message text
-
isSchemaLocation
private static boolean isSchemaLocation(java.lang.String name, java.lang.String ns)Check if a name/namespace pair matches a schema namespace location attribute.- Parameters:
name-ns-- Returns:
trueif a schema namespace location,falseif not
-
normalizeDecimal
private static java.lang.String normalizeDecimal(java.lang.String text)
Normalize a decimal value for comparison. A leading '+' sign is ignored, while a leading '-' is kept. If no non-zero digits are seen a "0" value is returned; otherwise, all significant digits are kept and returned (including a decimal point only if there are fraction digits)- Parameters:
text-- Returns:
- normalized value
-
equalValues
protected boolean equalValues(java.lang.String texta, java.lang.String textb)Check for equal values. If the schema compare flag is configured, this applies some basic schema rules in the comparisons, allowing '0' to match 'false' and '1' to match 'true', and comparing values using date or dateTime structure as decoded values.- Parameters:
texta-textb-- Returns:
trueif values match,falseif not
-
matchAttributes
protected boolean matchAttributes()
Verifies that the attributes on the current start tags match. Any mismatches are printed immediately.- Returns:
trueif the attributes match,falseif not
-
matchText
protected boolean matchText(java.lang.String texta, java.lang.String textb, java.lang.String lead)Check if two text strings match, ignoring leading and trailing spaces. Any mismatch is printed immediately, with the supplied lead text.- Parameters:
texta-textb-lead- error text lead- Returns:
trueif the texts match,falseif not
-
matchNames
protected boolean matchNames()
Verifies that the current start or end tag names match.- Returns:
trueif the names match,falseif not
-
compare
public boolean compare(java.io.Reader rdra, java.io.Reader rdrb)Compares a pair of documents by reading them in parallel from a pair of parsers. The comparison ignores differences in whitespace separating elements, but treats whitespace as significant within elements with only character data content.- Parameters:
rdra- reader for first document to be comparedrdrb- reader for second document to be compared- Returns:
trueif the documents are the same,falseif they're different
-
-