svn commit: r1297970 - in /lenya/branches/BRANCH_2_1_X/src/modules-core/versioning: ./ config/ config/sitemap/ java/ java/src/ java/src/org/ java/src/org/apache/ java/src/org/apache/lenya/ java/src/org/apache/lenya/versioning/

[email protected] Wed, 07 Mar 2012 13:44:56 -0000
Newsgroups gmane.comp.cms.lenya.cvs
Message-ID <[email protected]>
Author: andreas
Date: Wed Mar  7 13:44:55 2012
New Revision: 1297970

URL: http://svn.apache.org/viewvc?rev=1297970&view=rev
Log:
Adding versioning module.

Added:
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/module.xml
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/sitemap/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/sitemap/transformers.xmap
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/versioning/
    lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/versioning/RevisionTransformer.java

Added: lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/module.xml
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/module.xml?rev=1297970&view=auto
==============================================================================
--- lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/module.xml (added)
+++ lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/module.xml Wed Mar  7 13:44:55 2012
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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: publication.xml 374687 2006-02-03 15:24:55Z michi $ -->
+
+<module xmlns="http://apache.org/lenya/module/1.0">
+  <id>org.apache.lenya.modules.versioning</id>
+  <export package="org.apache.lenya.cms.cocoon.transformation"/>
+  <package>org.apache.lenya.modules</package>
+  <version>0.1-dev</version>
+  <name>Versioning</name>
+  <lenya-version>@lenya.version@</lenya-version>
+  <description>Components to handle versioning.</description>
+</module>

Added: lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/sitemap/transformers.xmap
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/sitemap/transformers.xmap?rev=1297970&view=auto
==============================================================================
--- lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/sitemap/transformers.xmap (added)
+++ lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/config/sitemap/transformers.xmap Wed Mar  7 13:44:55 2012
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+
+<xmap xpath="/sitemap/components/transformers" 
+  unless="/sitemap/components/transformers/transformer[@name = 'revision']"
+  xmlns:map="http://apache.org/cocoon/sitemap/1.0">
+  
+  <map:transformer name="revision" logger="lenya.sitemap.transformer.revision"
+    src="org.apache.lenya.versioning.RevisionTransformer"/>
+    
+</xmap>

Added: lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/versioning/RevisionTransformer.java
URL: http://svn.apache.org/viewvc/lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/versioning/RevisionTransformer.java?rev=1297970&view=auto
==============================================================================
--- lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/versioning/RevisionTransformer.java (added)
+++ lenya/branches/BRANCH_2_1_X/src/modules-core/versioning/java/src/org/apache/lenya/versioning/RevisionTransformer.java Wed Mar  7 13:44:55 2012
@@ -0,0 +1,84 @@
+package org.apache.lenya.versioning;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.transformation.AbstractSAXTransformer;
+import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentUtil;
+import org.apache.lenya.cms.repository.RepositoryUtil;
+import org.apache.lenya.cms.repository.Session;
+import org.apache.lenya.util.ServletHelper;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+public class RevisionTransformer extends AbstractSAXTransformer {
+
+    private String webappUrl = null;
+    private Integer revision = null;
+
+    public RevisionTransformer() {
+        this.defaultNamespaceURI = "http://apache.org/lenya/versioning";
+    }
+
+    @Override
+    public void setup(SourceResolver resolver, @SuppressWarnings("rawtypes") Map objectModel,
+            String src, Parameters params) throws ProcessingException, SAXException, IOException {
+        super.setup(resolver, objectModel, src, params);
+        final Request request = ObjectModelHelper.getRequest(objectModel);
+        this.webappUrl = ServletHelper.getWebappURI(request);
+    }
+
+    @Override
+    public void startElement(String uri, String name, String raw, Attributes attr)
+            throws SAXException {
+        final String revAttr = attr.getValue(this.namespaceURI, "attr");
+        if (revAttr == null) {
+            super.startElement(uri, name, raw, attr);
+        } else {
+            final AttributesImpl attrs = new AttributesImpl(attr);
+            final int pos = attrs.getIndex(this.namespaceURI, "attr");
+            attrs.removeAttribute(pos);
+            final String rev = Integer.toString(getRevision());
+            attrs.setValue(attrs.getIndex(revAttr), rev);
+            super.startElement(uri, name, raw, attrs);
+        }
+    }
+
+    protected int getRevision() {
+        if (this.revision == null) {
+            try {
+                final Session session = RepositoryUtil.getSession(this.manager, request);
+                final DocumentFactory docFactory = DocumentUtil.createDocumentFactory(this.manager,
+                        session);
+                final Document document = docFactory.getFromURL(this.webappUrl);
+                this.revision = document.getRepositoryNode().getHistory().getLatestRevision()
+                        .getNumber();
+            } catch (Exception e) {
+                throw new RuntimeException("Error getting document from webapp URL ["
+                        + this.webappUrl + "]: " + e.getMessage(), e);
+            }
+        }
+        return this.revision;
+    }
+
+    @Override
+    public void startTransformingElement(String uri, String name, String raw, Attributes attr)
+            throws ProcessingException, IOException, SAXException {
+        if ("revision".equals(name)) {
+            final char[] rev = Integer.toString(getRevision()).toCharArray();
+            characters(rev, 0, rev.length);
+        } else {
+            throw new ProcessingException("Unknown element <" + name + "> from namespace "
+                    + this.namespaceURI);
+        }
+    }
+
+}