KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSGrammarBucket


1 /*
2  * Copyright 2001, 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.impl.xs;
18
19 import java.util.Hashtable JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 /**
24  * A class used to hold the internal schema grammar set for the current instance
25  *
26  * @xerces.internal
27  *
28  * @author Sandy Gao, IBM
29  * @version $Id: XSGrammarBucket.java,v 1.10 2004/10/06 15:14:55 mrglavas Exp $
30  */

31 public class XSGrammarBucket {
32
33     // Data
34

35     /**
36      * Hashtable that maps between Namespace and a Grammar
37      */

38     Hashtable JavaDoc fGrammarRegistry = new Hashtable JavaDoc();
39     SchemaGrammar fNoNSGrammar = null;
40
41     /**
42      * Get the schema grammar for the specified namespace
43      *
44      * @param namespace
45      * @return SchemaGrammar associated with the namespace
46      */

47     public SchemaGrammar getGrammar(String JavaDoc namespace) {
48         if (namespace == null)
49             return fNoNSGrammar;
50         return (SchemaGrammar)fGrammarRegistry.get(namespace);
51     }
52
53     /**
54      * Put a schema grammar into the registry
55      * This method is for internal use only: it assumes that a grammar with
56      * the same target namespace is not already in the bucket.
57      *
58      * @param grammar the grammar to put in the registry
59      */

60     public void putGrammar(SchemaGrammar grammar) {
61         if (grammar.getTargetNamespace() == null)
62             fNoNSGrammar = grammar;
63         else
64             fGrammarRegistry.put(grammar.getTargetNamespace(), grammar);
65     }
66
67     /**
68      * put a schema grammar and any grammars imported by it (directly or
69      * inderectly) into the registry. when a grammar with the same target
70      * namespace is already in the bucket, and different from the one being
71      * added, it's an error, and no grammar will be added into the bucket.
72      *
73      * @param grammar the grammar to put in the registry
74      * @param deep whether to add imported grammars
75      * @return whether the process succeeded
76      */

77     public boolean putGrammar(SchemaGrammar grammar, boolean deep) {
78         // whether there is one with the same tns
79
SchemaGrammar sg = getGrammar(grammar.fTargetNamespace);
80         if (sg != null) {
81             // if the one we have is different from the one passed, it's an error
82
return sg == grammar;
83         }
84         // not deep import, then just add this one grammar
85
if (!deep) {
86             putGrammar(grammar);
87             return true;
88         }
89
90         // get all imported grammars, and make a copy of the Vector, so that
91
// we can recursively process the grammars, and add distinct ones
92
// to the same vector
93
Vector JavaDoc currGrammars = (Vector JavaDoc)grammar.getImportedGrammars();
94         if (currGrammars == null) {
95             putGrammar(grammar);
96             return true;
97         }
98         
99         Vector JavaDoc grammars = ((Vector JavaDoc)currGrammars.clone());
100         SchemaGrammar sg1, sg2;
101         Vector JavaDoc gs;
102         // for all (recursively) imported grammars
103
for (int i = 0; i < grammars.size(); i++) {
104             // get the grammar
105
sg1 = (SchemaGrammar)grammars.elementAt(i);
106             // check whether the bucket has one with the same tns
107
sg2 = getGrammar(sg1.fTargetNamespace);
108             if (sg2 == null) {
109                 // we need to add grammars imported by sg1 too
110
gs = sg1.getImportedGrammars();
111                 // for all grammars imported by sg2, but not in the vector
112
// we add them to the vector
113
if(gs == null) continue;
114                 for (int j = gs.size() - 1; j >= 0; j--) {
115                     sg2 = (SchemaGrammar)gs.elementAt(j);
116                     if (!grammars.contains(sg2))
117                         grammars.addElement(sg2);
118                 }
119             }
120             // we found one with the same target namespace
121
// if the two grammars are not the same object, then it's an error
122
else if (sg2 != sg1) {
123                 return false;
124             }
125         }
126
127         // now we have all imported grammars stored in the vector. add them
128
putGrammar(grammar);
129         for (int i = grammars.size() - 1; i >= 0; i--)
130             putGrammar((SchemaGrammar)grammars.elementAt(i));
131
132         return true;
133     }
134
135     /**
136      * get all grammars in the registry
137      *
138      * @return an array of SchemaGrammars.
139      */

140     public SchemaGrammar[] getGrammars() {
141         // get the number of grammars
142
int count = fGrammarRegistry.size() + (fNoNSGrammar==null ? 0 : 1);
143         SchemaGrammar[] grammars = new SchemaGrammar[count];
144         // get grammars with target namespace
145
Enumeration JavaDoc schemas = fGrammarRegistry.elements();
146         int i = 0;
147         while (schemas.hasMoreElements())
148             grammars[i++] = (SchemaGrammar)schemas.nextElement();
149         // add the grammar without target namespace, if any
150
if (fNoNSGrammar != null)
151             grammars[count-1] = fNoNSGrammar;
152         return grammars;
153     }
154
155     /**
156      * Clear the registry.
157      * REVISIT: update to use another XSGrammarBucket
158      */

159     public void reset() {
160         fNoNSGrammar = null;
161         fGrammarRegistry.clear();
162     }
163
164 } // class XSGrammarBucket
165
Popular Tags