KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > XMLGrammarPoolImpl


1 /*
2  * Copyright 1999-2002,2004 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
17 package org.apache.xerces.util;
18
19 import org.apache.xerces.xni.grammars.Grammar;
20 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
21 import org.apache.xerces.xni.grammars.XMLGrammarPool;
22
23 /**
24  * Stores grammars in a pool associated to a specific key. This grammar pool
25  * implementation stores two types of grammars: those keyed by the root element
26  * name, and those keyed by the grammar's target namespace.
27  *
28  * This is the default implementation of the GrammarPool interface.
29  * As we move forward, this will become more function-rich and robust.
30  *
31  * @author Jeffrey Rodriguez, IBM
32  * @author Andy Clark, IBM
33  * @author Neil Graham, IBM
34  * @author Pavani Mukthipudi, Sun Microsystems
35  * @author Neeraj Bajaj, SUN Microsystems
36  *
37  * @version $Id: XMLGrammarPoolImpl.java,v 1.8 2004/03/25 04:03:22 mrglavas Exp $
38  */

39 public class XMLGrammarPoolImpl implements XMLGrammarPool {
40
41     //
42
// Constants
43
//
44

45     /** Default size. */
46     protected static final int TABLE_SIZE = 11;
47
48     //
49
// Data
50
//
51

52     /** Grammars. */
53     protected Entry[] fGrammars = null;
54
55     // whether this pool is locked
56
protected boolean fPoolIsLocked;
57
58     // the number of grammars in the pool
59
protected int fGrammarCount = 0;
60
61     private static final boolean DEBUG = false ;
62
63     //
64
// Constructors
65
//
66

67     /** Constructs a grammar pool with a default number of buckets. */
68     public XMLGrammarPoolImpl() {
69         fGrammars = new Entry[TABLE_SIZE];
70         fPoolIsLocked = false;
71     } // <init>()
72

73     /** Constructs a grammar pool with a specified number of buckets. */
74     public XMLGrammarPoolImpl(int initialCapacity) {
75         fGrammars = new Entry[initialCapacity];
76         fPoolIsLocked = false;
77     }
78
79     //
80
// XMLGrammarPool methods
81
//
82

83     /* <p> Retrieve the initial known set of grammars. This method is
84      * called by a validator before the validation starts. The application
85      * can provide an initial set of grammars available to the current
86      * validation attempt. </p>
87      *
88      * @param grammarType The type of the grammar, from the
89      * <code>org.apache.xerces.xni.grammars.XMLGrammarDescription</code>
90      * interface.
91      * @return The set of grammars the validator may put in its "bucket"
92      */

93     public Grammar [] retrieveInitialGrammarSet (String JavaDoc grammarType) {
94         synchronized (fGrammars) {
95             int grammarSize = fGrammars.length ;
96             Grammar [] tempGrammars = new Grammar[fGrammarCount];
97             int pos = 0;
98             for (int i = 0; i < grammarSize; i++) {
99                 for (Entry e = fGrammars[i]; e != null; e = e.next) {
100                     if (e.desc.getGrammarType().equals(grammarType)) {
101                         tempGrammars[pos++] = e.grammar;
102                     }
103                 }
104             }
105             Grammar[] toReturn = new Grammar[pos];
106             System.arraycopy(tempGrammars, 0, toReturn, 0, pos);
107             return toReturn;
108         }
109     } // retrieveInitialGrammarSet (String): Grammar[]
110

111     /* <p> Return the final set of grammars that the validator ended up
112      * with. This method is called after the validation finishes. The
113      * application may then choose to cache some of the returned grammars.</p>
114      * <p>In this implementation, we make our choice based on whether this object
115      * is "locked"--that is, whether the application has instructed
116      * us not to accept any new grammars.</p>
117      *
118      * @param grammarType The type of the grammars being returned;
119      * @param grammars An array containing the set of grammars being
120      * returned; order is not significant.
121      */

122     public void cacheGrammars(String JavaDoc grammarType, Grammar[] grammars) {
123         if(!fPoolIsLocked) {
124             for (int i = 0; i < grammars.length; i++) {
125                 if(DEBUG) {
126                     System.out.println("CACHED GRAMMAR " + (i+1) ) ;
127                     Grammar temp = grammars[i] ;
128                     //print(temp.getGrammarDescription());
129
}
130                 putGrammar(grammars[i]);
131             }
132         }
133     } // cacheGrammars(String, Grammar[]);
134

135     /* <p> This method requests that the application retrieve a grammar
136      * corresponding to the given GrammarIdentifier from its cache.
137      * If it cannot do so it must return null; the parser will then
138      * call the EntityResolver. </p>
139      * <strong>An application must not call its EntityResolver itself
140      * from this method; this may result in infinite recursions.</strong>
141      *
142      * This implementation chooses to use the root element name to identify a DTD grammar
143      * and the target namespace to identify a Schema grammar.
144      *
145      * @param desc The description of the Grammar being requested.
146      * @return The Grammar corresponding to this description or null if
147      * no such Grammar is known.
148      */

149     public Grammar retrieveGrammar(XMLGrammarDescription desc) {
150         if(DEBUG){
151             System.out.println("RETRIEVING GRAMMAR FROM THE APPLICATION WITH FOLLOWING DESCRIPTION :");
152             //print(desc);
153
}
154         return getGrammar(desc);
155     } // retrieveGrammar(XMLGrammarDescription): Grammar
156

157     //
158
// Public methods
159
//
160

161     /**
162      * Puts the specified grammar into the grammar pool and associates it to
163      * its root element name or its target namespace.
164      *
165      * @param grammar The Grammar.
166      */

167     public void putGrammar(Grammar grammar) {
168         if(!fPoolIsLocked) {
169             synchronized (fGrammars) {
170                 XMLGrammarDescription desc = grammar.getGrammarDescription();
171                 int hash = hashCode(desc);
172                 int index = (hash & 0x7FFFFFFF) % fGrammars.length;
173                 for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
174                     if (entry.hash == hash && equals(entry.desc, desc)) {
175                         entry.grammar = grammar;
176                         return;
177                     }
178                 }
179                 // create a new entry
180
Entry entry = new Entry(hash, desc, grammar, fGrammars[index]);
181                 fGrammars[index] = entry;
182                 fGrammarCount++;
183             }
184         }
185     } // putGrammar(Grammar)
186

