From: Wolfgang M. M. <wol...@us...> - 2004-07-16 12:39:16
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/xquery In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1728/src/org/exist/xquery Modified Files: XQueryContext.java Log Message: Avoid loading modules multiple times. Index: XQueryContext.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/XQueryContext.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** XQueryContext.java 15 Jul 2004 19:43:23 -0000 1.19 --- XQueryContext.java 16 Jul 2004 12:39:04 -0000 1.20 *************** *** 402,409 **** * @param moduleClass */ ! public void loadBuiltInModule(String namespaceURI, String moduleClass) { ! if (modules.containsKey(namespaceURI)) { LOG.debug("module " + namespaceURI + " is already present"); ! return; } try { --- 402,410 ---- * @param moduleClass */ ! public Module loadBuiltInModule(String namespaceURI, String moduleClass) { ! Module module = (Module)modules.get(namespaceURI); ! if (module != null) { LOG.debug("module " + namespaceURI + " is already present"); ! return module; } try { *************** *** 414,423 **** + moduleClass + " is not an instance of org.exist.xquery.Module."); ! return; } ! Module module = (Module) mClass.newInstance(); if (!module.getNamespaceURI().equals(namespaceURI)) { LOG.warn("the module declares a different namespace URI. Skipping..."); ! return; } if (getPrefixForURI(module.getNamespaceURI()) == null --- 415,424 ---- + moduleClass + " is not an instance of org.exist.xquery.Module."); ! return null; } ! module = (Module) mClass.newInstance(); if (!module.getNamespaceURI().equals(namespaceURI)) { LOG.warn("the module declares a different namespace URI. Skipping..."); ! return null; } if (getPrefixForURI(module.getNamespaceURI()) == null *************** *** 434,437 **** --- 435,439 ---- LOG.warn("error while instantiating module class " + moduleClass, e); } + return module; } *************** *** 768,847 **** public void importModule(String namespaceURI, String prefix, String location) throws XPathException { ! if(location == null) ! location = namespaceURI; ! if(location.startsWith("java:")) { ! location = location.substring("java:".length()); ! loadBuiltInModule(namespaceURI, location); ! if(prefix != null) ! declareNamespace(prefix, namespaceURI); ! return; ! } ! if(location.indexOf(':') < 0) { ! File f = new File(moduleLoadPath + File.separatorChar + location); ! if(!f.canRead()) { ! f = new File(location); ! if(!f.canRead()) ! throw new XPathException("cannot read module source from file at " + f.getAbsolutePath()); ! } ! try { ! location = new URI(f.toURL().toString()).toASCIIString(); ! } catch (Exception e1) { ! } ! } ! LOG.debug("Loading module from " + location); ! InputStreamReader reader; ! try { ! URL url = new URL(location); ! reader = new InputStreamReader(url.openStream(), "UTF-8"); ! } catch (MalformedURLException e) { ! throw new XPathException("source location for module " + namespaceURI + " should be a valid URL"); ! } catch (UnsupportedEncodingException e) { ! throw new XPathException("unsupported source encoding"); ! } catch (IOException e) { ! throw new XPathException("IO exception while loading module " + namespaceURI, e); ! } ! XQueryContext context = new ModuleContext(this); ! XQueryLexer lexer = new XQueryLexer(reader); ! XQueryParser parser = new XQueryParser(lexer); ! XQueryTreeParser astParser = new XQueryTreeParser(context); ! try { ! parser.xpath(); ! if (parser.foundErrors()) { ! LOG.debug(parser.getErrorMessage()); ! throw new XPathException( ! "error found while loading module from " + location + ": " ! + parser.getErrorMessage()); ! } ! AST ast = parser.getAST(); ! ! PathExpr path = new PathExpr(context); ! astParser.xpath(ast, path); ! if (astParser.foundErrors()) { ! throw new XPathException( ! "error found while loading module from " + location + ": " ! + astParser.getErrorMessage(), ! astParser.getLastException()); } - - ExternalModule module = astParser.getModule(); - if(module == null) - throw new XPathException("source at " + location + " is not a valid module"); - if(!module.getNamespaceURI().equals(namespaceURI)) - throw new XPathException("namespace URI declared by module (" + module.getNamespaceURI() + - ") does not match namespace URI in import statement, which was: " + namespaceURI); - if(prefix == null) - prefix = module.getDefaultPrefix(); - declareNamespace(prefix, namespaceURI); - modules.put(module.getNamespaceURI(), module); - context.modules.put(module.getNamespaceURI(), module); - } catch (RecognitionException e) { - throw new XPathException( - "error found while loading module from " + location + ": " + e.getMessage(), - e); - } catch (TokenStreamException e) { - throw new XPathException( - "error found while loading module from " + location + ": " + e.getMessage(), - e); } } --- 770,852 ---- public void importModule(String namespaceURI, String prefix, String location) throws XPathException { ! Module module = (Module)modules.get(namespaceURI); ! if(module != null) { ! LOG.debug("Module " + namespaceURI + " already present."); ! } else { ! if(location == null) ! location = namespaceURI; ! // is it a Java module? ! if(location.startsWith("java:")) { ! location = location.substring("java:".length()); ! module = loadBuiltInModule(namespaceURI, location); ! } else { ! if(location.indexOf(':') < 0) { ! File f = new File(moduleLoadPath + File.separatorChar + location); ! if(!f.canRead()) { ! f = new File(location); ! if(!f.canRead()) ! throw new XPathException("cannot read module source from file at " + f.getAbsolutePath()); ! } ! try { ! location = new URI(f.toURL().toString()).toASCIIString(); ! } catch (Exception e1) { ! } ! } ! LOG.debug("Loading module from " + location); ! InputStreamReader reader; ! try { ! URL url = new URL(location); ! reader = new InputStreamReader(url.openStream(), "UTF-8"); ! } catch (MalformedURLException e) { ! throw new XPathException("source location for module " + namespaceURI + " should be a valid URL"); ! } catch (UnsupportedEncodingException e) { ! throw new XPathException("unsupported source encoding"); ! } catch (IOException e) { ! throw new XPathException("IO exception while loading module " + namespaceURI, e); ! } ! XQueryContext context = new ModuleContext(this); ! XQueryLexer lexer = new XQueryLexer(reader); ! XQueryParser parser = new XQueryParser(lexer); ! XQueryTreeParser astParser = new XQueryTreeParser(context); ! try { ! parser.xpath(); ! if (parser.foundErrors()) { ! LOG.debug(parser.getErrorMessage()); ! throw new XPathException( ! "error found while loading module from " + location + ": " ! + parser.getErrorMessage()); ! } ! AST ast = parser.getAST(); ! ! PathExpr path = new PathExpr(context); ! astParser.xpath(ast, path); ! if (astParser.foundErrors()) { ! throw new XPathException( ! "error found while loading module from " + location + ": " ! + astParser.getErrorMessage(), ! astParser.getLastException()); ! } ! ! module = astParser.getModule(); ! if(module == null) ! throw new XPathException("source at " + location + " is not a valid module"); ! if(!module.getNamespaceURI().equals(namespaceURI)) ! throw new XPathException("namespace URI declared by module (" + module.getNamespaceURI() + ! ") does not match namespace URI in import statement, which was: " + namespaceURI); ! modules.put(module.getNamespaceURI(), module); ! } catch (RecognitionException e) { ! throw new XPathException( ! "error found while loading module from " + location + ": " + e.getMessage(), ! e); ! } catch (TokenStreamException e) { ! throw new XPathException( ! "error found while loading module from " + location + ": " + e.getMessage(), ! e); ! } } } + if(prefix == null) + prefix = module.getDefaultPrefix(); + declareNamespace(prefix, namespaceURI); } |