KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > DigestFilter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: DigestFilter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.util;
21
22 import java.security.MessageDigest JavaDoc;
23 import java.security.NoSuchAlgorithmException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.TreeMap JavaDoc;
27
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.SAXNotRecognizedException JavaDoc;
31 import org.xml.sax.SAXNotSupportedException JavaDoc;
32 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
33
34 /**
35  * SAX filter which produces a digest over the XML elements.
36  * Insignificant whitespace as determined by the, comments and
37  * processing instructions are not part of the digest.
38  * If the filter is namespace aware, the URI and local name for
39  * each element is digested, otherwise the QName. Attributes are
40  * sorted before the name-value pairs are digested.
41  *
42  */

43 public class DigestFilter extends XMLFilterImpl JavaDoc {
44
45     private MessageDigest JavaDoc digest;
46     private byte value[];
47     private boolean isNamespaceAware;
48
49     public DigestFilter(String JavaDoc algorithm) throws NoSuchAlgorithmException JavaDoc {
50         digest = MessageDigest.getInstance(algorithm);
51     }
52
53     public byte[] getDigestValue() {
54         return value;
55     }
56
57     public String JavaDoc getDigestString() {
58         if (value != null) {
59             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(2 * value.length);
60             for (int i = 0; i < value.length; i++) {
61                 int val = value[i];
62                 int hi = (val >> 4) & 0xF;
63                 int lo = val & 0xF;
64                 if (hi < 10) {
65                     buffer.append((char) (hi + 0x30));
66                 } else {
67                     buffer.append((char) (hi + 0x61 - 10));
68                 }
69                 if (lo < 10) {
70                     buffer.append((char) (lo + 0x30));
71                 } else {
72                     buffer.append((char) (lo + 0x61 - 10));
73                 }
74             }
75             return buffer.toString();
76         } else {
77             return null;
78         }
79     }
80
81     /* (non-Javadoc)
82      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
83      */

84     public void characters(char[] chars, int start, int length)
85         throws SAXException JavaDoc {
86         digest.update(new String JavaDoc(chars, start, length).getBytes());
87         super.characters(chars, start, length);
88     }
89
90     /* (non-Javadoc)
91      * @see org.xml.sax.ContentHandler#endDocument()
92      */

93     public void endDocument() throws SAXException JavaDoc {
94         value = digest.digest();
95         super.endDocument();
96     }
97
98     /* (non-Javadoc)
99      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
100      */

101     public void startElement(
102         String JavaDoc url,
103         String JavaDoc localName,
104         String JavaDoc qName,
105         Attributes JavaDoc attr)
106         throws SAXException JavaDoc {
107         Map JavaDoc map = new TreeMap JavaDoc();
108         if (isNamespaceAware) {
109             digest.update(url.getBytes());
110             digest.update(localName.getBytes());
111             for (int i = 0; i < attr.getLength(); i++) {
112                 map.put(
113                     attr.getLocalName(i) + attr.getURI(i),
114                     attr.getValue(i));
115             }
116         } else {
117             digest.update(qName.getBytes());
118             for (int i = 0; i < attr.getLength(); i++) {
119                 map.put(attr.getQName(i), attr.getValue(i));
120             }
121         }
122         for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
123             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
124             digest.update(((String JavaDoc)entry.getKey()).getBytes());
125             digest.update(((String JavaDoc)entry.getValue()).getBytes());
126         }
127         super.startElement(url, localName, qName, attr);
128     }
129
130
131     /* (non-Javadoc)
132      * @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
133      */

134     public void setFeature(String JavaDoc feature, boolean value)
135         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
136         if(feature.equals("http://xml.org/sax/features/namespaces")) {
137             isNamespaceAware = value;
138         }
139         super.setFeature(feature, value);
140     }
141
142 }
143
Popular Tags