187     /**
188      * Returns the grammar associated to the specified grammar description.
189      * Currently, the root element name is used as the key for DTD grammars
190      * and the target namespace is used as the key for Schema grammars.
191      *
192      * @param desc The Grammar Description.
193      */

194     public Grammar getGrammar(XMLGrammarDescription desc) {
195         synchronized (fGrammars) {
196             int hash = hashCode(desc);
197         int index = (hash & 0x7FFFFFFF) % fGrammars.length;
198         for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) {
199             if ((entry.hash == hash) && equals(entry.desc, desc)) {
200                 return entry.grammar;
201             }
202         }
203         return null;
204     }
205     } // getGrammar(XMLGrammarDescription):Grammar
206

207     /**
208      * Removes the grammar associated to the specified grammar description from the
209      * grammar pool and returns the removed grammar. Currently, the root element name
210      * is used as the key for DTD grammars and the target namespace is used
211      * as the key for Schema grammars.
212      *
213      * @param desc The Grammar Description.
214      * @return The removed grammar.
215      */

216     public Grammar removeGrammar(XMLGrammarDescription desc) {
217         synchronized (fGrammars) {
218             int hash = hashCode(desc);
219         int index = (hash & 0x7FFFFFFF) % fGrammars.length;
220         for (Entry entry = fGrammars[index], prev = null ; entry != null ; prev = entry, entry = entry.next) {
221             if ((entry.hash == hash) && equals(entry.desc, desc)) {
222                 if (prev != null) {
223                         prev.next = entry.next;
224             }
225             else {
226                 fGrammars[index] = entry.next;
227             }
228                 Grammar tempGrammar = entry.grammar;
229                 entry.grammar = null;
230                 fGrammarCount--;
231                 return tempGrammar;
232             }
233         }
234         return null;
235         }
236     } // removeGrammar(XMLGrammarDescription):Grammar
237

