1 22 23 package org.xquark.xquery.normalize; 24 25 44 public class VisitorContext { 45 46 47 48 49 50 51 52 53 static final int _XQUERYEXPRESSIONSEQUENCE_EXISTS = 1; 54 56 static final int _VARIABLE_DECLARATION = 3; 57 static final int _VARIABLE_SUBSTITUTION4QUANTIFIED = 4; 58 static final int _VARIABLE_SUBSTITUTION4FLWR = 5; 59 static final int _VARIABLE_SUBSTITUTION4LETFLWR = 6; 60 static final int _VARIABLE_WASSUBSTITUTELETFLWR = 7; 61 static final int _VARIABLE_CHANGEEXPRESSION4FLWR = 8; 62 63 static final int _FLWREXPRESSION_GETFLWR = 9; 64 static final int _FLWREXPRESSION_INNERFLWR = 10; 65 static final int _QUANTIFIED_INNERFLWR = 14; 70 74 static final int _FLWREXPRESSION_GETANDREPLACEFLWRINWHERE = 17; 75 76 static final int _ELEMENT_EXISTS = 18; 77 79 static final int _LOCATEDEXPRESSION_INPREDICATE = 20; 81 82 static final int _LISTOPUNIONEXPRESSION_GETEXPRESSIONS = 21; 83 84 static final int _INCLUDEINPARENTHESIS = 22; 85 86 static final int _STEP_ISPARENTAXISSTEP = 23; 87 static final int _STEP_GETPREDICATE = 24; 88 89 static final int _INAGGREGATEFUNCTION = 25; 90 91 static final int _INTERNALFUNCTIONCALL_NAMEDUNORDERED_PARAMETERS = 26; 92 93 static final int _MULTIVALUED_NEEDEXISTSFUNCTION = 27; 94 95 static final int _FLWR_RETURNS_TAG = 28; 96 static final int _STEP_GETPREDICATE_VAR = 29; 98 99 103 static final int MAX_QUESTIONS = 32; 104 105 106 107 static final int _ANY_DEPTH = 0; 108 109 110 111 112 protected java.util.HashMap memoMap = null; 113 114 115 116 120 public VisitorContext() throws NormalizeException { 121 this.memoMap = new java.util.HashMap (); 123 } 124 125 132 public VisitorContext(int depth, int question, java.lang.Object response) throws NormalizeException { 133 this.memoMap = new java.util.HashMap (); 135 this.setMemo(depth, question, response, true); 136 } 137 138 139 140 147 public java.lang.Object getMemo(int depth, int question) throws NormalizeException { 148 Integer key = new Integer (depth * VisitorContext.MAX_QUESTIONS + question); 149 if (this.memoMap.containsKey(key)) { 150 return this.memoMap.get(key); 151 } else { 152 return null; 153 } 154 } 155 156 162 public boolean existsMemo(int depth, int question) throws NormalizeException { 163 Integer key = new Integer (depth * VisitorContext.MAX_QUESTIONS + question); 164 return this.memoMap.containsKey(key); 165 } 166 167 173 public boolean removeMemo(int depth, int question) throws NormalizeException { 174 Integer key = new Integer (depth * VisitorContext.MAX_QUESTIONS + question); 175 if (this.memoMap.containsKey(key)) { 176 this.memoMap.remove(key); 177 return true; 178 } else { 179 return false; 180 } 181 } 182 183 191 public void setMemo(int depth, int question, java.lang.Object response, boolean overwrite) throws NormalizeException { 192 Integer key = new Integer (depth * VisitorContext.MAX_QUESTIONS + question); 193 if ((!overwrite) && (this.memoMap.containsKey(key))) { 194 throw new NormalizeException("The memo couple \""+depth+"."+question+"\" already exist!"); 195 } else { 196 this.memoMap.put(key, response); 197 } 198 } 199 200 207 private void setMemo(Integer key, java.lang.Object response, boolean overwrite) throws NormalizeException { 208 if ((!overwrite) && (this.memoMap.containsKey(key))) { 209 int aKey = key.intValue(); 210 int depth = (int)(aKey / VisitorContext.MAX_QUESTIONS); 211 int question = (int)(aKey % VisitorContext.MAX_QUESTIONS); 212 throw new NormalizeException("The memo couple \""+depth+"."+question+"\" already exist!"); 213 } else { 214 this.memoMap.put(key, response); 215 } 216 } 217 218 225 public void merge(VisitorContext ctx, boolean keepPrevious) throws NormalizeException { 226 Object [] keys = ctx.memoMap.keySet().toArray(); 227 for (int i = 0; i < keys.length; i++) { 228 Integer aKey = (Integer )keys[i]; 229 if ((!this.memoMap.containsKey(aKey)) || (!keepPrevious)) { 230 this.memoMap.put(aKey, ctx.memoMap.get(aKey)); 231 } 232 } 233 } 234 235 240 public void update(VisitorContext ctx) throws NormalizeException { 241 Object [] keys = this.memoMap.keySet().toArray(); 242 for (int i = 0; i < keys.length; i++) { 243 Integer aKey = (Integer )keys[i]; 244 if (ctx.memoMap.containsKey(aKey)) { 245 if (aKey.intValue() == VisitorContext._FLWREXPRESSION_GETANDREPLACEFLWRINWHERE) { 246 java.util.ArrayList content1 = (java.util.ArrayList )this.memoMap.get(aKey); 248 java.util.ArrayList content2 = (java.util.ArrayList )ctx.memoMap.get(aKey); 249 if ((content1 != null) && (content2 != null)) { 250 for (int j = 0; j < content2.size(); j++) { 251 if (!content1.contains(content2.get(j))) { 252 content1.add(content2.get(j)); 253 } 254 } 255 } 256 this.memoMap.put(aKey, content1); 257 } else { 258 this.memoMap.put(aKey, ctx.memoMap.get(aKey)); 259 } 260 } 261 } 262 } 263 264 271 public void filter(int currentDepth) throws NormalizeException { 272 Object [] keys = this.memoMap.keySet().toArray(); 273 for (int i = 0; i < keys.length; i++) { 274 Integer aKey = (Integer )keys[i]; 275 int depth = (int)(aKey.intValue() / VisitorContext.MAX_QUESTIONS); 276 if ((depth != 0) && (depth < currentDepth)) { 277 this.memoMap.remove(aKey); 278 } 279 } 280 } 281 282 285 public void clear() { 286 this.memoMap.clear(); 287 } 288 289 293 public java.lang.String toString() { 294 java.lang.String ret = ""; 295 Object [] keys = this.memoMap.keySet().toArray(); 296 for (int i = 0; i < keys.length; i++) { 297 Integer aKey = (Integer )keys[i]; 298 int aKeyAsInt = aKey.intValue(); 299 int depth = (int)(aKeyAsInt / VisitorContext.MAX_QUESTIONS); 300 int question = (int)(aKeyAsInt % VisitorContext.MAX_QUESTIONS); 301 ret += depth+"."+question+" - "+this.memoMap.get(aKey)+"\n"; 302 } 303 if (ret.equals("")) { 304 return "EMPTY CONTEXT"; 305 } 306 return ret; 307 } 308 309 313 public VisitorContext cloneContext() throws NormalizeException { 314 VisitorContext clone = new VisitorContext(); 315 Object [] keys = this.memoMap.keySet().toArray(); 316 for (int i = 0; i < keys.length; i++) { 317 Integer aKey = (Integer )keys[i]; 318 int aKeyAsInt = aKey.intValue(); 319 int depth = (int)(aKeyAsInt / VisitorContext.MAX_QUESTIONS); 320 int question = (int)(aKeyAsInt % VisitorContext.MAX_QUESTIONS); 321 clone.setMemo(depth, question,this.memoMap.get(aKey), false); 322 } 323 return clone; 324 } 325 } 326 | Popular Tags |