1 23 24 package org.xquark.xquery.parser; 25 26 import java.util.ArrayList ; 27 28 import org.xquark.xquery.parser.util.Constants; 29 import org.xquark.xquery.typing.TypeException; 30 31 public class Variable extends XQueryExpression implements Cloneable { 32 private static final String RCSRevision = "$Revision: 1.32 $"; 33 private static final String RCSName = "$Name: $"; 34 35 protected SequenceType typeDeclaration = null; 36 protected QName varName = null; 38 protected XQueryExpression expression = null; 39 protected int bindingtype = -1; 41 protected ArrayList xpaths = null; 43 protected FLWRExpression parentFLWR = null; 44 protected Variable positionVar = null; 45 protected byte creator = 0; 46 protected boolean isDummy = false; 47 48 public static final byte PARSER_CREATOR = 0; 49 public static final byte NORM_CREATOR = 1; 50 public static final byte RES_CREATOR = 2; 51 public static final byte CAN_CREATOR = 3; 52 public static final byte CLONE_CREATOR = 4; 53 public static final byte DEPMAN_CREATOR = 5; 54 55 59 public void accept(ParserVisitor visitor) throws XQueryException { 60 visitor.visit(this); 61 } 62 63 67 public Variable(QName varName, XQueryModule parentModule) throws TypeException, XQueryException { 68 setVarName(varName); 69 setParentModule(parentModule); 70 } 73 74 78 public String toString() { 79 return toString(false, false, false); 80 } 81 82 public String getStringValue() { 83 return getStringValue(false, false); 84 } 85 86 public FLWRExpression getParentFLWRExpression() { 87 return parentFLWR; 88 } 89 90 public void setParentFLWRExpression(FLWRExpression parentFLWR) { 91 this.parentFLWR = parentFLWR; 92 } 93 94 public void setParentExpression(XQueryExpression expression) { 95 if (expression == null) 96 return; 97 if (parentExpression == null) 98 parentExpression = new ArrayList (1); 99 if (!parentExpression.contains(expression)) 101 parentExpression.add(expression); 102 } 103 104 public void setExpression(XQueryExpression expression, boolean reType) throws TypeException, XQueryException { 105 if (expression != this.expression) { 106 this.expression = expression; 107 if (this.expression != null) { 108 expression.setParentExpression(this); 109 } 110 if (reType && parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null) 111 accept(parentModule.getStaticContext().getTypeVisitor()); 112 } 113 } 114 115 123 public void setBindingType(int bindingtype) throws XQueryException { 124 if (bindingtype == -1 || bindingtype > Constants.MAX_BINDINGTYPE) 125 throw new XQueryException("bindingtype of Variable has incorrect value " + bindingtype); 126 this.bindingtype = bindingtype; 127 } 128 129 public Variable getPositionVar() { 130 return positionVar; 131 } 132 133 public XQueryExpression getExpression() { 134 return expression; 135 } 136 public int getBindingType() { 137 return bindingtype; 138 } 139 140 public boolean isInput() { 141 if (expression == null) 142 return false; 143 if (expression instanceof InputFunctionCall || (expression instanceof LocatedExpression && ((LocatedExpression) expression).getExpression() instanceof InputFunctionCall)) 144 return true; 145 return false; 146 } 147 148 public ArrayList getSubPathList() { 149 return xpaths; 150 } 151 152 public ArrayList getUniqueSubPathList() { 156 if (xpaths == null) 157 return null; 158 ArrayList tmpList = null; 159 if (xpaths.contains(this)) { 160 tmpList = new ArrayList (1); 161 tmpList.add(this); 162 } else { 163 tmpList = new ArrayList (); 164 for (int i = 0; i < xpaths.size(); i++) { 165 XQueryExpression tmpExpr = (XQueryExpression) xpaths.get(i); 166 if (tmpExpr instanceof LocatedExpression) { 167 LocatedExpression tmpLoc = (LocatedExpression) tmpExpr; 168 ArrayList steps = tmpLoc.getSteps(); 169 Step lastStep = tmpLoc.getStepNum(steps.size() - 1); 170 if (lastStep.getExpression() instanceof NodeTest) { 171 tmpLoc.setNodeAccessor(lastStep.getStepKind()); 172 steps.remove(steps.size() - 1); 173 } 174 } 175 if (tmpList.isEmpty()) 176 tmpList.add(tmpExpr); 177 else { 178 boolean found = false; 179 for (int j = 0; j < tmpList.size(); j++) 180 if (tmpExpr.getStringValue().equals(((XQueryExpression) tmpList.get(j)).getStringValue())) { 181 found = true; 182 break; 183 } 184 if (!found) 185 tmpList.add(tmpExpr); 186 } 187 } 188 } 189 return tmpList; 190 } 191 192 196 public ArrayList getXTrees() { 197 if (expression == null) 198 return null; 199 return expression.getXTrees(); 200 } 201 202 public void setTypeDeclaration(SequenceType typeDeclaration) { 204 this.typeDeclaration = typeDeclaration; 205 if (typeDeclaration != null) { 206 this.typeDeclaration.setParentModule(parentModule); 207 this.typeDeclaration.setParentExpression(this); 208 } 209 } 210 public SequenceType getTypeDeclaration() { 211 return typeDeclaration; 212 } 213 214 public void setVarName(QName varName) throws XQueryException { 215 if (varName == null || varName.getName().equals("")) 216 throw new XQueryException("name of Variable cannot be null or empty string"); 217 this.varName = varName; 218 this.varName.setParentModule(parentModule); 219 this.varName.setParentExpression(this); 220 } 221 222 public QName getVarName() { 223 return varName; 224 } 225 public String getName() { 226 return varName.getStringValue(); 227 } 228 public void setName(String name) { 229 varName.setName(name); 230 } 231 232 public void addSubPath(XQueryExpression expr) { 233 if (xpaths == null) 234 xpaths = new ArrayList (); 235 if (expr != null && !xpaths.contains(expr)) 236 xpaths.add(expr); 237 } 238 239 public byte getCreator() { 240 return creator; 241 } 242 243 public void setCreator(byte creator) { 244 this.creator = creator; 245 } 246 247 public void addParentExpression(XQueryExpression parentExpression) { 248 addParentExpression(parentExpression); 249 varName.addParentExpression(parentExpression); 250 if (expression != null) 251 expression.addParentExpression(parentExpression); 252 } 253 254 public void isDummy(boolean b) { 255 isDummy = b; 256 } 257 258 public boolean isDummy() { 259 return isDummy; 260 } 261 262 public boolean startsWithVariable() { 263 return true; 264 } 265 266 } 267 | Popular Tags |