svn commit: r647161 - in /lenya/trunk/src/modules-core/ac: config/sitemap/userinfo-transformer.xmap java/src/org/apache/lenya/modules/ java/src/org/apache/lenya/modules/ac/ java/src/org/apache/lenya/modules/ac/UserInfoTransformer.java

[email protected]
Newsgroups gmane.comp.cms.lenya.cvs
Message-ID <[email protected]>
Author: andreas
Date: Fri Apr 11 06:29:58 2008
New Revision: 647161

URL: http://svn.apache.org/viewvc?rev=647161&view=rev
Log:
Adding UserInfoTransformer.

Added:
    lenya/trunk/src/modules-core/ac/config/sitemap/userinfo-transformer.xmap
    lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/
    lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/ac/
    lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/ac/UserInfoTransformer.java

Added: lenya/trunk/src/modules-core/ac/config/sitemap/userinfo-transformer.xmap
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/ac/config/sitemap/userinfo-transformer.xmap?rev=647161&view=auto
==============================================================================
--- lenya/trunk/src/modules-core/ac/config/sitemap/userinfo-transformer.xmap (added)
+++ lenya/trunk/src/modules-core/ac/config/sitemap/userinfo-transformer.xmap Fri Apr 11 06:29:58 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 = 'userInfo']"
+  xmlns:map="http://apache.org/cocoon/sitemap/1.0">
+  <map:transformer name="userInfo" logger="lenya.sitemap.transformer.userInfo"
+    src="org.apache.lenya.modules.ac.UserInfoTransformer"/>
+</xmap>

Added: lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/ac/UserInfoTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/ac/UserInfoTransformer.java?rev=647161&view=auto
==============================================================================
--- lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/ac/UserInfoTransformer.java (added)
+++ lenya/trunk/src/modules-core/ac/java/src/org/apache/lenya/modules/ac/UserInfoTransformer.java Fri Apr 11 06:29:58 2008
@@ -0,0 +1,101 @@
+/*
+ * 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.ac;
+
+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.ac.AccessControlException;
+import org.apache.lenya.ac.User;
+import org.apache.lenya.cms.ac.PolicyUtil;
+import org.apache.lenya.util.ServletHelper;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>
+ * Transformer to retrieve user information based on the user ID. The user ID can
+ * either be provided using the <em>userId</em> attribute or as text inside the element.
+ * </p>
+ * <p>Elements:</p>
+ * <ul>
+ * <li><code>&lt;user:fullname&gt;</code> - The full name. If the user doesn't exist, the user ID is inserted.</li>
+ * </ul>
+ * <p>
+ * Usage examples:
+ * </p>
+ * <pre><code>
+ * &lt;user:fullname&gt;john&lt;/user:fullname&gt;
+ * 
+ * &lt;user:fullname userId="john"/&gt;
+ * </code></pre>
+ */
+public class UserInfoTransformer extends AbstractSAXTransformer {
+
+    protected static final String ATTR_USER_ID = "userId";
+    protected static final String ELEM_FULLNAME = "fullname";
+    protected static final String NAMESPACE = "http://apache.org/lenya/userinfo/1.0";
+    private String userId;
+    private String url;
+
+    public UserInfoTransformer() {
+        this.defaultNamespaceURI = NAMESPACE;
+    }
+
+    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters params)
+            throws ProcessingException, SAXException, IOException {
+        super.setup(resolver, objectModel, src, params);
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        this.url = ServletHelper.getWebappURI(request);
+        
+    }
+
+    public void startTransformingElement(String uri, String name, String raw, Attributes attr)
+            throws ProcessingException, IOException, SAXException {
+        if (name.equals(ELEM_FULLNAME)) {
+            String userId = attr.getValue(ATTR_USER_ID);
+            if (userId != null) {
+                this.userId = userId;
+            } else {
+                startTextRecording();
+            }
+        }
+    }
+
+    public void endTransformingElement(String uri, String name, String raw)
+            throws ProcessingException, IOException, SAXException {
+        if (name.equals(ELEM_FULLNAME)) {
+            String userId = this.userId != null ? this.userId : endTextRecording();
+            try {
+                User user = PolicyUtil.getUser(this.manager, this.url, userId, getLogger());
+                String output = user != null ? user.getName() : userId;
+                char[] chars = output.toCharArray();
+                characters(chars, 0, chars.length);
+            } catch (AccessControlException e) {
+                throw new ProcessingException(e);
+            }
+            this.userId = null;
+        }
+    }
+
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.