1 22 23 package org.xquark.xquery.parser; 24 25 import java.util.*; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.xquark.schema.SchemaManager; 30 import org.xquark.util.NamespaceContextStack; 31 import org.xquark.xquery.metadata.StaticContext; 32 import org.xquark.xquery.normalize.*; 33 import org.xquark.xquery.parser.util.Constants; 34 import org.xquark.xquery.typing.TypeVisitor; 35 36 53 54 public class XQueryModule { 55 private static final String RCSRevision = "$Revision: 1.36 $"; 56 private static final String RCSName = "$Name: $"; 57 58 60 protected String version = null; 61 protected String prefix = null; 62 protected String namespace = null; 63 protected NamespaceContextStack declarations = null; 64 protected String defaultFunctionNamespace = null; 65 protected int xmlspace = Constants.NOTHING; 66 protected String defaultCollation = null; 67 protected String baseURI = null; 68 69 protected HashMap hashVarList = null; 70 protected ArrayList declList = null; 71 protected HashMap hashExtVarList = null; 72 protected HashMap hashFuncList = null; 73 protected HashMap importSchemas = null; 74 protected ArrayList schemaList = null; 75 protected HashMap importModules = null; 76 protected ArrayList moduleList = null; 77 78 protected int validation = Constants.NOTHING; 79 protected ArrayList expressions = null; 80 81 protected SchemaManager schemaManager = null; 82 protected StaticContext staticContext = null; 83 84 88 public void accept(ParserVisitor visitor) throws XQueryException { 89 visitor.visit(this); 90 } 91 92 96 public XQueryModule() { 97 } 98 99 103 public ArrayList getExpressions() { 105 return expressions; 106 } 107 public void setExpressions(ArrayList expressions) throws XQueryException { 108 this.expressions = expressions; 109 if (expressions != null) 110 for (int i = 0; i < this.expressions.size(); i++) { 111 ((XQueryExpression) this.expressions.get(i)).setParentModule(this); 112 ((XQueryExpression) this.expressions.get(i)).setParentExpressions(null); 113 } 114 } 115 116 public void setElementAt(XQueryExpression expr, int index) { 117 expr.setParentModule(this); 118 expressions.set(index, expr); 119 } 120 121 public NamespaceContextStack getDeclarations() { 123 return declarations; 124 } 125 public void setDeclarations(NamespaceContextStack declarations) { 126 this.declarations = declarations; 127 } 128 129 public String getNameSpace(String prefixname) { 130 if (prefixname == null) 131 return null; 132 if (declarations == null) 133 return null; 134 String str = declarations.getNamespaceURI(prefixname); 135 if (str == null) 136 return null; 137 int len = str.length(); 138 if (str.indexOf("\"") == 0) 139 return str.substring(1, len - 2); 140 else 141 return str; 142 } 143 144 public HashMap getVariables() { 146 return hashVarList; 147 } 148 public void setDeclList(ArrayList declList) { 149 this.declList = declList; 150 } 151 public ArrayList getDeclList() { 152 return declList; 153 } 154 public void setVariables(HashMap hashVarList) { 155 this.hashVarList = hashVarList; 156 if (hashVarList != null) { 157 Collection varValues = hashVarList.values(); 158 Iterator itValues = varValues.iterator(); 159 while (itValues.hasNext()) { 160 HashMap map = (HashMap) itValues.next(); 161 Collection mapValues = map.values(); 162 Iterator itMapValues = mapValues.iterator(); 163 while (itMapValues.hasNext()) { 164 ((Variable) itMapValues.next()).setParentModule(this); 165 } 166 } 167 } 168 } 169 170 public Variable getVariableDefinition(String name) { 171 if (name == null || hashVarList == null) 172 return null; 173 return (Variable) hashVarList.get(name); 174 } 175 176 public HashMap getExternalVariables() { 178 return hashExtVarList; 179 } 180 public void setExternalVariables(HashMap hashExtVarList) { 181 this.hashExtVarList = hashExtVarList; 182 } 183 184 public HashMap getFunctions() { 186 return hashFuncList; 187 } 188 public void setFunctions(HashMap hashFuncList) { 189 this.hashFuncList = hashFuncList; 190 if (hashFuncList != null) { 191 Collection funcValues = hashFuncList.values(); 192 Iterator itValues = funcValues.iterator(); 193 while (itValues.hasNext()) { 194 HashMap map = (HashMap) itValues.next(); 195 Collection mapValues = map.values(); 196 Iterator itMapValues = mapValues.iterator(); 197 while (itMapValues.hasNext()) { 198 ((FunctionDeclaration) itMapValues.next()).setParentModule(this); 199 } 200 } 201 } 202 } 203 204 public void setDefaultCollation(String defaultCollation) { 206 this.defaultCollation = defaultCollation; 207 } 208 209 public String getDefaultCollation() { 210 return defaultCollation; 211 } 212 213 public void setVersion(String version) { 215 this.version = version; 216 } 217 218 public String getVersion() { 219 return version; 220 } 221 222 public void setPrefix(String prefix) { 224 this.prefix = prefix; 225 } 226 227 public String getPrefix() { 228 return prefix; 229 } 230 231 public void setNamespace(String location) { 233 this.namespace = location; 234 } 235 236 public String getNamespace() { 237 return namespace; 238 } 239 240 public void setDefaultFunctionNamespace(String namespace) { 242 this.defaultFunctionNamespace = namespace; 243 } 244 245 public String getDefaultFunctionNamespace() { 246 return defaultFunctionNamespace; 247 } 248 249 public void setBaseURI(String uri) { 251 this.baseURI = uri; 252 } 253 254 public String getBaseURI() { 255 return baseURI; 256 } 257 258 public void setXmlSpace(int xmlspace) { 260 this.xmlspace = xmlspace; 261 } 262 263 public int getXmlSpace() { 264 return xmlspace; 265 } 266 267 public void setValidation(int validation) { 269 this.validation = validation; 270 } 271 272 public int getValidation() { 273 return validation; 274 } 275 276 public HashMap getImportSchemas() { 278 return importSchemas; 279 } 280 public ArrayList getSchemaList() { 281 return schemaList; 282 } 283 public void setSchemaList(ArrayList schemaList) { 284 this.schemaList = schemaList; 285 } 286 public void setImportSchemas(HashMap importSchemas) { 287 this.importSchemas = importSchemas; 288 if (importSchemas != null) { 289 Collection importSchemaValues = importSchemas.values(); 290 Iterator itValues = importSchemaValues.iterator(); 291 while (itValues.hasNext()) 292 ((ImportSchema) itValues.next()).setParentModule(this); 293 } 294 } 295 296 public ImportSchema getImportSchema(String name) { 297 if (name == null || importSchemas == null) 298 return null; 299 return (ImportSchema) importSchemas.get(name); 300 } 301 302 public HashMap getImportModules() { 304 return importModules; 305 } 306 public void setModuleList(ArrayList moduleList) { 307 this.moduleList = moduleList; 308 } 309 public ArrayList getModuleList() { 310 return moduleList; 311 } 312 public void setImportModules(HashMap importModules) { 313 this.importModules = importModules; 314 if (importModules != null) { 315 Collection importModuleValues = importModules.values(); 316 Iterator itValues = importModuleValues.iterator(); 317 while (itValues.hasNext()) 318 ((ImportModule) itValues.next()).setParentModule(this); 319 } 320 } 321 322 public ImportModule getImportModule(String name) { 323 if (name == null || importModules == null) 324 return null; 325 return (ImportModule) importModules.get(name); 326 } 327 328 public void setSchemaManager(SchemaManager schemaManager) { 330 this.schemaManager = schemaManager; 331 } 332 public SchemaManager getSchemaManager() { 333 return schemaManager; 334 } 335 336 public void setStaticContext(StaticContext staticContext) { 338 this.staticContext = staticContext; 339 } 340 public StaticContext getStaticContext() { 341 return staticContext; 342 } 343 344 348 public void normalize(TypeVisitor typeVisitor) throws XQueryException { 349 normalize(typeVisitor, null); 350 } 351 352 public void normalize(TypeVisitor typeVisitor, String sourceName) throws XQueryException { 353 try { 356 ExpressionNormalizationVisitor normalizeVisitor = new ExpressionNormalizationVisitor(typeVisitor, sourceName); 357 accept(normalizeVisitor); 358 } catch (XQueryException ne) { 359 if (namespace != null) 360 throw new XQueryException(ne.getMessage() + " in module " + namespace); 361 else 362 throw new XQueryException(ne.getMessage() + " in main module"); 363 } 364 369 try { 372 if (expressions != null) { 373 RestructureVisitor restructureVisitor = new RestructureVisitor(typeVisitor, (sourceName != null)); 374 accept(restructureVisitor); 375 } 376 } catch (XQueryException xqe) { 377 if (namespace != null) 378 throw new XQueryException(xqe.getMessage() + " in module " + namespace); 379 else 380 throw new XQueryException(xqe.getMessage() + " in main module"); 381 } 382 } 387 388 public TransformXQueryExpression canonize(XQueryExpression expression, TypeVisitor typeVisitor) throws ParseException { 389 ArrayList QDB = null; 392 XQueryExpression QMEM = null; 393 try { 394 CanonizeVisitor canonizeVisitor = new CanonizeVisitor(typeVisitor); 395 expression.accept(canonizeVisitor); 396 QDB = canonizeVisitor.getQDB(); 399 QMEM = canonizeVisitor.getQMEM(); 400 } catch (XQueryException e) { 401 throw new ParseException("Invalid expression : " + this +" -> " + e.getMessage()); 402 } 403 if (QDB == null) 404 throw new ParseException("Invalid expression : " + this); 405 return new TransformXQueryExpression(QDB, QMEM); 412 } 413 414 418 public Set getSourceNames() { 419 Set sources = null; 420 if (expressions != null) { 421 for (int i = 0; i < expressions.size(); i++) { 422 Set addSet = ((XQueryExpression) expressions.get(i)).getSourceNames(); 423 if (addSet != null) { 424 if (sources == null) 425 sources = new HashSet(); 426 sources.addAll(addSet); 427 } 428 } 429 } 430 return sources; 431 } 432 433 public Set getUrls() { 434 Set urls = null; 435 if (expressions != null) { 436 for (int i = 0; i < expressions.size(); i++) { 437 Set addSet = ((XQueryExpression) expressions.get(i)).getUrls(); 438 if (addSet != null) { 439 if (urls == null) 440 urls = new HashSet(); 441 urls.addAll(addSet); 442 } 443 } 444 } 445 return urls; 446 } 447 448 452 public String toString(boolean doLineFeeds, boolean doImports) { 453 PrintVisitor pv = new PrintVisitor(); 454 pv.reset(doLineFeeds, doImports, false, true); 455 pv.setNamespaceContextStack(declarations); 456 try { 457 this.accept(pv); 458 } catch (XQueryException qe) { 459 } 460 return pv.getString(); 461 } 462 463 public String toString() { 464 PrintVisitor pv = new PrintVisitor(); 465 pv.reset(false, true, false, true); 466 try { 467 this.accept(pv); 468 } catch (XQueryException qe) { 469 } 470 return pv.getString(); 471 } 472 } 473 | Popular Tags |