KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > common > GrammarResolverImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.validators.common;
59
60
61 import java.util.Enumeration JavaDoc;
62 import java.util.Hashtable JavaDoc;
63
64 import org.enhydra.apache.xerces.validators.datatype.DatatypeValidatorFactory;
65 import org.enhydra.apache.xerces.validators.datatype.DatatypeValidatorFactoryImpl;
66
67
68 /**
69  * This class embodies the representation of a Schema Grammar
70  * pool.
71  * This class is called from the validator.
72  * Grammar pool maps to a set of Grammar Proxy classes.
73  *
74  * @author Jeffrey Rodriguez
75  * @version $Id: GrammarResolverImpl.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
76  */

77 public class GrammarResolverImpl implements GrammarResolver {
78
79     //
80
// Data
81
//
82

83     /**
84      * Hashtable structure that represents a mapping
85      * between Namespace and a Grammar
86      */

87     private Hashtable JavaDoc fGrammarRegistry = new Hashtable JavaDoc();//This class keeps a hashtable of references to Grammar structures
88

89     //optimization -el
90
//private DatatypeValidatorFactoryImpl fDataTypeReg = new DatatypeValidatorFactoryImpl();
91
private DatatypeValidatorFactoryImpl fDataTypeReg;
92     //
93
// Constructors
94
//
95

96     /** Default constructor. */
97     public GrammarResolverImpl() {
98     }
99
100     //
101
// GrammarResolver methods
102
//
103

104     /**
105      *
106      * @param nameSpaceKey
107      * Namespace key into Grammar pool
108      * @return Grammar abstraction associated
109      * with NameSpace key.
110      */

111     public Grammar getGrammar( String JavaDoc nameSpaceKey ) {
112         return(Grammar) ( fGrammarRegistry.get( nameSpaceKey ) );
113     }
114
115     public DatatypeValidatorFactory getDatatypeRegistry(){
116         if (fDataTypeReg == null) { //optimization -el
117
fDataTypeReg = new DatatypeValidatorFactoryImpl();
118         }
119         return fDataTypeReg;
120     }
121
122     /**
123      *
124      * @return Array of String key name spaces in Grammar pool
125      */

126     public String JavaDoc[] getNSKeysInPool() {
127         int numberOfNSKeysInPool = fGrammarRegistry.size();
128         String JavaDoc[] NSArray = new String JavaDoc[numberOfNSKeysInPool];
129         Enumeration JavaDoc enumOfKeys = nameSpaceKeys();
130         for (int i = 0; i<numberOfNSKeysInPool; i ++ ) {
131             NSArray[i] = (String JavaDoc )( enumOfKeys.nextElement() );
132         }
133         return NSArray;
134     }
135
136     /**
137      *
138      * @param nameSpaceKey
139      * Key to associate with Grammar
140      * abstraction
141      * @param grammar Grammar abstraction
142      * used by validator.
143      */

144     public void putGrammar( String JavaDoc nameSpaceKey, Grammar grammar ){
145         fGrammarRegistry.put( nameSpaceKey, grammar );
146     }
147
148     /**
149      *
150      * @return Length of grammar pool. Number of associations.
151      */

152     public int size() {
153         return fGrammarRegistry.size();
154     }
155
156      /**
157      *
158      * @return Enumeration of String key name spaces in Grammar pool
159      */

160
161     public Enumeration JavaDoc nameSpaceKeys(){
162         return fGrammarRegistry.keys();
163     }
164
165
166     /**
167      * Removes association of Namespace key and Grammar from
168      * Grammar pool
169      *
170      * @param nameSpaceKey
171      * Name space key
172      */

173     public Grammar removeGrammar( String JavaDoc nameSpaceKey ) {
174         if ( containsNameSpace( nameSpaceKey ) == true )
175           fGrammarRegistry.remove( nameSpaceKey );
176         return null;
177     }
178
179
180
181     /**
182      * Is Grammar abstraction in Grammar pool?
183      *
184      * @param grammar Grammar Abstraction
185      * @return true - Yes there is at least one instance
186      * false - No
187      */

188     public boolean contains( Grammar grammar ){
189         return fGrammarRegistry.contains( grammar );
190     }
191
192     /**
193      * Is Namespace key in Grammar pool
194      *
195      * @param nameSpaceKey
196      * Namespace key
197      * @return Boolean- true - Namespace key association
198      * is in grammar pool.
199      */

200     public boolean containsNameSpace( String JavaDoc nameSpaceKey ){
201         return fGrammarRegistry.containsKey( nameSpaceKey );
202     }
203
204     /**
205      * Reset internal Namespace/Grammar registry.
206      */

207     public void clearGrammarResolver() {
208         fGrammarRegistry.clear();
209         if (fDataTypeReg != null) { //optimization -el
210
fDataTypeReg.resetRegistry();
211         }
212        
213     }
214
215
216
217
218
219
220     /* Unit Test
221
222     static final int NGRAMMARS = 10;
223
224
225     public static void main( String args[] )
226     {
227         //static final int NGRAMMARS = 10;
228         SchemaGrammarResolver grammarPool = SchemaGrammarResolver.instanceGrammarResolver();
229         Grammar testGrammars[] = new Grammar[NGRAMMARS];
230         String testNameSpace[] = {
231         "http://www.foo1.org/",
232         "http://www.foo2.org/",
233         "http://www.foo3.org/",
234         "http://www.foo4.org/",
235         "http://www.foo5.org/",
236         "http://www.foo6.org/",
237         "http://www.foo7.org/",
238         "http://www.foo8.org/",
239         "http://www.foo9.org/",
240         "http://www.foox.org/" };
241
242         for( int i = 0; i< NGRAMMARS ; i++ ) {
243         testGrammars[i] = new Grammar( testNameSpace[i] );
244         }
245
246
247         for( int i = 0; i<testGrammars.length; i++ ) {
248             grammarPool.addGrammar( testNameSpace[i], testGrammars[i] );
249         }
250         String [] localNames = grammarPool.getNSKeysInPool();
251         for( int i = 0; i<localNames.length; i++ ){
252             System.out.println( "Key[" + i + "] =" + localNames[i] );
253         }
254         // Get a couple of Grammars.
255
256         Grammar gramm1 = grammarPool.getGrammar( "http://www.foo2.org/" );
257         Grammar gramm2 = grammarPool.getGrammar( "http://www.foox.org/" );
258
259         System.out.println( "Grammar1 id = " + gramm1.whatGrammarAmI() + " It should be http://www.foo2.org/" );
260         System.out.println( "Grammar1 id = " + gramm2.whatGrammarAmI() + " It should be http://www.foox.org/" );
261          
262         Grammar myTestGrammar = new Grammar( "testgrammar" );
263         
264         boolean isInPool = grammarPool.isGrammarInPool( myTestGrammar);
265         System.out.println( "Grammar " + myTestGrammar.whatGrammarAmI() + "Is in pool = " + isInPool );
266         
267         grammarPool.addGrammar("myNSTest", myTestGrammar );
268         isInPool = grammarPool.isGrammarInPool( myTestGrammar);
269         System.out.println( "Just added Grammar " + myTestGrammar.whatGrammarAmI() + "Is in pool = " + isInPool );
270                     
271         String myNSTest = "http://www.foo.com/";
272         isInPool = grammarPool.isNSInPool(myNSTest);
273
274         System.out.println( "NS: " + myNSTest + "Is in pool = " + isInPool );
275         grammarPool.addGrammar(myNSTest, new Grammar( myNSTest ));
276         
277         isInPool = grammarPool.isNSInPool(myNSTest);
278
279         System.out.println( "NS: " + myNSTest + "Is in pool = " + isInPool );
280
281         System.out.println( "Length of Grammar pool = " + grammarPool.length() );
282                
283         grammarPool.resetGrammarPool();
284         System.out.println( "Length of Grammar pool now = " + grammarPool.length() );
285
286         grammarPool.addGrammar("myNSTest", myTestGrammar ); // The same key
287         grammarPool.addGrammar("myNSTest", myTestGrammar );
288         grammarPool.addGrammar("myNSTest", myTestGrammar );
289         grammarPool.addGrammar("myNSTest", myTestGrammar );
290         grammarPool.addGrammar("myNSTest", myTestGrammar );
291
292         System.out.println( "Length of Grammar pool now better not be 5 = " + grammarPool.length() );
293         for( int i = 0; i<testGrammars.length; i++ ) {
294         grammarPool.addGrammar( testNameSpace[i], testGrammars[i] );
295         }
296         grammarPool.deleteGrammarForNS( "myNSTest" );
297         System.out.println( "Length of Grammar pool now better not be 5 = " + grammarPool.length() );
298         localNames = grammarPool.getNSKeysInPool();
299         for( int i = 0; i<localNames.length; i++ ){
300                    System.out.println( "Key[" + i + "] =" + localNames[i] );
301            }
302     }
303     */

304
305 } // class GrammarResolverImpl
306
Popular Tags