You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2005/04/08 21:20:42 UTC

svn commit: r160590 - in lenya/trunk/src: java/org/apache/lenya/ java/org/apache/lenya/cms/publication/ java/org/apache/lenya/cms/publication/templating/ java/org/apache/lenya/cms/publication/usecases/ webapp/WEB-INF/ webapp/lenya/pubs/blog/config/ webapp/lenya/pubs/default/config/ webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/ webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/

Author: andreas
Date: Fri Apr  8 12:20:39 2005
New Revision: 160590

URL: http://svn.apache.org/viewcvs?view=rev&rev=160590
Log:
introduced Instantiator interface to create instances of publication templates

Added:
    lenya/trunk/src/java/org/apache/lenya/cms/publication/templating/Instantiator.java
    lenya/trunk/src/webapp/lenya/pubs/default/config/instantiator.xconf
    lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/
    lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/
    lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/Instantiator.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/publication/Publication.java
    lenya/trunk/src/java/org/apache/lenya/cms/publication/PublicationImpl.java
    lenya/trunk/src/java/org/apache/lenya/cms/publication/usecases/CreatePublicationFromTemplate.java
    lenya/trunk/src/java/org/apache/lenya/lenya.roles
    lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl
    lenya/trunk/src/webapp/lenya/pubs/blog/config/publication.xconf
    lenya/trunk/src/webapp/lenya/pubs/default/config/publication.xconf

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/Publication.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/Publication.java?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/Publication.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/Publication.java Fri Apr  8 12:20:39 2005
@@ -69,7 +69,7 @@
     /**
      * <code>PENDING_PATH</code> The pending path
      */
-    String PENDING_PATH = "pending";    
+    String PENDING_PATH = "pending";
     /**
      * <code>DELETE_PATH</code> The delete path
      */
@@ -90,12 +90,12 @@
     String getId();
 
     /**
-     * Returns the servlet context this publication belongs to
-     * (usually, the <code>webapps/lenya</code> directory).
+     * Returns the servlet context this publication belongs to (usually, the
+     * <code>webapps/lenya</code> directory).
      * @return A <code>File</code> object.
      */
     File getServletContext();
-    
+
     /**
      * @return if this publication exists.
      */
@@ -110,7 +110,7 @@
     /**
      * Return the directory of a specific area.
      * @param area a <code>File</code> representing the root of the area content directory.
-     * @return the directory of the given content area. 
+     * @return the directory of the given content area.
      */
     File getContentDirectory(String area);
 
@@ -145,7 +145,8 @@
     String[] getLanguages();
 
     /**
-     * Get the breadcrumb prefix. It can be used as a prefix if a publication is part of a larger site
+     * Get the breadcrumb prefix. It can be used as a prefix if a publication is part of a larger
+     * site
      * @return the breadcrumb prefix
      */
     String getBreadcrumbPrefix();
@@ -158,27 +159,28 @@
 
     /**
      * Returns the document builder class of this instance.
-     * @return A class.
+     * @return A hint to use for service selection.
      */
     String getDocumentBuilderHint();
-    
+
     /**
-     * Returns the proxy which is used for a particular document. 
+     * Returns the publication template instantiator hint. If the publication does not allow
+     * templating, <code>null</code> is returned.
+     * @return A hint to use for service selection.
+     */
+    String getInstantiatorHint();
+
+    /**
+     * Returns the proxy which is used for a particular document.
      * @param document The document.
      * @param isSslProtected A boolean value.
-     * @return A proxy or <code>null</code> if no proxy is defined
-     * for this version.
+     * @return A proxy or <code>null</code> if no proxy is defined for this version.
      */
     Proxy getProxy(Document document, boolean isSslProtected);
-    
+
     /**
      * @return The templates of the publication.
      */
     Publication[] getTemplates();
-    
-    /**
-     * @return if the publication can be used as a template.
-     */
-    boolean supportsTemplating();
-    
+
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/PublicationImpl.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/PublicationImpl.java?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/PublicationImpl.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/PublicationImpl.java Fri Apr  8 12:20:39 2005
@@ -46,6 +46,7 @@
     private ArrayList languages = new ArrayList();
     private String defaultLanguage = null;
     private String breadcrumbprefix = null;
