KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > IDFilter


1 package net.sf.saxon.event;
2 import net.sf.saxon.trans.XPathException;
3 import net.sf.saxon.style.StandardNames;
4 import net.sf.saxon.om.NamePool;
5 import net.sf.saxon.type.SchemaType;
6 import net.sf.saxon.type.AtomicType;
7 import net.sf.saxon.type.Type;
8
9 import java.util.HashSet JavaDoc;
10
11
12 /**
13 * IDFilter is a ProxyReceiver that extracts the subtree of a document rooted at the
14 * element with a given ID value. Namespace declarations outside this subtree are
15 * treated as if they were present on the identified element.
16 */

17
18 public class IDFilter extends StartTagBuffer {
19
20     private String JavaDoc requiredId;
21     private int activeDepth = 0;
22     private boolean matched = false;
23     private HashSet JavaDoc nonIDs;
24
25     public IDFilter (String JavaDoc id) {
26         // System.err.println("IDFilter, looking for " + id);
27
this.requiredId = id;
28     }
29
30     /**
31      * startElement
32      */

33
34     public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {
35         matched = false;
36         if (activeDepth>0) {
37             activeDepth++;
38         }
39         super.startElement(nameCode, typeCode, locationId, properties); // this remembers the details
40
}
41
42     /**
43      * Notify an attribute. Attributes are notified after the startElement event, and before any
44      * children. Namespaces and attributes may be intermingled.
45      *
46      * @param nameCode The name of the attribute, as held in the name pool
47      * @param typeCode The type of the attribute, as held in the name pool
48      * @param properties Bit significant value. The following bits are defined:
49      * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
50      * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
51      * @throws IllegalStateException: attempt to output an attribute when there is no open element
52      * start tag
53      */

54
55     public void attribute(int nameCode, int typeCode, CharSequence JavaDoc value, int locationId, int properties) throws XPathException {
56         super.attribute(nameCode, typeCode, value, locationId, properties);
57         if ((nameCode & NamePool.FP_MASK) == StandardNames.XML_ID || isIDCode(typeCode)) {
58             if (value.toString().equals(requiredId)) {
59                 matched = true;
60             }
61         }
62     }
63
64     /**
65      * startContent: Test if a matching ID attribute was found; if so, start outputting.
66      */

67
68     public void startContent() throws XPathException {
69         if (activeDepth>0) {
70             super.startContent();
71         } else if (matched) {
72             activeDepth = 1;
73             super.startContent();
74         }
75     }
76
77     protected void declareNamespacesForStartElement() throws XPathException {
78         if (activeDepth == 1) {
79             declareAllNamespaces();
80         } else {
81             super.declareNamespacesForStartElement();
82         }
83     }
84
85     /**
86      * endElement:
87      */

88
89     public void endElement() throws XPathException {
90         if (activeDepth > 0) {
91             super.endElement();
92             activeDepth--;
93         } else {
94             undeclareNamespacesForElement();
95         }
96     }
97
98     /**
99      * Character data
100      */

101
102     public void characters(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
103         if (activeDepth > 0) {
104             super.characters(chars, locationId, properties);
105         }
106     }
107
108     /**
109      * Processing Instruction
110      */

111
112     public void processingInstruction(String JavaDoc target, CharSequence JavaDoc data, int locationId, int properties) throws XPathException {
113         if (activeDepth > 0) {
114             super.processingInstruction(target, data, locationId, properties);
115         }
116     }
117
118     /**
119      * Output a comment
120      */

121
122     public void comment(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
123         if (activeDepth > 0) {
124             super.comment(chars, locationId, properties);
125         }
126     }
127
128     /**
129      * Test whether a type annotation code represents the type xs:ID or one of its subtypes
130      */

131
132     private boolean isIDCode(int typeCode) {
133         if ((typeCode & NamePool.FP_MASK) == StandardNames.XS_ID) {
134             return true;
135         } else if (typeCode < 1024) {
136             // No other built-in type is an ID
137
return false;
138         } else {
139             if (nonIDs == null) {
140                 nonIDs = new HashSet JavaDoc(20);
141             }
142             Integer JavaDoc key = new Integer JavaDoc(typeCode);
143             if (nonIDs.contains(key)) {
144                 return false;
145             }
146             SchemaType type = getConfiguration().getSchemaType(typeCode);
147             if (type instanceof AtomicType) {
148                 if (Type.isSubType((AtomicType)type, Type.ID_TYPE)) {
149                     return true;
150                 } else {
151                     nonIDs.add(key);
152                     return false;
153                 }
154             } else {
155                 return false;
156             }
157         }
158     }
159
160 }
161 //
162
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
163
// you may not use this file except in compliance with the License. You may obtain a copy of the
164
// License at http://www.mozilla.org/MPL/
165
//
166
// Software distributed under the License is distributed on an "AS IS" basis,
167
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
168
// See the License for the specific language governing rights and limitations under the License.
169
//
170
// The Original Code is: all this file.
171
//
172
// The Initial Developer of the Original Code is Michael H. Kay.
173
//
174
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
175
//
176
// Contributor(s): none.
177
//
178
Popular Tags