KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > xpointer > ShortHandPointer


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

16 package org.apache.xerces.xpointer;
17
18 import org.apache.xerces.impl.Constants;
19 import org.apache.xerces.impl.dv.XSSimpleType;
20 import org.apache.xerces.util.SymbolTable;
21 import org.apache.xerces.xni.Augmentations;
22 import org.apache.xerces.xni.QName;
23 import org.apache.xerces.xni.XMLAttributes;
24 import org.apache.xerces.xni.XNIException;
25 import org.apache.xerces.xs.AttributePSVI;
26 import org.apache.xerces.xs.XSTypeDefinition;
27
28 /**
29  * <p>
30  * Implements the XPointerPart interface and handles processing of
31  * ShortHand Pointers. It identifies at most one element in the
32  * resource's information set; specifically, the first one (if any)
33  * in document order that has a matching NCName as an identifier.
34  * </p>
35  *
36  * @version $Id: ShortHandPointer.java,v 1.1 2005/06/17 22:00:20 nddelima Exp $
37  *
38  */

39 class ShortHandPointer implements XPointerPart {
40     
41     // The name of the ShortHand pointer
42
private String JavaDoc fShortHandPointer;
43     
44     // The name of the ShortHand pointer
45
private boolean fIsFragmentResolved = false;
46     
47     // SymbolTable
48
private SymbolTable fSymbolTable;
49     
50     //
51
// Constructors
52
//
53
public ShortHandPointer() {
54     }
55     
56     public ShortHandPointer(SymbolTable symbolTable) {
57         fSymbolTable = symbolTable;
58     }
59     
60     /**
61      * The XPointerProcessor takes care of this. Simply set the ShortHand Pointer here.
62      *
63      * @see org.apache.xerces.xpointer.XPointerPart#parseXPointer(java.lang.String)
64      */

65     public void parseXPointer(String JavaDoc part) throws XNIException {
66         fShortHandPointer = part;
67         // reset fIsFragmentResolved
68
fIsFragmentResolved = false;
69     }
70     
71     /**
72      * Resolves the XPointer ShortHand pointer based on the rules defined in
73      * Section 3.2 of the XPointer Framework Recommendation.
74      * Note that in the current implementation only supports DTD determined ID's.
75      *
76      * @see org.apache.xerces.xpointer.XPointerPart#resolveXPointer(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations, int event)
77      */

78     int fMatchingChildCount = 0;
79     public boolean resolveXPointer(QName element, XMLAttributes attributes,
80             Augmentations augs, int event) throws XNIException {
81
82         // reset fIsFragmentResolved
83
if (fMatchingChildCount == 0) {
84             fIsFragmentResolved = false;
85         }
86
87         // On startElement or emptyElement, if no matching elements or parent
88
// elements were found, check for a matching idenfitier.
89
if (event == XPointerPart.EVENT_ELEMENT_START) {
90             if (fMatchingChildCount == 0) {
91                 fIsFragmentResolved = hasMatchingIdentifier(element, attributes, augs,
92                     event);
93             }
94             if (fIsFragmentResolved) {
95                fMatchingChildCount++;
96             }
97         } else if (event == XPointerPart.EVENT_ELEMENT_EMPTY) {
98             if (fMatchingChildCount == 0) {
99                 fIsFragmentResolved = hasMatchingIdentifier(element, attributes, augs,
100                     event);
101             }
102         }
103         else {
104             // On endElement, decrease the matching child count if the child or
105
// its parent was resolved.
106
if (fIsFragmentResolved) {
107                 fMatchingChildCount--;
108             }
109         }
110         
111         return fIsFragmentResolved ;
112     }
113     
114     /**
115      *
116      * @param element
117      * @param attributes
118      * @param augs
119      * @param event
120      * @return
121      * @throws XNIException
122      */

123     private boolean hasMatchingIdentifier(QName element,
124             XMLAttributes attributes, Augmentations augs, int event)
125     throws XNIException {
126         String JavaDoc normalizedValue = null;
127         
128         // The identifiers of an element are determined by the
129
// ShortHand Pointer as follows:
130

131         if (attributes != null) {
132             for (int i = 0; i < attributes.getLength(); i++) {
133                 
134                 // 1. If an element information item has an attribute information item
135
// among its [attributes] that is a schema-determined ID, then it is
136
// identified by the value of that attribute information item's
137
// [schema normalized value] property;
138
normalizedValue = getSchemaDeterminedID(attributes, i);
139                 if (normalizedValue != null) {
140                     break;
141                 }
142                 
143                 // 2. If an element information item has an element information item among
144
// its [children] that is a schema-determined ID, then it is identified by
145
// the value of that element information item's [schema normalized value] property;
146
// ???
147
normalizedValue = getChildrenSchemaDeterminedID(attributes, i);
148                 if (normalizedValue != null) {
149                     break;
150                 }
151                 
152                 // 3. If an element information item has an attribute information item among
153
// its [attributes] that is a DTD-determined ID, then it is identified by the
154
// value of that attribute information item's [normalized value] property.
155
// An attribute information item is a DTD-determined ID if and only if it has
156
// a [type definition] property whose value is equal to ID.
157
normalizedValue = getDTDDeterminedID(attributes, i);
158                 if (normalizedValue != null) {
159                     break;
160                 }
161                 // 4. No externally determined ID's
162
}
163         }
164         
165         if (normalizedValue != null
166                 && normalizedValue.equals(fShortHandPointer)) {
167             return true;
168         }
169         
170         return false;
171     }
172     
173     /**
174      * Rerturns the DTD determine-ID
175      *
176      * @param attributes
177      * @param index
178      * @return String
179      * @throws XNIException
180      */

181     public String JavaDoc getDTDDeterminedID(XMLAttributes attributes, int index)
182     throws XNIException {
183         
184         if (attributes.getType(index).equals("ID")) {
185             return attributes.getValue(index);
186         }
187         return null;
188     }
189     
190     /**
191      * Returns the schema-determined-ID.
192      *
193      *
194      * @param attributes
195      * @param index
196      * @return A String containing the schema-determined ID.
197      * @throws XNIException
198      */

199     public String JavaDoc getSchemaDeterminedID(XMLAttributes attributes, int index)
200     throws XNIException {
201         Augmentations augs = attributes.getAugmentations(index);
202         AttributePSVI attrPSVI = (AttributePSVI) augs
203         .getItem(Constants.ATTRIBUTE_PSVI);
204         
205         if (attrPSVI != null) {
206             // An element or attribute information item is a schema-determined
207
// ID if and only if one of the following is true:]
208

209             // 1. It has a [member type definition] or [type definition] property
210
// whose value in turn has [name] equal to ID and [target namespace]
211
// equal to http://www.w3.org/2001/XMLSchema;
212

213             // 2. It has a [base type definition] whose value has that [name] and [target namespace];
214

215             // 3. It has a [base type definition] whose value has a [base type definition]
216
// whose value has that [name] and [target namespace], and so on following
217
// the [base type definition] property recursively;
218

219             XSTypeDefinition typeDef = attrPSVI.getMemberTypeDefinition();
220             if (typeDef != null) {
221                 typeDef = attrPSVI.getTypeDefinition();
222             }
223             
224             //
225
if (typeDef != null && ((XSSimpleType) typeDef).isIDType()) {
226                 return attrPSVI.getSchemaNormalizedValue();
227             }
228             
229             // 4 & 5 NA
230
}
231         
232         return null;
233     }
234     
235     /**
236      * Not quite sure how this can be correctly implemented.
237      *
238      * @param attributes
239      * @param index
240      * @return String - We return null since we currenly do not supprt this.
241      * @throws XNIException
242      */

243     public String JavaDoc getChildrenSchemaDeterminedID(XMLAttributes attributes,
244             int index) throws XNIException {
245         return null;
246     }
247     
248     /**
249      *
250      * @see org.apache.xerces.xpointer.XPointerPart#isFragmentResolved()
251      */

252     public boolean isFragmentResolved() {
253         return fIsFragmentResolved;
254     }
255     
256     /**
257      *
258      * @see org.apache.xerces.xpointer.XPointerPart#isChildFragmentResolved()
259      */

260     public boolean isChildFragmentResolved() {
261         return fIsFragmentResolved & ( fMatchingChildCount > 0);
262     }
263     
264     /**
265      * Returns the name of the ShortHand pointer
266      *
267      * @see org.apache.xerces.xpointer.XPointerPart#getSchemeName()
268      */

269     public String JavaDoc getSchemeName() {
270         return fShortHandPointer;
271     }
272     
273     /**
274      * @see org.apache.xerces.xpointer.XPointerPart#getSchemeData()
275      */

276     public String JavaDoc getSchemeData() {
277         return null;
278     }
279     
280     /**
281      * @see org.apache.xerces.xpointer.XPointerPart#setSchemeName(java.lang.String)
282      */

283     public void setSchemeName(String JavaDoc schemeName) {
284         fShortHandPointer = schemeName;
285     }
286     
287     /**
288      * @see org.apache.xerces.xpointer.XPointerPart#setSchemeData(java.lang.String)
289      */

290     public void setSchemeData(String JavaDoc schemeData) {
291         // NA
292
}
293 }
Popular Tags