1 24 25 package org.xquark.xquery.normalize; 26 27 import java.util.ArrayList ; 28 29 import org.xquark.xpath.Axis; 30 import org.xquark.xpath.NodeKind; 31 import org.xquark.xquery.parser.*; 32 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOLLECTION; 33 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionDATA; 34 import org.xquark.xquery.typing.*; 35 36 41 public class LocatedExpressionReducer { 42 43 44 45 46 47 48 private static final String RCSRevision = "$Revision: 1.3 $"; 49 50 51 private static final String RCSName = "$Name: $"; 52 53 54 55 56 57 58 protected LocatedExpression rootExpr; 59 60 61 protected ArrayList expressions; 62 63 64 protected ArrayList steps; 65 66 67 protected int currentPos; 68 69 70 protected boolean reduced; 71 72 73 75 76 private boolean isUnapplicable; 77 78 79 protected ArrayList reducedExpressions; 80 81 82 protected TypeVisitor typeVisitor; 83 84 85 86 87 88 95 protected LocatedExpressionReducer() { 97 } 98 99 protected void reset(LocatedExpression locExpr, TypeVisitor typeVisitor) throws NormalizeException { 101 rootExpr = locExpr; 102 XQueryExpression expr = rootExpr.getExpression(); 103 this.expressions = new ArrayList (); 104 if (expr instanceof XQueryExpressionSequence) { 105 this.expressions = ((XQueryExpressionSequence) expr).getSubExpressions(); 106 } else { 107 this.expressions.add(expr); 108 } 109 this.steps = locExpr.getSteps(); 110 this.currentPos = 1; 111 this.reduced = false; 112 this.isUnapplicable = false; 114 if (this.reducedExpressions == null) 115 this.reducedExpressions = new ArrayList (); 116 else 117 this.reducedExpressions.clear(); 118 this.typeVisitor = typeVisitor; 119 } 120 121 122 123 124 125 126 127 128 129 133 protected ArrayList getReducedExpressions() { 134 return this.reducedExpressions; 135 } 136 137 142 protected XQueryExpression buildExpression(ArrayList expressions) throws NormalizeException { 143 try { 144 if ((expressions == null) || (expressions.size() == 0)) { 145 return null; 146 } else if (expressions.size() == 1) { 147 return (XQueryExpression) expressions.get(0); 148 } else { 149 return new XQueryExpressionSequence(expressions, ((XQueryExpression) expressions.get(0)).getParentModule()); 150 } 151 } catch (XQueryException e) { 152 throw new NormalizeException(0, "Unexpected exception in LocatedExpressionReducer.buildExpression(...) :\n==>" + e.toString() + "<==\n", e); 153 } 154 } 155 156 160 protected XQueryExpression getReducedExpression() throws NormalizeException { 161 return this.buildExpression(this.reducedExpressions); 162 } 163 164 168 protected ArrayList getReducedSteps() { 169 return this.steps; 170 } 171 172 177 protected boolean isStepSelfNode(Step step) throws NormalizeException { 178 if ((step != null) && (step.getPredicates() == null) && (step.getAxis() == Axis.SELF)) { 179 XQueryExpression test = step.getExpression(); 180 if ((test != null) && (test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.NODE)) { 181 return true; 182 } 183 } 184 return false; 185 } 186 187 192 protected boolean isStepNode(Step step) throws NormalizeException { 193 if ((step != null) && (step.getPredicates() == null)) { 194 XQueryExpression test = step.getExpression(); 195 if ((test != null) && (test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.NODE)) { 196 return true; 197 } 198 } 199 return false; 200 } 201 202 207 protected boolean isSlashSlash(Step step) throws NormalizeException { 208 if ((step != null) && (step.getAxis() == Axis.DESCENDANT_OR_SELF) && (step.getPredicates() == null)) { 209 XQueryExpression test = step.getExpression(); 210 if ((test != null) && (test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.NODE)) { 211 return true; 212 } 213 } 214 return false; 215 } 216 217 235 protected Step[] getNextSteps() throws NormalizeException { 236 Step[] next = new Step[2]; 237 if (this.currentPos >= this.steps.size()) { 238 return null; 239 } else if (this.steps.size() - this.currentPos == 1) { 240 this.currentPos++; 241 next[0] = (Step) this.steps.get(this.currentPos - 1); 242 } else { 243 Step currentStep = (Step) this.steps.get(this.currentPos); 244 if (this.isSlashSlash(currentStep)) { 245 next[0] = currentStep; 246 this.currentPos++; 247 Step secondStep = (Step) this.steps.get(this.currentPos); 248 if ((this.steps.size() - this.currentPos >= 1) && ((this.isStepSelfNode(secondStep)) || (!(this.isStepNode(secondStep))))) { 249 next[1] = secondStep; 251 this.currentPos++; 252 } 253 } else { 254 next[0] = currentStep; 255 this.currentPos++; 256 } 257 } 258 return next; 259 } 260 261 316 static protected boolean matchStepExpr(XQueryExpression expr1, XQueryExpression expr2) throws NormalizeException { 351 boolean resp = false; String namespace1 = null; 354 String localname1 = null; 355 String namespace2 = null; 356 String localname2 = null; 357 if (expr1 instanceof QName) { 358 namespace1 = ((QName)expr1).getNameSpace(); 359 localname1 = ((QName)expr1).getLocalName(); 360 } else if (expr1 instanceof ValueString) { 361 localname1 = ((ValueString)expr1).getValue(); 362 } 363 if (expr2 instanceof QName) { 364 namespace2 = ((QName)expr2).getNameSpace(); 365 localname2 = ((QName)expr2).getLocalName(); 366 } else if (expr2 instanceof ValueString) { 367 localname2 = ((ValueString)expr2).getValue(); 368 } 369 370 if (localname1 == null || localname2 == null) { 372 throw new NormalizeException(0, "Error in normalization, case not yet handled! (matching : " + expr1 + " with " + expr2 + ")"); 374 } 375 if (((namespace1 == null) && (localname1.equals("*"))) || ((namespace2 == null) && (localname2.equals("*")))) { 377 throw new NormalizeException(0, "Impossible case : namespace is null and localname is '*' !"); 378 } 379 if (namespace1 == null) { if (namespace2 == null) { resp = localname1.equals(localname2); 383 } else if (namespace2.equals("*")) { resp = (localname2.equals("*")) || (localname1.equals(localname2)); 385 } } else if (namespace1.equals("*")) { if (namespace2 == null) { resp = (localname1.equals("*")) || (localname1.equals(localname2)); 389 } else if (namespace2.equals("*")) { resp = (localname1.equals("*")) || (localname2.equals("*")) || (localname1.equals(localname2)); 391 } else { resp = (localname1.equals("*")) || (localname2.equals("*")) || (localname1.equals(localname2)); 393 } 394 } else { if (namespace2 == null) { } else if (namespace2.equals("*")) { resp = (localname1.equals("*")) || (localname2.equals("*")) || (localname1.equals(localname2)); 399 } else if (namespace1.equals(namespace2)) { resp = (localname1.equals("*")) || (localname2.equals("*")) || (localname1.equals(localname2)); 401 } } 403 return resp; 405 } 406 407 411 412 static protected Step getDOSNodeStep() throws NormalizeException { 414 try { 415 return new Step(true, Axis.DESCENDANT_OR_SELF, new NodeTest(NodeKind.NODE, null, null), null, null); 422 } catch (XQueryException e) { 423 throw new NormalizeException(0, "Unexpected exception in LocatedExpressionReducer.getDOSNode() :\n==>" + e.toString() + "<==\n", e); 424 } 425 } 426 427 431 432 static protected Step getParentNodeStep() throws NormalizeException { 434 try { 435 return new Step(true, Axis.PARENT, new NodeTest(NodeKind.NODE, null, null), null, null); 442 } catch (XQueryException e) { 443 throw new NormalizeException(0, "Unexpected exception in LocatedExpressionReducer.getParentNode() :\n==>" + e.toString() + "<==\n", e); 444 } 445 } 446 447 451 static protected Step modifyStep(boolean separator, byte axis, Step aStep) throws NormalizeException { 452 aStep.setHasSeparator(separator); 453 aStep.setAxis(axis); 454 return aStep; 455 } 456 457 static protected void splitStep(ArrayList stepList, int firstStepPosition, int caseFirstStep) throws NormalizeException { 460 Step firstStep = (Step) stepList.get(firstStepPosition); 461 if (caseFirstStep == 14) 462 stepList.set(firstStepPosition, getParentNodeStep()); 463 else 464 stepList.set(firstStepPosition, getDOSNodeStep()); 465 if ((caseFirstStep == 10) || (caseFirstStep == 11) || (caseFirstStep == 14)) { 466 stepList.add(firstStepPosition + 1, modifyStep(true, Axis.SELF, firstStep)); 467 } else { stepList.add(firstStepPosition + 1, modifyStep(true, Axis.CHILD, firstStep)); 469 } 470 } 471 472 static protected Step createUnionStep(ArrayList list1, ArrayList list2) throws NormalizeException { 473 try { 474 ((Step) list1.get(0)).setHasSeparator(false); 475 ((Step) list2.get(0)).setHasSeparator(false); 476 LocatedExpression tmpLoc1 = new LocatedExpression(list1, false, null, false); 478 LocatedExpression tmpLoc2 = new LocatedExpression(list2, false, null, false); 479 ListOpUNIONExpression tmpListUnion = new ListOpUNIONExpression(tmpLoc1, tmpLoc2, null); 481 tmpListUnion.setParenthesis(true); 482 return new Step(true, Axis.NONE, tmpListUnion, null, null); 484 } catch (XQueryException e) { 485 throw new NormalizeException("Could not construct Union Step " + e.getMessage()); 486 } 487 } 488 512 static final int SELF_NT = 1; 513 static final int SELF_TEXT = 2; 514 static final int SELF_NODE = 3; 515 static final int CHILD_NT = 4; 516 static final int CHILD_TEXT = 5; 517 static final int CHILD_NODE = 6; 518 static final int DESC_NT = 7; 519 static final int DESC_TEXT = 8; 520 static final int DESC_NODE = 9; 521 static final int DOS_NT = 10; 522 static final int DOS_TEXT = 11; 523 static final int DOS_NODE = 12; 524 static final int ATT_NT = 13; 525 static final int PARENT_NT = 14; 526 static final int PARENT_TEXT = 15; 527 static final int PARENT_NODE = 16; 528 static final int LOCATED = 20; 530 static final int UNION = 21; 531 532 static protected int getType(Step oneStep, boolean hasRootStep) throws NormalizeException { 533 if (hasRootStep && !oneStep.hasSeparator()) { 534 throw new NormalizeException(0, "Separator type " + oneStep.hasSeparator() + " of step " + oneStep + " not supported !"); 535 } 536 int innerExpr = -1; XQueryExpression subExpr = oneStep.getExpression(); 539 if (subExpr instanceof QName) { 540 innerExpr = 1; 541 } else if (subExpr instanceof NodeTest) { 542 NodeTest thisNode = (NodeTest) subExpr; 543 if (thisNode.getKind() == NodeKind.TEXT) { 544 innerExpr = 2; 545 } else if (thisNode.getKind() == NodeKind.NODE) { 546 innerExpr = 3; 547 } else { 548 throw new NormalizeException(0, "Node type " + NodeKind.NODEKINDSTRINGS[thisNode.getKind()] + " of step " + oneStep + " not supported !"); 549 } 550 } else if (subExpr instanceof LocatedExpression) { 551 return LOCATED; 552 } else if (subExpr instanceof ListOpUNIONExpression) { 553 return UNION; 554 } else { 555 throw new NormalizeException(0, "Expression " + subExpr + " of step " + oneStep + " not supported !"); 556 } 557 int axis = oneStep.getAxis(); 559 switch (axis) { 560 case Axis.SELF : 561 return innerExpr; 562 case Axis.CHILD : 563 return innerExpr + 3; 564 case Axis.DESCENDANT : 565 return innerExpr + 6; 566 case Axis.DESCENDANT_OR_SELF : 567 return innerExpr + 9; 568 case Axis.ATTRIBUTE : 569 if (innerExpr == 1) 570 return innerExpr + 12; 571 throw new NormalizeException(0, "Node test or text test on attribute of step " + oneStep + " not supported !"); 572 case Axis.PARENT : 573 if (innerExpr == 1 || innerExpr == 3) 574 return innerExpr + 13; 575 throw new NormalizeException(0, "Text test on parent axis of step " + oneStep + " not supported !"); 576 default : 577 if (axis < 0) { 578 throw new NormalizeException(0, "Axis (value = " + axis + ") of step " + oneStep + " not supported !"); 579 } else { 580 throw new NormalizeException(0, "Axis " + Axis.AXISSTRINGS[axis] + " of step " + oneStep + " not supported !"); 581 } 582 } 583 } 584 585 593 static public boolean eliminateSteps(ArrayList stepList, boolean hasRootStep) throws NormalizeException { 594 if (stepList == null) 596 return true; 597 Step firstStep = null; 598 Step secondStep = null; 599 int stepsReduced = 0; 600 if (stepList.size() <= (hasRootStep ? 1 : 0)) { 604 } else if (stepList.size() >= (hasRootStep ? 2 : 1)) { 606 int caseFirstStep; 609 int caseSecondStep; 610 int firstStepPosition = (hasRootStep ? 1 : 0); 611 boolean hasPredicate = false; 612 do { 615 firstStep = (Step) stepList.get(firstStepPosition); 616 caseFirstStep = getType(firstStep, hasRootStep); hasPredicate = firstStep.hasPredicates(); 618 caseSecondStep = -1; 619 if (stepList.size() > firstStepPosition + 1) { 620 secondStep = (Step) stepList.get(firstStepPosition + 1); 621 caseSecondStep = getType(secondStep, true); } 624 firstStepPosition++; 625 if (caseFirstStep == 0 || caseSecondStep == 0) 626 continue; 627 if (caseSecondStep == -1) { 629 } else { 630 switch (caseFirstStep) { 632 case SELF_NT : 633 switch (caseSecondStep) { 634 case SELF_NT : 635 if (!matchStepExpr(firstStep.getExpression(),secondStep.getExpression())) { 636 return true; 638 } 639 break; 640 case SELF_TEXT : 641 return true; 643 } 644 break; 645 case SELF_TEXT : 646 switch (caseSecondStep) { 647 case PARENT_NT : 648 case PARENT_NODE : 649 case SELF_TEXT : 650 case SELF_NODE : 651 case DOS_NODE : 652 break; 653 default : 654 return true; 656 } 657 break; 658 case SELF_NODE : 659 break; 660 case CHILD_NT : 661 switch (caseSecondStep) { 662 case SELF_NT : 663 if (!matchStepExpr(firstStep.getExpression(),secondStep.getExpression())) { 664 return true; 666 } 667 break; 668 case SELF_TEXT : 669 return true; 671 } 672 break; 673 case CHILD_TEXT : 674 switch (caseSecondStep) { 675 case PARENT_NT : 676 case PARENT_NODE : 677 case SELF_TEXT : 678 case SELF_NODE : 679 case DOS_NODE : 680 break; 681 default : 682 return true; 684 } 685 break; 686 case CHILD_NODE : 687 break; 688 case DESC_NT : 689 switch (caseSecondStep) { 690 case SELF_TEXT : 691 return true; 693 } 694 break; 695 case DESC_TEXT : 696 switch (caseSecondStep) { 697 case SELF_TEXT : 698 case SELF_NODE : 699 case PARENT_NT : 700 case PARENT_NODE : 701 case DOS_NODE : 702 break; 703 default : 704 return true; 706 } 707 break; 708 case DESC_NODE : 709 break; 710 case DOS_NT : 711 switch (caseSecondStep) { 712 case SELF_TEXT : 713 return true; 715 } 716 break; 717 case DOS_TEXT : 718 switch (caseSecondStep) { 719 case SELF_TEXT : 720 case SELF_NODE : 721 case PARENT_NT : 722 case PARENT_NODE : 723 break; 724 default : 725 return true; 727 } 728 break; 729 case DOS_NODE : 730 break; 731 case ATT_NT : 732 switch (caseSecondStep) { 733 case SELF_NODE : 734 case PARENT_NT : 735 case PARENT_NODE : 736 case DOS_NODE : 737 break; 738 default : 739 return true; 741 } 742 break; 743 case PARENT_NT : 744 switch (caseSecondStep) { 745 case SELF_TEXT : 746 return true; 748 } 749 break; 750 case PARENT_NODE : 751 switch (caseSecondStep) { 752 case SELF_TEXT : 753 return true; 755 } 756 break; 757 } 758 } 759 } while ((stepList.size() >= firstStepPosition) && (firstStep != null) && (caseSecondStep != -1)); 760 } 762 return false; 765 } 766 767 static public int simplifySteps(ArrayList steps, boolean inPredicate, XQueryModule parentModule) throws NormalizeException { 768 769 boolean hasRootVar = false; 770 try { 771 Step step0 = (Step) steps.get(0); 772 if (step0.getExpression() instanceof Variable) 773 hasRootVar = true; 774 if (step0.getAxis() != -1 && step0.hasSeparator()) { 775 ArrayList argVect = new ArrayList (); 776 argVect.add(new ValueString("*:*", parentModule)); 777 FunctionCOLLECTION funcColExpr = new FunctionCOLLECTION(argVect, parentModule); 778 Step step = new Step(false, Axis.NONE, funcColExpr, null, parentModule); 779 step.setParentModule(parentModule); 780 steps.add(0, step); 781 } else if (inPredicate && !step0.hasSeparator()) { 782 if (step0.getExpression() instanceof QName || step0.getExpression() instanceof NodeTest) { 783 hasRootVar = true; 784 if (step0.getAxis() == -1 && step0.getExpression() instanceof QName) 785 step0.setAxis(Axis.CHILD); 786 } 787 } 788 } catch (XQueryException xqe) { 789 throw new NormalizeException(xqe.getMessage(), xqe); 790 } 791 return simplifySteps(steps, ((Step) steps.get(0)).getAxis() == Axis.NONE, hasRootVar, parentModule); 792 } 793 794 static private int simplifySteps(ArrayList stepList, boolean hasRootStep, boolean hasRootVar, XQueryModule parentModule) throws NormalizeException { 795 if (stepList == null || eliminateSteps(stepList, hasRootStep)) 797 return 0; 798 Step firstStep = null; 799 Step secondStep = null; 800 int stepsReduced = 0; 801 boolean hasSeparator = ((Step) stepList.get(0)).hasSeparator(); 802 if (stepList.size() <= (hasRootStep ? 1 : 0)) { 806 } else if (stepList.size() >= (hasRootStep ? 2 : 1)) { 808 int caseFirstStep; 811 int caseSecondStep; 812 int firstStepPosition = (hasRootStep ? 1 : 0); 813 int minStepPosition = firstStepPosition; 814 boolean hasPredicate = false; 815 do { 818 firstStep = (Step) stepList.get(firstStepPosition); 819 caseFirstStep = getType(firstStep, hasRootStep); hasPredicate = firstStep.hasPredicates(); 821 caseSecondStep = -1; 822 if (stepList.size() > firstStepPosition + 1) { 823 secondStep = (Step) stepList.get(firstStepPosition + 1); 824 caseSecondStep = getType(secondStep, true); } 827 if (caseSecondStep == -1) { 829 switch (caseFirstStep) { 830 case SELF_NT : 831 case SELF_TEXT : 832 case CHILD_NT : 833 case CHILD_TEXT : 834 case CHILD_NODE : 835 case DOS_NODE : 836 case ATT_NT : 837 case PARENT_NODE : 838 ((Step) stepList.get(0)).setHasSeparator(hasSeparator); 839 return (stepsReduced != 0) ? 1 : 2; 840 case SELF_NODE : 841 if (firstStepPosition != minStepPosition) { 845 stepList.remove(firstStepPosition); 846 ((Step) stepList.get(firstStepPosition - 1)).addPredicates(firstStep.getPredicates()); 847 stepsReduced++; 848 } else if (!firstStep.hasPredicates() && hasRootStep == true) { 849 stepList.remove(firstStepPosition); 850 stepsReduced++; 851 } 852 ((Step) stepList.get(0)).setHasSeparator(hasSeparator); 853 return (stepsReduced != 0) ? 1 : 2; 854 case DESC_NT : 855 case DESC_TEXT : 856 case DESC_NODE : 857 case DOS_NT : 858 case DOS_TEXT : 859 case PARENT_NT : 860 splitStep(stepList, firstStepPosition, caseFirstStep); 867 if (firstStepPosition > minStepPosition) 868 firstStepPosition--; 869 caseSecondStep = 0; 871 break; 872 } 873 continue; 874 } 875 if (caseSecondStep == LOCATED) { 876 if ((caseFirstStep == CHILD_NT || caseFirstStep == CHILD_NODE) && !firstStep.hasPredicates()) { 877 880 LocatedExpression tmpLoc = (LocatedExpression) secondStep.getExpression(); 881 ArrayList tmpSteps = tmpLoc.getSteps(); 882 ArrayList predicates = new ArrayList (2); 884 try { 886 ArrayList tmpList = new ArrayList (1); 887 tmpList.add(modifyStep(false, Axis.CHILD, (Step) tmpSteps.get(0))); 888 ((Step) tmpList.get(0)).setHasSeparator(false); 889 LocatedExpression pred1 = new LocatedExpression(tmpList, true, null); 891 predicates.add(pred1); 893 } catch (XQueryException e) { 894 throw new NormalizeException("Could not construct predicate " + e.getMessage()); 895 } 896 secondStep = (Step) tmpSteps.get(1); 898 if (secondStep.getPredicates() != null) 899 predicates.addAll(secondStep.getPredicates()); 900 try { 902 ArrayList tmpList = new ArrayList (1); 903 firstStep.setHasSeparator(false); 904 tmpList.add(firstStep); 905 LocatedExpression pred1 = new LocatedExpression(tmpList, true, null); 907 ArrayList tmpPreds = new ArrayList (1); 909 tmpPreds.add(pred1); 910 stepList.set(firstStepPosition, new Step(true, Axis.SELF, new NodeTest(NodeKind.NODE, null, null), tmpPreds, null)); 911 } catch (XQueryException e) { 912 throw new NormalizeException("Could not construct step " + e.getMessage()); 913 } 914 ArrayList tmpSteps1 = new ArrayList (1); 917 try { 918 tmpSteps1.add(new Step(false, Axis.SELF, new NodeTest(NodeKind.NODE, null, null), null, null)); 919 } catch (XQueryException e) { 920 throw new NormalizeException("Could not construct step " + e.getMessage()); 921 } 922 ArrayList tmpSteps2 = new ArrayList (2); 924 try { 925 firstStep = (Step) firstStep.clone(); 926 } catch (CloneNotSupportedException e) { 927 throw new NormalizeException("Could not clone step " + e.getMessage()); 928 } 929 firstStep.setPredicates(null); 930 tmpSteps2.add(firstStep); 931 tmpSteps2.add(getDOSNodeStep()); 932 933 firstStep = createUnionStep(tmpSteps1, tmpSteps2); 934 firstStep.setPredicates(predicates); 935 stepList.set(firstStepPosition + 1, firstStep); 936 937 if (firstStepPosition > minStepPosition) 938 firstStepPosition--; 939 940 } else { 941 throw new NormalizeException("Could not handle " + stepList); 942 } 943 continue; 944 } 945 if (caseSecondStep == UNION) { 946 if (firstStep.hasPredicates()) { 947 firstStepPosition++; 948 continue; 949 } 950 ArrayList tmpSteps1 = ((LocatedExpression) ((ListOpUNIONExpression) secondStep.getExpression()).getExpression1()).getSteps(); 953 ((Step) tmpSteps1.get(0)).setHasSeparator(true); 954 try { 955 tmpSteps1.add(0, firstStep.clone()); 956 } catch (CloneNotSupportedException e) { 957 throw new NormalizeException("Could not clone " + firstStep); 958 } 959 int retVal1 = simplifySteps(tmpSteps1, false, false, parentModule); 960 ArrayList tmpSteps2 = ((LocatedExpression) ((ListOpUNIONExpression) secondStep.getExpression()).getExpression2()).getSteps(); 962 ((Step) tmpSteps2.get(0)).setHasSeparator(true); 963 try { 964 tmpSteps2.add(0, firstStep.clone()); 965 } catch (CloneNotSupportedException e) { 966 throw new NormalizeException("Could not clone " + firstStep); 967 } 968 int retVal2 = simplifySteps(tmpSteps2, false, false, parentModule); 969 if (retVal1 == 0 && retVal2 == 0) 970 return 0; 971 if (retVal1 == 0) { 972 stepList.remove(firstStepPosition); 973 stepList.addAll(firstStepPosition, tmpSteps2); 974 if (firstStepPosition > minStepPosition) 975 firstStepPosition--; 976 } else if (retVal2 == 0) { 977 stepList.remove(firstStepPosition); 978 stepList.addAll(firstStepPosition, tmpSteps1); 979 if (firstStepPosition > minStepPosition) 980 firstStepPosition--; 981 } else if (retVal1 == 2 && retVal2 == 2) { 982 tmpSteps1.remove(0); 983 tmpSteps2.remove(0); 984 ((Step) tmpSteps1.get(0)).setHasSeparator(false); 985 ((Step) tmpSteps2.get(0)).setHasSeparator(false); 986 firstStepPosition++; 987 } else { 988 stepList.remove(firstStepPosition); 989 ((Step) tmpSteps1.get(0)).setHasSeparator(false); 990 ((Step) tmpSteps2.get(0)).setHasSeparator(false); 991 if (firstStepPosition > minStepPosition) 992 firstStepPosition--; 993 } 994 continue; 995 } 996 switch (caseFirstStep) { 997 case UNION : 998 firstStepPosition++; 1000 break; 1001 case SELF_NT : 1002 switch (caseSecondStep) { 1003 case SELF_NT : 1004 if (matchStepExpr(firstStep.getExpression(), secondStep.getExpression())) { 1006 if (firstStep.getExpression() instanceof QName && ((QName) firstStep.getExpression()).getLocalName().equals("*")) { 1007 stepList.remove(firstStepPosition); 1009 secondStep.addPredicates(firstStep.getPredicates()); 1010 stepsReduced++; 1011 } else if (secondStep.getExpression() instanceof QName && ((QName) secondStep.getExpression()).getLocalName().equals("*")) { 1012 stepList.remove(firstStepPosition + 1); 1014 firstStep.addPredicates(secondStep.getPredicates()); 1015 stepsReduced++; 1016 } else { 1017 stepList.remove(firstStepPosition); 1020 secondStep.addPredicates(firstStep.getPredicates()); 1021 stepsReduced++; 1022 } 1023 } else { 1024 return 0; 1026 } 1027 break; 1028 case SELF_TEXT : 1029 return 0; 1032 case SELF_NODE : 1033 stepList.remove(firstStepPosition + 1); 1036 firstStep.addPredicates(secondStep.getPredicates()); 1037 stepsReduced++; 1038 break; 1039 case PARENT_NT : 1040 case PARENT_NODE : 1041 stepsReduced++; 1044 stepList.remove(firstStepPosition); 1046 ArrayList tmpSteps = new ArrayList (1); 1048 tmpSteps.add(modifyStep(false, Axis.CHILD, firstStep)); 1049 try { 1050 LocatedExpression tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1052 secondStep.addPredicate(0, tmpLoc); 1054 } catch (XQueryException e) { 1055 throw new NormalizeException("Could not add predicate " + e.getMessage()); 1056 } 1057 if (firstStepPosition > minStepPosition) 1058 firstStepPosition--; 1059 break; 1060 default : 1069 firstStepPosition++; 1071 break; 1072 } 1073 break; 1074 case SELF_TEXT : 1075 switch (caseSecondStep) { 1076 case PARENT_NT : 1077 case PARENT_NODE : 1078 stepsReduced++; 1081 stepList.remove(firstStepPosition); 1083 ArrayList tmpSteps = new ArrayList (1); 1085 tmpSteps.add(modifyStep(false, Axis.CHILD, firstStep)); 1086 try { 1087 LocatedExpression tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1089 secondStep.addPredicate(0, tmpLoc); 1091 } catch (XQueryException e) { 1092 throw new NormalizeException("Could not add predicate " + e.getMessage()); 1093 } 1094 if (firstStepPosition > minStepPosition) 1095 firstStepPosition--; 1096 break; 1097 case SELF_TEXT : 1098 case SELF_NODE : 1099 case DOS_NODE : 1100 stepList.remove(firstStepPosition + 1); 1104 firstStep.addPredicates(secondStep.getPredicates()); 1105 stepsReduced++; 1106 break; 1107 default : 1108 return 0; 1110 } 1111 break; 1112 case SELF_NODE : 1113 if (firstStep.hasPredicates()) { 1114 if (firstStepPosition != minStepPosition) { 1115 stepList.remove(firstStepPosition); 1116 ((Step) stepList.get(firstStepPosition - 1)).addPredicates(firstStep.getPredicates()); 1117 stepsReduced++; 1118 firstStepPosition--; 1119 } else { 1120 firstStepPosition++; 1122 } 1123 } else { 1124 stepList.remove(firstStepPosition); 1125 stepsReduced++; 1126 if (firstStepPosition > minStepPosition) 1127 firstStepPosition--; 1128 } 1129 break; 1130 case CHILD_NT : 1131 switch (caseSecondStep) { 1132 case SELF_NT : 1133 if (matchStepExpr(firstStep.getExpression(), secondStep.getExpression())) { 1134 if (firstStep.getExpression() instanceof QName && ((QName) firstStep.getExpression()).getLocalName().equals("*")) { 1135 stepList.remove(firstStepPosition); 1137 secondStep.addPredicates(firstStep.getPredicates()); 1138 stepList.set(firstStepPosition, modifyStep(true, Axis.CHILD, secondStep)); 1140 stepsReduced++; 1141 if (firstStepPosition > minStepPosition) 1143 firstStepPosition--; 1144 } else if (secondStep.getExpression() instanceof QName && ((QName) secondStep.getExpression()).getLocalName().equals("*")) { 1145 stepList.remove(firstStepPosition + 1); 1147 firstStep.addPredicates(secondStep.getPredicates()); 1148 stepsReduced++; 1149 } else { 1150 stepList.remove(firstStepPosition + 1); 1153 firstStep.addPredicates(secondStep.getPredicates()); 1154 stepsReduced++; 1155 } 1156 break; 1157 } else { 1158 return 0; 1160 } 1161 case SELF_TEXT : 1162 return 0; 1164 case SELF_NODE : 1165 stepList.remove(firstStepPosition + 1); 1167 firstStep.addPredicates(secondStep.getPredicates()); 1168 stepsReduced++; 1169 break; 1170 case PARENT_NT : 1171 case PARENT_NODE : 1172 stepsReduced++; 1173 stepList.remove(firstStepPosition); 1175 secondStep = modifyStep(true, Axis.SELF, secondStep); 1176 stepList.set(firstStepPosition, secondStep); 1177 ArrayList tmpSteps = new ArrayList (1); 1179 tmpSteps.add(modifyStep(false, Axis.CHILD, firstStep)); 1180 try { 1181 LocatedExpression tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1183 secondStep.addPredicate(0, tmpLoc); 1185 } catch (XQueryException e) { 1186 throw new NormalizeException("Could not add predicate " + e.getMessage()); 1187 } 1188 if (firstStepPosition > minStepPosition) 1189 firstStepPosition--; 1190 break; 1191 default : 1192 firstStepPosition++; 1194 break; 1195 } 1196 break; 1197 case CHILD_TEXT : 1198 switch (caseSecondStep) { 1199 case PARENT_NT : 1200 case PARENT_NODE : 1201 stepsReduced++; 1202 stepList.remove(firstStepPosition); 1204 secondStep = modifyStep(true, Axis.SELF, secondStep); 1205 stepList.set(firstStepPosition, secondStep); 1206 ArrayList tmpSteps = new ArrayList (1); 1208 firstStep.setHasSeparator(false); 1209 tmpSteps.add(firstStep); 1210 try { 1211 LocatedExpression tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1213 secondStep.addPredicate(0, tmpLoc); 1215 } catch (XQueryException e) { 1216 throw new NormalizeException("Could not add predicate " + e.getMessage()); 1217 } 1218 if (firstStepPosition > minStepPosition) 1219 firstStepPosition--; 1220 break; 1221 case SELF_TEXT : 1222 case SELF_NODE : 1223 case DOS_NODE : 1224 stepList.remove(firstStepPosition + 1); 1226 firstStep.addPredicates(secondStep.getPredicates()); 1227 stepsReduced++; 1228 break; 1229 default : 1230 return 0; 1232 } 1233 break; 1234 case CHILD_NODE : 1235 switch (caseSecondStep) { 1236 case SELF_NT : 1237 case SELF_TEXT : 1238 if (!firstStep.hasPredicates()) { 1239 stepsReduced++; 1240 stepList.set(firstStepPosition, modifyStep(true, Axis.CHILD, secondStep)); 1242 stepList.remove(firstStepPosition + 1); 1244 if (firstStepPosition > minStepPosition) 1246 firstStepPosition--; 1247 } else { 1248 firstStepPosition++; 1250 } 1251 break; 1252 case SELF_NODE : 1253 stepsReduced++; 1254 stepList.remove(firstStepPosition + 1); 1255 firstStep.addPredicates(secondStep.getPredicates()); 1256 break; 1257 case DOS_NODE : 1258 stepsReduced++; 1259 if (!firstStep.hasPredicates()) { 1261 stepList.set(firstStepPosition, secondStep); 1262 stepList.set(firstStepPosition + 1, firstStep); 1263 if (firstStepPosition > minStepPosition) 1264 firstStepPosition--; 1265 } else { 1266 firstStepPosition++; 1268 } 1269 break; 1270 case PARENT_NT : 1271 case PARENT_NODE : 1272 stepsReduced++; 1273 stepList.remove(firstStepPosition); 1275 secondStep = modifyStep(true, Axis.SELF, secondStep); 1276 stepList.set(firstStepPosition, secondStep); 1277 ArrayList tmpSteps = new ArrayList (1); 1279 firstStep.setHasSeparator(false); 1280 tmpSteps.add(firstStep); 1281 try { 1282 LocatedExpression tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1284 secondStep.addPredicate(0, tmpLoc); 1286 } catch (XQueryException e) { 1287 throw new NormalizeException("Could not add predicate " + e.getMessage()); 1288 } 1289 if (firstStepPosition > minStepPosition) 1290 firstStepPosition--; 1291 break; 1292 default : 1293 firstStepPosition++; 1295 break; 1296 } 1297 break; 1298 case DESC_NT : 1299 switch (caseSecondStep) { 1300 case SELF_TEXT : 1301 return 0; 1303 default : 1304 stepsReduced++; 1305 stepList.set(firstStepPosition, modifyStep(true, Axis.CHILD, firstStep)); 1306 stepList.add(firstStepPosition, getDOSNodeStep()); 1307 if (firstStepPosition > minStepPosition) 1308 firstStepPosition--; 1309 else 1310 firstStepPosition++; 1311 break; 1312 } 1313 break; 1314 case DESC_TEXT : 1315 switch (caseSecondStep) { 1316 case SELF_TEXT : 1317 case SELF_NODE : 1318 case DOS_NODE : 1319 case PARENT_NT : 1320 case PARENT_NODE : 1321 stepsReduced++; 1322 stepList.set(firstStepPosition, modifyStep(true, Axis.CHILD, firstStep)); 1323 stepList.add(firstStepPosition, getDOSNodeStep()); 1324 if (firstStepPosition > minStepPosition) 1325 firstStepPosition--; 1326 else 1327 firstStepPosition++; 1328 break; 1329 default : 1330 return 0; 1332 } 1333 break; 1334 case DESC_NODE : 1335 stepsReduced++; 1336 stepList.set(firstStepPosition, modifyStep(true, Axis.CHILD, firstStep)); 1337 stepList.add(firstStepPosition, getDOSNodeStep()); 1338 if (firstStepPosition > minStepPosition) 1339 firstStepPosition--; 1340 else 1341 firstStepPosition++; 1342 break; 1343 case DOS_NT : 1344 switch (caseSecondStep) { 1345 case SELF_TEXT : 1346 return 0; 1348 default : 1349 stepsReduced++; 1350 stepList.set(firstStepPosition, modifyStep(true, Axis.SELF, firstStep)); 1351 stepList.add(firstStepPosition, getDOSNodeStep()); 1352 if (firstStepPosition > minStepPosition) 1353 firstStepPosition--; 1354 else 1355 firstStepPosition++; 1356 break; 1357 } 1358 break; 1359 case DOS_TEXT : 1360 switch (caseSecondStep) { 1361 case SELF_TEXT : 1362 case SELF_NODE : 1363 case DOS_NODE : 1364 case PARENT_NT : 1365 case PARENT_NODE : 1366 stepsReduced++; 1367 stepList.set(firstStepPosition, modifyStep(true, Axis.SELF, firstStep)); 1368 stepList.add(firstStepPosition, getDOSNodeStep()); 1369 if (firstStepPosition > minStepPosition) 1370 firstStepPosition--; 1371 else 1372 firstStepPosition++; 1373 break; 1374 default : 1375 return 0; 1377 } 1378 break; 1379 case DOS_NODE : 1380 switch (caseSecondStep) { 1381 case SELF_NODE : 1382 stepList.remove(firstStepPosition + 1); 1384 firstStep.addPredicates(secondStep.getPredicates()); 1385 stepsReduced++; 1386 break; 1387 case DESC_NT : 1388 case DESC_TEXT : 1389 case DESC_NODE : 1390 case DOS_NT : 1391 case DOS_TEXT : 1392 case DOS_NODE : 1393 if (!firstStep.hasPredicates()) { 1394 stepsReduced++; 1395 stepList.remove(firstStepPosition); 1396 if (firstStepPosition > minStepPosition) 1397 firstStepPosition--; 1398 } else { 1399 firstStepPosition++; 1400 break; 1401 } 1402 break; 1403 case PARENT_NODE : 1405 1406 if (firstStepPosition == minStepPosition) { 1410 if (hasRootVar) { 1411 firstStepPosition++; 1413 } else { 1416 modifyStep(firstStep.hasSeparator(), Axis.DESCENDANT, firstStep); 1418 } 1419 break; 1420 } 1421 stepList.remove(firstStepPosition + 1); 1424 LocatedExpression tmpLoc = null; 1426 ArrayList tmpSteps = new ArrayList (3); 1427 firstStep.setHasSeparator(false); 1428 tmpSteps.add(firstStep); 1429 tmpSteps.add(secondStep); 1430 try { 1431 tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1432 firstStep = new Step(true, Axis.NONE, tmpLoc, null, null); 1433 } catch (XQueryException e) { 1434 throw new NormalizeException("Could not create located expression " + e.getMessage()); 1435 } 1436 stepList.set(firstStepPosition, firstStep); 1437 if (firstStepPosition > minStepPosition) 1438 firstStepPosition--; 1439 1440 1474 1476 break; 1477 1478 default : 1479 firstStepPosition++; 1481 break; 1482 } 1483 break; 1484 case ATT_NT : 1485 switch (caseSecondStep) { 1486 case SELF_NODE : 1487 case DOS_NODE : 1488 stepList.remove(firstStepPosition + 1); 1490 firstStep.addPredicates(secondStep.getPredicates()); 1491 stepsReduced++; 1492 break; 1493 case SELF_NT : 1494 case SELF_TEXT : 1495 case CHILD_NT : 1496 case CHILD_TEXT : 1497 case CHILD_NODE : 1498 case DESC_NT : 1499 case DESC_TEXT : 1500 case DESC_NODE : 1501 case DOS_NT : 1502 case DOS_TEXT : 1503 case ATT_NT : 1504 return 0; 1505 case PARENT_NODE : 1507 stepsReduced++; 1509 stepList.remove(firstStepPosition); 1511 secondStep = modifyStep(true, Axis.SELF, secondStep); 1512 stepList.set(firstStepPosition, secondStep); 1513 ArrayList tmpSteps = new ArrayList (1); 1515 firstStep.setHasSeparator(false); 1516 tmpSteps.add(firstStep); 1517 try { 1518 LocatedExpression tmpLoc = new LocatedExpression(tmpSteps, true, null, false); 1520 secondStep.addPredicate(0, tmpLoc); 1522 } catch (XQueryException e) { 1523 throw new NormalizeException("Could not add predicate " + e.getMessage()); 1524 } 1525 if (firstStepPosition > minStepPosition) 1526 firstStepPosition--; 1527 break; 1528 default : 1529 firstStepPosition++; 1531 break; 1532 } 1533 break; 1534 case PARENT_NT : 1535 switch (caseSecondStep) { 1536 case SELF_TEXT : 1537 return 0; 1539 default : 1540 stepsReduced++; 1541 stepList.set(firstStepPosition, modifyStep(true, Axis.SELF, firstStep)); 1542 stepList.add(firstStepPosition, getParentNodeStep()); 1543 if (firstStepPosition > minStepPosition) 1544 firstStepPosition--; 1545 else 1546 firstStepPosition++; 1547 break; 1548 } 1549 break; 1550 case PARENT_NODE : 1551 switch (caseSecondStep) { 1552 case SELF_TEXT : 1553 return 0; 1555 case SELF_NODE : 1556 stepList.remove(firstStepPosition + 1); 1558 firstStep.addPredicates(secondStep.getPredicates()); 1559 stepsReduced++; 1560 break; 1561 default : 1562 firstStepPosition++; 1564 break; 1565 } 1566 break; 1567 } 1568 } while ((stepList.size() >= firstStepPosition) && (firstStep != null) && (caseSecondStep != -1)); 1569 } 1571 ((Step) stepList.get(0)).setHasSeparator(hasSeparator); 1574 return (stepsReduced != 0) ? 1 : 2; 1575 } 1576 1577 1583 protected ArrayList recursiveReduction(Element element, Step oneFinalStep, boolean noAttributes) throws NormalizeException { 1584 try { 1586 ArrayList subExpr = new ArrayList (); 1587 ArrayList subExprs = element.getSubExpressions(); 1588 if (subExprs != null) { 1589 for (int i = 0; i < subExprs.size(); i++) { 1590 XQueryExpression subExpri = (XQueryExpression) subExprs.get(i); 1591 if (noAttributes && subExpri.getQType() != null && subExpri.getQType().getSubClass() == QType.ATTRIBUTE) 1592 continue; 1593 subExpr.addAll(this.applyFinalStep(subExpri, oneFinalStep, true, 0, element.getParentModule())); 1594 } 1595 } 1596 return subExpr; 1597 } catch (XQueryException e) { 1598 throw new NormalizeException(0, e.getMessage()); 1599 } 1600 } 1601 1602 1651 private ArrayList applyFinalStep(XQueryExpression expressionToNormalize, Step appliedStep, boolean slashSlash, int depth, XQueryModule parentModule) throws NormalizeException { 1652 try { 1655 ArrayList results = new ArrayList (); 1656 if (expressionToNormalize == null) { 1657 return results; 1658 } 1659 if (expressionToNormalize instanceof XQueryExpressionSequence) { 1661 ArrayList subExpr = ((XQueryExpressionSequence) expressionToNormalize).getSubExpressions(); 1662 for (int i = 0; i < subExpr.size(); i++) { 1663 results.addAll(this.applyFinalStep((XQueryExpression) subExpr.get(i), appliedStep, slashSlash, depth, parentModule)); 1664 } 1665 } else { 1666 if (appliedStep == null) { 1667 results.add(expressionToNormalize); 1668 } else if (appliedStep.getPredicates() != null) { 1669 this.reduced = false; 1671 } else { 1674 XQueryExpression test = null; 1676 if (expressionToNormalize instanceof ValueText) { 1677 test = appliedStep.getExpression(); 1681 if ((appliedStep.getAxis() == Axis.SELF) && (test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.TEXT)) { 1682 if (appliedStep.getPredicates() == null) { 1684 this.reduced = true; 1685 results.add((ValueText) expressionToNormalize); 1686 } 1687 } else if ((appliedStep.getAxis() == Axis.SELF) && (test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.NODE)) { 1688 if (appliedStep.getPredicates() == null) { 1690 this.reduced = true; 1691 results.add(expressionToNormalize); 1692 } 1693 } else { 1694 this.reduced = true; 1697 } 1698 } else if (expressionToNormalize instanceof Element) { 1699 Element elt = (Element) expressionToNormalize; 1703 test = appliedStep.getExpression(); 1705 if ((appliedStep.getAxis() == Axis.ATTRIBUTE) && (test instanceof QName)) { 1706 this.reduced = true; 1708 ArrayList tmplist = elt.getAttributes(); 1709 if (tmplist != null) { 1710 for (int i = 0; i < tmplist.size(); i++) { 1711 AttributeValuePair atti = (AttributeValuePair) tmplist.get(i); 1712 if (!atti.isXmlns() && matchStepExpr(atti.getAttributeName(), test)) { 1713 results.add(tmplist.get(i)); 1714 } 1715 } 1716 } 1717 tmplist = elt.getSubExpressions(); 1720 if (tmplist != null) { 1721 for (int i = 0; i < tmplist.size(); i++) { 1722 XQueryExpression expri = (XQueryExpression) tmplist.get(i); 1723 if (expri.getQType() != null && expri.getQType().getSubClass() == QType.ATTRIBUTE) { 1724 XQueryExpression expriname = ((QTypeAttribute) expri.getQType()).getName(); 1725 if (matchStepExpr( expriname, test)) { 1726 results.add(expri); 1727 } 1728 } 1729 } 1730 } 1731 if (slashSlash) { results.addAll(this.recursiveReduction(elt, appliedStep, false)); 1733 } 1734 } else if (appliedStep.getAxis() == Axis.SELF) { 1735 if (test instanceof QName) { 1737 this.reduced = true; 1739 XQueryExpression eltName = elt.getStartTag(); 1740 if (matchStepExpr(eltName, test)) { 1741 if (slashSlash) { results.add(elt.clone()); 1743 results.addAll(this.recursiveReduction(elt, appliedStep, true)); 1744 } else { 1745 results.add(elt); 1746 } 1747 } else { 1748 if (slashSlash) { results.addAll(this.recursiveReduction(elt, appliedStep, true)); 1750 } 1752 } 1753 } else if ((test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.NODE)) { 1754 this.reduced = true; 1756 if (slashSlash) { results.add(elt.clone()); 1758 results.addAll(this.recursiveReduction(elt, appliedStep, true)); 1759 } else { 1760 results.add(elt); 1761 } 1762 } else if ((test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.TEXT)) { 1763 this.reduced = true; 1767 if (slashSlash) { results.addAll(this.recursiveReduction(elt, appliedStep, true)); 1769 } 1770 } else { 1771 throw new NormalizeException("Unsupported step (" + appliedStep + ") in LocatedExpressionReducer !"); 1772 } 1773 } else if (appliedStep.getAxis() == Axis.CHILD) { 1774 ArrayList subNodes = elt.getSubExpressions(); 1776 if ((subNodes == null) || (subNodes.size() == 0)) { 1777 this.reduced = true; 1780 } else { 1781 if ((test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.TEXT)) { 1782 for (int i = 0; i < subNodes.size(); i++) { 1784 if (subNodes.get(i) instanceof ValueText) { 1785 this.reduced = true; 1786 results.add((ValueText) subNodes.get(i)); 1787 } else if (!(subNodes.get(i) instanceof Element)) { 1788 Step newStep = (Step) appliedStep.clone(); 1789 newStep.setAxis(Axis.SELF); 1790 results.addAll(this.applyFinalStep((XQueryExpression) subNodes.get(i), newStep, slashSlash, depth + 1, parentModule)); 1791 } else { 1792 this.reduced = true; 1794 if (slashSlash) { Step newStep = (Step) appliedStep.clone(); 1796 newStep.setAxis(Axis.SELF); 1797 results.addAll(this.recursiveReduction((Element) subNodes.get(i), newStep, true)); 1798 } 1799 } 1800 1801 } 1802 } else if ((test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.NODE)) { 1803 for (int i = 0; i < subNodes.size(); i++) { 1805 XQueryExpression subExpri = (XQueryExpression) subNodes.get(i); 1806 if (subExpri.getQType() != null && subExpri.getQType().getSubClass() == QType.ATTRIBUTE) 1807 continue; 1808 if ((subExpri instanceof Element) || (subExpri instanceof ValueText)) { 1809 this.reduced = true; 1810 results.add(subExpri); 1811 } else { 1812 Step newStep = (Step) appliedStep.clone(); 1813 newStep.setAxis(Axis.SELF); 1814 results.addAll(this.applyFinalStep(subExpri, newStep, slashSlash, depth + 1, parentModule)); 1815 } 1816 } 1817 } else if (test instanceof QName) { 1818 for (int i = 0; i < subNodes.size(); i++) { 1820 Step newStep = (Step) appliedStep.clone(); 1821 newStep.setAxis(Axis.SELF); 1822 results.addAll(this.applyFinalStep((XQueryExpression) subNodes.get(i), newStep, slashSlash, depth + 1, parentModule)); 1823 } 1824 } else { 1825 throw new NormalizeException("Unsupported step test (" + appliedStep + ") in LocatedExpressionReducer !"); 1826 } 1827 } 1828 } else { 1829 } 1832 } else if (expressionToNormalize instanceof FLWRExpression) { 1833 FLWRExpression flwr = (FLWRExpression) expressionToNormalize; 1838 XQueryExpression returnClause = flwr.getReturnClause(); 1839 ArrayList newReturnClause = this.applyFinalStep(returnClause, appliedStep, slashSlash, depth, parentModule); 1840 XQueryExpression newReturnExpression = this.buildExpression(newReturnClause); 1841 if (newReturnExpression != null) { 1842 flwr.setReturnClause(newReturnExpression); 1843 results.add(flwr); 1844 } 1845 } else if (expressionToNormalize instanceof LocatedExpression) { 1846 if (appliedStep.getAxis() == Axis.ATTRIBUTE && expressionToNormalize.getQType() != null && expressionToNormalize.getQType().getSubClass() == QType.ATTRIBUTE) { 1851 } else { 1853 LocatedExpression tmploc = (LocatedExpression) expressionToNormalize; 1854 if (slashSlash) { 1856 tmploc.addStep(getDOSNodeStep()); 1857 } 1858 tmploc.addStep(appliedStep); 1859 if (!this.isUnapplicable) { 1860 this.reduced = true; 1861 } 1862 if (simplifySteps(tmploc.getSteps(), true, (tmploc.getStepNum(0).getExpression() instanceof Variable), tmploc.getStepNum(0).getExpression().getParentModule()) != 0) { 1863 boolean correctType = true; 1864 try { 1865 if (this.typeVisitor != null) { 1866 expressionToNormalize.accept(this.typeVisitor); 1867 } 1868 } catch (TypeException te) { 1869 correctType = false; 1871 } 1872 if (correctType) 1873 results.add(expressionToNormalize); 1874 } 1875 } 1876 } else if (expressionToNormalize instanceof Variable) { 1877 this.reduced = true; 1882 if (this.isStepSelfNode(appliedStep)) { 1883 results.add(expressionToNormalize); 1884 } else { 1885 ArrayList newSteps = new ArrayList (); 1886 newSteps.add(new Step(false, Axis.NONE, expressionToNormalize, null, null)); 1887 if (slashSlash) { 1888 newSteps.add(getDOSNodeStep()); 1889 } 1890 newSteps.add(appliedStep); 1891 try { 1892 results.add(new LocatedExpression(newSteps, parentModule)); 1893 } catch (TypeException te) { 1894 } 1896 } 1897 } else if (expressionToNormalize instanceof ITEExpression) { 1898 ITEExpression ite = (ITEExpression) expressionToNormalize; 1903 XQueryExpression thenClause = ite.getThenExpression(); 1904 XQueryExpression elseClause = ite.getElseExpression(); 1905 ArrayList newThenClause = this.applyFinalStep(thenClause, appliedStep, slashSlash, depth, parentModule); 1906 ArrayList newElseClause = this.applyFinalStep(elseClause, appliedStep, slashSlash, depth, parentModule); 1907 ite.setThenExpression(this.buildExpression(newThenClause)); 1908 ite.setElseExpression(this.buildExpression(newElseClause)); 1909 results.add(ite); 1910 } else if (expressionToNormalize instanceof ListOpUNIONExpression) { 1911 ListOpUNIONExpression loue = (ListOpUNIONExpression) expressionToNormalize; 1916 XQueryExpression left = loue.getExpression1(); 1917 XQueryExpression rigth = loue.getExpression2(); 1918 ArrayList newLeft = this.applyFinalStep(left, appliedStep, slashSlash, depth, parentModule); 1919 ArrayList newRigth = this.applyFinalStep(rigth, appliedStep, slashSlash, depth, parentModule); 1920 loue.setExpression1(this.buildExpression(newLeft)); 1921 loue.setExpression2(this.buildExpression(newRigth)); 1922 results.add(loue); 1923 } else if (expressionToNormalize instanceof FunctionDATA) { 1924 test = appliedStep.getExpression(); 1925 if (appliedStep.getAxis() == Axis.SELF && test instanceof NodeTest && ((NodeTest) test).getKind() == NodeKind.TEXT) { 1926 results.add(expressionToNormalize); 1927 } else { 1928 } 1930 1937 } else if (expressionToNormalize instanceof Value) { 1938 this.reduced = true; 1941 } else if (expressionToNormalize instanceof AggregateFunctionCall) { 1942 test = appliedStep.getExpression(); 1943 if (appliedStep.getAxis() == Axis.SELF) { 1944 if ((test instanceof NodeTest) && (((NodeTest) test).getKind() == NodeKind.TEXT)) { 1945 results.add(expressionToNormalize); 1946 } 1947 } 1948 } else if ((expressionToNormalize instanceof PrimitiveFunctionCall) && !(expressionToNormalize instanceof InputFunctionCall)) { 1949 } else if (expressionToNormalize instanceof XQueryBooleanOperatorExpression) { 1950 } else if (expressionToNormalize instanceof AttributeValuePair) { 1951 } else if (expressionToNormalize instanceof UnOpMinusExpression) { 1952 } else if (expressionToNormalize instanceof ListOpArithExpression) { 1953 } else if (expressionToNormalize instanceof ListOpCompExpression) { 1954 this.reduced = true; 1957 } else { 1958 this.isUnapplicable = true; 1961 ArrayList newSteps = new ArrayList (); 1962 newSteps.add(new Step(false, Axis.NONE, expressionToNormalize, null, null)); 1963 if (slashSlash) { 1964 newSteps.add(getDOSNodeStep()); 1965 } 1966 newSteps.add(appliedStep); 1967 try { 1968 results.add(new LocatedExpression(newSteps, parentModule)); 1969 } catch (TypeException te) { 1970 } 1972 } 1973 } 1974 } 1975 return results; 1977 } catch (CloneNotSupportedException e) { 1978 throw new NormalizeException(0, e.getMessage(), e); 1979 } catch (XQueryException e) { 1980 throw new NormalizeException(0, e.getMessage()); 1981 } 1982 } 1983 1984 1988 1989 protected boolean reduceLocatedExpression(XQueryModule parentModule) throws NormalizeException { 1990 try { 1991 this.reduced = false; 1992 boolean stepsReduced = false; 1994 int simplified = simplifySteps(steps, true, (rootExpr.getExpression() instanceof Variable), parentModule); 1996 if (simplified == 0) { 1997 this.reduced = true; 1999 } else { 2000 stepsReduced = (simplified == 1); 2001 Step[] next = null; 2002 boolean firstTime = true; 2003 do { 2005 next = this.getNextSteps(); 2006 if (next != null) { 2008 boolean slashSlash = this.isSlashSlash(next[0]); 2010 boolean special = false; 2011 if (slashSlash) { 2012 if (next[1] != null) { 2013 next[0] = next[1]; 2014 } else { 2015 next[0] = new Step(true, Axis.SELF, new NodeTest(NodeKind.NODE, null, parentModule), null, parentModule); 2017 special = true; 2018 } 2019 next[1] = null; 2020 } 2021 if (firstTime && (this.expressions.size() == 1)) { 2023 if (this.expressions.get(0) instanceof Variable || this.expressions.get(0) instanceof InputFunctionCall) { 2027 if (steps.size() == 1 && ((Step) steps.get(0)).getAxis() == Axis.NONE) 2036 this.reducedExpressions.add(rootExpr.getExpression()); 2037 else 2038 this.reducedExpressions.add(rootExpr); 2039 return this.reduced || stepsReduced; 2040 } 2041 } 2042 firstTime = false; 2043 for (int i = 0; i < this.expressions.size(); i++) { 2044 ArrayList partialExprs = this.applyFinalStep((XQueryExpression) this.expressions.get(i), next[0], slashSlash, 0, parentModule); 2045 if (partialExprs != null && !partialExprs.isEmpty()) { 2046 XQueryExpression tmpExpr = (XQueryExpression) partialExprs.get(partialExprs.size() - 1); 2047 this.reducedExpressions.addAll(partialExprs); 2052 } 2053 } 2054 this.expressions.clear(); 2055 this.expressions.addAll(this.reducedExpressions); 2056 this.reducedExpressions.clear(); 2057 } else { 2058 this.reducedExpressions.addAll(this.expressions); 2059 } 2060 } while (next != null); 2061 } 2062 return this.reduced || stepsReduced; 2063 } catch (XQueryException e) { 2064 throw new NormalizeException(0, e.getMessage()); 2065 } 2066 } 2067 2068 2069 2070 2071 2072 2073 2074 2075} 2076 | Popular Tags |