1 22 23 package org.xquark.xquery.metadata; 24 25 import java.util.*; 26 27 import org.xquark.schema.SchemaManager; 28 import org.xquark.schema.datatypes.URI; 29 import org.xquark.util.NamespaceContextStack; 30 import org.xquark.xpath.StepExpr; 31 import org.xquark.xpath.XNode; 32 import org.xquark.xpath.XTreeNode; 33 import org.xquark.xquery.ModuleManager; 34 import org.xquark.xquery.metadata.resolver.CollectionMetadata; 35 import org.xquark.xquery.metadata.resolver.MetadataAccess; 36 import org.xquark.xquery.metadata.resolver.SourceMetadata; 37 import org.xquark.xquery.parser.*; 38 import org.xquark.xquery.typing.*; 39 40 public class StaticContext { 41 42 public static final String XQUERY_FUNCTIONS_URI = "http://www.w3.org/2002/11/xquery-functions"; 43 public static final String XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema"; 44 public static final String XML_SCHEMA_INSTANCE_URI = "http://www.w3.org/2001/XMLSchema-instance"; 45 46 private NamespaceContextStack prefixes = new NamespaceContextStack(); 47 private String defaultFunctionNamespace = XQUERY_FUNCTIONS_URI; 48 private SchemaManager schemaManager; 49 private ModuleManager moduleManager; 50 private MetadataAccess resolver = null; 51 private HashMap functions = new HashMap(); 52 private HashMap collations = new HashMap(); 53 private String defaultCollation = null; 54 private String baseURI = null; 55 private boolean stripWhitespace = true; 56 private TypeVisitor typeVisitor = null; 57 58 private VarCounter varCounter = null; 59 60 public StaticContext(MetadataAccess resolver, SchemaManager schemamanager, ModuleManager modulemanager, VarCounter varCounter) { 61 this.resolver = resolver; 62 this.varCounter = varCounter; 63 schemaManager = new SchemaManager(schemamanager); 64 moduleManager = new ModuleManager(modulemanager); 65 moduleManager.setStaticContext(this); 66 } 67 68 public VarCounter getVarCounter() { 69 return varCounter; 70 } 71 72 76 public String getBaseURI() { 77 return baseURI; 78 } 79 80 84 public String getDefaultCollation() { 85 return defaultCollation; 86 } 87 88 92 public String getDefaultFunctionNamespace() { 93 return defaultFunctionNamespace; 94 } 95 96 100 public SchemaManager getSchemaManager() { 101 return schemaManager; 102 } 103 104 public void setTypeVisitor(TypeVisitor typeVisitor) { 105 this.typeVisitor = typeVisitor; 106 } 107 public TypeVisitor getTypeVisitor() { 108 return typeVisitor; 109 } 110 111 115 public void setBaseURI(String baseURI) { 116 this.baseURI = baseURI; 117 } 118 119 123 public void setDefaultCollation(String defaultCollation) { 124 this.defaultCollation = defaultCollation; 125 } 126 127 131 public void setDefaultFunctionNamespace(String defaultFunctionNamespace) { 132 this.defaultFunctionNamespace = defaultFunctionNamespace; 133 } 134 135 139 public HashMap getCollations() { 140 return collations; 141 } 142 143 147 public HashMap getFunctions() { 148 return functions; 149 } 150 151 155 public NamespaceContextStack getPrefixes() { 156 return prefixes; 157 } 158 159 163 public ModuleManager getModuleManager() { 164 return moduleManager; 165 } 166 170 174 179 180 184 public boolean isStripWhitespace() { 185 return stripWhitespace; 186 } 187 188 192 public void setStripWhitespace(boolean stripWhitespace) { 193 this.stripWhitespace = stripWhitespace; 194 } 195 196 198 public ArrayList resolveXPath(ArrayList xnodes, ArrayList steps) { 199 if (steps == null || steps.isEmpty()) 200 return xnodes; 201 for (int i = 0; i < steps.size(); i++) { 202 xnodes = resolveXPath(xnodes, (StepExpr) steps.get(i)); 203 } 204 return xnodes; 205 } 206 207 public ArrayList resolveXPath(ArrayList xnodes, StepExpr step) { 208 if (xnodes == null || xnodes.isEmpty()) 209 return null; 210 ArrayList reslist = new ArrayList(); 211 for (int i = 0; i < xnodes.size(); i++) { 212 Collection col = ((XTreeNode) xnodes.get(i)).navigate(step); 213 reslist.addAll(col); 214 } 215 if (reslist.isEmpty()) 216 reslist = null; 217 return reslist; 218 } 219 220 public QType resolveQType(byte context, QType qtype, StepExpr step, boolean inPredicate) throws TypeException, XQueryException { 222 return Utils.getSubType(context, qtype, step, schemaManager, inPredicate); 224 } 225 226 private final static String COLON = ":"; 227 private final static String STAR = "*"; 228 private final static String DOT = "."; 229 230 public ArrayList getCollectionRootNodes(String sourceName, String collectionName) { 231 232 ArrayList al = new ArrayList(); 233 SourceMetadata mw = null; 234 mw = resolver.getSourceMetadata(sourceName); 235 if (mw != null) { 236 CollectionMetadata metacol = mw.getCollectionMetadata(collectionName); 237 if (metacol != null) 238 al.add(metacol.getXTree().getRoot()); 239 else { Collection coll = mw.getCollectionsMetadata(); 241 for (Iterator it = coll.iterator(); it.hasNext();) { 242 metacol = (CollectionMetadata) it.next(); 243 String colName = metacol.getCollectionName(); 244 if (STAR.equals(collectionName) || colName.endsWith(DOT + collectionName)) { 245 al.add(metacol.getXTree().getRoot()); 247 } 249 } 250 } 251 } else { 252 Collection sources = resolver.getSources(); 253 for (Iterator e = sources.iterator(); e.hasNext();) { 254 mw = (SourceMetadata) e.next(); 255 CollectionMetadata metacol = mw.getCollectionMetadata(collectionName); 256 if (metacol != null) 257 al.add(metacol.getXTree().getRoot()); 258 else { Collection coll = mw.getCollectionsMetadata(); 260 for (Iterator it = coll.iterator(); it.hasNext();) { 261 metacol = (CollectionMetadata) it.next(); 262 String colName = metacol.getCollectionName(); 263 if (STAR.equals(collectionName) || colName.endsWith(DOT + collectionName)) { 264 al.add(metacol.getXTree().getRoot()); 266 } 268 } 269 } 270 } 271 } 272 if (al.isEmpty()) 273 al = null; 274 return al; 275 } 276 277 public MetadataAccess getMetaData() { 278 return resolver; 279 } 280 281 public XNode getDocumentRootNode(URI documentURI) { 282 return null; 283 } 284 285 public ArrayList getInputRootNodes() { 286 return null; 287 } 288 289 private final String VAR_NAME = "var"; 290 291 public Variable createVariable(int bindingType, XQueryExpression expr, byte creator) throws XQueryException { 292 if (typeVisitor != null && expr != null && expr.getQType() == null) 293 expr.accept(typeVisitor); 294 Variable var = new Variable(getVarName(expr.getParentModule()), expr.getParentModule()); 295 var.setBindingType(bindingType); 296 var.setExpression(expr,true); 297 var.setCreator(creator); 298 return var; 299 } 300 public QName getVarName(XQueryModule parentModule) throws XQueryException { 301 return new QName(parentModule.getNamespace(), parentModule.getPrefix(), VAR_NAME + (++varCounter.lastVarIndex), parentModule, parentModule.getStaticContext().getPrefixes()); 302 } 303 304 } 305 | Popular Tags |