+    private String instantiatorHint = null;
 
     private static final String ELEMENT_PROXY = "proxy";
     private static final String ATTRIBUTE_AREA = "area";
@@ -67,7 +68,7 @@
     private static final String ELEMENT_DOCUMENT_BUILDER = "document-builder";
     private static final String ELEMENT_SITE_MANAGER = "site-manager";
     private static final String ATTRIBUTE_NAME = "name";
-    private static final String ATTRIBUTE_SUPPORTS_TEMPLATING = "supports-templating";
+    private static final String ELEMENT_TEMPLATE_INSTANTIATOR = "template-instantiator";
     private static final String LANGUAGES = "languages";
     private static final String DEFAULT_LANGUAGE_ATTR = "default";
     private static final String BREADCRUMB_PREFIX = "breadcrumb-prefix";
@@ -178,8 +179,12 @@
                 }
             }
 
-            this.supportsTemplating = config
-                    .getAttributeAsBoolean(PublicationImpl.ATTRIBUTE_SUPPORTS_TEMPLATING, false);
+            Configuration templateInstantiatorConfig = config
+                    .getChild(ELEMENT_TEMPLATE_INSTANTIATOR, false);
+            if (templateInstantiatorConfig != null) {
+                this.instantiatorHint = templateInstantiatorConfig
+                        .getAttribute(PublicationImpl.ATTRIBUTE_NAME);
+            }
 
         } catch (final Exception e) {
             throw new RuntimeException("Problem with config file: " + configFile.getAbsolutePath(),
@@ -384,21 +389,19 @@
         return (Publication[]) list.toArray(new Publication[list.size()]);
     }
 
-    private boolean supportsTemplating = false;
-
     /**
-     * @see org.apache.lenya.cms.publication.Publication#supportsTemplating()
+     * @see org.apache.lenya.cms.publication.Publication#getSiteManagerHint()
      */
-    public boolean supportsTemplating() {
+    public String getSiteManagerHint() {
         loadConfiguration();
-        return this.supportsTemplating;
+        return this.siteManagerName;
     }
 
     /**
-     * @see org.apache.lenya.cms.publication.Publication#getSiteManagerHint()
+     * @see org.apache.lenya.cms.publication.Publication#getInstantiatorHint()
      */
-    public String getSiteManagerHint() {
+    public String getInstantiatorHint() {
         loadConfiguration();
-        return this.siteManagerName;
+        return this.instantiatorHint;
     }
 }

Added: lenya/trunk/src/java/org/apache/lenya/cms/publication/templating/Instantiator.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/templating/Instantiator.java?view=auto&rev=160590
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/templating/Instantiator.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/templating/Instantiator.java Fri Apr  8 12:20:39 2005
@@ -0,0 +1,43 @@
+/*
+ * 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.publication.templating;
+
+import org.apache.lenya.cms.publication.Publication;
+
+/**
+ * Object to create an instance of a templating-enabled publication.
+ * 
+ * @version $Id:$
+ */
+public interface Instantiator {
+
+    /**
+     * The Avalon role.
+     */
+    String ROLE = Instantiator.class.getName();
+
+    /**
+     * Instantiate a publication.
+     * @param templatePublication The template publication.
+     * @param newPublicationId The ID of the new publication instance.
+     * @param newPublicationName The name of the new publication.
+     * @throws Exception if an error occurs.
+     */
+    void instantiate(Publication templatePublication, String newPublicationId,
+            String newPublicationName) throws Exception;
+
+}
\ No newline at end of file

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/usecases/CreatePublicationFromTemplate.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/usecases/CreatePublicationFromTemplate.java?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/usecases/CreatePublicationFromTemplate.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/usecases/CreatePublicationFromTemplate.java Fri Apr  8 12:20:39 2005
@@ -16,32 +16,18 @@
  */
 package org.apache.lenya.cms.publication.usecases;
 
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-
-import org.apache.avalon.framework.configuration.DefaultConfiguration;
-import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
-import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
-import org.apache.excalibur.source.ModifiableSource;
+import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.SourceUtil;
 import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.publication.PublicationException;
 import org.apache.lenya.cms.publication.PublicationFactory;
-import org.apache.lenya.cms.publication.PublicationImpl;
+import org.apache.lenya.cms.publication.templating.Instantiator;
 import org.apache.lenya.cms.usecase.AbstractUsecase;
-import org.apache.lenya.xml.DocumentHelper;
-import org.apache.lenya.xml.NamespaceHelper;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 
 /**
  * Create a new publication based on a template publication.
@@ -66,7 +52,7 @@
             Publication[] pubs = factory.getPublications(this.manager);
             List templates = new ArrayList();
             for (int i = 0; i < pubs.length; i++) {
-                if (pubs[i].supportsTemplating()) {
+                if (pubs[i].getInstantiatorHint() != null) {
                     templates.add(pubs[i].getId());
                 }
             }
@@ -109,6 +95,9 @@
 
         SourceResolver resolver = null;
         Source contextSource = null;
+        ServiceSelector selector = null;
+        Instantiator instantiator = null;
+
         try {
             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
             Source context = resolver.resolveURI("context://");
@@ -116,9 +105,20 @@
             PublicationFactory factory = PublicationFactory.getInstance(getLogger());
 
             Publication template = factory.getPublication(templateId, contextPath);
-            createPublication(template, getParameterAsString(PUBLICATION_ID), resolver);
+            String name = getParameterAsString(PUBLICATION_NAME);
+
+            selector = (ServiceSelector) this.manager.lookup(Instantiator.ROLE + "Selector");
+            instantiator = (Instantiator) selector.select(template.getInstantiatorHint());
+
+            instantiator.instantiate(template, getParameterAsString(PUBLICATION_ID), name);
 
         } finally {
+            if (selector != null) {
+                if (instantiator != null) {
+                    selector.release(instantiator);
+                }
+                this.manager.release(selector);
+            }
             if (resolver != null) {
                 if (contextSource != null) {
                     resolver.release(contextSource);
@@ -129,124 +129,4 @@
 
     }
 
-    protected static final String[] sourcesToCopy = { "publication.xml",
-            "config/publication.xconf", "config/ac/passwd/visit.rml",
-            "config/ac/passwd/reviewer.gml", "config/ac/passwd/review.rml",
-            "config/ac/passwd/localhost.ipml", "config/ac/passwd/lenya.iml",
-            "config/ac/passwd/ldap.properties.sample", "config/ac/passwd/editor.gml",
-            "config/ac/passwd/edit.rml", "config/ac/passwd/alice.iml",
-            "config/ac/passwd/admin.rml", "config/ac/passwd/admin.gml", "config/ac/ac.xconf",
-            "config/doctypes/doctypes.xconf", "config/workflow/workflow.xml",
-            "content/authoring/sitetree.xml", "content/authoring/index/index_en.xml" };
-
-    /**
-     * Creates a publication from a template.
-     * @param template The template.
-     * @param publicationId The ID of the new publication.
-     * @param resolver The source resolver to use.
-     * @throws Exception if an error occurs.
-     */
-    protected void createPublication(Publication template, String publicationId,
-            SourceResolver resolver) throws Exception {
-
-        Source publicationsSource = null;
-        ModifiableSource metaSource = null;
-        ModifiableSource configSource = null;
-        try {
-
-            publicationsSource = resolver.resolveURI("context://"
-                    + PublicationImpl.PUBLICATION_PREFIX_URI);
-            String publicationsUri = publicationsSource.getURI();
-
-            for (int i = 0; i < sourcesToCopy.length; i++) {
-
-                String source = sourcesToCopy[i];
-                copySource(template, publicationId, resolver, publicationsUri, source);
-            }
-
-            metaSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
-                    + publicationId + "/publication.xml");
-            Document metaDoc = DocumentHelper.readDocument(metaSource.getInputStream());
-            NamespaceHelper helper = new NamespaceHelper(
-                    "http://apache.org/cocoon/lenya/publication/1.0", "lenya", metaDoc);
-            Element nameElement = helper.getFirstChild(metaDoc.getDocumentElement(), "name");
-            String name = getParameterAsString(PUBLICATION_NAME);
-            DocumentHelper.setSimpleElementText(nameElement, name);
-
-            save(metaDoc, metaSource);
-
-            configSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
-                    + publicationId + "/config/publication.xconf");
-            DefaultConfiguration config = (DefaultConfiguration) new DefaultConfigurationBuilder()
-                    .build(configSource.getInputStream());
-            DefaultConfiguration templatesConfig = new DefaultConfiguration("templates");
-            DefaultConfiguration templateConfig = new DefaultConfiguration("template");
-            templateConfig.setAttribute("id", template.getId());
-            templatesConfig.addChild(templateConfig);
-            config.addChild(templatesConfig);
-            OutputStream oStream = configSource.getOutputStream();
-            new DefaultConfigurationSerializer().serialize(oStream, config);
-            if (oStream != null) {
-                oStream.flush();
-                try {
-                    oStream.close();
-                } catch (Throwable t) {
-                    if (getLogger().isDebugEnabled()) {
-                        getLogger().debug("Exception closing output stream: ", t);
-                    }
-                    throw new RuntimeException("Could not write document: ", t);
-                }
-            }
-
-        } finally {
-            if (publicationsSource != null) {
-                resolver.release(publicationsSource);
-            }
-            if (metaSource != null) {
-                resolver.release(metaSource);
-            }
-            if (configSource != null) {
-                resolver.release(configSource);
-            }
-        }
-
-    }
-
-    protected void copySource(Publication template, String publicationId, SourceResolver resolver,
-            String publicationsUri, String source) throws MalformedURLException, IOException {
-        Source templateSource = null;
-        ModifiableSource targetSource = null;
-        try {
-            templateSource = resolver.resolveURI(publicationsUri + "/" + template.getId() + "/"
-                    + source);
-            targetSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
-                    + publicationId + "/" + source);
-
-            org.apache.lenya.cms.cocoon.source.SourceUtil.copy(templateSource, targetSource, false);
-        } finally {
-            if (templateSource != null) {
-                resolver.release(templateSource);
-            }
-            if (targetSource != null) {
-                resolver.release(targetSource);
-            }
-        }
-    }
-
-    protected void save(Document metaDoc, ModifiableSource metaSource) throws IOException,
-            TransformerConfigurationException, TransformerException {
-        OutputStream oStream = metaSource.getOutputStream();
-        DocumentHelper.writeDocument(metaDoc, new OutputStreamWriter(oStream));
-        if (oStream != null) {
-            oStream.flush();
-            try {
-                oStream.close();
-            } catch (Throwable t) {
-                if (getLogger().isDebugEnabled()) {
-                    getLogger().debug("Exception closing output stream: ", t);
-                }
-                throw new RuntimeException("Could not write document: ", t);
-            }
-        }
-    }
 }

