Author: jalkanen
Date: Sat Jun 14 05:34:03 2008
New Revision: 667790
URL: http://svn.apache.org/viewvc?rev=667790&view=rev
Log:
JSPWIKI-246: IndexPlugin is replaced.
Added:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/plugin/IndexPlugin.java
Added: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/plugin/IndexPlugin.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/plugin/IndexPlugin.java?rev=667790&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/plugin/IndexPlugin.java
(added)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/plugin/IndexPlugin.java Sat
Jun 14 05:34:03 2008
@@ -0,0 +1,161 @@
+/*
+ JSPWiki - a JSP-based WikiWiki clone.
+
+ 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 com.ecyrd.jspwiki.plugin;
+
+import java.util.*;
+import java.util.regex.Pattern;
+
+import org.apache.ecs.Element;
+import org.apache.ecs.xhtml.div;
+import org.apache.ecs.xhtml.span;
+import org.apache.log4j.Logger;
+
+import com.ecyrd.jspwiki.WikiContext;
+import com.ecyrd.jspwiki.providers.ProviderException;
+
+/**
+ * A plugin which creates an index of pages according to a certain
+ * pattern.
+ * <p>
+ * This is a complete rewrite of the old IndexPlugin under an Apache license.
+ * <p>
+ * Usage:
+ * <ul>
+ * <li><b>include</b> - A regexp pattern for marking which pages should be
included.</li>
+ * <li><b>exclude</b> - A regexp pattern for marking which pages should be
excluded.</li>
+ * </ul>
+ * The default is to include all pages.
+ */
+public class IndexPlugin implements WikiPlugin
+{
+ private static final String PARAM_INCLUDE = "include";
+ private static final String PARAM_EXCLUDE = "exclude";
+ private static Logger log = Logger.getLogger( IndexPlugin.class );
+
+ /**
+ * [EMAIL PROTECTED]
+ */
+ public String execute( WikiContext context, Map params ) throws
PluginException
+ {
+ String include = (String)params.get( PARAM_INCLUDE );
+ String exclude = (String)params.get( PARAM_EXCLUDE );
+
+ List<String> pages;
+ div masterDiv = new div();
+ masterDiv.setClass( "index" );
+
+ div indexDiv = new div();
+
+ masterDiv.addElement( indexDiv );
+ indexDiv.setClass( "header" );
+ try
+ {
+ pages = listPages( context, include, exclude );
+ Collections.sort( pages );
+
+ char initialChar = ' ';
+
+ div currentDiv = new div();
+
+ for( String name : pages )
+ {
+ if( name.charAt( 0 ) != initialChar )
+ {
+ if( initialChar != ' ' ) indexDiv.addElement( " - " );
+ initialChar = name.charAt( 0 );
+
+ masterDiv.addElement( makeHeader(initialChar) );
+
+ currentDiv = new div();
+ currentDiv.setClass("body");
+ masterDiv.addElement( currentDiv );
+
+ indexDiv.addElement( "<a
href='#"+initialChar+"'>"+initialChar+"</a>" );
+ }
+ else
+ {
+ currentDiv.addElement( ", " );
+ }
+
+ String link = "<a href='"+
+ context.getURL( WikiContext.VIEW, name )+
+ "'>"+name+"</a>";
+
+ currentDiv.addElement( link );
+ }
+ }
+ catch( ProviderException e )
+ {
+ log.warn("Could not load page index",e);
+ throw new PluginException( e.getMessage() );
+ }
+
+ return masterDiv.toString();
+ }
+
+ /**
+ * Create the DOM for a heading
+ * @param initialChar
+ * @return
+ */
+ private Element makeHeader( char initialChar )
+ {
+ span s = new span();
+ s.setClass( "section" );
+ s.addElement( "<a name='"+initialChar+"'>"+initialChar+"</a>" );
+
+ return s;
+ }
+
+ /**
+ * Grabs a list of all pages and filters them according to the
include/exclude patterns.
+ *
+ * @param context
+ * @param include
+ * @param exclude
+ * @return
+ * @throws ProviderException
+ */
+ private List<String> listPages( WikiContext context, String include,
String exclude )
+ throws ProviderException
+ {
+ Pattern includePtrn = include != null ? Pattern.compile( include ) :
Pattern.compile(".*");
+ Pattern excludePtrn = exclude != null ? Pattern.compile( exclude ) :
Pattern.compile("\\p{Cntrl}"); // There are no control characters in page names
+
+ ArrayList<String> result = new ArrayList<String>();
+
+ Collection pages =
context.getEngine().getReferenceManager().findCreated();
+
+ for( Iterator i = pages.iterator(); i.hasNext(); )
+ {
+ String pageName = (String) i.next();
+
+ if( excludePtrn.matcher( pageName ).matches() ) continue;
+ if( includePtrn.matcher( pageName ).matches() )
+ {
+ result.add( pageName );
+ }
+ }
+
+ return result;
+ }
+
+}