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 2008/02/06 23:02:46 UTC

svn commit: r619172 - in /lenya/trunk/src/modules/menubar: ./ config/sitemap/ java/ java/src/ java/src/org/ java/src/org/apache/ java/src/org/apache/lenya/ java/src/org/apache/lenya/modules/ java/src/org/apache/lenya/modules/menubar/

Author: andreas
Date: Wed Feb  6 14:02:37 2008
New Revision: 619172

URL: http://svn.apache.org/viewvc?rev=619172&view=rev
Log:
Adding MenuFilterTransformer to replace filter-menu stylesheet.

Added:
    lenya/trunk/src/modules/menubar/config/sitemap/
    lenya/trunk/src/modules/menubar/config/sitemap/menu-filter-transformer.xmap
    lenya/trunk/src/modules/menubar/java/
    lenya/trunk/src/modules/menubar/java/src/
    lenya/trunk/src/modules/menubar/java/src/org/
    lenya/trunk/src/modules/menubar/java/src/org/apache/
    lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/
    lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/
    lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/menubar/
    lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/menubar/MenuFilterTransformer.java
Modified:
    lenya/trunk/src/modules/menubar/sitemap.xmap

Added: lenya/trunk/src/modules/menubar/config/sitemap/menu-filter-transformer.xmap
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/menubar/config/sitemap/menu-filter-transformer.xmap?rev=619172&view=auto
==============================================================================
--- lenya/trunk/src/modules/menubar/config/sitemap/menu-filter-transformer.xmap (added)
+++ lenya/trunk/src/modules/menubar/config/sitemap/menu-filter-transformer.xmap Wed Feb  6 14:02:37 2008
@@ -0,0 +1,24 @@
+<?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 = 'menufilter']"
+  xmlns:map="http://apache.org/cocoon/sitemap/1.0">
+  <map:transformer name="menufilter" logger="lenya.sitemap.transformer.menufilter"
+    src="org.apache.lenya.modules.menubar.MenuFilterTransformer"/>
+</xmap>