Modified: lenya/trunk/src/java/org/apache/lenya/lenya.roles
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/lenya.roles?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/lenya.roles (original)
+++ lenya/trunk/src/java/org/apache/lenya/lenya.roles Fri Apr  8 12:20:39 2005
@@ -122,6 +122,10 @@
   	    shorthand="document-builders"
   	    default-class="org.apache.cocoon.components.ExtendedComponentSelector"/>
         
+  <role name="org.apache.lenya.cms.publication.templating.InstantiatorSelector"
+  	    shorthand="template-instantiators"
+  	    default-class="org.apache.cocoon.components.ExtendedComponentSelector"/>
+        
   <role name="org.apache.lenya.cms.usecase.scheduling.UsecaseScheduler"
   	    shorthand="usecase-scheduler"
   	    default-class="org.apache.lenya.cms.usecase.scheduling.UsecaseSchedulerImpl"/>

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?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl (original)
+++ lenya/trunk/src/webapp/WEB-INF/cocoon-xconf.xsl Fri Apr  8 12:20:39 2005
@@ -420,7 +420,9 @@
     <component-instance name="default" logger="lenya.publication"
                         class="org.apache.lenya.cms.publication.DefaultDocumentBuilder"/>
   </document-builders>
-             
+  
+  <template-instantiators/>
+  
   <!-- move scheduler to the end, datasources have to be available -->
   <xsl:apply-templates select="component[@role = 'org.apache.cocoon.components.cron.JobScheduler']" mode="scheduler"/>
              

