KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > XSGrammarBucket


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 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) 2001, 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 com.sun.org.apache.xerces.internal.impl.xs;
59
60 import java.util.Hashtable JavaDoc;
61 import java.util.Enumeration JavaDoc;
62 import java.util.Vector JavaDoc;
63
64 /**
65  * A class used to hold the internal schema grammar set for the current instance
66  * @author Sandy Gao, IBM
67  * @version $Id: XSGrammarBucket.java,v 1.8 2003/09/23 21:42:31 mrglavas Exp $
68  */

69 public class XSGrammarBucket {
70
71     // Data
72

73     /**
74      * Hashtable that maps between Namespace and a Grammar
75      */

76     Hashtable JavaDoc fGrammarRegistry = new Hashtable JavaDoc();
77     SchemaGrammar fNoNSGrammar = null;
78
79     /**
80      * Get the schema grammar for the specified namespace
81      *
82      * @param namespace
83      * @return SchemaGrammar associated with the namespace
84      */

85     public SchemaGrammar getGrammar(String JavaDoc namespace) {
86         if (namespace == null)
87             return fNoNSGrammar;
88         return (SchemaGrammar)fGrammarRegistry.get(namespace);
89     }
90
91     /**
92      * Put a schema grammar into the registry
93      * This method is for internal use only: it assumes that a grammar with
94      * the same target namespace is not already in the bucket.
95      *
96      * @param grammar the grammar to put in the registry
97      */

98     public void putGrammar(SchemaGrammar grammar) {
99         if (grammar.getTargetNamespace() == null)
100             fNoNSGrammar = grammar;
101         else
102             fGrammarRegistry.put(grammar.getTargetNamespace(), grammar);
103     }
104
105     /**
106      * put a schema grammar and any grammars imported by it (directly or
107      * inderectly) into the registry. when a grammar with the same target
108      * namespace is already in the bucket, and different from the one being
109      * added, it's an error, and no grammar will be added into the bucket.
110      *
111      * @param grammar the grammar to put in the registry
112      * @param deep whether to add imported grammars
113      * @return whether the process succeeded
114      */

115     public boolean putGrammar(SchemaGrammar grammar, boolean deep) {
116         // whether there is one with the same tns
117
SchemaGrammar sg = getGrammar(grammar.fTargetNamespace);
118         if (sg != null) {
119             // if the one we have is different from the one passed, it's an error
120
return sg == grammar;
121         }
122         // not deep import, then just add this one grammar
123
if (!deep) {
124             putGrammar(grammar);
125             return true;
126         }
127
128         // get all imported grammars, and make a copy of the Vector, so that
129
// we can recursively process the grammars, and add distinct ones
130
// to the same vector
131
Vector JavaDoc currGrammars = (Vector JavaDoc)grammar.getImportedGrammars();
132         if (currGrammars == null) {
133             putGrammar(grammar);
134             return true;
135         }
136         
137         Vector JavaDoc grammars = ((Vector JavaDoc)currGrammars.clone());
138         SchemaGrammar sg1, sg2;
139         Vector JavaDoc gs;
140         // for all (recursively) imported grammars
141
for (int i = 0; i < grammars.size(); i++) {
142             // get the grammar
143
sg1 = (SchemaGrammar)grammars.elementAt(i);
144             // check whether the bucket has one with the same tns
145
sg2 = getGrammar(sg1.fTargetNamespace);
146             if (sg2 == null) {
147                 // we need to add grammars imported by sg1 too
148
gs = sg1.getImportedGrammars();
149                 // for all grammars imported by sg2, but not in the vector
150
// we add them to the vector
151
if(gs == null) continue;
152                 for (int j = gs.size() - 1; j >= 0; j--) {
153                     sg2 = (SchemaGrammar)gs.elementAt(j);
154                     if (!grammars.contains(sg2))
155                         grammars.addElement(sg2);
156                 }
157             }
158             // we found one with the same target namespace
159
// if the two grammars are not the same object, then it's an error
160
else if (sg2 != sg1) {
161                 return false;
162             }
163         }
164
165         // now we have all imported grammars stored in the vector. add them
166
putGrammar(grammar);
167         for (int i = grammars.size() - 1; i >= 0; i--)
168             putGrammar((SchemaGrammar)grammars.elementAt(i));
169
170         return true;
171     }
172
173     /**
174      * get all grammars in the registry
175      *
176      * @return an array of SchemaGrammars.
177      */

178     public SchemaGrammar[] getGrammars() {
179         // get the number of grammars
180
int count = fGrammarRegistry.size() + (fNoNSGrammar==null ? 0 : 1);
181         SchemaGrammar[] grammars = new SchemaGrammar[count];
182         // get grammars with target namespace
183
Enumeration JavaDoc schemas = fGrammarRegistry.elements();
184         int i = 0;
185         while (schemas.hasMoreElements())
186             grammars[i++] = (SchemaGrammar)schemas.nextElement();
187         // add the grammar without target namespace, if any
188
if (fNoNSGrammar != null)
189             grammars[count-1] = fNoNSGrammar;
190         return grammars;
191     }
192
193     /**
194      * Clear the registry.
195      * REVISIT: update to use another XSGrammarBucket
196      */

197     public void reset() {
198         fNoNSGrammar = null;
199         fGrammarRegistry.clear();
200     }
201
202 } // class XSGrammarBucket
203
Popular Tags