1 22 23 package org.xquark.xquery.normalize; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.xquery.parser.*; 28 29 public class AddLoopInfoVisitor extends DefaultParserVisitor { 30 private static final String RCSRevision = "$Revision: 1.1 $"; 31 private static final String RCSName = "$Name: $"; 32 33 private ArrayList loopIDs = new ArrayList (1); 34 private String varliststring = null; 35 private int varlistsize = 0; 36 37 public AddLoopInfoVisitor(ArrayList varlist) { 38 if (varlist != null && !varlist.isEmpty()) { 39 this.varlistsize = varlist.size(); 40 StringBuffer buf = new StringBuffer (); 41 for (int i=0;i<varlist.size();i++) buf.append(((Variable)varlist.get(i)).toString()); 42 this.varliststring = buf.toString(); 43 } 44 } 45 46 private void addLoopIDs(ArrayList list) { 47 if (list == null) return; 48 if (list.isEmpty()) return; 49 for (int i=0;i<list.size();i++) { 50 Variable var = (Variable)list.get(i); 51 if (!(loopIDs.contains(var))) 52 loopIDs.add(var); 53 } 54 } 55 56 public void visit(AttributeValuePair arg) throws XQueryException { 58 XQueryExpression attValue = arg.getAttributeValue(); 59 attValue.accept(this); 60 } 61 67 public void visit(Element arg) throws XQueryException { 68 ArrayList prevLoopIDs = (ArrayList )loopIDs.clone(); 69 addLoopIDs(arg.getDependIDs()); 70 addLoopIDs(arg.getSkolemIDs()); 71 ArrayList attributes = arg.getAttributes(); 72 if (attributes != null) 73 for (int i=0;i<attributes.size();i++) 74 ((AttributeValuePair)attributes.get(i)).accept(this); 75 ArrayList subExpressions = arg.getSubExpressions(); 76 if (subExpressions != null) 77 for (int i=0;i<subExpressions.size();i++) 78 ((XQueryExpression)subExpressions.get(i)).accept(this); 79 loopIDs = prevLoopIDs; 80 } 81 82 public void visit(ITEExpression arg) throws XQueryException { 88 ArrayList prevLoopIDs = (ArrayList )loopIDs.clone(); 89 addLoopIDs(arg.getDependIDs()); 90 addLoopIDs(arg.getSkolemIDs()); 91 arg.getIfExpression().accept(this); 92 arg.getThenExpression().accept(this); 93 arg.getElseExpression().accept(this); 94 loopIDs = prevLoopIDs; 95 } 96 105 public void visit(LocatedExpression arg) throws XQueryException { 106 ArrayList prevLoopIDs = (ArrayList )loopIDs.clone(); 107 addLoopIDs(arg.getDependIDs()); 108 addLoopIDs(arg.getSkolemIDs()); 109 if (loopIDs.isEmpty())arg.setLoopIDs(varliststring); 110 else { 111 if (loopIDs.size() > this.varlistsize) arg.setLoopIDs(null); 112 else { 113 StringBuffer buf = new StringBuffer (); 114 for (int i=0;i<loopIDs.size();i++) buf.append(((Variable)loopIDs.get(i)).toString()); 115 arg.setLoopIDs(buf.toString()); 116 } 117 } 118 loopIDs = prevLoopIDs; 119 ArrayList steps = arg.getSteps(); 121 XQueryExpression tmpExpr = ((Step)steps.get(0)).getExpression(); 122 if (tmpExpr instanceof Variable) { 123 ((Variable)tmpExpr).addSubPath(arg); } 125 } 126 127 145 public void visit(Variable arg) throws XQueryException { 146 ArrayList prevLoopIDs = (ArrayList )loopIDs.clone(); 147 addLoopIDs(arg.getDependIDs()); 148 addLoopIDs(arg.getSkolemIDs()); 149 if (loopIDs.isEmpty()) arg.setLoopIDs(varliststring); 150 else { 151 if (loopIDs.size() > this.varlistsize) arg.setLoopIDs(null); 152 else { 153 StringBuffer buf = new StringBuffer (); 154 for (int i=0;i<loopIDs.size();i++) buf.append(((Variable)loopIDs.get(i)).toString()); 155 arg.setLoopIDs(buf.toString()); 156 } 157 } 158 loopIDs = prevLoopIDs; 159 arg.addSubPath(arg); 161 } 162 163 166 public void visit(XQueryExpressionSequence arg) throws XQueryException { 167 ArrayList prevLoopIDs = (ArrayList )loopIDs.clone(); 168 addLoopIDs(arg.getDependIDs()); 169 addLoopIDs(arg.getSkolemIDs()); 170 ArrayList subExpressions = arg.getSubExpressions(); 171 if (subExpressions != null) 172 for (int i=0;i<subExpressions.size();i++) 173 ((XQueryExpression)subExpressions.get(i)).accept(this); 174 loopIDs = prevLoopIDs; 175 } 176 177 184 186 } 187 | Popular Tags |