001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.validators.common;
059:
060: import java.util.*;
061: import org.apache.xerces.validators.common.Grammar;
062: import org.apache.xerces.validators.common.GrammarResolver;
063: import org.apache.xerces.validators.datatype.DatatypeValidatorFactory;
064: import org.apache.xerces.validators.datatype.DatatypeValidatorFactoryImpl;
065:
066: /**
067: * This class embodies the representation of a Schema Grammar
068: * pool.
069: * This class is called from the validator.
070: * Grammar pool maps to a set of Grammar Proxy classes.
071: *
072: * @author Jeffrey Rodriguez
073: * @version $Id: GrammarResolverImpl.java,v 1.4 2000/12/01 02:52:08 jeffreyr Exp $
074: */
075: public class GrammarResolverImpl implements GrammarResolver {
076:
077: //
078: // Data
079: //
080:
081: /**
082: * Hashtable structure that represents a mapping
083: * between Namespace and a Grammar
084: */
085: private Hashtable fGrammarRegistry = new Hashtable();//This class keeps a hashtable of references to Grammar structures
086:
087: //optimization -el
088: //private DatatypeValidatorFactoryImpl fDataTypeReg = new DatatypeValidatorFactoryImpl();
089: private DatatypeValidatorFactoryImpl fDataTypeReg;
090:
091: //
092: // Constructors
093: //
094:
095: /** Default constructor. */
096: public GrammarResolverImpl() {
097: }
098:
099: //
100: // GrammarResolver methods
101: //
102:
103: /**
104: *
105: * @param nameSpaceKey
106: * Namespace key into Grammar pool
107: * @return Grammar abstraction associated
108: * with NameSpace key.
109: */
110: public Grammar getGrammar(String nameSpaceKey) {
111: return (Grammar) (fGrammarRegistry.get(nameSpaceKey));
112: }
113:
114: public DatatypeValidatorFactory getDatatypeRegistry() {
115: if (fDataTypeReg == null) { //optimization -el
116: fDataTypeReg = new DatatypeValidatorFactoryImpl();
117: }
118: return fDataTypeReg;
119: }
120:
121: /**
122: *
123: * @return Array of String key name spaces in Grammar pool
124: */
125: public String[] getNSKeysInPool() {
126: int numberOfNSKeysInPool = fGrammarRegistry.size();
127: String[] NSArray = new String[numberOfNSKeysInPool];
128: Enumeration enumOfKeys = nameSpaceKeys();
129: for (int i = 0; i < numberOfNSKeysInPool; i++) {
130: NSArray[i] = (String) (enumOfKeys.nextElement());
131: }
132: return NSArray;
133: }
134:
135: /**
136: *
137: * @param nameSpaceKey
138: * Key to associate with Grammar
139: * abstraction
140: * @param grammar Grammar abstraction
141: * used by validator.
142: */
143: public void putGrammar(String nameSpaceKey, Grammar grammar) {
144: fGrammarRegistry.put(nameSpaceKey, grammar);
145: }
146:
147: /**
148: *
149: * @return Length of grammar pool. Number of associations.
150: */
151: public int size() {
152: return fGrammarRegistry.size();
153: }
154:
155: /**
156: *
157: * @return Enumeration of String key name spaces in Grammar pool
158: */
159:
160: public Enumeration nameSpaceKeys() {
161: return fGrammarRegistry.keys();
162: }
163:
164: /**
165: * Removes association of Namespace key and Grammar from
166: * Grammar pool
167: *
168: * @param nameSpaceKey
169: * Name space key
170: */
171: public Grammar removeGrammar(String nameSpaceKey) {
172: if (containsNameSpace(nameSpaceKey) == true)
173: fGrammarRegistry.remove(nameSpaceKey);
174: return null;
175: }
176:
177: /**
178: * Is Grammar abstraction in Grammar pool?
179: *
180: * @param grammar Grammar Abstraction
181: * @return true - Yes there is at least one instance
182: * false - No
183: */
184: public boolean contains(Grammar grammar) {
185: return fGrammarRegistry.contains(grammar);
186: }
187:
188: /**
189: * Is Namespace key in Grammar pool
190: *
191: * @param nameSpaceKey
192: * Namespace key
193: * @return Boolean- true - Namespace key association
194: * is in grammar pool.
195: */
196: public boolean containsNameSpace(String nameSpaceKey) {
197: return fGrammarRegistry.containsKey(nameSpaceKey);
198: }
199:
200: /**
201: * Reset internal Namespace/Grammar registry.
202: */
203: public void clearGrammarResolver() {
204: fGrammarRegistry.clear();
205: if (fDataTypeReg != null) { //optimization -el
206: fDataTypeReg.resetRegistry();
207: }
208:
209: }
210:
211: /* Unit Test
212:
213: static final int NGRAMMARS = 10;
214:
215:
216: public static void main( String args[] )
217: {
218: //static final int NGRAMMARS = 10;
219: SchemaGrammarResolver grammarPool = SchemaGrammarResolver.instanceGrammarResolver();
220: Grammar testGrammars[] = new Grammar[NGRAMMARS];
221: String testNameSpace[] = {
222: "http://www.foo1.org/",
223: "http://www.foo2.org/",
224: "http://www.foo3.org/",
225: "http://www.foo4.org/",
226: "http://www.foo5.org/",
227: "http://www.foo6.org/",
228: "http://www.foo7.org/",
229: "http://www.foo8.org/",
230: "http://www.foo9.org/",
231: "http://www.foox.org/" };
232:
233: for( int i = 0; i< NGRAMMARS ; i++ ) {
234: testGrammars[i] = new Grammar( testNameSpace[i] );
235: }
236:
237:
238: for( int i = 0; i<testGrammars.length; i++ ) {
239: grammarPool.addGrammar( testNameSpace[i], testGrammars[i] );
240: }
241: String [] localNames = grammarPool.getNSKeysInPool();
242: for( int i = 0; i<localNames.length; i++ ){
243: System.out.println( "Key[" + i + "] =" + localNames[i] );
244: }
245: // Get a couple of Grammars.
246:
247: Grammar gramm1 = grammarPool.getGrammar( "http://www.foo2.org/" );
248: Grammar gramm2 = grammarPool.getGrammar( "http://www.foox.org/" );
249:
250: System.out.println( "Grammar1 id = " + gramm1.whatGrammarAmI() + " It should be http://www.foo2.org/" );
251: System.out.println( "Grammar1 id = " + gramm2.whatGrammarAmI() + " It should be http://www.foox.org/" );
252:
253: Grammar myTestGrammar = new Grammar( "testgrammar" );
254:
255: boolean isInPool = grammarPool.isGrammarInPool( myTestGrammar);
256: System.out.println( "Grammar " + myTestGrammar.whatGrammarAmI() + "Is in pool = " + isInPool );
257:
258: grammarPool.addGrammar("myNSTest", myTestGrammar );
259: isInPool = grammarPool.isGrammarInPool( myTestGrammar);
260: System.out.println( "Just added Grammar " + myTestGrammar.whatGrammarAmI() + "Is in pool = " + isInPool );
261:
262: String myNSTest = "http://www.foo.com/";
263: isInPool = grammarPool.isNSInPool(myNSTest);
264:
265: System.out.println( "NS: " + myNSTest + "Is in pool = " + isInPool );
266: grammarPool.addGrammar(myNSTest, new Grammar( myNSTest ));
267:
268: isInPool = grammarPool.isNSInPool(myNSTest);
269:
270: System.out.println( "NS: " + myNSTest + "Is in pool = " + isInPool );
271:
272: System.out.println( "Length of Grammar pool = " + grammarPool.length() );
273:
274: grammarPool.resetGrammarPool();
275: System.out.println( "Length of Grammar pool now = " + grammarPool.length() );
276:
277: grammarPool.addGrammar("myNSTest", myTestGrammar ); // The same key
278: grammarPool.addGrammar("myNSTest", myTestGrammar );
279: grammarPool.addGrammar("myNSTest", myTestGrammar );
280: grammarPool.addGrammar("myNSTest", myTestGrammar );
281: grammarPool.addGrammar("myNSTest", myTestGrammar );
282:
283: System.out.println( "Length of Grammar pool now better not be 5 = " + grammarPool.length() );
284: for( int i = 0; i<testGrammars.length; i++ ) {
285: grammarPool.addGrammar( testNameSpace[i], testGrammars[i] );
286: }
287: grammarPool.deleteGrammarForNS( "myNSTest" );
288: System.out.println( "Length of Grammar pool now better not be 5 = " + grammarPool.length() );
289: localNames = grammarPool.getNSKeysInPool();
290: for( int i = 0; i<localNames.length; i++ ){
291: System.out.println( "Key[" + i + "] =" + localNames[i] );
292: }
293: }
294: */
295:
296: } // class GrammarResolverImpl
|