Debian Codegraph @

net/sf/saxon/event/CheckSumFilter.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * A filter to go on a Receiver pipeline and calculate a checksum of the data passing through the pipeline.
20 * Optionally the filter will also check any checksum (represented by a processing instruction with name
21 * SIGMA) found in the file.
22 *
23 * <p>The checksum takes account of element, attribute, and text nodes only. The order of attributes
24 * within an element makes no difference.</p>
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 * Ask the filter to check any existing checksums found in the file
45 * @param check true if existing checksums are to be checked
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 * Append an arbitrary item (node or atomic value) to the output
62 *
63 * @param item the item to be appended
64 * @param locationId the location of the calling instruction, for diagnostics
65 * @param copyNamespaces if the item is an element node, this indicates whether its namespaces
66 * need to be copied. Values are {@link ReceiverOptions#ALL_NAMESPACES},
67 * {@link ReceiverOptions#LOCAL_NAMESPACES}; the default (0) means
68 * no namespaces
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 * Notify an attribute. Attributes are notified after the startElement event, and before any
81 * children. Namespaces and attributes may be intermingled.
82 * @param nameCode The name of the attribute
83 * @param typeCode The type of the attribute
84 * @param value The attribute value
85 * @param locationId The location of the attribute
86 * @param properties Bit significant value. The following bits are defined:
87 * <dl>
88 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
89 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
90 * </dl>
91 * @throws IllegalStateException: attempt to output an attribute when there is no open element
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 * Character data
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 * Notify the start of an element
120 * @param elemName integer code identifying the name of the element within the name pool.
121 * @param typeCode integer code identifying the element's type within the name pool.
122 * @param location location of the element
123 * @param properties properties of the element node
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 * End of element
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 * Processing Instruction
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 * Ask whether a checksum has been found
168 * @return true if a checksum processing instruction has been found (whether or not the checksum was correct)
169 */
170
171 public boolean isChecksumFound() {
172 return checksumFound;
173 }
174
175 /**
176 * Get the accumulated checksum
177 * @return the checksum of the events passed through the filter so far.
178 */
179
180 public int getChecksum() {
181 return checksum;
182 }
183
184 /**
185 * Ask if a correct checksum has been found in the file
186 * @return true if a checksum has been found, if its value matches, and if no significant data has been encountered
187 * after the checksum
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 //System.err.println("hash(" + n.getLocalPart() + ") " + hash(n.getLocalPart(), sequence) + "/" + hash(n.getURI(), sequence));
204 return hash(n.getLocalPart(), sequence) ^ hash(n.getURI(), sequence);
205 }
206 }
207