Modified: lenya/trunk/src/webapp/lenya/pubs/blog/config/publication.xconf
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/webapp/lenya/pubs/blog/config/publication.xconf?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/webapp/lenya/pubs/blog/config/publication.xconf (original)
+++ lenya/trunk/src/webapp/lenya/pubs/blog/config/publication.xconf Fri Apr  8 12:20:39 2005
@@ -15,7 +15,7 @@
   limitations under the License.
 -->
 
-<!-- $Id: publication.xconf,v 1.7 2004/03/16 11:12:16 gregor Exp $ -->
+<!-- $Id$ -->
 
 <publication>
   <languages>
@@ -23,7 +23,7 @@
     <language>de</language>
   </languages>
   <path-mapper>org.apache.lenya.cms.publication.SingleLanguageIdentityMapper</path-mapper>
-  <document-builder>org.apache.lenya.cms.publication.DefaultDocumentBuilder</document-builder>
+  <document-builder name="default"/>
   <breadcrumb-prefix/>
   <ssl-prefix/>
 </publication>

Added: lenya/trunk/src/webapp/lenya/pubs/default/config/instantiator.xconf
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/webapp/lenya/pubs/default/config/instantiator.xconf?view=auto&rev=160590
==============================================================================
--- lenya/trunk/src/webapp/lenya/pubs/default/config/instantiator.xconf (added)
+++ lenya/trunk/src/webapp/lenya/pubs/default/config/instantiator.xconf Fri Apr  8 12:20:39 2005
@@ -0,0 +1,29 @@
+<?xml version="1.0"?>
+<!--
+  Copyright 1999-2005 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.
+-->
+
+<!-- $Id: usecase.xmap 158794 2005-03-23 16:21:15Z andreas $ -->
+<!--
+    This file defines the publication specific template instantiator
+-->
+
+  <xconf xpath="/cocoon/template-instantiators" unless="/cocoon/template-instantiators/component-instance[@name = 'default']">
+
+    <component-instance name="default"
+                        logger="lenya.publication.templating"
+                        class="org.apache.lenya.defaultpub.cms.publication.templating.Instantiator"/>
+
+  </xconf>

