svn commit: r791016 - in /lenya/contributions/2_0_X/modules/tagcloud: ./ config/ config/cocoon-xconf/ 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/tag...

[email protected]
Newsgroups gmane.comp.cms.lenya.cvs
Message-ID <[email protected]>
Author: andreas
Date: Fri Jul  3 21:19:04 2009
New Revision: 791016

URL: http://svn.apache.org/viewvc?rev=791016&view=rev
Log:
First version of tagcloud module.

Added:
    lenya/contributions/2_0_X/modules/tagcloud/
    lenya/contributions/2_0_X/modules/tagcloud/config/
    lenya/contributions/2_0_X/modules/tagcloud/config/cocoon-xconf/
    lenya/contributions/2_0_X/modules/tagcloud/config/cocoon-xconf/PathToUuidModule.xconf
    lenya/contributions/2_0_X/modules/tagcloud/config/module.xml
    lenya/contributions/2_0_X/modules/tagcloud/java/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/IndexTermsGenerator.java
    lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/PathToUuidModule.java
    lenya/contributions/2_0_X/modules/tagcloud/sitemap.xmap
    lenya/contributions/2_0_X/modules/tagcloud/xslt/
    lenya/contributions/2_0_X/modules/tagcloud/xslt/terms2xhtml.xsl

Added: lenya/contributions/2_0_X/modules/tagcloud/config/cocoon-xconf/PathToUuidModule.xconf
URL: http://svn.apache.org/viewvc/lenya/contributions/2_0_X/modules/tagcloud/config/cocoon-xconf/PathToUuidModule.xconf?rev=791016&view=auto
==============================================================================
--- lenya/contributions/2_0_X/modules/tagcloud/config/cocoon-xconf/PathToUuidModule.xconf (added)
+++ lenya/contributions/2_0_X/modules/tagcloud/config/cocoon-xconf/PathToUuidModule.xconf Fri Jul  3 21:19:04 2009
@@ -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.
+-->
+
+<!-- $Id: doctypes.xconf 164635 2005-04-25 20:01:43Z tschlabach $ -->
+
+<xconf xpath="/cocoon/input-modules" unless="/cocoon/input-modules/component-instance[@name = 'path2uuid']">
+
+  <component-instance logger="core.modules.input.path-to-uuid" name="path2uuid"
+    class="org.apache.lenya.modules.tagcloud.PathToUuidModule"/>
+  
+</xconf>

Added: lenya/contributions/2_0_X/modules/tagcloud/config/module.xml
URL: http://svn.apache.org/viewvc/lenya/contributions/2_0_X/modules/tagcloud/config/module.xml?rev=791016&view=auto
==============================================================================
--- lenya/contributions/2_0_X/modules/tagcloud/config/module.xml (added)
+++ lenya/contributions/2_0_X/modules/tagcloud/config/module.xml Fri Jul  3 21:19:04 2009
@@ -0,0 +1,27 @@
+<?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.
+-->
+
+<module xmlns="http://apache.org/lenya/module/1.0">
+  <id>org.apache.lenya.modules.tagcloud</id>
+  <depends module="org.apache.lenya.modules.lucene"/>
+  <package>org.apache.lenya.modules</package>
+  <version>0.1-dev</version>
+  <name>Tag Cloud</name>
+  <lenya-version>1.0-SNAPSHOT</lenya-version>
+  <description>Tag cloud</description>
+</module>
\ No newline at end of file