238     /**
239      * Returns true if the grammar pool contains a grammar associated
240      * to the specified grammar description. Currently, the root element name
241      * is used as the key for DTD grammars and the target namespace is used
242      * as the key for Schema grammars.
243      *
244      * @param desc The Grammar Description.
245      */

246     public boolean containsGrammar(XMLGrammarDescription desc) {
247         synchronized (fGrammars) {
248             int hash = hashCode(desc);
249         int index = (hash & 0x7FFFFFFF) % fGrammars.length;
250         for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) {
251             if ((entry.hash == hash) && equals(entry.desc, desc)) {
252                 return true;
253             }
254         }
255         return false;
256     }
257     } // containsGrammar(XMLGrammarDescription):boolean
258

259     /* <p> Sets this grammar pool to a "locked" state--i.e.,
260      * no new grammars will be added until it is "unlocked".
261      */

262     public void lockPool() {
263         fPoolIsLocked = true;
264     } // lockPool()
265

266     /* <p> Sets this grammar pool to an "unlocked" state--i.e.,
267      * new grammars will be added when putGrammar or cacheGrammars
268      * are called.
269      */

270     public void unlockPool() {
271         fPoolIsLocked = false;
272     } // unlockPool()
273

274     /*
275      * <p>This method clears the pool-i.e., removes references
276      * to all the grammars in it.</p>
277      */

278     public void clear() {
279         for (int i=0; i<fGrammars.length; i++) {
280             if(fGrammars[i] != null) {
281                 fGrammars[i].clear();
282                 fGrammars[i] = null;
283             }
284         }
285         fGrammarCount = 0;
286     } // clear()
287

288     /**
289      * This method checks whether two grammars are the same. Currently, we compare
290      * the root element names for DTD grammars and the target namespaces for Schema grammars.
291      * The application can override this behaviour and add its own logic.
292      *
293      * @param desc1 The grammar description
294      * @param desc2 The grammar description of the grammar to be compared to
295      * @return True if the grammars are equal, otherwise false
296      */

297     public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) {
298         return desc1.equals(desc2);
299     }
300
301     /**
302      * Returns the hash code value for the given grammar description.
303      *
304      * @param desc The grammar description
305      * @return The hash code value
306      */

307     public int hashCode(XMLGrammarDescription desc) {
308         return desc.hashCode();
309     }
310
311     /**
312      * This class is a grammar pool entry. Each entry acts as a node
313      * in a linked list.
314      */

315     protected static final class Entry {
316         public int hash;
317         public XMLGrammarDescription desc;
318         public Grammar grammar;
319         public Entry next;
320
321         protected Entry(int hash, XMLGrammarDescription desc, Grammar grammar, Entry next) {
322             this.hash = hash;
323             this.desc = desc;
324             this.grammar = grammar;
325             this.next = next;
326         }
327
328         // clear this entry; useful to promote garbage collection
329
// since reduces reference count of objects to be destroyed
330
protected void clear () {
331             desc = null;
332             grammar = null;
333             if(next != null) {
334                 next.clear();
335                 next = null;
336             }
337         } // clear()
338
} // class Entry
339

340     /* For DTD build we can't import here XSDDescription. Thus, this method is commented out.. */
341     /* public void print(XMLGrammarDescription description){
342         if(description.getGrammarType().equals(XMLGrammarDescription.XML_DTD)){
343
344         }
345         else if(description.getGrammarType().equals(XMLGrammarDescription.XML_SCHEMA)){
346             XSDDescription schema = (XSDDescription)description ;
347             System.out.println("Context = " + schema.getContextType());
348             System.out.println("TargetNamespace = " + schema.getTargetNamespace());
349             String [] temp = schema.getLocationHints();
350
351             for (int i = 0 ; (temp != null && i < temp.length) ; i++){
352                 System.out.println("LocationHint " + i + " = "+ temp[i]);
353             }
354
355             System.out.println("Triggering Component = " + schema.getTriggeringComponent());
356             System.out.println("EnclosingElementName =" + schema.getEnclosingElementName());
357
358         }
359
360     }//print
361     */

362
363 } // class XMLGrammarPoolImpl
364
Popular Tags