Author: chestnut
Date: Wed Sep 28 16:04:26 2005
New Revision: 292341
URL: http://svn.apache.org/viewcvs?rev=292341&view=rev
Log:
added template-fallback source factory (used template-fallback:// instead of
template:// in the example):
<quote
src="http://lenya.apache.org/1_4/reference/publication-templating/index.html">
To simplify overriding of XSLT stylesheets, it would be useful to import
the template stylesheet. For this purpose, an additional template source
factory could be used, which skips the current publication when resolving
the file:
<xsl:import href="template://template/xslt/common/header.xsl"/>
</quote>
Added:
lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/TemplateFallbackSourceFactory.java
(with props)
Modified:
lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl
Added:
lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/TemplateFallbackSourceFactory.java
URL:
http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/TemplateFallbackSourceFactory.java?rev=292341&view=auto
==============================================================================
---
lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/TemplateFallbackSourceFactory.java
(added)
+++
lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/TemplateFallbackSourceFactory.java
Wed Sep 28 16:04:26 2005
@@ -0,0 +1,206 @@
+/*
+ * 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.source;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Map;
+
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.SourceUtil;
+import org.apache.excalibur.source.URIAbsolutizer;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.PublicationManager;
+import org.apache.lenya.cms.publication.URLInformation;
+import org.apache.lenya.cms.publication.templating.ExistingSourceResolver;
+import org.apache.lenya.cms.publication.templating.PublicationTemplateManager;
+import org.apache.lenya.transaction.AbstractOperation;
+
+/**
+ * Source factory following the fallback principle.
+ *
+ * @version $Id: FallbackSourceFactory.java 264153 2005-08-29 15:11:14Z
andreas $
+ */
+public class TemplateFallbackSourceFactory extends AbstractOperation
implements SourceFactory, Serviceable,
+ Contextualizable, URIAbsolutizer {
+
+ /**
+ * Ctor.
+ */
+ public TemplateFallbackSourceFactory() {
+ super();
+ }
+
+ /**
+ * @see
org.apache.excalibur.source.SourceFactory#getSource(java.lang.String,
+ * java.util.Map)
+ */
+ public Source getSource(final String location, Map parameters) throws
IOException,
+ MalformedURLException {
+
+ String resolvedUri = null;
+
+ long startTime = new GregorianCalendar().getTimeInMillis();
+
+ // Remove the protocol and the first '//'
+ final int pos = location.indexOf("://");
+
+ if (pos == -1) {
+ throw new RuntimeException("The location [" + location
+ + "] does not contain the string '://'");
+ }
+
+ final String path = location.substring(pos + 3);
+
+ if (path.length() == 0) {
+ throw new RuntimeException("The path after the protocol must not
be empty!");
+ }
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Location: [" + location + "]");
+ getLogger().debug("Path: [" + path + "]");
+ }
+
+ PublicationManager pubMgr = null;
+ PublicationTemplateManager templateManager = null;
+ SourceResolver sourceResolver = null;
+ Source source;
+ try {
+ sourceResolver = (SourceResolver)
this.manager.lookup(SourceResolver.ROLE);
+
+ templateManager = (PublicationTemplateManager) this.manager
+ .lookup(PublicationTemplateManager.ROLE);
+
+ Request request = ContextHelper.getRequest(this.context);
+ String webappUrl =
request.getRequestURI().substring(request.getContextPath().length());
+
+ URLInformation info = new URLInformation(webappUrl);
+ String publicationId = info.getPublicationId();
+
+ pubMgr = (PublicationManager)
this.manager.lookup(PublicationManager.ROLE);
+ Publication pub = pubMgr.getPublication(publicationId);
+
+ //Get the template publication
+ String pubTemplateId = pub.getTemplateIds()[0];
+ if (pubTemplateId.length() > 0)
+ pub = pubMgr.getPublication(pubTemplateId);
+
+ if (pub.exists()) {
+ ExistingSourceResolver resolver = new ExistingSourceResolver();
+ templateManager.visit(pub, path, resolver);
+ resolvedUri = resolver.getURI();
+ }
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Resolved URI: [" + resolvedUri + "]");
+ }
+
+ if (resolvedUri == null) {
+ final String contextUri = "context://" +
location.substring("fallback://".length());
+ resolvedUri = contextUri;
+ }
+
+ source = sourceResolver.resolveURI(resolvedUri);
+
+ } catch (Exception e) {
+ throw new RuntimeException("Resolving path [" + location + "]
failed: ", e);
+ } finally {
+ if (templateManager != null) {
+ this.manager.release(templateManager);
+ }
+ if (pubMgr != null) {
+ this.manager.release(pubMgr);
+ }
+ if (sourceResolver != null) {
+ this.manager.release(sourceResolver);
+ }
+ }
+
+ if (getLogger().isDebugEnabled()) {
+ long endTime = new GregorianCalendar().getTimeInMillis();
+ long time = endTime - startTime;
+ getLogger().debug("Processing time: "
+ + new SimpleDateFormat("hh:mm:ss.S").format(new
Date(time)));
+ }
+
+ return source;
+ }
+
+ private org.apache.avalon.framework.context.Context context;
+
+ /** The ServiceManager */
+ private ServiceManager manager;
+
+ /**
+ * @see
org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+ */
+ public void service(ServiceManager _manager) throws ServiceException {
+ super.service(_manager);
+ this.manager = _manager;
+ }
+
+ /**
+ * @see
org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
+ */
+ public void contextualize(org.apache.avalon.framework.context.Context
_context)
+ throws ContextException {
+ this.context = _context;
+ }
+
+ /**
+ * @see
org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+ */
+ public void release(Source source) {
+ // In fact, this method should never be called as this factory
+ // returns a source object from a different factory. So that
+ // factory should release the source
+ if (null != source) {
+ if (this.getLogger().isDebugEnabled()) {
+ this.getLogger().debug("Releasing source " + source.getURI());
+ }
+ SourceResolver resolver = null;
+ try {
+ resolver = (SourceResolver)
this.manager.lookup(SourceResolver.ROLE);
+ resolver.release(source);
+ } catch (ServiceException ignore) {
+ // ignore the exception
+ } finally {
+ this.manager.release(resolver);
+ }
+ }
+ }
+
+ /**
+ * @see
org.apache.excalibur.source.URIAbsolutizer#absolutize(java.lang.String,
+ * java.lang.String)
+ */
+ public String absolutize(String baseURI, String location) {
+ return SourceUtil.absolutize(baseURI, location, true);
+ }
+}
\ No newline at end of file
Propchange:
lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/TemplateFallbackSourceFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl
URL:
http://svn.apache.org/viewcvs/lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl?rev=292341&r1=292340&r2=292341&view=diff
==============================================================================
--- lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl (original)
+++ lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl Wed Sep 28 16:04:26 2005
@@ -47,6 +47,7 @@
<xsl:apply-templates/>
<component-instance
class="org.apache.lenya.cms.cocoon.source.FallbackSourceFactory"
logger="lenya.source.fallback" name="fallback"/>
+ <component-instance
class="org.apache.lenya.cms.cocoon.source.TemplateFallbackSourceFactory"
logger="lenya.source.templatefallback" name="template-fallback"/>
<component-instance
class="org.apache.lenya.cms.cocoon.source.LenyaSourceFactory"
logger="lenya.source.lenya" name="lenya" scheme="context:"/>
</xsl:copy>
</xsl:template>
|