Added: lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/IndexTermsGenerator.java
URL: http://svn.apache.org/viewvc/lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/IndexTermsGenerator.java?rev=791016&view=auto
==============================================================================
--- lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/IndexTermsGenerator.java (added)
+++ lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/IndexTermsGenerator.java Fri Jul  3 21:19:04 2009
@@ -0,0 +1,164 @@
+/*
+ * 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.tagcloud;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.avalon.framework.parameters.ParameterException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.generation.AbstractGenerator;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermEnum;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+public class IndexTermsGenerator extends AbstractGenerator {
+
+    public static final String NAMESPACE = "http://apache.org/lenya/search";
+    public static final String ELEM_TERMS = "terms";
+    public static final String ELEM_TERM = "term";
+    public static final String ATTR_FIELD = "field";
+    public static final String ATTR_HITS = "hits";
+    public static final String ATTR_RELATIVE_HITS = "relativeHits";
+
+    protected static final String PARAM_INDEX_PATH = "indexPath";
+    protected static final String PARAM_FIELD = "field";
+    protected static final String PARAM_LANGUAGE = "language";
+    protected static final String PARAM_MAX = "max";
+    protected static final int DEFAULT_MAX = 10;
+    protected static final String ALL_LANGUAGES = "*";
+
+    private String indexPath;
+    private String fieldName;
+    private float max;
+    private String language;
+
+    @Override
+    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
+            throws ProcessingException, SAXException, IOException {
+        super.setup(resolver, objectModel, src, par);
+        try {
+            this.indexPath = par.getParameter(PARAM_INDEX_PATH);
+            this.fieldName = par.getParameter(PARAM_FIELD);
+            this.language = par.getParameter(PARAM_LANGUAGE, ALL_LANGUAGES);
+            this.max = par.getParameterAsFloat(PARAM_MAX, DEFAULT_MAX);
+        } catch (final ParameterException e) {
+            throw new ProcessingException(e);
+        }
+    }
+
+    public void generate() throws IOException, SAXException, ProcessingException {
+
+        this.contentHandler.startDocument();
+        this.contentHandler.startPrefixMapping("", NAMESPACE);
+
+        final AttributesImpl attrs = new AttributesImpl();
+        attrs.addAttribute("", ATTR_FIELD, ATTR_FIELD, "CDATA", this.fieldName);
+        this.contentHandler.startElement(NAMESPACE, ELEM_TERMS, ELEM_TERMS, attrs);
+
+        final SortedMap<String, Integer> value2hits = getIndexedValues();
+
+        final SortedSet<Integer> hitSet = new TreeSet<Integer>(value2hits.values());
+        final float maxHits = hitSet.last();
+        final float factor = this.max / maxHits;
+
+        for (String value : value2hits.keySet()) {
+            final AttributesImpl termAttrs = new AttributesImpl();
+            final int hits = value2hits.get(value);
+            termAttrs.addAttribute("", ATTR_HITS, ATTR_HITS, "CDATA", Integer.toString(hits));
+            final int relHits = Math.round(hits * factor);
+            termAttrs.addAttribute("", ATTR_RELATIVE_HITS, ATTR_RELATIVE_HITS, "CDATA", Integer
+                    .toString(relHits));
+            this.contentHandler.startElement(NAMESPACE, ELEM_TERM, ELEM_TERM, termAttrs);
+            final char[] chars = value.toCharArray();
+            this.contentHandler.characters(chars, 0, chars.length);
+            this.contentHandler.endElement(NAMESPACE, ELEM_TERM, ELEM_TERM);
+        }
+
+        this.contentHandler.endElement(NAMESPACE, ELEM_TERMS, ELEM_TERMS);
+        this.contentHandler.endPrefixMapping("");
+        this.contentHandler.endDocument();
+
+    }
+
+    protected SortedMap<String, Integer> getIndexedValues() throws SAXException, IOException {
+        final SortedMap<String, Integer> value2hits = new TreeMap<String, Integer>();
+        TermEnum terms = null;
+        IndexReader indexReader = null;
+        IndexSearcher searcher = null;
+        try {
+            indexReader = IndexReader.open(this.indexPath);
+            searcher = new IndexSearcher(indexReader);
+
+            // For more information, see
+            // "How do I retrieve all the values of a particular field that exists within an index, across all documents?"
+            // http://wiki.apache.org/lucene-java/LuceneFAQ#head-a801793d7479264e29157d92440199b35266dc18
+
+            terms = indexReader.terms(new Term(this.fieldName, ""));
+            while (this.fieldName.equals(terms.term().field())) {
+                final Term term = terms.term();
+                final String value = term.text();
+                
+                // do expensive language filtering only if language parameter was set
+                if (this.language.equals(ALL_LANGUAGES)) {
+                    value2hits.put(term.text(), indexReader.docFreq(term));
+                } else {
+                    final Query langQuery = new TermQuery(new Term("language", this.language));
+                    final Query tagQuery = new TermQuery(new Term(this.fieldName, value));
+                    final BooleanQuery query = new BooleanQuery();
+                    query.add(langQuery, true, false);
+                    query.add(tagQuery, true, false);
+                    final Hits hits = searcher.search(query);
+                    final int length = hits.length();
+                    if (length > 0) {
+                        value2hits.put(value, hits.length());
+                    }
+                }
+                if (!terms.next())
+                    break;
+            }
+
+        } catch (final Exception e) {
+            throw new SAXException(e);
+        } finally {
+            if (terms != null) {
+                terms.close();
+            }
+            if (searcher != null) {
+                searcher.close();
+            }
+            if (indexReader != null) {
+                indexReader.close();
+            }
+        }
+        return value2hits;
+    }
+
+}

Added: lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/PathToUuidModule.java
URL: http://svn.apache.org/viewvc/lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/PathToUuidModule.java?rev=791016&view=auto
==============================================================================
--- lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/PathToUuidModule.java (added)
+++ lenya/contributions/2_0_X/modules/tagcloud/java/src/org/apache/lenya/modules/tagcloud/PathToUuidModule.java Fri Jul  3 21:19:04 2009
@@ -0,0 +1,68 @@
+/*
+ * 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.tagcloud;
+
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+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.modules.input.AbstractInputModule;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.lenya.cms.publication.Area;
+import org.apache.lenya.cms.publication.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentUtil;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.repository.RepositoryException;
+import org.apache.lenya.cms.repository.RepositoryUtil;
+import org.apache.lenya.cms.repository.Session;
+
+public class PathToUuidModule extends AbstractInputModule implements Serviceable {
+
+    private ServiceManager manager;
+
+    @Override
+    public Object getAttribute(String name, Configuration modeConf, Map objectModel)
+            throws ConfigurationException {
+
+        StringTokenizer tokens = new StringTokenizer(name, ":");
+        String pubId = tokens.nextToken();
+        String areaName = tokens.nextToken();
+        String path = tokens.nextToken();
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        try {
+            Session session = RepositoryUtil.getSession(this.manager, request);
+            DocumentFactory factory = DocumentUtil.createDocumentFactory(this.manager, session);
+            Publication pub = factory.getPublication(pubId);
+            Area area = pub.getArea(areaName);
+            String uuid = area.getSite().getNode(path).getUuid();
+            return uuid;
+        } catch (RepositoryException e) {
+            throw new ConfigurationException(e.getMessage(), e);
+        }
+
+    }
+
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+}

Added: lenya/contributions/2_0_X/modules/tagcloud/sitemap.xmap
URL: http://svn.apache.org/viewvc/lenya/contributions/2_0_X/modules/tagcloud/sitemap.xmap?rev=791016&view=auto
==============================================================================
--- lenya/contributions/2_0_X/modules/tagcloud/sitemap.xmap (added)
+++ lenya/contributions/2_0_X/modules/tagcloud/sitemap.xmap Fri Jul  3 21:19:04 2009
@@ -0,0 +1,54 @@
+<?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.
+-->
+
+<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
+  
+  <map:components>
+    <map:generators default="file">
+      <map:generator name="index-terms" src="org.apache.lenya.modules.tagcloud.IndexTermsGenerator"/>
+    </map:generators>
+  </map:components>
+
+  <map:pipelines>
+
+    <map:pipeline>
+      
+      <!-- pattern: {1:pub}/{2:area}/{3:lang}/terms.xml -->
+      <map:match pattern="*/*/*/terms.xml">
+        <map:generate type="index-terms">
+          <map:parameter name="indexPath" value="{index-path:{1}-{2}}"/>
+          <map:parameter name="field" value="\{http://purl.org/dc/elements/1.1/}subject"/>
+          <map:parameter name="language" value="{3}"/>
+        </map:generate>
+        <map:serialize type="xml"/>
+      </map:match>
+      
+      <!-- pattern: {1:pub}/{2:area}/{3:lang}/tagcloud.xml -->
+      <map:match pattern="*/*/*/tagcloud.xml">
+        <map:generate src="cocoon:/{1}/{2}/{3}/terms.xml"/>
+        <map:transform src="xslt/terms2xhtml.xsl">
+          <map:parameter name="searchDocUuid" value="{path2uuid:{1}:{2}:/search}"/>
+          <map:parameter name="field" value="\\{http\://purl.org/dc/elements/1.1/\}subject"/>
+        </map:transform>
+        <map:serialize type="xml"/>
+      </map:match>
+
+    </map:pipeline>
+    
+  </map:pipelines>
+</map:sitemap>

Added: lenya/contributions/2_0_X/modules/tagcloud/xslt/terms2xhtml.xsl
URL: http://svn.apache.org/viewvc/lenya/contributions/2_0_X/modules/tagcloud/xslt/terms2xhtml.xsl?rev=791016&view=auto
==============================================================================
--- lenya/contributions/2_0_X/modules/tagcloud/xslt/terms2xhtml.xsl (added)
+++ lenya/contributions/2_0_X/modules/tagcloud/xslt/terms2xhtml.xsl Fri Jul  3 21:19:04 2009
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
+  xmlns:search="http://apache.org/lenya/search"
+  xmlns="http://www.w3.org/1999/xhtml"
+  exclude-result-prefixes="search">
+  
+  
+  <xsl:param name="searchDocUuid"/>
+  <xsl:param name="field"/>
+  
+  <xsl:template match="search:terms">
+    <div class="tagCloud">
+      <h3>Tags</h3>
+      <xsl:apply-templates/>
+    </div>
+  </xsl:template>
+  
+  
+  <xsl:template match="search:term">
+    <a style="font-size: {11 + @relativeHits}px"
+      href="lenya-document:{$searchDocUuid}?type=raw&amp;queryString={$field}:{.}">
+      <xsl:value-of select="."/>
+    </a>
+    <xsl:text> </xsl:text>
+  </xsl:template>
+  
+  
+</xsl:stylesheet>
\ No newline at end of file
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.