Added: lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/menubar/MenuFilterTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/menubar/MenuFilterTransformer.java?rev=619172&view=auto
==============================================================================
--- lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/menubar/MenuFilterTransformer.java (added)
+++ lenya/trunk/src/modules/menubar/java/src/org/apache/lenya/modules/menubar/MenuFilterTransformer.java Wed Feb  6 14:02:37 2008
@@ -0,0 +1,178 @@
+/*
+ * 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.
+ */
+package org.apache.lenya.modules.menubar;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+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.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentUtil;
+import org.apache.lenya.cms.publication.URLInformation;
+import org.apache.lenya.cms.repository.RepositoryException;
+import org.apache.lenya.cms.usecase.Usecase;
+import org.apache.lenya.cms.usecase.UsecaseResolver;
+import org.apache.lenya.cms.usecase.gui.Tab;
+import org.apache.lenya.util.ServletHelper;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+public class MenuFilterTransformer extends AbstractSAXTransformer {
+
+    protected static final String NAMESPACE = "http://apache.org/cocoon/lenya/menubar/1.0";
+    private static final String ATTR_AREAS = "areas";
+    private static final String ATTR_RESOURCE_TYPES = "resourceTypes";
+    private Set attributeHandlers;
+
+    public MenuFilterTransformer() {
+        this.defaultNamespaceURI = NAMESPACE;
+    }
+
+    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters params)
+            throws ProcessingException, SAXException, IOException {
+        super.setup(resolver, objectModel, src, params);
+
+        this.attributeHandlers = new HashSet();
+
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        String webappUri = ServletHelper.getWebappURI(request);
+
+        String tabGroup = getTabGroup(request);
+        URLInformation url = new URLInformation(webappUri);
+        String area = tabGroup != null ? tabGroup : url.getArea();
+
+        this.attributeHandlers.add(new AttributeHandler(ATTR_AREAS, area));
+        
+        try {
+            DocumentFactory factory = DocumentUtil.getDocumentFactory(this.manager, request);
+            if (factory.isDocument(webappUri)) {
+                String resourceType = factory.getFromURL(webappUri).getResourceType().getName();
+                this.attributeHandlers.add(new AttributeHandler(ATTR_RESOURCE_TYPES, resourceType));
+            }
+        } catch (RepositoryException e) {
+            throw new ProcessingException(e);
+        }
+
+    }
+
+    protected String getTabGroup(Request request) throws ProcessingException {
+        String webappUri = ServletHelper.getWebappURI(request);
+        String tabGroup = null;
+        String usecaseName = request.getParameter("lenya.usecase");
+        if (usecaseName != null) {
+            UsecaseResolver usecaseResolver = null;
+            Usecase usecase = null;
+            try {
+                usecaseResolver = (UsecaseResolver) this.manager.lookup(UsecaseResolver.ROLE);
+                usecase = usecaseResolver.resolve(webappUri, usecaseName);
+                if (usecase.getView() != null) {
+                    Tab tab = usecase.getView().getTab();
+                    if (tab != null) {
+                        tabGroup = tab.getGroup();
+                    }
+                }
+            } catch (ServiceException e) {
+                throw new ProcessingException(e);
+            } finally {
+                if (usecaseResolver != null) {
+                    if (usecase != null) {
+                        try {
+                            usecaseResolver.release(usecase);
+                        } catch (ServiceException e) {
+                            throw new RuntimeException(e);
+                        }
+                    }
+                    this.manager.release(usecaseResolver);
+                }
+            }
+        }
+        return tabGroup;
+    }
+
+    public void startTransformingElement(String uri, String name, String raw, Attributes attr)
+            throws ProcessingException, IOException, SAXException {
+
+        if (this.ignoreEventsCount > 0) {
+            this.ignoreEventsCount++;
+        } else {
+            boolean matches = true;
+            for (Iterator i = this.attributeHandlers.iterator(); i.hasNext();) {
+                AttributeHandler handler = (AttributeHandler) i.next();
+                matches = matches && handler.matches(attr);
+            }
+            if (matches) {
+                super.startTransformingElement(uri, name, raw, attr);
+            } else {
+                this.ignoreEventsCount++;
+            }
+        }
+    }
+
+    public void endTransformingElement(String uri, String name, String raw)
+            throws ProcessingException, IOException, SAXException {
+        if (this.ignoreEventsCount > 0) {
+            this.ignoreEventsCount--;
+        } else {
+            super.endTransformingElement(uri, name, raw);
+        }
+    }
+
+    /**
+     * Searches an attribute value (space-separated list) for a certain value.
+     */
+    protected static class AttributeHandler {
+
+        protected static final String DELIMITER = " ";
+        private String attributeName;
+        private String value;
+
+        protected AttributeHandler(String attributeName, String value) {
+            this.attributeName = attributeName;
+            this.value = value;
+        }
+
+        protected boolean matches(Attributes attr) {
+            if (this.value == null) {
+                return false;
+            }
+            String attrValue = attr.getValue(this.attributeName);
+            if (attrValue == null) {
+                return true;
+            } else {
+                StringTokenizer tokens = new StringTokenizer(attrValue, DELIMITER);
+                while (tokens.hasMoreTokens()) {
+                    if (tokens.nextToken().equals(this.value)) {
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+
+    }
+
+}

Modified: lenya/trunk/src/modules/menubar/sitemap.xmap
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/menubar/sitemap.xmap?rev=619172&r1=619171&r2=619172&view=diff
==============================================================================
--- lenya/trunk/src/modules/menubar/sitemap.xmap (original)
+++ lenya/trunk/src/modules/menubar/sitemap.xmap Wed Feb  6 14:02:37 2008
@@ -76,7 +76,7 @@
       <map:match pattern="menu-xml/module/*/*.xml">
         <map:select type="resource-exists">
           <map:when test="fallback://lenya/modules/{2}/menus.xmap">
-            <map:mount uri-prefix="menu-xml/module/{1}/" src="fallback://lenya/modules/{2}/menus.xmap" check-reload="true" reload-method="synchron" pass-through="true"/>
+            <map:mount uri-prefix="menu-xml/module/{1}/" src="{fallback:lenya/modules/{2}/menus.xmap}" check-reload="true" reload-method="synchron" pass-through="true"/>
           </map:when>
           <map:otherwise>
             <map:generate type="serverpages" src="fallback://lenya/content/menus/live.xsp"/>



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