Return-Path: Mailing-List: contact cocoon-dev-help@xml.apache.org; run by ezmlm Delivered-To: mailing list cocoon-dev@xml.apache.org Received: (qmail 11727 invoked from network); 28 Oct 2000 22:51:41 -0000 Received: from adsl-63-200-48-176.dsl.snfc21.pacbell.net (HELO ce175879.cup.hp.com) (63.200.48.176) by locus.apache.org with SMTP; 28 Oct 2000 22:51:41 -0000 Received: (from ovidiu@localhost) by ce175879.cup.hp.com (8.9.3/8.9.3) id PAA28225; Sat, 28 Oct 2000 15:51:16 -0700 Message-Id: <200010282251.PAA28225@ce175879.cup.hp.com> X-Mailer: exmh version 2.2 06/23/2000 with nmh-1.0.4 From: Ovidiu Predescu To: cocoon-dev@xml.apache.org Subject: [PATCH] resource: URL handling (cleaned-up code) X-Url: http://www.geocities.com/SiliconValley/Monitor/7464/ X-Image-Url: http://www.geocities.com/SiliconValley/Monitor/7464/ovidiu.tiff X-Face: ?(@Y~qjBA}~8ZMh5gM4{Q{bE_*:sCJ3@Z?{B*Co=J!#8bb~-z?-0._vJjt~MM59!MjxG%>U 5>MW^2-\7~z04buszR^=m^U|m66>FdR@cFwhb;.A(8*D.QmLkK]z,md0'HiOE\pyeiv_PACR+P:Cm. wq_%l':E:q]g-UCc>r&s@BVo'kFN;(\9PF22Myg5w%nUBWQ6MJJ#qL#w>2oxckP'H:\$9F"mxsz]Dg k{1`fTcP'Y$CgGnc^paTV$dzhVX+;(U$;Eb)P<>G)g) Mime-Version: 1.0 Content-Type: multipart/mixed ; boundary="==_Exmh_-8159284680" Date: Sat, 28 Oct 2000 15:51:16 -0700 Sender: ovidiu@cup.hp.com X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N This is a multipart MIME message. --==_Exmh_-8159284680 Content-Type: text/plain; charset=us-ascii Hi, The following patch cleans up the way Cocoon handles the resource: URLs. This allows the resource: URL to be used everywhere as it is now a first class URL. To be able to use it, you need to add the following line in jserv.properties (for other servlet containers the setup should be similar): wrapper.bin.parameters=-Djava.protocol.handler.pkgs=org.apache.cocoon.protocol In particular with this modification one can include in an XSLT sheet, documents which are to be found inside jar files. Greetings, -- Ovidiu Predescu http://orion.nsr.hp.com/ (inside HP's firewall only) http://www.geocities.com/SiliconValley/Monitor/7464/ --==_Exmh_-8159284680 Content-Type: text/plain ; name="URL-handling.diff"; charset=us-ascii Content-Description: URL-handling.diff Content-Disposition: attachment; filename="URL-handling.diff" diff -ruN -x TAGS -x *docs* -x *CVS* -x .cvsignore -x *.jar Cocoon.orig/src/org/apache/cocoon/Utils.java cocoon/src/org/apache/cocoon/Utils.java --- Cocoon.orig/src/org/apache/cocoon/Utils.java Fri Jul 21 16:38:26 2000 +++ cocoon/src/org/apache/cocoon/Utils.java Tue Oct 24 20:38:05 2000 @@ -301,44 +301,24 @@ /* * Returns the resource pointed by the given location. */ - public static final Object getLocationResource(String location) throws MalformedURLException { - Object resource = null; - - if (location.indexOf("://") < 0) { - resource = new File(location); - } else if (location.startsWith("resource://")) { - // FIXME (SM): this should _not_ be system resource, but rather a resource of current classloader - resource = ClassLoader.getSystemResource(location.substring("resource://".length())); - } else { - resource = new URL(location); - } - - return resource; - } - - /* - * Returns the resource pointed by the given location relative to the given request. - */ - public static final Object getLocationResource(String location, HttpServletRequest request, ServletContext context) throws Exception { - Object resource = null; - - if (location.indexOf("://") < 0) { - if (location.charAt(0) == '/') { - // Location is relative to webserver's root - location = request.getRealPath(location); - } else { - // Location is relative to requested page's virtual directory - String basename = getBasename(request, context); - location = basename.substring(0, basename.lastIndexOf('/') + 1) + location; - } - resource = new File(location); - } else if (location.startsWith("resource://")) { - // FIXME (SM): this should _not_ be system resource, but rather a resource of current classloader - resource = ClassLoader.getSystemResource(location.substring("resource://".length())); - } else { - resource = new URL(location); - } - - return resource; - } + public static final URL getLocationResource(String location) + throws MalformedURLException + { + URL resource = new URL(location); + return resource; + } + + /* + * Returns the resource pointed by the given location relative to + * the given request. */ + public static final URL getLocationResource(String location, + HttpServletRequest request, + ServletContext context) + throws Exception + { + String basename = getBasename(request, context); + URL top = new URL("file:" + basename); + URL resource = new URL(top, location); + return resource; + } } diff -ruN -x TAGS -x *docs* -x *CVS* -x .cvsignore -x *.jar Cocoon.orig/src/org/apache/cocoon/cocoon.properties cocoon/src/org/apache/cocoon/cocoon.properties --- Cocoon.orig/src/org/apache/cocoon/cocoon.properties Mon Oct 2 13:44:43 2000 +++ cocoon/src/org/apache/cocoon/cocoon.properties Sat Oct 28 13:29:39 2000 @@ -142,22 +148,23 @@ # secret, we highly suggest that you protect the repository from untrusted # access, even read-only. Only Cocoon and the system administrators should # have access here. processor.xsp.repository = ./repository # Set the libraries associated with the given namespace. # Use the syntax: # processor.xsp.logicsheet.. = URL to file # where "URL to file" is usually starting with file:// if you locate # your custom library in your file system. -processor.xsp.logicsheet.context.java = resource://org/apache/cocoon/processor/xsp/library/java/context.xsl -processor.xsp.logicsheet.cookie.java = resource://org/apache/cocoon/processor/xsp/library/java/cookie.xsl -processor.xsp.logicsheet.global.java = resource://org/apache/cocoon/processor/xsp/library/java/global.xsl -processor.xsp.logicsheet.request.java = resource://org/apache/cocoon/processor/xsp/library/java/request.xsl -processor.xsp.logicsheet.response.java = resource://org/apache/cocoon/processor/xsp/library/java/response.xsl -processor.xsp.logicsheet.session.java = resource://org/apache/cocoon/processor/xsp/library/java/session.xsl -processor.xsp.logicsheet.util.java = resource://org/apache/cocoon/processor/xsp/library/java/util.xsl -processor.xsp.logicsheet.sql.java = resource://org/apache/cocoon/processor/xsp/library/sql/sql.xsl -processor.xsp.logicsheet.fp.java = resource://org/apache/cocoon/processor/xsp/library/fp/fp.xsl +processor.xsp.logicsheet.context.java = resource:/org/apache/cocoon/processor/xsp/library/java/context.xsl +processor.xsp.logicsheet.cookie.java = resource:/org/apache/cocoon/processor/xsp/library/java/cookie.xsl +processor.xsp.logicsheet.global.java = resource:/org/apache/cocoon/processor/xsp/library/java/global.xsl +processor.xsp.logicsheet.request.java = resource:/org/apache/cocoon/processor/xsp/library/java/request.xsl +processor.xsp.logicsheet.response.java = resource:/org/apache/cocoon/processor/xsp/library/java/response.xsl +processor.xsp.logicsheet.session.java = resource:/org/apache/cocoon/processor/xsp/library/java/session.xsl +processor.xsp.logicsheet.util.java = resource:/org/apache/cocoon/processor/xsp/library/java/util.xsl +processor.xsp.logicsheet.sql.java = resource:/org/apache/cocoon/processor/xsp/library/sql/sql.xsl +processor.xsp.logicsheet.fp.java = resource:/org/apache/cocoon/processor/xsp/library/fp/fp.xsl # Turbine DB Connection Pool ############################ @@ -173,14 +180,13 @@ # These are the supported jdbc-drivers and their adaptors. # These properties are used by the DBFactory. processor.xsp.pool.database.adaptor=DBWeblogic,DBOracle,DBInstantDB,DBPostgres,DBSybase,DBInformix,DBMySQL processor.xsp.pool.database.adaptor.DBWeblogic=weblogic.jdbc.pool.Driver processor.xsp.pool.database.adaptor.DBOracle=oracle.jdbc.driver.OracleDriver processor.xsp.pool.database.adaptor.DBInstantDB=org.enhydra.instantdb.jdbc.idbDriver processor.xsp.pool.database.adaptor.DBPostgres=postgresql.Driver processor.xsp.pool.database.adaptor.DBInformix=com.informix.jdbc.IfxDriver processor.xsp.pool.database.adaptor.DBSybase=com.sybase.jdbc.SybDriver processor.xsp.pool.database.adaptor.DBMySQL=org.gjt.mm.mysql.Driver # The full path name to a pool log file # if not given, commands to log events using org.apache.turbine.util.Log will be ignored. diff -ruN -x TAGS -x *docs* -x *CVS* -x .cvsignore -x *.jar Cocoon.orig/src/org/apache/cocoon/parser/XercesParser.java cocoon/src/org/apache/cocoon/parser/XercesParser.java --- Cocoon.orig/src/org/apache/cocoon/parser/XercesParser.java Sun Oct 15 15:51:31 2000 +++ cocoon/src/org/apache/cocoon/parser/XercesParser.java Thu Oct 5 16:23:57 2000 @@ -93,8 +92,10 @@ String id = input.getSystemId(); if (id != null) { id = id.replace('\\','/'); - if ((id.indexOf("://") < 0) && (!id.startsWith("file:"))) { - id = "file:" + id; + if (id.indexOf(":") < 0) { + /* The system id has no protocol, assume it refers to a + file */ + id = "file:" + id; } input.setSystemId(id); } public interface QueryCreator { diff -ruN -x TAGS -x *docs* -x *CVS* -x .cvsignore -x *.jar Cocoon.orig/src/org/apache/cocoon/processor/xslt/XSLTProcessor.java cocoon/src/org/apache/cocoon/processor/xslt/XSLTProcessor.java --- Cocoon.orig/src/org/apache/cocoon/processor/xslt/XSLTProcessor.java Fri Jul 21 16:55:04 2000 +++ cocoon/src/org/apache/cocoon/processor/xslt/XSLTProcessor.java Tue Oct 24 20:40:37 2000 @@ -100,11 +103,14 @@ HttpServletRequest request = (HttpServletRequest) parameters.get("request"); ServletContext context = (ServletContext) parameters.get("context"); String path = (String) parameters.get("path"); String browser = (String) parameters.get("browser"); Hashtable params = this.filterParameters(request); try { - Object resource = getResource(context, request, document, path, browser); + URL resource = getResource(context, request, document, path, browserMedia); Document stylesheet = getStylesheet(resource, request); Document result = this.parser.createEmptyDocument(); return transformer.transform(document, null, stylesheet, resource.toString(), result, params); @@ -150,32 +156,31 @@ return params; } - private Object getResource(ServletContext context, HttpServletRequest request, Document document, String path, String browser) throws ProcessorException { + private URL getResource(ServletContext context, HttpServletRequest request, Document document, String path, String browser) throws ProcessorException { - Object resource = null; + URL resource = null; Enumeration pis = Utils.getAllPIs(document, STYLESHEET_PI).elements(); while (pis.hasMoreElements()) { - Hashtable attributes = Utils.getPIPseudoAttributes((ProcessingInstruction) pis.nextElement()); + Hashtable attributes + = Utils.getPIPseudoAttributes((ProcessingInstruction) + pis.nextElement()); String type = (String) attributes.get("type"); if ((type != null) && (type.equals("text/xsl"))) { String url = (String) attributes.get("href"); if (url != null) { - Object local = null; + URL top = null, local = null; - try { - if (url.charAt(0) == '/') { - local = new File(Utils.getRootpath(request, context) + url); - } else if (url.indexOf("://") < 0) { - local = new File(Utils.getBasepath(request, context) + url); - } else { - local = new URL(url); - } - } catch (MalformedURLException e) { - throw new ProcessorException("Could not associate stylesheet to document: " - + url + " is a malformed URL."); - } + try { + top = new URL("file:" + path); + local = new URL(top, url); + } + catch (MalformedURLException e) { + throw new ProcessorException("Could not associate " + + "stylesheet to document: " + + url+" is a malformed URL."); + } String media = (String) attributes.get("media"); @@ -200,7 +205,7 @@ } } - private Document getStylesheet(Object resource, HttpServletRequest request) throws ProcessorException { + private Document getStylesheet(URL resource, HttpServletRequest request) throws ProcessorException { try { Object o = this.store.get(resource); @@ -219,8 +224,10 @@ } } - private Document getDocument(Object resource) throws Exception { - return this.parser.parse(new InputSource(resource.toString()), false); // do not validate stylesheets + private Document getDocument(URL resource) throws Exception { + InputSource input = new InputSource(resource.openStream()); + input.setSystemId(resource.toString()); + return this.parser.parse(input, false); // do not validate stylesheets } public boolean hasChanged(Object context) { diff -ruN -x TAGS -x *docs* -x *CVS* -x .cvsignore -x *.jar Cocoon/build/src/org/apache/cocoon/protocol/resource/Handler.java mobilesdk/cocoon/build/src/org/apache/cocoon/protocol/resource/Handler.java --- Cocoon/build/src/org/apache/cocoon/protocol/resource/Handler.java Wed Dec 31 16:00:00 1969 +++ mobilesdk/cocoon/build/src/org/apache/cocoon/protocol/resource/Handler.java Sat Oct 28 15:42:02 2000 @@ -0,0 +1,96 @@ +/* + Handler.java + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999-2000 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "Cocoon" and "Apache Software Foundation" must not be used to + endorse or promote products derived from this software without prior + written permission. For written permission, please contact + apache@apache.org. + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many + individuals on behalf of the Apache Software Foundation and was + originally based on software Copyright (c) 2000, Hewlett-Packard + Company (HP), http://www.hp.com/e-speak/. For more information on the + Apache Software Foundation, please see . + + */ + +package org.apache.cocoon.protocol.resource; + +import java.net.*; +import java.io.IOException; +import org.apache.cocoon.processor.xsp.library.espeak.Utils; +import java.io.FileNotFoundException; + +/* This class implements the handler for the resource: URL + + @author: Ovidiu Predescu + Date: September 20, 2000 +*/ +public class Handler extends URLStreamHandler +{ + public Handler() + { + super(); + } + + public URLConnection openConnection(URL url) + throws IOException + { + ClassLoader loader = this.getClass().getClassLoader(); + if (loader == null) + loader = ClassLoader.getSystemClassLoader(); + + String file = url.getFile(); + + /* Remove all the forward slashes at the beginning of the filename */ + if (file.charAt(0) == '/') + file = file.substring(1); + + URL resource = loader.getResource(file); + + URLConnection connection = null; + if (resource != null) + connection = resource.openConnection(); + else + throw new FileNotFoundException(file); + + return connection; + } +} diff -ruN -x TAGS -x *docs* -x *CVS* -x .cvsignore -x *.jar Cocoon.orig/src/org/apache/cocoon/processor/xsp/XSPProcessor.java cocoon/src/org/apache/cocoon/processor/xsp/XSPProcessor.java --- Cocoon.orig/src/org/apache/cocoon/processor/xsp/XSPProcessor.java Mon Oct 23 19:20:48 2000 +++ cocoon/src/org/apache/cocoon/processor/xsp/XSPProcessor.java Tue Oct 24 20:38:06 2000 @@ -258,8 +244,9 @@ if (byLanguage == null) byLanguage = new Hashtable(1); try { - Object resource = Utils.getLocationResource(location); - if (resource == null) throw new Exception("Resource not found or retrieving error."); + URL resource = Utils.getLocationResource(location); + if (resource == null) + throw new Exception("Resource not found or retrieving error."); XSPPreprocessor preprocessor = null; String preprocessorName = @@ -528,15 +515,16 @@ return page; } - private void refreshLogicsheet(Object resource) throws Exception { + private void refreshLogicsheet(URL resource) throws Exception { this.refreshLogicsheet(resource, null); } // FIXME: A common class with XSLProcessor? - private void refreshLogicsheet(Object resource, XSPPreprocessor preprocessor) + private void refreshLogicsheet(URL resource, XSPPreprocessor preprocessor) throws Exception { - String name = resource.toString(); + String name = resource.toString(); + XSPLogicsheet logicsheet = (XSPLogicsheet) this.store.get(name); // Parse logicsheet @@ -574,7 +562,7 @@ (XSPPreprocessor) Class.forName(preprocessorName).newInstance(); } - Object resource = Utils.getLocationResource(location, request, servletContext); + URL resource = Utils.getLocationResource(location, request, servletContext); this.refreshLogicsheet(resource, preprocessor); @@ -593,23 +581,10 @@ } // FIXME: Utils.java: return InputStream (getDocumentStream) - private Document getDocument(Object resource) throws Exception { - InputSource input = new InputSource(); + private Document getDocument(URL resource) throws Exception { + InputSource input = new InputSource(resource.openStream()); input.setSystemId(resource.toString()); - if (resource instanceof File) { - input.setCharacterStream(new FileReader(((File) resource))); - } else if (resource instanceof URL) { - input.setCharacterStream( - new InputStreamReader(((URL) resource).openStream()) - ); - } else { - // should never happen - throw new Error( - "Fatal error: Could not elaborate given resource: " + resource - ); - } - // do not validate stylesheets return this.parser.parse(input, false); } @@ -666,7 +641,7 @@ int logicsheetCount = this.logicsheets.size(); for (int i = 0; i < logicsheetCount; i++) { - Object resource = this.logicsheets.elementAt(i); + URL resource = (URL)this.logicsheets.elementAt(i); Object object = store.get(resource.toString()); if (object == null) { @@ -691,7 +666,7 @@ int changeCount = 0; int logicsheetCount = this.logicsheets.size(); for (int i = 0; i < logicsheetCount; i++) { - Object resource = this.logicsheets.elementAt(i); + URL resource = (URL)this.logicsheets.elementAt(i); if ( monitor.hasChanged(resource.toString()) || --==_Exmh_-8159284680--