KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > Wildcard


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
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.schema;
24
25 /**
26  * class for Wildcard.
27  *
28  * <p>This is the implementation of the Wildcard.
29  * The following properties are defined:</p>
30  * <p> 1. any : a boolean.</p>
31  * <p> 2. namespaces : a set of namespaces.</p>
32  * <p> 3. process contents : one of {SKIP, LAX, STRICT}.</p>
33  *
34  * <p>See XML Schema Part 1: Structures 3.10
35  * W3C Recommendation 2 May 2001</p>
36  *
37  */

38 public final class Wildcard extends SchemaComponent {
39 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
40 private static final String JavaDoc RCSName = "$Name: $";
41
42   private boolean any;
43   private boolean exclude = false;
44   private java.util.HashSet JavaDoc namespaces = new java.util.HashSet JavaDoc();
45   private int processContents = STRICT;
46   /**
47    * Create a new Wildcard.
48    * no parameter, in the case, any = false, by default
49    * @param
50    */

51   Wildcard(Schema schema) {
52     this(schema, false);
53   }
54
55   /**
56    * Create a new Wildcard.
57    *
58    * @param any The any, a boolean.
59    */

60   public Wildcard(Schema schema, boolean any) {
61     super(schema, null);
62     this.any = any;
63   }
64   
65   public void accept(SchemaVisitor visitor) throws SchemaException {
66     visitor.visit(this);
67   }
68
69   public boolean isAllowed(String JavaDoc namespace) {
70     if (any) return true;
71     if (exclude)
72       return (namespace != null && !namespaces.contains(namespace));
73     return namespaces.contains(namespace);
74   }
75
76   public boolean isUnqualifiedAllowed() {
77     return isAllowed(null);
78   }
79
80   public String JavaDoc checkNamespaceAllowed(String JavaDoc namespace) {
81     if (any) return null;
82     if (exclude) {
83       if (namespaces.contains(namespace))
84         return "cvc-wildcard-namespace.2.2";
85       if (namespace == null)
86         return "cvc-wildcard-namespace.2.3";
87       return null;
88     }
89     if (!namespaces.contains(namespace))
90       return "cvc-wildcard-namespace.3";
91     return null;
92   }
93   
94   public String JavaDoc validWildcardSubset(Wildcard ancestor) {
95     if ( ancestor.any ) return null;
96     if ( this.any ) return "cos-ns-subset";
97     if ( this.exclude ) {
98       if ( ancestor.exclude == false ) {
99         // Parent wildcard constraint must ba a pair of not and a namespace
100
return "cos-ns-subset.2.2";
101       }
102       java.util.Iterator JavaDoc it = this.namespaces.iterator();
103       while ( it.hasNext() ) {
104         Object JavaDoc ns = it.next();
105         if ( ancestor.namespaces.contains(ns) == false ) {
106           // Parent wildcard constraint must be a pair of not and the same namespace
107
return "cos-ns-subset.2";
108         }
109       }
110     }
111     else {
112       java.util.Iterator JavaDoc it = this.namespaces.iterator();
113       while ( it.hasNext() ) {
114         Object JavaDoc ns = it.next();
115         if ( ancestor.isAllowed((String JavaDoc)ns) == false ) {
116           // All namespaces in namespace list must be allowed in parent wildcard.
117
return "cos-ns-subset.3";
118         }
119       }
120     }
121     return null;
122   }
123
124   /**
125    * Look up the process contents.
126    *
127    * @return The process contents, one of {SKIP, LAX, STRICT}.
128    */

129   public int getProcessContents() {
130     return processContents;
131   }
132   
133   /**
134    * Look up the any.
135    *
136    * @return The any, a boolean.
137    */

138   boolean isAny() {
139     return any;
140   }
141
142   boolean isInclude() {
143     return !exclude;
144   }
145
146   /**
147    * Look up the namespaces.
148    *
149    * @return A set of namespaces.
150    */

151   java.util.Set JavaDoc getNamespaces() {
152     return namespaces;
153   }
154   
155   public void add(String JavaDoc namespace) {
156     any = false;
157     namespaces.add(namespace);
158   }
159
160   public void exclude(String JavaDoc namespace) {
161     any = false;
162     exclude = true;
163     namespaces.add(namespace);
164   }
165
166   public void setProcessContents(int processContents) {
167     this.processContents = processContents;
168   }
169   
170   boolean equals(Wildcard wild) {
171     if ( this == wild ) return true;
172     if ( wild == null ) return false;
173     if ( this.any != wild.any ) return false;
174     if ( this.exclude != wild.exclude ) return false;
175     if ( this.processContents != wild.processContents ) return false;
176     if ( this.namespaces.size() != wild.namespaces.size() ) return false;
177     java.util.Iterator JavaDoc it1 = this.namespaces.iterator();
178     java.util.Iterator JavaDoc it2 = wild.namespaces.iterator();
179     while ( it1.hasNext() ) {
180         Object JavaDoc ns1 = it1.next();
181         Object JavaDoc ns2 = it2.next();
182         if ( ns1 != null ) {
183             if ( ns1.equals(ns2) == false ) return false;
184         }
185         else if ( ns2 != null )
186             return false;
187     }
188     return true;
189   }
190   
191   boolean overlapUPAC(Object JavaDoc obj) {
192     if ( obj == null ) return false;
193     if ( obj instanceof ElementDeclaration )
194       return overlapUPAC((ElementDeclaration)obj);
195     else if ( obj instanceof Wildcard )
196        return overlapUPAC((Wildcard)obj);
197     else
198       return false;
199  }
200   
201   boolean overlapUPAC(ElementDeclaration decl) {
202     if ( decl == null ) return false;
203     else return decl.overlapUPAC(this);
204   }
205    
206   boolean overlapUPAC(Wildcard wild) {
207     if ( wild == null ) return false;
208     if ( this == wild ) return true;
209     
210     try {
211       Wildcard intersectionWild = this.wildcardIntersection(wild);
212       if ( intersectionWild.getNamespaces().size() > 0 ) return true;
213     }
214     catch ( SchemaException se ) {
215       // it's empty
216
}
217     
218     return false;
219   }
220   
221   Wildcard wildcardIntersection(Wildcard wild)
222     throws SchemaException
223   {
224     if ( this.isAny() ) return wild;
225     if (wild == null || wild.isAny()) return this;
226     
227     Wildcard result = new Wildcard(schema);
228     result.setProcessContents(this.getProcessContents());
229     if (this.isInclude()) {
230       java.util.Iterator JavaDoc it = this.getNamespaces().iterator();
231       while (it.hasNext()) {
232         String JavaDoc ns = (String JavaDoc)it.next();
233         if (wild.isAllowed(ns)) result.add(ns);
234       }
235     } else if (wild.isInclude()) {
236       java.util.Iterator JavaDoc it = wild.getNamespaces().iterator();
237       while (it.hasNext()) {
238         String JavaDoc ns = (String JavaDoc)it.next();
239         if (this.isAllowed(ns)) result.add(ns);
240       }
241     } else {
242 // "Attribute wildcards cannot be combined";
243
throw new SchemaException("cos-aw-intersect.5", this);
244     }
245     return result;
246   }
247     
248   Wildcard wildcardUnion(Wildcard wild)
249     throws SchemaException
250   {
251     if (wild == null) return this;
252     Wildcard result = new Wildcard(schema, true);
253     result.setProcessContents(this.getProcessContents());
254     if (this.isAny() || wild.isAny()) {
255       // result is any
256
} else if (this.isInclude()) {
257       if (wild.isInclude()) {
258         java.util.Iterator JavaDoc it = this.getNamespaces().iterator();
259         while (it.hasNext()) {
260           String JavaDoc ns = (String JavaDoc)it.next();
261           result.add(ns);
262         }
263         it = wild.getNamespaces().iterator();
264         while (it.hasNext()) {
265           String JavaDoc ns = (String JavaDoc)it.next();
266           if (!this.isAllowed(ns)) result.add(ns);
267         }
268       } else {
269         java.util.Iterator JavaDoc it = wild.getNamespaces().iterator();
270         String JavaDoc ns = (String JavaDoc)it.next();
271         if (this.isAllowed(ns)) {
272           // any
273
} else {
274           result.exclude(ns);
275         }
276       }
277     } else if (wild.isInclude()) {
278       java.util.Iterator JavaDoc it = this.getNamespaces().iterator();
279       String JavaDoc ns = (String JavaDoc)it.next();
280       if (wild.isAllowed(ns)) {
281         // any
282
} else {
283         result.exclude(ns);
284       }
285     } else {
286 // "Attribute wildcards cannot be combined";
287
throw new SchemaException("cos-aw-union.4", this);
288     }
289     return result;
290   }
291   
292 }
293
Popular Tags