Sprint hacks:

LenyaSourceFactory and lenya protocol integration


git-svn-id: https://svn.apache.org/repos/asf/incubator/lenya/branches/Lenya_Sprint_2004_5_13-branch@43010 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/lenya/cms/cocoon/components/source/impl/LenyaSourceFactory.java b/src/java/org/apache/lenya/cms/cocoon/components/source/impl/LenyaSourceFactory.java
new file mode 100644
index 0000000..3f42c8e
--- /dev/null
+++ b/src/java/org/apache/lenya/cms/cocoon/components/source/impl/LenyaSourceFactory.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lenya.cms.cocoon.components.source.impl;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+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.components.ContextHelper;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.cms.publication.PageEnvelope;
+import org.apache.lenya.cms.publication.PageEnvelopeException;
+import org.apache.lenya.cms.publication.PageEnvelopeFactory;
+import org.apache.lenya.cms.publication.Publication;
+
+
+/**
+ * A factory for the lenya protocol.
+ *
+ * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
+ * @version $$
+ */
+public class LenyaSourceFactory 
+    extends AbstractLogEnabled 
+    implements SourceFactory, ThreadSafe, Contextualizable, Serviceable, Configurable {
+    
+    protected static final String SCHEME = "lenya:";
+    
+    /** fallback if no configuration is available */
+    protected static final String DEFAULT_DELEGATION_SCHEME = "context:";    
+    protected static final String DEFAULT_DELEGATION_PREFIX = "/" + Publication.PUBLICATION_PREFIX_URI;
+    
+    private Context context;
+    private ServiceManager manager;
+    private SourceResolver sourceResolver;
+    private String delegationScheme;
+    private String delegationPrefix;
+    
+    /**
+     * Contextualizable, get the object model
+     */
+    public void contextualize( Context context ) throws ContextException {
+        this.context = context;
+    }    
+    
+    /**
+     * Lookup the SlideRepository.
+     * 
+     * @param manager ServiceManager.
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        //this.sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+        this.manager = manager;
+    }
+    
+    public void configure(Configuration configuration)
+        throws ConfigurationException {
+        this.delegationScheme = configuration.getAttribute("scheme", DEFAULT_DELEGATION_SCHEME);
+        this.delegationPrefix = configuration.getAttribute("prefix", DEFAULT_DELEGATION_PREFIX);
+    }    
+
+    /**
+     * Get a <code>Source</code> object.
+     * @param parameters This is optional.
+     */
+    public Source getSource(final String location, final Map parameters)
+        throws MalformedURLException, IOException, SourceException {
+        
+        try {
+            this.sourceResolver = (SourceResolver) manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);
+        } catch (ServiceException e) {
+            throw new SourceException(e.getMessage());
+        }
+        
+        String path = location.substring(SCHEME.length());
+        
+        if (!path.startsWith("//")) {
+            
+            Map objectModel = ContextHelper.getObjectModel( this.context );
+            try {
+                PageEnvelopeFactory pageEnvelopeFactory = PageEnvelopeFactory.getInstance(); 
+                
+                if (pageEnvelopeFactory != null) {
+                    PageEnvelope pageEnvelope = pageEnvelopeFactory.getPageEnvelope(objectModel);
+                
+                    if (pageEnvelope != null) {
+                        String publicationID = pageEnvelope.getPublication().getId();
+                        String area = pageEnvelope.getDocument().getArea();
+                        path = "/" + publicationID + "/" + Publication.CONTENT_PATH + "/" + area + path;
+                        
+                    }
+                }                
+            } catch (PageEnvelopeException e) {
+                throw new SourceException("Cannot attach publication-id and/or area to "+path, e);
+            }
+        }
+        
+        path = this.delegationScheme + this.delegationPrefix + path;
+        
+        return sourceResolver.resolveURI(path);
+    }
+    
+    public void release(Source source) {
+        // do nothing beacuse the deligated factory does this.
+    }
+  
+    
+}
diff --git a/src/webapp/WEB-INF/cocoon-xconf.xsl b/src/webapp/WEB-INF/cocoon-xconf.xsl
index e13065e..773cf0b 100644
--- a/src/webapp/WEB-INF/cocoon-xconf.xsl
+++ b/src/webapp/WEB-INF/cocoon-xconf.xsl
@@ -15,7 +15,7 @@
   limitations under the License.
 -->
 
-<!-- $Id: cocoon-xconf.xsl,v 1.44 2004/04/22 10:01:31 gregor Exp $ -->
+<!-- $Id: cocoon-xconf.xsl,v 1.44.2.1 2004/05/16 10:45:33 roku Exp $ -->
 
 <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
@@ -106,6 +106,21 @@
   </xsl:copy>
 </xsl:template>
 
+<xsl:template match="source-factories">
+     
+  <xsl:copy>
+    <xsl:copy-of select="@*"/>
+    <xsl:apply-templates/>
+    
+    <component-instance
+        class="org.apache.lenya.cms.cocoon.components.source.impl.LenyaSourceFactory"
+        logger="lenya.source"
+        name="lenya"
+        scheme="context:"/>
+
+  </xsl:copy>
+</xsl:template>
+
 <xsl:template match="cocoon">
   <xsl:copy>
   <xsl:copy-of select="@*"/>
diff --git a/src/webapp/lenya/pubs/default/doctypes.xmap b/src/webapp/lenya/pubs/default/doctypes.xmap
index e924d03..6470e15 100644
--- a/src/webapp/lenya/pubs/default/doctypes.xmap
+++ b/src/webapp/lenya/pubs/default/doctypes.xmap
@@ -15,7 +15,7 @@
   limitations under the License.
 -->
 
-<!-- $Id: doctypes.xmap,v 1.17 2004/03/24 13:20:08 edith Exp $ -->
+<!-- $Id: doctypes.xmap,v 1.17.2.1 2004/05/16 10:45:33 roku Exp $ -->
 
 <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
 
@@ -49,7 +49,8 @@
       <!-- parametrized doctype matcher -->
       <!-- pattern="{rendertype}/{area}/{doctype}/{document-path}" -->
       <map:match pattern="*/*/*/**.xml">
-        <map:generate src="content/{2}/{4}.xml"/>
+        <!--map:generate src="content/{2}/{4}.xml"/-->
+        <map:generate src="lenya:/{4}.xml"/>        
         <map:transform src="xslt/{3}2xhtml.xsl">
           <map:parameter name="rendertype" value="{1}"/>
           <map:parameter name="nodeid" value="{page-envelope:document-node-id}"/>