1 22 23 package org.xquark.xquery.typing; 24 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.xquark.xquery.parser.XQueryException; 30 31 98 99 public abstract class QTypePrime extends QType { 100 101 105 protected PrimeList primeList = null; 106 109 protected byte primeOccurence = OCC_1_1; 110 111 QTypePrime(List list, byte subclass) { 113 this.subclass = subclass; 114 computePrimeList(list); 116 } 118 119 122 public List getList() { 123 return primeList; 124 } 125 126 public List getPrimeList() { 127 return primeList; 128 } 129 130 public byte getOccurence() { 131 return primeOccurence; 132 } 133 134 private void computePrimeList(List list) { 135 primeList = new PrimeList(); 136 if (list == null || list.isEmpty()) 137 return; 138 if (list == null || list.isEmpty()) 140 return; 141 for (int i = 0; i < list.size(); i++) { 142 QType qtypei = (QType) list.get(i); 143 List listi = ((QType) list.get(i)).getList(); 144 if (listi == null) { primeList.add(qtypei); 146 } else { 147 primeList.addAll(listi); 149 } 150 computeOccurence(qtypei.getOccurence()); 151 } 152 } 153 154 private void computeOccurence(byte compOccurence) { 155 switch (subclass) { 156 case SEQUENCE : 157 switch (primeOccurence) { 158 case OCC_1_1 : 159 case OCC_1_N : 160 primeOccurence = OCC_1_N; 161 break; 162 case OCC_0_1 : 163 case OCC_0_N : 164 switch (compOccurence) { 165 case OCC_1_1 : 166 case OCC_1_N : 167 primeOccurence = OCC_1_N; 168 break; 169 case OCC_0_1 : 170 case OCC_0_N : 171 primeOccurence = OCC_0_N; 172 break; 173 } 174 break; 175 } 176 break; 177 case UNION : 178 primeOccurence = computeOccurence(primeOccurence, compOccurence); 179 break; 180 } 181 } 182 183 public boolean equals(Object qtype) { 185 if (!(qtype instanceof QTypePrime)) 186 return false; 187 List list2 = ((QTypePrime) qtype).getPrimeList(); 190 int size1 = primeList.size(); 191 int size2 = list2.size(); 192 if (size1 != size2) 193 return false; 194 for (int i = 0; i < size1; i++) { 195 QType qtype1 = (QType) primeList.get(i); 196 boolean found = false; 197 for (int j = 0; j < size2; j++) 198 if (found = qtype1.equals((QType) list2.get(j))) 199 break; 200 if (!found) 201 return false; 202 } 203 return true; 204 } 205 206 public boolean isNumeric() { 208 Iterator it = primeList.iterator(); 209 while (it.hasNext()) 210 if (((QType) it.next()).isNumeric()) 211 return true; 212 return false; 213 } 214 215 public boolean isBoolean() { 217 int size = primeList.size(); 218 for (int i = 0; i < size; i++) 219 if (((QType) primeList.get(i)).isBoolean()) 220 return true; 221 return false; 222 } 223 224 public boolean isInteger() { 226 int size = primeList.size(); 227 for (int i = 0; i < size; i++) 228 if (((QType) primeList.get(i)).isInteger()) 229 return true; 230 return false; 231 } 232 233 public boolean isString() { 235 int size = primeList.size(); 236 for (int i = 0; i < size; i++) 237 if (((QType) primeList.get(i)).isString()) 238 return true; 239 return false; 240 } 241 242 public boolean isQName() { 244 int size = primeList.size(); 245 for (int i = 0; i < size; i++) 246 if (((QType) primeList.get(i)).isQName()) 247 return true; 248 return false; 249 } 250 251 public boolean isAtom() { 253 int size = primeList.size(); 254 for (int i = 0; i < size; i++) 255 if (((QType) primeList.get(i)).isAtom()) 256 return true; 257 return false; 258 } 259 260 public boolean isText() { 262 Iterator it = primeList.iterator(); 263 while (it.hasNext()) 264 if (((QType) it.next()).isText()) 265 return true; 266 return false; 267 } 268 269 public boolean isDate() { 271 Iterator it = primeList.iterator(); 272 while (it.hasNext()) 273 if (((QType) it.next()).isDate()) 274 return true; 275 return false; 276 } 277 278 public boolean isAttribute() { 280 Iterator it = primeList.iterator(); 281 while (it.hasNext()) 282 if (((QType) it.next()).isAttribute()) 283 return true; 284 return false; 285 } 286 287 public boolean isOnlyAttribute() { 289 Iterator it = primeList.iterator(); 290 while (it.hasNext()) 291 if (!((QType) it.next()).isOnlyAttribute()) 292 return false; 293 return true; 294 } 295 296 public boolean isIDAttribute() { 298 Iterator it = primeList.iterator(); 299 while (it.hasNext()) 300 if (((QType) it.next()).isIDAttribute()) 301 return true; 302 return false; 303 } 304 305 public boolean isNode() { 307 Iterator it = primeList.iterator(); 308 while (it.hasNext()) 309 if (((QType) it.next()).isNode()) 310 return true; 311 return false; 312 } 313 314 public boolean isSimpleTypeNode() { 316 Iterator it = primeList.iterator(); 317 while (it.hasNext()) 318 if (((QType) it.next()).isSimpleTypeNode()) 319 return true; 320 return false; 321 } 322 323 public boolean isDocument() { 325 Iterator it = primeList.iterator(); 326 while (it.hasNext()) 327 if (((QType) it.next()).isDocument()) 328 return true; 329 return false; 330 } 331 332 341 public boolean isAtomic() { 343 Iterator it = primeList.iterator(); 344 while (it.hasNext()) 345 if (((QType) it.next()).isAtomic()) 346 return true; 347 return false; 348 } 349 350 public boolean isMixed() { 352 Iterator it = primeList.iterator(); 353 while (it.hasNext()) 354 if (((QType) it.next()).isMixed()) 355 return true; 356 return false; 357 } 358 359 public boolean isElement() { 361 Iterator it = primeList.iterator(); 362 while (it.hasNext()) 363 if (((QType) it.next()).isElement()) 364 return true; 365 return false; 366 } 367 368 public boolean canBeCastedInto(int castType) { 369 Iterator it = primeList.iterator(); 370 while (it.hasNext()) 371 if (((QType) it.next()).canBeCastedInto(castType)) 372 return true; 373 return false; 374 } 375 376 public boolean canBeComparedTo(QType compType) { 377 if (compType == null) 378 return false; 379 Iterator it = primeList.iterator(); 380 while (it.hasNext()) 381 if (((QType) it.next()).canBeComparedTo(compType)) 382 return true; 383 return false; 384 } 385 386 public QType applyDATAFunction() throws TypeException { 387 ArrayList newList = new ArrayList (); 388 Iterator it = primeList.iterator(); 389 while (it.hasNext()) { 390 QType qtype = (QType) it.next(); 391 qtype = qtype.applyDATAFunction(); 392 if (qtype != null) 393 newList.add(qtype); 394 } 395 if (!newList.isEmpty()) { 396 if (newList.size() == 1) 397 return (QType) newList.get(0); 398 else 399 return new QTypeUnion(newList); 400 } 401 return null; 402 } 403 404 public QType changeOccurence(byte occurence) { 405 QTypePrime qtype = null; 406 try { 407 qtype = (QTypePrime) clone(); 408 } catch (CloneNotSupportedException cnse) {} 409 qtype.primeOccurence = occurence; 410 return qtype; 411 } 412 413 public void accept(QTypeVisitor visitor) throws XQueryException { 414 visitor.visit(this); 415 } 416 417 } 418 | Popular Tags |