KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > typing > OccurrenceVisitor


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery.typing;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.xquark.schema.*;
30 import org.xquark.xquery.parser.QName;
31 import org.xquark.xquery.parser.XQueryException;
32
33 public class OccurrenceVisitor extends DefaultSchemaVisitor {
34     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
35     private static final String JavaDoc RCSName = "$Name: $";
36     private static final String JavaDoc STAR = "*";
37
38     private int[] occurs = new int[2];
39     private String JavaDoc namespace = null;
40     private String JavaDoc name = null;
41     private List JavaDoc matches = new ArrayList JavaDoc();
42     private boolean matchesWildcard = false;
43     private SchemaManager schemamanager = null;
44
45     public void reset(String JavaDoc namespace, String JavaDoc name, SchemaManager schemamanager) {
46         occurs[0] = occurs[1] = 0;
47         matches.clear();
48         matchesWildcard = false;
49         this.namespace = namespace;
50         this.name = name;
51         this.schemamanager = schemamanager;
52     }
53
54     public QType getQType() throws XQueryException {
55         // calculate occurence
56
byte occurrence = QType.OCC_0_0;
57         if (occurs[0] == 0) {
58             if (occurs[1] == 0)
59                 occurrence = QType.OCC_0_0;
60             else if (occurs[1] == 1)
61                 occurrence = QType.OCC_0_1;
62             else
63                 occurrence = QType.OCC_0_N;
64         } else if (occurs[0] == 1) {
65             if (occurs[1] == 1)
66                 occurrence = QType.OCC_1_1;
67             else
68                 occurrence = QType.OCC_1_N;
69         }
70         if (matchesWildcard) {
71             return new QTypeElement(new QName(namespace, null, name, null, null), schemamanager.getType(SchemaConstants.XMLSCHEMA_URI, TypeVisitor.SC_ANYTYPE), occurrence);
72         }
73         if (!matches.isEmpty()) {
74             if (matches.size() == 1) {
75                 return getQTypeFromDeclaration((Declaration) matches.get(0), occurrence);
76             } else {
77                 ArrayList JavaDoc qtypes = new ArrayList JavaDoc(matches.size());
78                 for (int i = 0; i < matches.size(); i++) {
79                     qtypes.add(getQTypeFromDeclaration((Declaration) matches.get(i), QType.OCC_1_1));
80                 }
81                 return new QTypeUnion(qtypes, occurrence);
82             }
83         }
84         return null;
85     }
86
87     // public int[] getOccurrenceCount() {
88
// return occurs;
89
// }
90
// public List getMatches() {
91
// return matches;
92
// }
93

94     private void setValues(long min, long max) {
95         if (max > Integer.MAX_VALUE)
96             max = Integer.MAX_VALUE;
97         occurs[0] = (int) min;
98         occurs[1] = (int) max;
99     }
100
101     public void visit(Particle particle) throws SchemaException {
102         ((SchemaVisitable) particle.getTerm()).accept(this);
103         long min = occurs[0] * particle.getMinOccurs();
104         long max = occurs[1] * particle.getMaxOccurs();
105         setValues(min, max);
106     }
107
108     public void visit(ModelGroup group) throws SchemaException {
109         long min = 0;
110         long max = 0;
111         Iterator JavaDoc it = group.iterator();
112         while (it.hasNext()) {
113             occurs[0] = occurs[1] = 0;
114             ((SchemaVisitable) it.next()).accept(this);
115             min += occurs[0];
116             max += occurs[1];
117         }
118         setValues(min, max);
119     }
120
121     public void visit(ChoiceModelGroup group) throws SchemaException {
122         long min = Integer.MAX_VALUE;
123         long max = 0;
124         Iterator JavaDoc it = group.iterator();
125         while (it.hasNext()) {
126             occurs[0] = occurs[1] = 0;
127             ((SchemaVisitable) it.next()).accept(this);
128             if (occurs[0] < min)
129                 min = occurs[0];
130             if (occurs[1] > max)
131                 max = occurs[1];
132         }
133         setValues(min, max);
134     }
135
136     public void visit(ElementDeclaration decl) throws SchemaException {
137         if (namespace != null && namespace.equals(STAR)) {
138             if (name.equals(STAR)) {
139                 matches.add(decl);
140                 if (decl.getSubstitutionElementList() != null)
141                     matches.addAll(decl.getSubstitutionElementList());
142             } else {
143                 if (name.equals(decl.getName())) {
144                     matches.add(decl);
145                 }
146                 List JavaDoc decllist = decl.getSubstitutionElementList();
147                 if (decllist != null && !decllist.isEmpty()) {
148                     for (int i = 0; i < decllist.size(); i++) {
149                         if (name.equals(((Declaration) decllist.get(i)).getName())) {
150                             matches.add(decllist.get(i));
151                         }
152                     }
153                 }
154             }
155         } else if (name.equals(STAR)) {
156             if (namespace != null && namespace.equals(decl.getNamespace())) {
157                 matches.add(decl);
158             }
159             List JavaDoc decllist = decl.getSubstitutionElementList();
160             if (decllist != null && !decllist.isEmpty()) {
161                 for (int i = 0; i < decllist.size(); i++) {
162                     if (namespace != null && namespace.equals(((Declaration) decllist.get(i)).getNamespace())) {
163                         matches.add(decllist.get(i));
164                     }
165                 }
166             }
167         }
168         if (decl.hasName(namespace, name) || (decl = decl.getSubstitutionElement(namespace, name)) != null) {
169             occurs[0] = occurs[1] = 1;
170             matches.add(decl);
171         }
172     }
173
174     public void visit(Wildcard wildcard) throws SchemaException {
175         if (wildcard.isAllowed(namespace)) {
176             occurs[0] = occurs[1] = 1;
177             matchesWildcard = true;
178         }
179     }
180
181     // private methods
182
private QType getQTypeFromDeclaration(Declaration decl, byte occurrence) throws XQueryException {
183         QName qname = new QName(decl.getNamespace(), null, decl.getName(), null, null);
184         if (decl instanceof AttributeDeclaration) {
185             return new QTypeAttribute(qname, (AttributeDeclaration)decl, (SimpleType) decl.getType());
186         } else if (decl instanceof ElementDeclaration) {
187             return new QTypeElement(qname, decl.getType(), occurrence);
188         }
189         return null;
190     }
191
192 }
193
Popular Tags