KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > TypedContentHandler


1 package net.sf.saxon.dom;
2
3 import net.sf.saxon.event.ContentHandlerProxy;
4 import net.sf.saxon.event.ReceiverOptions;
5 import net.sf.saxon.om.NamespaceConstant;
6 import net.sf.saxon.style.StandardNames;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.AnySimpleType;
9 import net.sf.saxon.type.AnyType;
10 import net.sf.saxon.type.SchemaType;
11 import org.w3c.dom.TypeInfo JavaDoc;
12
13 import javax.xml.validation.TypeInfoProvider JavaDoc;
14
15 /**
16  * This class is an extension of ContentHandlerProxy that provides access to type
17  * information, using the DOM Level 3 TypeInfo interfaces.
18  * The ContentHandlerProxy also acts as a TypeInfoProvider, providing information
19  * about the type of the current element or attribute.
20  */

21
22 public class TypedContentHandler extends ContentHandlerProxy {
23     private int pendingElementTypeCode;
24
25     /**
26      * Get a TypeInfoProvider to provide type information for the current element or attribute
27      * event.
28      */

29
30     public TypeInfoProvider JavaDoc getTypeInfoProvider() {
31         return new TypeInfoProviderImpl();
32     }
33
34
35     /**
36      * Notify the start of an element
37      */

38
39     public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {
40         pendingElementTypeCode = typeCode;
41         super.startElement(nameCode, typeCode, locationId, properties);
42     }
43
44
45     ///////////////////////////////////////////////////////////////////////////////////////////
46
// TypeInfoProvider
47
///////////////////////////////////////////////////////////////////////////////////////////
48

49     public class TypeInfoProviderImpl extends TypeInfoProvider JavaDoc {
50
51         /**
52          * Returns the immutable {@link org.w3c.dom.TypeInfo} object for the current element.
53          *
54          * @return An immutable {@link org.w3c.dom.TypeInfo} object that represents the
55          * type of the current element.
56          * Note that the caller can keep references to the obtained
57          * {@link org.w3c.dom.TypeInfo} longer than the callback scope.
58          * <p/>
59          * Otherwise, this method returns null if the validator is unable to
60          * determine the type of the current element for some reason
61          */

62
63         public TypeInfo JavaDoc getElementTypeInfo() {
64             if (pendingElementTypeCode == -1) {
65                 return new TypeInfoImpl(getConfiguration(), AnyType.getInstance());
66             } else {
67                 return new TypeInfoImpl(getConfiguration(),
68                         getConfiguration().getSchemaType(pendingElementTypeCode));
69             }
70         }
71
72         /**
73          * Returns the immutable {@link org.w3c.dom.TypeInfo} object for the specified
74          * attribute of the current element.
75          * <p/>
76          * The method may only be called by the startElement event of
77          * the {@link org.xml.sax.ContentHandler} that the application sets to the
78          * {@link javax.xml.validation.ValidatorHandler}.
79          *
80          * @param index The index of the attribute. The same index for
81          * the {@link org.xml.sax.Attributes} object passed to the
82          * <tt>startElement</tt> callback.
83          * @return An immutable {@link org.w3c.dom.TypeInfo} object that represents the
84          * type of the specified attribute.
85          * Note that the caller can keep references to the obtained
86          * {@link org.w3c.dom.TypeInfo} longer than the callback scope.
87          * <p/>
88          * Otherwise, this method returns null if the validator is unable to
89          * determine the type.
90          * @throws IndexOutOfBoundsException If the index is invalid.
91          * @throws IllegalStateException If this method is called from other {@link org.xml.sax.ContentHandler}
92          * methods.
93          */

94         public TypeInfo JavaDoc getAttributeTypeInfo(int index) {
95             if (index < 0 || index > pendingAttributes.getLength()) {
96                 throw new IndexOutOfBoundsException JavaDoc(""+index);
97             }
98             int type = pendingAttributes.getTypeAnnotation(index);
99             if (type == -1) {
100                 return new TypeInfoImpl(getConfiguration(), AnySimpleType.getInstance());
101             } else {
102                 return new TypeInfoImpl(getConfiguration(),
103                         getConfiguration().getSchemaType(type));
104             }
105         }
106
107         /**
108          * Returns <tt>true</tt> if the specified attribute is determined
109          * to be an ID.
110          * @param index The index of the attribute. The same index for
111          * the {@link org.xml.sax.Attributes} object passed to the
112          * <tt>startElement</tt> callback.
113          * @return true
114          * if the type of the specified attribute is ID.
115          */

116         public boolean isIdAttribute(int index) {
117             int type = pendingAttributes.getTypeAnnotation(index);
118             return (type == StandardNames.XS_ID ||
119                     getAttributeTypeInfo(index).isDerivedFrom(
120                             NamespaceConstant.SCHEMA, "ID", SchemaType.DERIVATION_RESTRICTION));
121         }
122
123         /**
124          * Returns <tt>false</tt> if the attribute was added by the validator.
125          *
126          * @param index The index of the attribute. The same index for
127          * the {@link org.xml.sax.Attributes} object passed to the
128          * <tt>startElement</tt> callback.
129          * @return <tt>true</tt> if the attribute was present before the validator
130          * processes input. <tt>false</tt> if the attribute was added
131          * by the validator.
132          */

133
134         public boolean isSpecified(int index) {
135             return (pendingAttributes.getProperties(index) & ReceiverOptions.DEFAULTED_ATTRIBUTE) == 0;
136         }
137
138     }
139 }
140
141 //
142
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
143
// you may not use this file except in compliance with the License. You may obtain a copy of the
144
// License at http://www.mozilla.org/MPL/
145
//
146
// Software distributed under the License is distributed on an "AS IS" basis,
147
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
148
// See the License for the specific language governing rights and limitations under the License.
149
//
150
// The Original Code is: all this file.
151
//
152
// The Initial Developer of the Original Code is Michael H. Kay.
153
//
154
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
155
//
156
// Contributor(s): none.
157
//
158
Popular Tags