1 22 23 package org.xquark.schema; 24 25 38 public final class Wildcard extends SchemaComponent { 39 private static final String RCSRevision = "$Revision: 1.1 $"; 40 private static final String RCSName = "$Name: $"; 41 42 private boolean any; 43 private boolean exclude = false; 44 private java.util.HashSet namespaces = new java.util.HashSet (); 45 private int processContents = STRICT; 46 51 Wildcard(Schema schema) { 52 this(schema, false); 53 } 54 55 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 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 checkNamespaceAllowed(String 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 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 return "cos-ns-subset.2.2"; 101 } 102 java.util.Iterator it = this.namespaces.iterator(); 103 while ( it.hasNext() ) { 104 Object ns = it.next(); 105 if ( ancestor.namespaces.contains(ns) == false ) { 106 return "cos-ns-subset.2"; 108 } 109 } 110 } 111 else { 112 java.util.Iterator it = this.namespaces.iterator(); 113 while ( it.hasNext() ) { 114 Object ns = it.next(); 115 if ( ancestor.isAllowed((String )ns) == false ) { 116 return "cos-ns-subset.3"; 118 } 119 } 120 } 121 return null; 122 } 123 124 129 public int getProcessContents() { 130 return processContents; 131 } 132 133 138 boolean isAny() { 139 return any; 140 } 141 142 boolean isInclude() { 143 return !exclude; 144 } 145 146 151 java.util.Set getNamespaces() { 152 return namespaces; 153 } 154 155 public void add(String namespace) { 156 any = false; 157 namespaces.add(namespace); 158 } 159 160 public void exclude(String 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 it1 = this.namespaces.iterator(); 178 java.util.Iterator it2 = wild.namespaces.iterator(); 179 while ( it1.hasNext() ) { 180 Object ns1 = it1.next(); 181 Object 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 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 } 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 it = this.getNamespaces().iterator(); 231 while (it.hasNext()) { 232 String ns = (String )it.next(); 233 if (wild.isAllowed(ns)) result.add(ns); 234 } 235 } else if (wild.isInclude()) { 236 java.util.Iterator it = wild.getNamespaces().iterator(); 237 while (it.hasNext()) { 238 String ns = (String )it.next(); 239 if (this.isAllowed(ns)) result.add(ns); 240 } 241 } else { 242 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 } else if (this.isInclude()) { 257 if (wild.isInclude()) { 258 java.util.Iterator it = this.getNamespaces().iterator(); 259 while (it.hasNext()) { 260 String ns = (String )it.next(); 261 result.add(ns); 262 } 263 it = wild.getNamespaces().iterator(); 264 while (it.hasNext()) { 265 String ns = (String )it.next(); 266 if (!this.isAllowed(ns)) result.add(ns); 267 } 268 } else { 269 java.util.Iterator it = wild.getNamespaces().iterator(); 270 String ns = (String )it.next(); 271 if (this.isAllowed(ns)) { 272 } else { 274 result.exclude(ns); 275 } 276 } 277 } else if (wild.isInclude()) { 278 java.util.Iterator it = this.getNamespaces().iterator(); 279 String ns = (String )it.next(); 280 if (wild.isAllowed(ns)) { 281 } else { 283 result.exclude(ns); 284 } 285 } else { 286 throw new SchemaException("cos-aw-union.4", this); 288 } 289 return result; 290 } 291 292 } 293 | Popular Tags |