1 28 29 package org.jibx.extras; 30 31 import java.io.IOException ; 32 import java.io.PrintStream ; 33 import java.io.Reader ; 34 35 import org.xmlpull.v1.XmlPullParser; 36 import org.xmlpull.v1.XmlPullParserException; 37 import org.xmlpull.v1.XmlPullParserFactory; 38 39 49 50 public class DocumentComparator 51 { 52 53 protected XmlPullParser m_parserA; 54 55 56 protected XmlPullParser m_parserB; 57 58 59 protected PrintStream m_differencePrint; 60 61 67 68 public DocumentComparator(PrintStream print) throws XmlPullParserException { 69 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 70 factory.setNamespaceAware(true); 71 m_parserA = factory.newPullParser(); 72 m_parserB = factory.newPullParser(); 73 m_differencePrint = print; 74 } 75 76 82 83 protected String buildPositionString(XmlPullParser parser) { 84 return " line " + parser.getLineNumber() + ", col " + 85 parser.getColumnNumber(); 86 } 87 88 94 95 protected void printError(String msg) { 96 if (m_differencePrint != null) { 97 m_differencePrint.println(msg + " - from " + 98 buildPositionString(m_parserA) + " to " + 99 buildPositionString(m_parserB)); 100 } 101 } 102 103 109 110 protected boolean matchAttributes() { 111 int count = m_parserA.getAttributeCount(); 112 if (m_parserB.getAttributeCount() != count) { 113 return false; 114 } 115 for (int i = 0; i < count; i++) { 116 String name = m_parserA.getAttributeName(i); 117 String ns = m_parserA.getAttributeNamespace(i); 118 String value = m_parserA.getAttributeValue(i); 119 if (!value.equals(m_parserB.getAttributeValue(ns, name))) { 120 return false; 121 } 122 } 123 return true; 124 } 125 126 131 132 protected boolean matchNames() { 133 return m_parserA.getName().equals(m_parserB.getName()) && 134 m_parserA.getNamespace().equals(m_parserB.getNamespace()); 135 } 136 137 148 149 public boolean compare(Reader rdra, Reader rdrb) { 150 try { 151 152 m_parserA.setInput(rdra); 154 m_parserB.setInput(rdrb); 155 boolean content = false; 156 String texta = ""; 157 String textb = ""; 158 while (true) { 159 160 if (m_parserA.getEventType() == XmlPullParser.TEXT) { 162 texta = m_parserA.getText(); 163 m_parserA.next(); 164 } 165 if (m_parserB.getEventType() == XmlPullParser.TEXT) { 166 textb = m_parserB.getText(); 167 m_parserB.next(); 168 } 169 170 int typea = m_parserA.getEventType(); 172 int typeb = m_parserB.getEventType(); 173 if (typea != typeb) { 174 printError("Different document structure"); 175 return false; 176 } else if (typea == XmlPullParser.START_TAG) { 177 178 content = true; 180 if (!matchNames()) { 181 printError("Different start tags"); 182 return false; 183 } else if (!matchAttributes()) { 184 printError("Different attributes"); 185 return false; 186 } else if (!texta.trim().equals(textb.trim())) { 187 printError("Different text content between elements"); 188 return false; 189 } 190 texta = textb = ""; 191 192 } else if (typea == XmlPullParser.END_TAG) { 193 194 if (!matchNames()) { 196 printError("Different end tags"); 197 return false; 198 } 199 if (content) { 200 if (!texta.equals(textb)) { 201 printError("Different text content"); 202 if (m_differencePrint != null) { 203 m_differencePrint.println(" \"" + texta + 204 "\" (length " + texta.length() + " vs. \"" + 205 textb + "\" (length " + textb.length() + ')'); 206 207 } 208 return false; 209 } 210 content = false; 211 } else if (!texta.trim().equals(textb.trim())) { 212 printError("Different text content between elements"); 213 return false; 214 } 215 texta = textb = ""; 216 217 } else if (typea == XmlPullParser.END_DOCUMENT) { 218 return true; 219 } 220 221 m_parserA.next(); 223 m_parserB.next(); 224 225 } 226 } catch (IOException ex) { 227 if (m_differencePrint != null) { 228 ex.printStackTrace(m_differencePrint); 229 } 230 return false; 231 } catch (XmlPullParserException ex) { 232 if (m_differencePrint != null) { 233 ex.printStackTrace(m_differencePrint); 234 } 235 return false; 236 } 237 } 238 } | Popular Tags |