1 23 24 package org.xquark.mediator.decomposer; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Set ; 29 30 import org.xquark.xquery.metadata.MetaDataImpl; 31 import org.xquark.xquery.metadata.resolver.PathResolver; 32 import org.xquark.xquery.parser.*; 33 34 public class StepLists { 35 private static final String RCSRevision = "$Revision: 1.6 $"; 39 private static final String RCSName = "$Name: $"; 40 private ArrayList stepslists = null; 44 private ArrayList cur_stepslists = null; 45 46 private HashMap mapvar = null; 47 private MetaDataImpl metadata = null; 48 49 private PathResolver pathresolver = null; 50 private MakeStepsListVisitor stepslistvisitor = null; 51 52 58 public StepLists(MetaDataImpl metadata) { 59 stepslists = new ArrayList (); 60 mapvar = new HashMap (); 61 this.metadata = metadata; 62 pathresolver = new PathResolver(metadata); 63 stepslistvisitor = new MakeStepsListVisitor(); 64 } 65 66 public StepLists(HashMap mapvar, MetaDataImpl metadata) { 67 stepslists = new ArrayList (); 68 this.mapvar = mapvar; 69 this.metadata = metadata; 70 pathresolver = new PathResolver(metadata); 71 stepslistvisitor = new MakeStepsListVisitor(); 72 } 73 74 public void newStepslist() throws XQueryException { 78 if ((cur_stepslists != null) && (cur_stepslists.isEmpty())) 79 return; 80 cur_stepslists = new ArrayList (1); 81 cur_stepslists.add(new ArrayList ()); 82 stepslists.addAll(cur_stepslists); 83 } 84 85 public ArrayList getStepsLists() { 86 return stepslists; 87 } 88 89 public Set getRegisteredVariables() { 90 return mapvar.keySet(); 91 } 92 93 public void registerVars(ArrayList vars) throws XQueryException { 94 if (vars == null) 95 return; 96 for (int i = 0; i < vars.size(); i++) 97 this.registerVar((Variable) vars.get(i)); 98 } 99 public void registerVar(Variable var) throws XQueryException { 100 101 if ((var.getSourceNames() != null && !var.getSourceNames().isEmpty()) || (var.getUrls() != null && !var.getUrls().isEmpty())) { 103 if (mapvar.get(var) != null) 104 return; 105 mapvar.put(var, null); 106 return; 107 } 108 110 XQueryExpression exp = var.getExpression(); 111 112 if ((exp.getSourceNames() != null && !exp.getSourceNames().isEmpty()) || (exp.getUrls() != null && !exp.getUrls().isEmpty())) { 114 var.setSourceNames(exp.getSourceNames()); 115 var.setUrls(exp.getUrls()); 116 if (mapvar.get(var) != null) 117 return; 118 mapvar.put(var, null); 119 return; 120 } 121 123 StepLists tmplist = new StepLists(this.mapvar, this.metadata); 124 stepslistvisitor.setStepsLists(tmplist); 125 exp.accept(stepslistvisitor); 126 127 if ((exp.getSourceNames() != null && !exp.getSourceNames().isEmpty()) || (exp.getUrls() != null && !exp.getUrls().isEmpty())) { 129 var.setSourceNames(exp.getSourceNames()); 130 var.setUrls(exp.getUrls()); 131 if (mapvar.get(var) != null) 132 return; 133 mapvar.put(var, null); 134 return; 135 } 136 138 ArrayList al = tmplist.getStepsLists(); 142 143 if (al != null && !al.isEmpty()) { 144 149 ArrayList endlist = (ArrayList ) al.get(al.size() - 1); 150 mapvar.put(var, endlist); 151 152 if (!endlist.isEmpty()) { 153 if (((Step)endlist.get(0)).getExpression() instanceof Variable) { 154 Variable frontvar = (Variable)((Step)endlist.get(0)).getExpression(); 155 var.setSourceNames(frontvar.getSourceNames()); 156 var.setUrls(frontvar.getUrls()); 157 exp.setSourceNames(frontvar.getSourceNames()); 158 exp.setUrls(frontvar.getUrls()); 159 } else if (metadata != null) { 160 pathresolver.resolveSteps(endlist); 161 var.setSourceNames(pathresolver.getSourceNames()); 162 var.setUrls(pathresolver.getUrls()); 163 exp.setSourceNames(pathresolver.getSourceNames()); 164 exp.setUrls(pathresolver.getUrls()); 165 } 166 } 167 } 168 187 } 188 189 public void addReplaceVar(Variable var) throws XQueryException { 190 ArrayList prefix = (ArrayList ) mapvar.get(var); 191 if (prefix == null) 192 return; for (int i = 0; i < prefix.size(); i++) { 194 Step stepi = (Step) prefix.get(i); 195 addStep(stepi); 196 } 197 } 198 199 public void addStep(XQueryExpression step) throws XQueryException { 200 if (step == null) 201 return; 202 203 if (cur_stepslists == null) 204 newStepslist(); 205 206 if (step instanceof Variable) { 207 newStepslist(); 208 addReplaceVar((Variable) step); 209 return; 210 } 211 212 if (!(step instanceof Step)) 213 throw new XQueryException("Error: step is not type Step or Variable"); 214 215 Step stepp = (Step) step; 216 235 if (stepp.getExpression() instanceof ListOpUNIONExpression) { 236 ArrayList steps = ((LocatedExpression)((ListOpUNIONExpression)stepp.getExpression()).getExpression1()).getSteps(); 239 ArrayList tmplist = new ArrayList (cur_stepslists.size()); 241 for (int i=0;i<cur_stepslists.size();i++) 242 tmplist.add(((ArrayList )cur_stepslists.get(i)).clone()); 243 for (int i=0;i<steps.size();i++) 244 addStep((Step)steps.get(i)); 245 ArrayList oldlist = cur_stepslists; 247 stepslists.addAll(tmplist); 248 cur_stepslists = tmplist; 249 steps = ((LocatedExpression)((ListOpUNIONExpression)stepp.getExpression()).getExpression2()).getSteps(); 251 for (int i=0;i<steps.size();i++) 252 addStep((Step)steps.get(i)); 253 cur_stepslists.addAll(oldlist); 255 } 256 else { 257 for (int i = 0; i < cur_stepslists.size(); i++) 258 ((ArrayList ) cur_stepslists.get(i)).add(stepp); 259 } 260 } 263 264 } 283
| Popular Tags
|