sylvain 2003/01/08 03:04:53
Modified: src/java/org/apache/cocoon/components/source/impl
ContextSourceFactory.java
Log:
Fix bug #15279 (writeable source doesn't work with context: URLs)
Revision Changes Path
1.7 +27 -29
xml-cocoon2/src/java/org/apache/cocoon/components/source/impl/ContextSourceFactory.java
Index: ContextSourceFactory.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/impl/ContextSourceFactory.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ContextSourceFactory.java 26 Jun 2002 14:07:22 -0000 1.6
+++ ContextSourceFactory.java 8 Jan 2003 11:04:53 -0000 1.7
@@ -50,6 +50,7 @@
*/
package org.apache.cocoon.components.source.impl;
+import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
@@ -71,9 +72,12 @@
import org.apache.cocoon.environment.Context;
/**
- * A factory for the context protocol using the context of the servlet api
+ * A factory for the context protocol using the context of the servlet api. It
builds the
+ * source by asking the environment context for the real URL
+ * ({@see org.apache.cocoon.environment.Context#getResource(String)}) and then
resolving this real URL.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
+ * @author <a href="http://www.apache.org/~sylvain">Sylvain Wallez</a>
* @version $Id$
*/
public class ContextSourceFactory
@@ -82,7 +86,7 @@
{
/** The context */
- protected org.apache.avalon.framework.context.Context context;
+ protected Context envContext;
/** The component manager */
protected ComponentManager manager;
@@ -93,8 +97,9 @@
/**
* Composable Interface
*/
- public void compose(ComponentManager manager) {
+ public void compose(ComponentManager manager) throws ComponentException {
this.manager = manager;
+ this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE);
}
/**
@@ -112,7 +117,7 @@
*/
public void contextualize(org.apache.avalon.framework.context.Context context)
throws ContextException {
- this.context = context;
+ this.envContext =
(Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
}
/**
@@ -127,34 +132,27 @@
this.getLogger().debug( "Creating source object for " + location );
}
- Context envContext = null;
- try {
- envContext =
(Context)this.context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
- } catch (ContextException e){
- getLogger().error("ContextException in getURL",e);
- }
- final int pos = location.indexOf( "://" );
- location = location.substring( pos + 3 );
+ // Remove the protocol and the first '/'
+ int pos = location.indexOf(":/");
+ String path = location.substring(pos+1);
+
URL u;
- if (envContext == null) {
- this.getLogger().warn("no environment-context in application context
(making an absolute URL)");
- u = new URL(location);
+
+ // Try to get a file first and fall back to a resource URL
+ String actualPath = envContext.getRealPath(path);
+ if (actualPath != null) {
+ u = new File(actualPath).toURL();
} else {
- u = envContext.getResource("/" + location);
- }
- if ( u == null ) {
- this.getLogger().error(location + " could not be found. (possible
context problem)");
- throw new SourceNotFoundException(location + " could not be found.
(possible context problem)");
+ u = envContext.getResource(path);
}
- if (this.resolver == null) {
- try {
- this.resolver = (SourceResolver)this.manager.lookup(
SourceResolver.ROLE );
- } catch (ComponentException ce) {
- throw new SourceException("Unable to lookup source resolver.", ce);
- }
+ if (u != null) {
+ return this.resolver.resolveURI(u.toExternalForm());
+
+ } else {
+ String message = location + " could not be found. (possible context
problem)";
+ getLogger().info(message);
+ throw new MalformedURLException(message);
}
- return this.resolver.resolveURI( u.toExternalForm(), null, parameters);
}
-
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]