1 23 24 package org.xquark.xquery.normalize; 25 26 import java.util.ArrayList ; 28 import java.util.HashSet ; 29 import java.util.Set ; 30 31 import org.xquark.schema.AttributeDeclaration; 32 import org.xquark.schema.ElementDeclaration; 33 import org.xquark.schema.Type; 34 import org.xquark.xpath.Axis; 35 import org.xquark.xquery.parser.*; 36 import org.xquark.xquery.typing.*; 37 38 public class SubExpressionVisitor extends DefaultParserVisitor { 39 40 private static final String RCSRevision = "$Revision: 1.2 $"; 41 private static final String RCSName = "$Name: $"; 42 43 private ArrayList resultExprs = new ArrayList (1); 44 private XQueryExpression subExpression = null; 45 private byte axis = Axis.NONE; 46 private int depth = 1; 47 private boolean last = false; 48 private String subNameSpace = null; 49 private String subLocalName = null; 50 private TypeVisitor typeVisitor = null; 51 52 public ArrayList getResultExprs() { 53 return resultExprs; 54 } 55 56 public SubExpressionVisitor(TypeVisitor typeVisitor) { 57 this.typeVisitor = typeVisitor; 58 } 59 60 public void setSubExpression(byte axis) { 61 setSubExpression(null, axis, 1, false); 62 } 63 public void setSubExpression(byte axis, int depth) { 64 setSubExpression(null, axis, depth, false); 65 } 66 public void setSubExpression(QName qname) { 67 setSubExpression(qname, Axis.NONE, 1, false); 68 } 69 public void setSubExpression(QName qname, boolean last) { 70 setSubExpression(qname, Axis.CHILD, 1, last); 71 } 72 public void setSubExpression(QName qname, byte axis) { 73 setSubExpression(qname, axis, 1, false); 74 } 75 public void setSubExpression(QName qname, byte axis, int depth, boolean last) { 76 subExpression = qname; 77 this.axis = axis; 78 this.depth = depth; 79 this.last = last; 80 if (subExpression != null) { 81 subNameSpace = ((QName) subExpression).getNameSpace(); 82 subLocalName = ((QName) subExpression).getLocalName(); 83 } else { 84 subNameSpace = null; 85 subLocalName = null; 86 } 87 resultExprs.clear(); 88 } 89 90 93 94 public void visit(AttributeValuePair arg) throws XQueryException { 95 resultExprs.clear(); 96 if (depth == 0 && axis == Axis.ATTRIBUTE) { 98 XQueryExpression attValPairName = arg.getAttributeName(); 99 if (!(attValPairName instanceof QName)) 100 return; 101 String nameSpace = ((QName) attValPairName).getNameSpace(); 102 String localName = ((QName) attValPairName).getLocalName(); 103 if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) { 104 XQueryExpression attValPairValue = arg.getAttributeValue(); 105 if (attValPairValue instanceof XQueryExpressionSequence) 106 resultExprs.add((XQueryExpression) ((XQueryExpressionSequence) attValPairValue).getSubExpressions().get(0)); 107 else 108 resultExprs.add(attValPairValue); 109 } 110 } 111 } 112 113 120 121 public void visit(Element arg) throws XQueryException { 122 resultExprs.clear(); 123 ArrayList subExpressions = arg.getSubExpressions(); 125 ArrayList attributes = arg.getAttributes(); 126 QType qtype = arg.getQType(); 127 Type type = qtype.getType(); 128 if (subExpression == null) { 129 if (subExpressions != null && subExpressions.size() == 1) { 130 XQueryExpression tmpExpr = (XQueryExpression) subExpressions.get(0); 131 switch (axis) { 133 case Axis.SELF : 134 if (type != null && type.getValueType() != null) { 135 if (tmpExpr instanceof XQueryExpressionSequence) 136 subExpressions = ((XQueryExpressionSequence) tmpExpr).getSubExpressions(); 137 resultExprs.add(subExpressions.get(0)); 138 } 139 break; 140 } 141 } 142 return; 143 } 144 XQueryExpression startTag = arg.getStartTag(); 145 if (!(startTag instanceof QName)) { 146 return; 147 } 148 if (depth == 0) { 149 String nameSpace = null; 150 String localName = null; 151 switch (axis) { 152 case Axis.NONE : 153 case Axis.CHILD : 154 QName startTagName = (QName) startTag; 155 nameSpace = startTagName.getNameSpace(); 156 localName = startTagName.getLocalName(); 157 if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) { 158 if (last) { 159 if (qtype.getType().isSimpleType()) { 160 if (subExpressions != null && subExpressions.size() == 1) { 161 XQueryExpression tmpExpr = (XQueryExpression) subExpressions.get(0); 162 if (tmpExpr instanceof XQueryExpressionSequence) 163 subExpressions = ((XQueryExpressionSequence) tmpExpr).getSubExpressions(); 164 resultExprs.add(subExpressions.get(0)); 165 } 166 } 167 } else { 168 if (subExpressions != null) 169 for (int i = 0; i < subExpressions.size(); i++) 170 resultExprs.add(subExpressions.get(i)); 171 if (attributes != null) 172 for (int i = 0; i < attributes.size(); i++) 173 resultExprs.add(attributes.get(i)); 174 } 175 return; 176 } 177 break; 178 194 } 195 } else { 196 if (depth == 1 && axis == Axis.ATTRIBUTE) { 197 if (attributes != null) { 198 for (int i = 0; i < attributes.size(); i++) { 199 AttributeValuePair attValPair = (AttributeValuePair) attributes.get(i); 200 XQueryExpression attValPairName = attValPair.getAttributeName(); 201 if (!(attValPairName instanceof QName)) 202 continue; 203 String nameSpace = ((QName) attValPairName).getNameSpace(); 204 String localName = ((QName) attValPairName).getLocalName(); 205 if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) { 206 XQueryExpression attValPairValue = attValPair.getAttributeValue(); 207 if (attValPairValue instanceof XQueryExpressionSequence) 208 resultExprs.add((XQueryExpression) ((XQueryExpressionSequence) attValPairValue).getSubExpressions().get(0)); 209 else 210 resultExprs.add(attValPairValue); 211 break; 212 } 213 } 214 } 215 } else { 216 if (depth == -1) { 217 Set tmpResults = new HashSet (); 218 if (attributes != null) { 219 for (int i = 0; i < attributes.size(); i++) { 220 AttributeValuePair attValPair = (AttributeValuePair) attributes.get(i); 221 depth = 0; 222 attValPair.accept(this); 223 if (resultExprs != null) 224 tmpResults.addAll(resultExprs); 225 depth = -1; 226 } 227 } 228 if (subExpressions != null) { 229 for (int i = 0; i < subExpressions.size(); i++) { 230 XQueryExpression subExpr = (XQueryExpression) subExpressions.get(i); 231 depth = 0; 232 subExpr.accept(this); 233 if (resultExprs != null) 234 tmpResults.addAll(resultExprs); 235 depth = -1; 236 subExpr.accept(this); 237 if (resultExprs != null) 238 tmpResults.addAll(resultExprs); 239 } 240 } 241 resultExprs = new ArrayList (tmpResults); 242 } else { 243 if (subExpressions != null) { 244 ArrayList tmpResults = new ArrayList (); 245 for (int i = 0; i < subExpressions.size(); i++) { 246 XQueryExpression subExpr = (XQueryExpression) subExpressions.get(i); 247 248 depth--; 249 resultExprs.clear(); 250 subExpr.accept(this); 251 if (resultExprs != null) 252 tmpResults.addAll(resultExprs); 253 depth++; 254 } 255 resultExprs = new ArrayList (tmpResults); 256 } 257 } 258 } 259 } 260 261 } 262 263 boolean compareQNames(String nameSpace1, String localName1, String nameSpace2, String localName2) { 265 if ((((nameSpace1 == null || nameSpace1.equals("")) && (nameSpace2 == null || nameSpace2.equals(""))) || (nameSpace1 != null && nameSpace1.equals("*")) || (nameSpace2 != null) && (nameSpace2.equals("*") || nameSpace2.equals(nameSpace1))) && localName1 != null && localName2 != null && localName1.equals(localName2)) 266 return true; 267 return false; 268 } 269 270 286 287 public void visit(LocatedExpression arg) throws XQueryException { 288 if (depth != -1) 289 resultExprs.clear(); 290 QType qtype = arg.getQType(); 293 if (!(qtype instanceof QTypeElement)) { 294 return; 295 } 296 QTypeElement qtypeelt = (QTypeElement) qtype; 297 XQueryExpression name = qtypeelt.getName(); 298 if (!(name instanceof QName)) { 299 return; 300 } 301 QName qname = (QName) name; 302 Type type = qtypeelt.getType(); 303 if (subExpression == null) { 305 if (axis == Axis.SELF) { 306 if (!type.isSimpleType()) { 307 return; 308 } 309 resultExprs.add(arg); 310 } 311 return; 312 } 313 if (depth == 0) { 314 if (!type.isSimpleType() && !type.getName().equals(Type.ANY_TYPE)) { 315 return; 316 } 317 if (!compareQNames(qname.getNameSpace(), qname.getLocalName(), subNameSpace, subLocalName)) { 318 return; 319 } 320 resultExprs.add(arg); 321 } else if (depth == 1) { 322 if (type.isSimpleType()) { 323 return; 324 } 325 if (axis == Axis.ATTRIBUTE) { 326 AttributeDeclaration attDecl = type.getAttributeDeclaration(subNameSpace, subLocalName); 327 if (attDecl != null) { 328 ArrayList steps = (ArrayList ) arg.getSteps().clone(); 329 steps.add(new Step(true, Axis.ATTRIBUTE, subExpression, null, arg.getParentModule())); 330 resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule())); 331 return; 332 } 333 } else { 334 ElementDeclaration eltDecl = type.getElementDeclaration(subNameSpace, subLocalName); 335 if (eltDecl != null) { 336 ArrayList steps = (ArrayList ) arg.getSteps().clone(); 337 steps.add(new Step(true, Axis.CHILD, subExpression, null, arg.getParentModule())); 338 resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule())); 339 return; 340 } 341 } 342 343 } else if (depth == -1) { 344 } 345 } 346 347 365 public void visit(Variable arg) throws XQueryException { 366 resultExprs.clear(); 367 QType qtype = arg.getQType(); 369 Type type = qtype.getType(); 370 if (subExpression == null) { 372 if (axis == Axis.SELF) { 373 if (type != null && !type.isSimpleType()) { 374 return; 375 } 376 resultExprs.add(arg); 377 } 378 return; 379 } 380 if (!(qtype instanceof QTypeElement || (qtype instanceof QTypeUnion && qtype.isElement()))) { 381 return; 382 } 383 if (qtype instanceof QTypeElement) { 384 QTypeElement qtypeelt = (QTypeElement) qtype; 385 XQueryExpression name = qtypeelt.getName(); 386 if (!(name instanceof QName)) { 387 return; 388 } 389 QName qname = (QName) name; 390 if (depth == 0) { 391 if ( 392 subExpression instanceof QName) { 393 String nameSpace = ((QName) qtype.getName()).getNameSpace(); 394 String localName = ((QName) qtype.getName()).getLocalName(); 395 if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) { 396 resultExprs.add(arg); 397 return; 398 } 399 } 400 } 401 if (depth != -1) { 404 if (subExpression instanceof QName) { 405 QType qt = Utils.getSubType(QTypeDocument.ANY_ORIGIN, qtype, new Step(false, axis, subExpression, null, null), this.typeVisitor.getSchemaManager(), false); 406 if (qt != null) { 407 ArrayList steps = new ArrayList (); 408 steps.add(new Step(false, Axis.NONE, arg, null, arg.getParentModule())); 409 steps.add(new Step(true, axis, subExpression, null, arg.getParentModule())); 410 resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule())); 411 return; 412 } 413 } 414 } 415 419 } else { 420 QTypeUnion qtypeunion = (QTypeUnion) qtype; 421 boolean add1 = true; 422 boolean add2 = true; 423 for (int i = 0; i < qtypeunion.getList().size() && (add1 || add2); i++) { 424 QTypeElement qtypeelt = (QTypeElement) qtypeunion.getList().get(i); 425 XQueryExpression name = qtypeelt.getName(); 426 if (!(name instanceof QName)) { 427 add1 = false; 428 add2 = false; 429 break; 430 } 431 QName qname = (QName) name; 432 if (add1 && depth == 0) { 433 if ( 434 subExpression instanceof QName) { 435 String nameSpace = ((QName) qtypeelt.getName()).getNameSpace(); 436 String localName = ((QName) qtypeelt.getName()).getLocalName(); 437 if (!compareQNames(nameSpace, localName, subNameSpace, subLocalName)) 438 add1 = false; 439 } 440 } 441 if (!add1 && add2 && depth != -1) { 444 if (subExpression instanceof QName) { 445 Object obj = Utils.getSubType(QTypeDocument.ANY_ORIGIN, qtypeelt, new Step(false, axis, subExpression, null, null), this.typeVisitor.getSchemaManager(), false); 446 if (obj == null) 447 add2 = false; 448 } 449 } 450 454 } 455 if (add1 || add2) { 456 if (add1 && depth == 0) { 457 resultExprs.add(arg); 458 return; 459 } 460 if (add2 && depth != -1) { 463 ArrayList steps = new ArrayList (); 464 steps.add(new Step(false, Axis.NONE, arg, null, arg.getParentModule())); 465 steps.add(new Step(true, axis, subExpression, null, arg.getParentModule())); 466 resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule())); 467 return; 468 } 469 } 470 } 471 } 472 473 476 public void visit(XQueryExpression arg) throws XQueryException { 477 resultExprs.clear(); 478 } 479 480 public void visit(XQueryExpressionSequence arg) throws XQueryException { 481 resultExprs.clear(); 482 if (subExpression == null) { 484 return; 485 } 486 ArrayList subExpressions = arg.getSubExpressions(); 487 for (int i = 0; i < subExpressions.size(); i++) { 488 resultExprs.clear(); 489 XQueryExpression subExpr = (XQueryExpression) subExpressions.get(i); 490 subExpr.accept(this); 491 } 493 } 494 502 541 } 542 | Popular Tags |