You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2003/12/12 16:15:19 UTC
cvs commit: cocoon-2.1/src/java/org/apache/cocoon/selection ResourceExistsSelector.java
vgritsenko 2003/12/12 07:15:19
Modified: . status.xml
src/java/org/apache/cocoon/selection
ResourceExistsSelector.java
Log:
update resource exists selector to match resource exists action functionality
Revision Changes Path
1.212 +11 -5 cocoon-2.1/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/cocoon-2.1/status.xml,v
retrieving revision 1.211
retrieving revision 1.212
diff -u -r1.211 -r1.212
--- status.xml 11 Dec 2003 21:25:12 -0000 1.211
+++ status.xml 12 Dec 2003 15:15:19 -0000 1.212
@@ -189,13 +189,19 @@
<changes>
<release version="@version@" date="@date@">
+ <action dev="VG" type="update">
+ ResourceExistsSelector now works the same way as ResourceExistsAction does.
+ It can now detect existence of any Cocoon resource, not only files in context.
+ Default value for prefix was changed from '/' to ''. For old behavior, use prefix
+ 'context://'.
+ </action>
<action dev="SW" type="update">
- Fixed and updated the stores and made them instrumentable to follow their size and
- the number of hits and missed.
+ Fixed and updated the stores and made them instrumentable to follow their size and
+ the number of hits and missed.
</action>
<action dev="SW" type="add">
- New PipelineUtil class that extends the features or <code>cocoon.processPipelineTo()</code>
- to SAX and DOM outputs.
+ New PipelineUtil class that extends the features or <code>cocoon.processPipelineTo()</code>
+ to SAX and DOM outputs.
</action>
<action dev="SW" type="add">
The "cocoon" object in flowscript has two new methods that allow Java classes
1.3 +63 -54 cocoon-2.1/src/java/org/apache/cocoon/selection/ResourceExistsSelector.java
Index: ResourceExistsSelector.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/selection/ResourceExistsSelector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ResourceExistsSelector.java 20 Mar 2003 12:32:18 -0000 1.2
+++ ResourceExistsSelector.java 12 Dec 2003 15:15:19 -0000 1.3
@@ -50,91 +50,100 @@
*/
package org.apache.cocoon.selection;
-import java.net.MalformedURLException;
-import java.net.URL;
import java.util.Map;
+import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.cocoon.environment.Context;
-import org.apache.cocoon.environment.ObjectModelHelper;
-
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceResolver;
/**
- * Selects the first of a set of Resources (usually files) that exists in the
- * context.
- * <p>
- * The 'test' expression is interpreted as a context-rooted ('/' = context)
- * path, resolved by the servlet container, <em>not</em> a Source.
+ * Selects the first of a set of Resources (usually files) that exists.
+ *
* <p>
- * A parameter,
+ * A parameter 'prefix',
* <pre>
- * <map:parameter src="prefix" value="<code>/</code>"/>
+ * <map:parameter src="prefix" value="<code>some/path</code>"/<
* </pre>
* may be supplied to the selector instance. This prefix is prepended to all
- * test expressions before evaluation. The default prefix is '<code>/</code>',
- * meaning that all expressions are context root-relative, unless explicitly
- * overridden.
+ * test expressions before evaluation. The default prefix is '' (empty string),
+ * meaning that all expressions are relative to the current sitemap, unless
+ * explicitly overridden.
+ *
+ * <p><b>NOTE:</b>
+ * Provided resource URI is resolved as Source, relative to the current
+ * sitemap, which differs from behavior of selector in previous versions.
+ * To resolve resource paths relative to the context root, provide prefix
+ * parameter:
+ * <pre>
+ * <map:parameter name="prefix" value="context://"/<
+ * </pre>
+ *
* <p>
* For example, we could define a ResourceExistsSelector with:
* <pre>
* <map:selector name="resource-exists"
* logger="sitemap.selector.resource-exists"
- * src="org.apache.cocoon.selection.ResourceExistsSelector" />
+ * src="org.apache.cocoon.selection.ResourceExistsSelector" /<
* </pre>
* And use it to build a PDF from XSL:FO or a higher-level XML format with:
*
* <pre>
- * <map:match pattern="**.pdf">
- * <map:select type="resource-exists">
- * <map:when test="context/xdocs/{1}.fo">
- * <map:generate src="content/xdocs/{1}.fo" />
- * </map:when>
- * <map:otherwise>
- * <map:generate src="content/xdocs/{1}.xml" />
- * <map:transform src="stylesheets/document2fo.xsl" />
- * </map:otherwise>
- * </map:select>
- * <map:serialize type="fo2pdf" />
+ * <map:match pattern="**.pdf"<
+ * <map:select type="resource-exists"<
+ * <map:when test="context/xdocs/{1}.fo"<
+ * <map:generate src="content/xdocs/{1}.fo" /<
+ * </map:when<
+ * <map:otherwise<
+ * <map:generate src="content/xdocs/{1}.xml" /<
+ * <map:transform src="stylesheets/document2fo.xsl" /<
+ * </map:otherwise<
+ * </map:select<
+ * <map:serialize type="fo2pdf" /<
* </pre>
*
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
* @version CVS $Id$
*/
public class ResourceExistsSelector extends AbstractLogEnabled
- implements ThreadSafe, Selector {
+ implements ThreadSafe, Serviceable, Disposable, Selector {
+ private ServiceManager manager;
+ private SourceResolver resolver;
+
+ public void service(ServiceManager manager) throws ServiceException {
+ this.manager = manager;
+ this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
+ }
+
+ public void dispose() {
+ this.manager.release(this.resolver);
+ this.resolver = null;
+ this.manager = null;
+ }
+
public boolean select(String expression, Map objectModel, Parameters parameters) {
-
- Context context = ObjectModelHelper.getContext(objectModel);
- URL url = null;
- expression = parameters.getParameter("prefix", "/") + expression;
+ String resourceURI = parameters.getParameter("prefix", "") + expression;
+ Source source = null;
try {
- url = context.getResource(expression);
- if (url == null) {
- return false;
- } else { return true; }
- } catch (MalformedURLException e) {
- getLogger().warn("Selector expression '"+expression+"' is not a valid URL");
+ source = resolver.resolveURI(resourceURI);
+ return source.exists();
+ } catch (SourceNotFoundException e) {
return false;
- }
-
- /* While the servlet Javadocs state that getResource should return null
- * if a resource doesn't exist, early versions of Tomcat didn't respect
- * this. If this turns to be an issue with other containers, remove
- * the 'else return true' above and uncomment this code. (JT)
- InputStream is = null;
- try {
- is = url.openStream();
- return true;
- } catch (IOException e) {
+ } catch (Exception e) {
+ getLogger().warn("Exception resolving resource " + resourceURI, e);
return false;
} finally {
- try {
- if (is != null) is.close();
- } catch (IOException e) {}
+ if (source != null) {
+ resolver.release(source);
+ }
}
- */
}
}