KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999,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 import org.enhydra.apache.xerces.framework.XMLContentSpec;
61 import org.enhydra.apache.xerces.framework.XMLErrorReporter;
62 import org.enhydra.apache.xerces.utils.QName;
63 import org.enhydra.apache.xerces.utils.StringPool;
64 import org.enhydra.apache.xerces.validators.schema.SchemaMessageProvider;
65 import org.enhydra.apache.xerces.validators.schema.SubstitutionGroupComparator;
66
67 /**
68  * ElementWildcard is used to check whether two element declarations conflict
69  */

70 public class ElementWildcard {
71     private ElementWildcard(){}
72
73     private static boolean uriInWildcard(QName qname, int wildcard, int wtype,
74                                          SubstitutionGroupComparator comparator) throws Exception JavaDoc {
75         int type = wtype & 0x0f;
76
77         if (type == XMLContentSpec.CONTENTSPECNODE_ANY) {
78             return true;
79         }
80         else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_NS) {
81             // substitution of "uri" satisfies "wtype:wildcard"
82
if (comparator != null) {
83                 if (comparator.isAllowedByWildcard(qname, wildcard, false))
84                     return true;
85             } else {
86                 if (qname.uri == wildcard)
87                     return true;
88             }
89         }
90         else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) {
91             // substitution of "uri" satisfies "wtype:wildcard"
92
if (comparator != null) {
93                 if (comparator.isAllowedByWildcard(qname, wildcard, true))
94                     return true;
95             } else {
96                 //if (wildcard != uri && uri != StringPool.EMPTY_STRING) // ???
97
if (wildcard != qname.uri)
98                     return true;
99             }
100         }
101
102         return false;
103     }
104
105     private static boolean wildcardIntersect(int w1, int t1, int w2, int t2) {
106         int type1 = t1 & 0x0f, type2 = t2 & 0x0f;
107
108         // if either one is "##any", then intersects
109
if (type1 == XMLContentSpec.CONTENTSPECNODE_ANY ||
110             type2 == XMLContentSpec.CONTENTSPECNODE_ANY) {
111             return true;
112         }
113
114         // if both are "some_namespace" and equal, then intersects
115
if (type1 == XMLContentSpec.CONTENTSPECNODE_ANY_NS &&
116             type2 == XMLContentSpec.CONTENTSPECNODE_ANY_NS &&
117             w1 == w2) {
118             return true;
119         }
120
121         // if both are "##other", and equal, then intersects
122
if (type1 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER &&
123             type2 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) {
124             return true;
125         }
126
127         // if one "##other" and one namespace, if not equal, then intersects
128
if ((type1 == XMLContentSpec.CONTENTSPECNODE_ANY_NS &&
129              type2 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER ||
130              type1 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER &&
131              type2 == XMLContentSpec.CONTENTSPECNODE_ANY_NS) &&
132             w1 != w2) {
133             return true;
134         }
135
136         return false;
137     }
138
139     // check whether two elements conflict
140
private static boolean conflic(int type1, int local1, int uri1,
141                                    int type2, int local2, int uri2,
142                                    SubstitutionGroupComparator comparator) throws Exception JavaDoc {
143         QName q1 = new QName(), q2 = new QName();
144         q1.localpart = local1;
145         q1.uri = uri1;
146         q2.localpart = local2;
147         q2.uri = uri2;
148
149         if (type1 == XMLContentSpec.CONTENTSPECNODE_LEAF &&
150             type2 == XMLContentSpec.CONTENTSPECNODE_LEAF) {
151             if (comparator != null) {
152                 if (comparator.isEquivalentTo(q1, q2) ||
153                     comparator.isEquivalentTo(q2, q1))
154                     return true;
155             } else {
156                 if (q1.localpart == q2.localpart &&
157                     q1.uri == q2.uri)
158                     return true;
159             }
160         } else if (type1 == XMLContentSpec.CONTENTSPECNODE_LEAF) {
161             if (uriInWildcard(q1, uri2, type2, comparator))
162                 return true;
163         } else if (type2 == XMLContentSpec.CONTENTSPECNODE_LEAF) {
164             if (uriInWildcard(q2, uri1, type1, comparator))
165                 return true;
166         } else {
167             if (wildcardIntersect(uri1, type1, uri2, type2))
168                 return true;
169         }
170
171         return false;
172     }
173
174     public static boolean conflict(int type1, int local1, int uri1,
175                                    int type2, int local2, int uri2,
176                                    SubstitutionGroupComparator comparator) throws Exception JavaDoc {
177         boolean ret = conflic(type1, local1, uri1, type2, local2, uri2, comparator);
178
179         if (ret && comparator != null) {
180             StringPool stringPool = comparator.getStringPool();
181             XMLErrorReporter err = comparator.getErrorReporter();
182             err.reportError(err.getLocator(),
183                             SchemaMessageProvider.SCHEMA_DOMAIN,
184                             SchemaMessageProvider.UniqueParticleAttribution,
185                             SchemaMessageProvider.MSG_NONE,
186                             new Object JavaDoc[]{eleString(type1, local1, uri1, stringPool),
187                                          eleString(type2, local2, uri2, stringPool)},
188                             XMLErrorReporter.ERRORTYPE_RECOVERABLE_ERROR);
189         }
190
191         return ret;
192     }
193
194     private static String JavaDoc eleString(int type, int local, int uri, StringPool stringPool) {
195         switch (type & 0x0f) {
196         case XMLContentSpec.CONTENTSPECNODE_LEAF:
197             return stringPool.toString(uri) + "," + stringPool.toString(local);
198         case XMLContentSpec.CONTENTSPECNODE_ANY:
199             return "##any,*";
200         case XMLContentSpec.CONTENTSPECNODE_ANY_NS:
201             return "##any(" + stringPool.toString(uri) + "),*";
202         case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER:
203             return "##other(" + stringPool.toString(uri) + "),*";
204         }
205
206         return "";
207     }
208 } // class ElementWildcard
209
Popular Tags