Modified: lenya/trunk/src/webapp/lenya/pubs/default/config/publication.xconf
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/webapp/lenya/pubs/default/config/publication.xconf?view=diff&r1=160589&r2=160590
==============================================================================
--- lenya/trunk/src/webapp/lenya/pubs/default/config/publication.xconf (original)
+++ lenya/trunk/src/webapp/lenya/pubs/default/config/publication.xconf Fri Apr  8 12:20:39 2005
@@ -17,11 +17,12 @@
 
 <!-- $Id$ -->
 
-<publication supports-templating="true">
+<publication>
   <languages>
     <language default="true">en</language>
     <language>de</language>
   </languages>
+  <template-instantiator name="default"/>
   <path-mapper>org.apache.lenya.cms.publication.DefaultDocumentIdToPathMapper</path-mapper>
   <document-builder name="default"/>
   <breadcrumb-prefix/>

Added: lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/Instantiator.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/Instantiator.java?view=auto&rev=160590
==============================================================================
--- lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/Instantiator.java (added)
+++ lenya/trunk/src/webapp/lenya/pubs/default/java/src/org/apache/lenya/defaultpub/cms/publication/templating/Instantiator.java Fri Apr  8 12:20:39 2005
@@ -0,0 +1,181 @@
+/*
+ * 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.defaultpub.cms.publication.templating;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.MalformedURLException;
+
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
+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.excalibur.source.ModifiableSource;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.PublicationImpl;
+import org.apache.lenya.xml.DocumentHelper;
+import org.apache.lenya.xml.NamespaceHelper;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Instantiate the publication.
+ * 
+ * @version $Id:$
+ */
+public class Instantiator extends AbstractLogEnabled implements
+        org.apache.lenya.cms.publication.templating.Instantiator, Serviceable {
+
+    protected static final String[] sourcesToCopy = { "publication.xml",
+            "config/publication.xconf", "config/ac/passwd/visit.rml",
+            "config/ac/passwd/reviewer.gml", "config/ac/passwd/review.rml",
+            "config/ac/passwd/localhost.ipml", "config/ac/passwd/lenya.iml",
+            "config/ac/passwd/ldap.properties.sample", "config/ac/passwd/editor.gml",
+            "config/ac/passwd/edit.rml", "config/ac/passwd/alice.iml",
+            "config/ac/passwd/admin.rml", "config/ac/passwd/admin.gml", "config/ac/ac.xconf",
+            "config/doctypes/doctypes.xconf", "config/workflow/workflow.xml",
+            "content/authoring/sitetree.xml", "content/authoring/index/index_en.xml" };
+
+    /**
+     * @see org.apache.lenya.cms.publication.templating.Instantiator#instantiate(org.apache.lenya.cms.publication.Publication,
+     *      java.lang.String, java.lang.String)
+     */
+    public void instantiate(Publication template, String newPublicationId, String name)
+            throws Exception {
+
+        SourceResolver resolver = null;
+        Source publicationsSource = null;
+        ModifiableSource metaSource = null;
+        ModifiableSource configSource = null;
+        try {
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            publicationsSource = resolver.resolveURI("context://"
+                    + PublicationImpl.PUBLICATION_PREFIX_URI);
+            String publicationsUri = publicationsSource.getURI();
+
+            for (int i = 0; i < sourcesToCopy.length; i++) {
+
+                String source = sourcesToCopy[i];
+                copySource(template, newPublicationId, resolver, publicationsUri, source);
+            }
+
+            metaSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
+                    + newPublicationId + "/publication.xml");
+            Document metaDoc = DocumentHelper.readDocument(metaSource.getInputStream());
+            NamespaceHelper helper = new NamespaceHelper(
+                    "http://apache.org/cocoon/lenya/publication/1.0", "lenya", metaDoc);
+            Element nameElement = helper.getFirstChild(metaDoc.getDocumentElement(), "name");
+            DocumentHelper.setSimpleElementText(nameElement, name);
+
+            save(metaDoc, metaSource);
+
+            configSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
+                    + newPublicationId + "/config/publication.xconf");
+            DefaultConfiguration config = (DefaultConfiguration) new DefaultConfigurationBuilder()
+                    .build(configSource.getInputStream());
+            DefaultConfiguration templatesConfig = new DefaultConfiguration("templates");
+            DefaultConfiguration templateConfig = new DefaultConfiguration("template");
+            templateConfig.setAttribute("id", template.getId());
+            templatesConfig.addChild(templateConfig);
+            config.addChild(templatesConfig);
+            OutputStream oStream = configSource.getOutputStream();
+            new DefaultConfigurationSerializer().serialize(oStream, config);
+            if (oStream != null) {
+                oStream.flush();
+                try {
+                    oStream.close();
+                } catch (Throwable t) {
+                    if (getLogger().isDebugEnabled()) {
+                        getLogger().debug("Exception closing output stream: ", t);
+                    }
+                    throw new RuntimeException("Could not write document: ", t);
+                }
+            }
+
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+                if (publicationsSource != null) {
+                    resolver.release(publicationsSource);
+                }
+                if (metaSource != null) {
+                    resolver.release(metaSource);
+                }
+                if (configSource != null) {
+                    resolver.release(configSource);
+                }
+            }
+        }
+    }
+
+    protected void copySource(Publication template, String publicationId, SourceResolver resolver,
+            String publicationsUri, String source) throws MalformedURLException, IOException {
+        Source templateSource = null;
+        ModifiableSource targetSource = null;
+        try {
+            templateSource = resolver.resolveURI(publicationsUri + "/" + template.getId() + "/"
+                    + source);
+            targetSource = (ModifiableSource) resolver.resolveURI(publicationsUri + "/"
+                    + publicationId + "/" + source);
+
+            org.apache.lenya.cms.cocoon.source.SourceUtil.copy(templateSource, targetSource, false);
+        } finally {
+            if (templateSource != null) {
+                resolver.release(templateSource);
+            }
+            if (targetSource != null) {
+                resolver.release(targetSource);
+            }
+        }
+    }
+
+    protected void save(Document metaDoc, ModifiableSource metaSource) throws IOException,
+            TransformerConfigurationException, TransformerException {
+        OutputStream oStream = metaSource.getOutputStream();
+        DocumentHelper.writeDocument(metaDoc, new OutputStreamWriter(oStream));
+        if (oStream != null) {
+            oStream.flush();
+            try {
+                oStream.close();
+            } catch (Throwable t) {
+                if (getLogger().isDebugEnabled()) {
+                    getLogger().debug("Exception closing output stream: ", t);
+                }
+                throw new RuntimeException("Could not write document: ", t);
+            }
+        }
+    }
+
+    private ServiceManager manager;
+
+    /**
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org