| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.NodeName; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.type.SchemaType; |
| 15 |
import net.sf.saxon.type.SimpleType; |
| 16 |
import net.sf.saxon.value.Whitespace; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class CheckSumFilter extends ProxyReceiver { |
| 28 |
|
| 29 |
private final static boolean DEBUG = false; |
| 30 |
|
| 31 |
int checksum = 0; |
| 32 |
int sequence = 0; |
| 33 |
boolean checkExistingChecksum = false; |
| 34 |
boolean checksumCorrect = false; |
| 35 |
boolean checksumFound = false; |
| 36 |
|
| 37 |
public final static String SIGMA = "\u03A3"; |
| 38 |
|
| 39 |
public CheckSumFilter(Receiver nextReceiver) { |
| 40 |
super(nextReceiver); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public void setCheckExistingChecksum(boolean check) { |
| 49 |
this.checkExistingChecksum = check; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void startDocument(int properties) throws XPathException { |
| 54 |
if (DEBUG) { |
| 55 |
System.err.println("CHECKSUM - START DOC"); |
| 56 |
} |
| 57 |
super.startDocument(properties); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@Override |
| 71 |
public void append(Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 72 |
checksum ^= hash(item.toString(), sequence++); |
| 73 |
if (DEBUG) { |
| 74 |
System.err.println("After append: " + Integer.toHexString(checksum)); |
| 75 |
} |
| 76 |
super.append(item, locationId, copyNamespaces); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 95 |
checksum ^= hash(nameCode, sequence); |
| 96 |
checksum ^= hash(value, sequence); |
| 97 |
if (DEBUG) { |
| 98 |
System.err.println("After attribute " + nameCode.getDisplayName() + ": " + checksum + |
| 99 |
"(" + hash(nameCode, sequence) + "," + hash(value, sequence) + ")"); |
| 100 |
} |
| 101 |
super.attribute(nameCode, typeCode, value, locationId, properties); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 109 |
if (!Whitespace.isWhite(chars)) { |
| 110 |
checksum ^= hash(chars, sequence++); |
| 111 |
if (DEBUG) { |
| 112 |
System.err.println("After characters " + chars + ": " + Integer.toHexString(checksum)); |
| 113 |
} |
| 114 |
} |
| 115 |
super.characters(chars, locationId, properties); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Override |
| 126 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 127 |
checksum ^= hash(elemName, sequence++); |
| 128 |
if (DEBUG) { |
| 129 |
System.err.println("After startElement " + elemName.getDisplayName() + ": " + checksum); |
| 130 |
} |
| 131 |
checksumCorrect = false; |
| 132 |
super.startElement(elemName, typeCode, location, properties); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
@Override |
| 139 |
public void endElement() throws XPathException { |
| 140 |
checksum ^= 1; |
| 141 |
if (DEBUG) { |
| 142 |
System.err.println("After endElement: " + checksum); |
| 143 |
} |
| 144 |
super.endElement(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@Override |
| 151 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 152 |
if (target.equals(SIGMA)) { |
| 153 |
checksumFound = true; |
| 154 |
if (checkExistingChecksum) { |
| 155 |
try { |
| 156 |
int found = (int) Long.parseLong("0" + data, 16); |
| 157 |
checksumCorrect = found == checksum; |
| 158 |
} catch (NumberFormatException e) { |
| 159 |
checksumCorrect = false; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
super.processingInstruction(target, data, locationId, properties); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public boolean isChecksumFound() { |
| 172 |
return checksumFound; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public int getChecksum() { |
| 181 |
return checksum; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public boolean isChecksumCorrect() { |
| 191 |
return checksumCorrect || "skip".equals(System.getProperty("saxon-checksum")); |
| 192 |
} |
| 193 |
|
| 194 |
private int hash(CharSequence s, int sequence) { |
| 195 |
int h = sequence<<8; |
| 196 |
for (int i=0; i<s.length(); i++) { |
| 197 |
h = (h<<1) + s.charAt(i); |
| 198 |
} |
| 199 |
return h; |
| 200 |
} |
| 201 |
|
| 202 |
private int hash(NodeName n, int sequence) { |
| 203 |
|
| 204 |
return hash(n.getLocalPart(), sequence) ^ hash(n.getURI(), sequence); |
| 205 |
} |
| 206 |
} |
| 207 |
|