KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > IDFilter


1 package com.icl.saxon;
2 import org.xml.sax.*;
3 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
4 import java.util.Stack JavaDoc;
5
6
7 /**
8 * IDFilter is a SAX filter that extracts the subtree of a document rooted at the
9 * element with a given ID value. Namespace declarations outside this subtree are
10 * treated as if they were present on the identified element.
11 */

12
13 // TODO: there could be a problem with namespace declarations that are present on
14
// outer elements and then cancelled/overridden.
15

16 public class IDFilter extends XMLFilterImpl JavaDoc {
17
18     private String JavaDoc id;
19     private int activeDepth = 0;
20     private Stack JavaDoc namespacePrefixes = new Stack JavaDoc();
21     private Stack JavaDoc namespaceURIs = new Stack JavaDoc();
22     
23     public IDFilter (String JavaDoc id) {
24         // System.err.println("IDFilter, looking for " + id);
25
this.id = id;
26     }
27
28     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
29     throws SAXException {
30         // System.err.println("Start prefix " + prefix + " at " + activeDepth);
31
if (activeDepth > 0) {
32             super.startPrefixMapping(prefix, uri);
33         } else {
34             namespacePrefixes.push(prefix);
35             namespaceURIs.push(uri);
36         }
37     }
38     
39     public void endPrefixMapping(String JavaDoc prefix)
40     throws SAXException {
41         // System.err.println("End prefix " + prefix + " at " + activeDepth);
42
if (activeDepth > 0) {
43             super.endPrefixMapping(prefix);
44         } else {
45             namespacePrefixes.pop();
46             namespaceURIs.pop();
47         }
48     }
49             
50
51     public void startElement(String JavaDoc localName, String JavaDoc uri, String JavaDoc qname, Attributes atts)
52     throws SAXException {
53         // System.err.println("Start element " + qname + " at " + activeDepth);
54
if (activeDepth==0) {
55             for (int a=0; a<atts.getLength(); a++) {
56                 if (atts.getType(a).equals("ID") && atts.getValue(a).equals(id)) {
57                     activeDepth = 1;
58                     break;
59                 }
60             }
61             if (activeDepth==1) {
62                 for (int n=0; n<namespacePrefixes.size(); n++) {
63                     super.startPrefixMapping(
64                             (String JavaDoc)namespacePrefixes.elementAt(n),
65                             (String JavaDoc)namespaceURIs.elementAt(n) );
66                 }
67             }
68         } else {
69             activeDepth++;
70         }
71         if (activeDepth>0) {
72             super.startElement(localName, uri, qname, atts);
73         }
74     }
75     
76     public void endElement(String JavaDoc localName, String JavaDoc uri, String JavaDoc qname)
77     throws SAXException {
78         // System.err.println("End element " + qname + " at " + activeDepth);
79
if (activeDepth > 0) {
80             super.endElement(localName, uri, qname);
81             activeDepth--;
82             if (activeDepth==0) {
83                 for (int n=namespacePrefixes.size()-1; n>=0; n--) {
84                     super.endPrefixMapping(
85                             (String JavaDoc)namespacePrefixes.elementAt(n) );
86                 }
87             }
88         }
89     }
90
91     public void characters(char[] chars, int start, int len)
92     throws SAXException {
93         if (activeDepth > 0) {
94             super.characters(chars, start, len);
95         }
96     }
97         
98     public void ignorableWhitespace(char[] chars, int start, int len)
99     throws SAXException {
100         if (activeDepth > 0) {
101             super.ignorableWhitespace(chars, start, len);
102         }
103     }
104     
105     public void processingInstruction(String JavaDoc name, String JavaDoc data)
106     throws SAXException {
107         if (activeDepth > 0) {
108             super.processingInstruction(name, data);
109         }
110     }
111 }
112 //
113
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
114
// you may not use this file except in compliance with the License. You may obtain a copy of the
115
// License at http://www.mozilla.org/MPL/
116
//
117
// Software distributed under the License is distributed on an "AS IS" basis,
118
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
119
// See the License for the specific language governing rights and limitations under the License.
120
//
121
// The Original Code is: all this file.
122
//
123
// The Initial Developer of the Original Code is
124
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
125
//
126
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
127
//
128
// Contributor(s): none.
129
//
130
Popular Tags