1 22 23 package org.xquark.xquery; 24 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 31 import org.xml.sax.InputSource ; 32 import org.xquark.schema.SchemaManager; 33 import org.xquark.xquery.metadata.StaticContext; 34 import org.xquark.xquery.metadata.VarCounter; 35 import org.xquark.xquery.metadata.resolver.MetadataAccess; 36 import org.xquark.xquery.parser.*; 37 import org.xquark.xquery.typing.TypeVisitor; 38 39 public class ModuleManager extends DefaultModuleLocator { 40 private static final String RCSRevision = "$Revision: 1.4 $"; 41 private static final String RCSName = "$Name: $"; 42 43 44 private HashMap modules = new HashMap (); 46 private boolean useCache = true; 47 48 private StaticContext staticContext = null; 49 50 public ModuleManager() { 51 this(null, true); 52 } 53 54 58 public ModuleManager(ModuleManager parent) { 59 this(parent, true); 60 } 61 62 public ModuleManager(ModuleManager parent, boolean useCache) { 63 this.useCache = useCache; 64 if (parent != null) { 65 Iterator it = parent.getModules().iterator(); 66 while (it.hasNext()) { 67 XQueryModule s = (XQueryModule) it.next(); 68 if (s != null) 70 modules.put(s.getNamespace(), s); 72 } 73 } 74 } 75 76 public void setStaticContext(StaticContext staticContext) { 77 this.staticContext = staticContext; 78 } 79 80 public boolean isRegistered(String namespace) { 81 return modules.containsKey(namespace); 82 } 83 84 public XQueryModule getModule(String namespace) { 85 return (XQueryModule) modules.get(namespace); 86 } 87 88 public Collection getModules() { 89 return modules.values(); 90 } 91 92 public XQueryModule removeModule(String namespace) { 93 return (XQueryModule) modules.remove(namespace); 94 } 95 96 private void checkNamespace(String ns, XQueryModule module) throws XQueryException { 97 if ((ns != null && !ns.equals(module.getNamespace())) || (ns == null && module.getNamespace() != null)) 98 throw new XQueryException("Loaded module target namespace " + module.getNamespace() + " does not match expected URI: " + ns); 99 } 100 101 public XQueryModule loadModule(String namespace, SchemaManager sc, TypeVisitor typeVisitor) throws XQueryException { 102 return loadModule(namespace, sc, typeVisitor, new VarCounter()); 103 } 104 public XQueryModule loadModule(String namespace, SchemaManager sc, TypeVisitor typeVisitor, VarCounter varCounter) throws XQueryException { 105 if (namespace.startsWith("java:")) { 106 return new JavaModule(namespace.substring(5), typeVisitor); 107 } else if (namespace.startsWith("wsdl:")) { 108 return new WSDLModule(namespace.substring(5)); 109 } 110 return loadModule(this, namespace, sc, varCounter); 111 } 112 113 public XQueryModule loadModule(String namespace, SchemaManager sc) throws XQueryException { 114 return loadModule(namespace, sc, new VarCounter()); 115 } 116 public XQueryModule loadModule(String namespace, SchemaManager sc, VarCounter varCounter) throws XQueryException { 117 return loadModule(this, namespace, sc, varCounter); 118 } 119 120 public XQueryModule loadModule(ModuleLocator locator, String namespace, SchemaManager sc) throws XQueryException { 121 return loadModule(locator, namespace, sc, new VarCounter()); 122 } 123 public XQueryModule loadModule(ModuleLocator locator, String namespace, SchemaManager sc, VarCounter varCounter) throws XQueryException { 124 if (isRegistered(namespace)) 126 return getModule(namespace); 127 128 XQueryModule result = null; 129 InputSource source = null; 130 Iterator it1 = locator.getModuleLocations(namespace); 132 if (it1 != null) { 133 while (it1.hasNext()) { 134 source = locator.resolveLocation(namespace, (String ) it1.next()); 135 if (source != null) 136 result = loadModule(locator, namespace, source, sc, varCounter); 137 if (result != null) 138 return result; 139 } 140 } 141 142 source = locator.resolveLocation(namespace, namespace); 144 if (source != null) 145 result = loadModule(locator, namespace, source, sc, varCounter); 146 if (result != null) 147 return result; 148 149 Iterator it2 = locator.getModuleLocations(namespace); 151 if (it2 != null) { 152 while (it2.hasNext()) { 153 try { 154 URL url = new URL ((String ) it2.next()); 155 source = locator.resolveLocation(namespace, url); 156 if (source != null) 157 result = loadModule(locator, namespace, source, sc, varCounter); 158 if (result != null) 159 return result; 160 } catch (MalformedURLException ex) { 161 } 163 } 164 } 165 166 throw new XQueryException("Could not find module "+namespace); 168 } 169 170 private XQueryModule loadModule(ModuleLocator locator, String namespace, InputSource source, SchemaManager sc, VarCounter varCounter) throws XQueryException { 171 if (isRegistered(namespace)) 173 return getModule(namespace); 174 175 XQueryModule result = null; 176 177 try { 178 XQueryParser parser = new XQueryParser(source.getByteStream()); 179 parser.setBaseURI(source.getSystemId()); 181 result = parser.Start(staticContext.getMetaData(), sc, this, varCounter); 182 checkNamespace(namespace, result); 183 } catch (ParseException ex) { 184 throw new XQueryException(ex.getMessage(), ex); 185 } 186 modules.put(namespace, result); 188 return result; 189 } 190 private XQueryModule loadModule(ModuleLocator locator, InputSource source, MetadataAccess metadata, SchemaManager sc, VarCounter varCounter) throws XQueryException { 191 XQueryModule result = null; 192 193 try { 194 XQueryParser parser = new XQueryParser(source.getByteStream()); 195 parser.setBaseURI(source.getSystemId()); 197 result = parser.Start(metadata, sc, this, varCounter); 199 } catch (ParseException ex) { 200 throw new XQueryException(ex.getMessage(), ex); 201 } 202 String namespace = result.getNamespace(); 204 modules.put(namespace, result); 205 return result; 206 } 207 208 public XQueryModule loadModule(InputSource source, MetadataAccess metadata, SchemaManager sc) throws XQueryException { 209 return loadModule(this, source, metadata, sc, new VarCounter()); 210 } 211 public XQueryModule loadModule(String namespace, InputSource source, SchemaManager sc) throws XQueryException { 212 return loadModule(this, namespace, source, sc, new VarCounter()); 213 } 214 public XQueryModule loadModule(String namespace, InputSource source, SchemaManager sc, VarCounter varCounter) throws XQueryException { 215 return loadModule(this, namespace, source, sc, varCounter); 216 } 217 218 public XQueryModule loadModule(ModuleLocator locator, String namespace, URL location, SchemaManager sc) throws XQueryException { 219 return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, new VarCounter()); 220 } 221 public XQueryModule loadModule(ModuleLocator locator, String namespace, URL location, SchemaManager sc, VarCounter varCounter) throws XQueryException { 222 return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, varCounter); 223 } 224 225 public XQueryModule loadModule(String namespace, URL location, SchemaManager sc) throws XQueryException { 226 return loadModule(this, namespace, location, sc); 227 } 228 public XQueryModule loadModule(String namespace, URL location, SchemaManager sc, VarCounter varCounter) throws XQueryException { 229 return loadModule(this, namespace, location, sc, varCounter); 230 } 231 232 public XQueryModule loadModule(ModuleLocator locator, String namespace, String location, SchemaManager sc) throws XQueryException { 233 return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, new VarCounter()); 234 } 235 public XQueryModule loadModule(ModuleLocator locator, String namespace, String location, SchemaManager sc, VarCounter varCounter) throws XQueryException { 236 return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, varCounter); 237 } 238 239 public XQueryModule loadModule(String namespace, String location, SchemaManager sc) throws XQueryException { 240 return loadModule(this, namespace, location, sc); 241 } 242 public XQueryModule loadModule(String namespace, String location, SchemaManager sc, VarCounter varCounter) throws XQueryException { 243 return loadModule(this, namespace, location, sc, varCounter); 244 } 245 246 271 public boolean putModule(XQueryModule module) throws XQueryException { 272 if (isRegistered(module.getNamespace())) 273 return false; 274 modules.put(module.getNamespace(), module); 275 return true; 276 } 277 278 } 279 | Popular Tags |