This is version . It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]

Refactoring of the Basic Plugin classes#

I hope this patch can be useful, at least it is for me.

The folloging patch refactors the function of the AbstractReferralPlugin (which should be renamed to something like AbstractLinkCollectionHandler) so that:

  • All the parameters in the plugins extending it are moved down
  • The include and exclude stuff from IndexPlugin is put also here (my final goal will be to reimplement the IndexPlugin to inherit from here, tool)
  • sort is configurable
  • New parameters for finer control of generation
  • I reimplemented it with no instance variables to avoid potential threading issues
  • Functionality is split in three methods:
    • ~filterCollection, filters a collection with include/exclude patterns, and returns a sorted copy
    • ~wikitizeCollection, makes the link collection into wiki, using prelink, separator, empty and extras parameters
    • ~makeHTML, generates HTML cutting into maxwidth characters

So, now you can say [{UnusedPagesPlugin maxwidth=10}] or [{UnusedPagesPlugin maxwidth=3 prelink=' ' separator=',' exclude='(.*blogentry.*|.*comments.*|NavMenu.*)'}]

Filters#

I applied the code in IndexPlugin, changing it slightly to use regexp, so that I can exclude='(NavMenu.*|.*blogentry.*|.*comment.*)', for instance. This can be applied to all inheriting classes, UnusedPages, UndefinedPages and ReferringPages

Sorting#

The ~Locale.~getInstance() call is broken for the Spanish Locale. So, I used rule based locale, which gives a very fine control. See the javadocs for how to use it. separator='<z;Z<y;Y<x;X<w;W<v,V<u;ú;ü,U,Ú<t,T<s,S<r,R<q,Q<p,P<o;ó,O;Ó<ñ,Ñ<n,N<m,M<l,L <k,K<j,J<i;í,I;Í<h,H<g,G<f,F<e;é,E;É<d,D<c;ç,C;Ç<b,B<a;á,A;Á' will give reverse alphabetic sorting for Spanish,

[{UndefinedPagesPlugin
prelink='# '
sortorder='<a;á,A;Á<b,B<c;ç,C;Ç<d,D<e;é,E;É<f,F<g,G<h,H<i;í,I;Í<j,J<k,K<l,L<m,M<n,N<ñ,Ñ <o;ó,O;Ó<p,P<q,Q<r,R<s,S<t,T<u;ú;ü,U,Ú<v,V<w,W<x,X<y,Y<z,Z'
}]
will generate a normal Spanish sort, with a numbered list of Undefined Pages.

Remaining stuff#

I will change the implementation of IndexPlugin so that it uses this functionality.

Missing#

Maybe a foreach=5,foreachtext='
'
could be interesting. separator=',' is applied after every Link, so the last one has an extra ",". Difficult to solve and be generic at the same time.

Index: src/com/ecyrd/jspwiki/plugin/AbstractReferralPlugin.java
===================================================================
RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/plugin/AbstractReferralPlugin.java,v
retrieving revision 1.4
diff -i -u -r1.4 AbstractReferralPlugin.java
--- src/com/ecyrd/jspwiki/plugin/AbstractReferralPlugin.java	8 Dec 2002 10:59:01 -0000	1.4
+++ src/com/ecyrd/jspwiki/plugin/AbstractReferralPlugin.java	23 Feb 2003 19:45:04 -0000
@@ -25,80 +25,266 @@
 import java.io.StringReader;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.PatternMatcher;
+import org.apache.oro.text.regex.Perl5Matcher;
+import org.apache.oro.text.regex.PatternCompiler;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.MalformedPatternException;
+import java.text.Collator;
+import java.text.RuleBasedCollator;
+import java.text.ParseException;
 
 /**
  *  This is a base class for all plugins using referral things.
  *
  *  @author Janne Jalkanen
+ *  @author Santiago Gala (sgala@hisitech.com, refactored to group functionality)
  */
 public abstract class AbstractReferralPlugin
     implements WikiPlugin
 {
     private static Category log = Category.getInstance( AbstractReferralPlugin.class );
 
-    public static final int    ALL_ITEMS       = -1;
-    public static final String PARAM_MAXWIDTH  = "maxwidth";
-    public static final String PARAM_SEPARATOR = "separator";
+    public static final String PARAM_MAXWIDTH    = "maxwidth";
+    public static final int    MAXWIDTH_DEFAULT  = Integer.MAX_VALUE;
+    public static final String PARAM_PRELINK     = "prelink";
+    public static final String PRELINK_DEFAULT   = " ";
+    public static final String PARAM_SEPARATOR   = "separator";
+    public static final String SEPARATOR_DEFAULT = "\\\\";
+
+    public static final String PARAM_MAX         = "max";
+    public static final int    MAXITEMS_DEFAULT  = Integer.MAX_VALUE;
+    public static final String PARAM_EXTRAS      = "extras";
+    public static final String EXTRAS_DEFAULT    = "...and %d mode\\\\";
+    public static final String PARAM_EMPTY       = "empty";
+    public static final String EMPTY_DEFAULT     = "...nobody\\\\";
+
+    public static final String PARAM_INCLUDE     = "include";
+    public static final String INCLUDE_DEFAULT   = ".*";
+    public static final String PARAM_EXCLUDE     = "exclude";
+    public static final String EXCLUDE_DEFAULT   = "";
+    public static final String PARAM_SORTORDER   = "sortorder";
+
+
 
-    protected           int    m_maxwidth = Integer.MAX_VALUE;
-    protected           String m_separator = "\\\\";
 
     /**
-     *  Used to initialize some things.  All plugins must call this first.
+     *  Filters a Collection, using include and exclude regexps.
+     *  While we are into this, we sort using a collator using sortorder
+     *  parameter, or a Collator.getInstance() if the parameter is missing.
+     *  parameters:
+     *  <ul>
+     *  <li>include: regexp to specify inclusion</li>
+     *  <li>exclude: regexp to specify exclusion</li>
+     *  <li>sortorder: rules for rulebased collator</li>
+     *  </ul>
      *
-     *  @since 1.6.4
+     *  @param links Collection to make into WikiText.
+     *  @param params A map with parameter values (include, defaults to ".*",
+     *   exclude, default to "", sortorder, defaults to Server Locale sorting).
+     *  @return Collection a Collection with 
      */
-    public void initialize( WikiContext context, Map params )
+    protected SortedSet filterCollection( Collection links,
+                                          Map params )
         throws PluginException
     {
-        m_maxwidth = TextUtil.parseIntParameter( (String)params.get( PARAM_MAXWIDTH ), Integer.MAX_VALUE ); 
-        String s = (String) params.get( PARAM_SEPARATOR );
+        Pattern  includePattern;
+        Pattern  excludePattern;
 
-        if( s != null )
+        String   sortorder = null;
+        Collator collator   = null;
+
+        //Parse sortorder argument and create collator
+        sortorder = (String) params.get(PARAM_SORTORDER);
+        try
         {
-            m_separator = s;
+            if( sortorder == null )
+            {
+                //Use Locale Default Collator
+                collator = Collator.getInstance();
+            }
+            else
+            {
+                collator = new RuleBasedCollator( sortorder );
+            }
         }
+        catch( ParseException pe )
+         {
+             log.error("Exception in collator: ", pe);
+             log.info("Using plain collator.");
+             collator = Collator.getInstance();
+         }
+        collator.setStrength( Collator.PRIMARY );
+
+        //We'll use the collator to sort the result
+        SortedSet result = new TreeSet( collator );
+        
+
+        //No process done
+        if(links == null || links.isEmpty() )
+        {
+            //Return empty result
+            return result;
+        }
+
+
+        //
+        //  Parse filter arguments and create patterns.
+        //
+        PatternCompiler compiler = new Perl5Compiler();
+        try
+        {
+            String ptrn = (String) params.get(PARAM_INCLUDE);
+            if( ptrn == null ) ptrn = INCLUDE_DEFAULT;
+            includePattern = compiler.compile(ptrn);
+
+            ptrn = (String) params.get(PARAM_EXCLUDE);
+            if( ptrn == null ) ptrn = EXCLUDE_DEFAULT;
+            excludePattern = compiler.compile(ptrn);
+        }
+        catch( MalformedPatternException e )
+        {
+            throw new PluginException("Illegal pattern detected."); // FIXME, make a proper error.
+        }
+
+
+
+
+        //Filter through include/exclude params...
+        Iterator it     = links.iterator();
+        
+        while( it.hasNext() )
+        {
+            PatternMatcher matcher = new Perl5Matcher();
+
+            String value = (String)it.next();
+            if( matcher.matches( value, includePattern ) )
+            {
+                if( !matcher.matches( value, excludePattern ) )
+                {
+                    
+                    result.add( value );
+
+                }
+            }
+        }
+
+        return result;
 
-        if( m_maxwidth < 0 ) m_maxwidth = 0;
 
-        // log.debug( "Requested maximum width is "+m_maxwidth );
     }
 
     /**
      *  Makes WikiText from a Collection.
+     *  uses parameters:
+     *  <ul>
+     *  <li>prelink, for text before each item</li>
+     *  <li>separator, for text after each item</li>
+     *  <li>maxitems, number of items to show</li>
+     *  <li>extras, text to insert when limit reached</li>
+     *  <li>empty, text to insert if collection is empty</li>
+     *  </ul>
      *
      *  @param links Collection to make into WikiText.
-     *  @param separator Separator string to use.
-     *  @param numItems How many items to show.
      */
-    protected String wikitizeCollection( Collection links, String separator, int numItems )
+    protected String wikitizeCollection( Collection links, Map params )
     {
+
+        String   prelink   = PRELINK_DEFAULT;
+        String   separator = SEPARATOR_DEFAULT;
+
+        int      maxitems  = MAXITEMS_DEFAULT;
+        String   extras    = EXTRAS_DEFAULT;
+        String   empty     = EMPTY_DEFAULT;
+
+
+
+        //Process prelink
+        String s = (String) params.get( PARAM_PRELINK );
+        if( s != null )
+        {
+            prelink = s;
+        }
+        //Process separator
+        s = (String) params.get( PARAM_SEPARATOR );
+        if( s != null )
+        {
+            separator = s;
+        }
+
+
+        //Process maxitems
+        maxitems = TextUtil.parseIntParameter( (String)params.get( PARAM_MAX ), Integer.MAX_VALUE );
+        s = (String)params.get( PARAM_EXTRAS );
+        if( s != null )
+        {
+            extras = s;
+        }
+
+        //Process empty
+        s = (String)params.get( PARAM_EMPTY );
+        if( s != null )
+        {
+            empty = s;
+        }
+
+        //Collection is empty or null, return empty parameter
         if(links == null || links.isEmpty() )
-            return( "" );
+            return( empty );
 
         StringBuffer output = new StringBuffer();
         
         Iterator it     = links.iterator();
-        int      count  = 0;
         
-        while( it.hasNext() && ( (count < numItems) || ( numItems == ALL_ITEMS ) ) )
+        int      count  = 0;
+
+        while( it.hasNext() && (count < maxitems) )
         {
             String value = (String)it.next();
             // Make a Wiki markup link. See TranslatorReader.
-            output.append( "[" + value + "]" + separator +"\n");
+            output.append( prelink );
+            output.append( "[" );
+            output.append( value );
+            output.append( "]" );
+            output.append( separator );
+            output.append( "\n");
             count++;
         }
-
+        if( maxitems < links.size() && maxitems > 0 )
+        {
+            extras = TextUtil.replaceString( extras, "%d", 
+                                             ""+(links.size()-maxitems) );
+            output.append( extras );
+        }
+        if( count == 0)
+        {
+            output.append(empty);
+        }
         return( output.toString() );
     }
 
     /**
      *  Makes HTML with common parameters.
+     *  parameters:
+     *  <ul>
+     *  <li>maxwidth: max number of chars in the link</li>
+     *  </ul>
      *
      *  @since 1.6.4
      */
-    protected String makeHTML( WikiContext context, String wikitext )
+    protected String makeHTML( WikiContext context, String wikitext, Map params )
     {
+        int      maxwidth;
+
+        //Process maxwidth
+        maxwidth = TextUtil.parseIntParameter( (String)params.get( PARAM_MAXWIDTH ),
+                                               MAXWIDTH_DEFAULT ); 
+        if( maxwidth < 0 ) maxwidth = 0;
+        // log.debug( "Requested maximum width is "+m_maxwidth );
+
+
+
         String result = "";
         TranslatorReader in = null;
 
@@ -106,7 +292,7 @@
         {
             in     = new TranslatorReader( context,
                                            new StringReader( wikitext ) );
-            in.addLinkTransmutator( new CutMutator(m_maxwidth) );
+            in.addLinkTransmutator( new CutMutator(maxwidth) );
 
             result = FileUtil.readContents( in );
         }
@@ -126,6 +312,8 @@
             }
         }
 
+        //log.debug( "UnSet Variable page");
+        //context.setVariable( "page", null );
         return result;
     }
     
@@ -144,6 +332,8 @@
 

         public String mutate( WikiContext context, String text )
         {
+            //log.debug( "Set Variable page:" + text +".");
+            //context.setVariable( "page", text );
             if( text.length() > m_length )
             {
                 return text.substring( 0, m_length ) + "...";
Index: src/com/ecyrd/jspwiki/plugin/ReferringPagesPlugin.java
===================================================================
RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/plugin/ReferringPagesPlugin.java,v
retrieving revision 1.8
diff -i -u -r1.8 ReferringPagesPlugin.java
--- src/com/ecyrd/jspwiki/plugin/ReferringPagesPlugin.java	20 Jan 2003 22:33:12 -0000	1.8
+++ src/com/ecyrd/jspwiki/plugin/ReferringPagesPlugin.java	23 Feb 2003 19:45:07 -0000
@@ -29,62 +29,77 @@
 /**
  *  Displays the pages referring to the current page.
  *
- *  Parameters: max: How many items to show.<BR>
- *              extras: How to announce extras.<BR>
+ *  Parameters: @seeAbstractReferralPlugin
+ *  page: the name of the page we want the Referrals
  *
  *  @author Janne Jalkanen
+ *  @author Santiago Gala (sgala@hisitech.com, refactored to group functionality)
  */
 public class ReferringPagesPlugin
     extends AbstractReferralPlugin
 {
     private static Category log = Category.getInstance( ReferringPagesPlugin.class );
 
-    public static final String PARAM_MAX      = "max";
-    public static final String PARAM_EXTRAS   = "extras";
+    public static final String PARAM_PAGE   = "page";
 
     public String execute( WikiContext context, Map params )
         throws PluginException
     {
+
         ReferenceManager refmgr = context.getEngine().getReferenceManager();
         WikiPage         page   = context.getPage();
+        String s = (String) params.get( PARAM_PAGE );
+        if( s != null )
+        {
+        //Try the given page instead of the current one
+            page = context.getEngine().getPage( s );
+            if( page == null )
+            {
+                //Even if it is undefined
+                page= new WikiPage( s);
+                page.setVersion( WikiProvider.LATEST_VERSION );
+            }
+        }
+        //else
+        //{
+        //    s = (String)context.getVariable( PARAM_PAGE );
+        //    if( s != null)
+        //    {
+        //        page = context.getEngine().getPage( s );
+        //        if( page == null )
+        //        {
+        //            page= new WikiPage( s);
+        //            page.setVersion( WikiProvider.LATEST_VERSION );
+        //        }
+        //    }
+        //}
 
         if( page != null )
         {
             Collection   links  = refmgr.findReferrers( page.getName() );
             String       wikitext;
 
-            super.initialize( context, params );
-
-            int items = TextUtil.parseIntParameter( (String)params.get( PARAM_MAX ), ALL_ITEMS );
-            String extras = (String)params.get( PARAM_EXTRAS );
-            if( extras == null )
-            {
-                extras = "...and %d more\\\\";
-            }
-
-            log.debug( "Fetching referring pages for "+context.getPage().getName()+
-                       " with a max of "+items);
+            //log.debug( "Fetching referring pages for "+page.getName()+
+            //           " with a max of "+items);
         
             if( links != null && links.size() > 0 )
             {
-                wikitext = wikitizeCollection( links, m_separator, items );
-
-                if( items < links.size() && items > 0 )
-                {
-                    extras = TextUtil.replaceString( extras, "%d", 
-                                                     ""+(links.size()-items) );
-                    wikitext += extras;
-                }
-            }
-            else
-            {
-                wikitext = "...nobody";
+                Collection result = filterCollection( links, params);
+                wikitext = wikitizeCollection( links, params );
+                return makeHTML( context, wikitext, params );
             }
 
-            return makeHTML( context, wikitext );
         }
 
-        return "";
+        String   empty = EMPTY_DEFAULT;
+        //Process empty
+        s = (String)params.get( PARAM_EMPTY );
+        if( s != null )
+        {
+            empty = s;
+        }
+
+        return makeHTML( context, empty, params );
     }
 
 }
Index: src/com/ecyrd/jspwiki/plugin/UndefinedPagesPlugin.java
===================================================================
RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/plugin/UndefinedPagesPlugin.java,v
retrieving revision 1.5
diff -i -u -r1.5 UndefinedPagesPlugin.java
--- src/com/ecyrd/jspwiki/plugin/UndefinedPagesPlugin.java	8 Dec 2002 10:59:23 -0000	1.5
+++ src/com/ecyrd/jspwiki/plugin/UndefinedPagesPlugin.java	23 Feb 2003 19:45:07 -0000
@@ -26,9 +26,10 @@
 import java.text.SimpleDateFormat;
 
 /**
- *  Parameters: none.
+ *  Parameters: @seeAbstractReferralPlugin
  *
  *  @author Janne Jalkanen
+ *  @author Santiago Gala (sgala@hisitech.com, refactored to group functionality)
  */
 public class UndefinedPagesPlugin
     extends AbstractReferralPlugin
@@ -41,13 +42,9 @@
         ReferenceManager refmgr = context.getEngine().getReferenceManager();
         Collection links = refmgr.findUncreated();
 
-        super.initialize( context, params );
 
-        TreeSet sortedSet = new TreeSet();
-        sortedSet.addAll( links );
-
-        String wikitext = wikitizeCollection( sortedSet, m_separator, ALL_ITEMS );
+        String wikitext = wikitizeCollection( filterCollection( links, params ), params );
         
-        return makeHTML( context, wikitext );
+        return makeHTML( context, wikitext, params );
     }
 }
Index: src/com/ecyrd/jspwiki/plugin/UnusedPagesPlugin.java
===================================================================
RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/plugin/UnusedPagesPlugin.java,v
retrieving revision 1.4
diff -i -u -r1.4 UnusedPagesPlugin.java
--- src/com/ecyrd/jspwiki/plugin/UnusedPagesPlugin.java	8 Dec 2002 10:59:31 -0000	1.4
+++ src/com/ecyrd/jspwiki/plugin/UnusedPagesPlugin.java	23 Feb 2003 19:45:07 -0000
@@ -26,9 +26,10 @@
 import java.text.SimpleDateFormat;
 
 /**
- *  Parameters: none.
+ *  Parameters: @seeAbstractReferralPlugin
  *
  *  @author Janne Jalkanen
+ *  @author Santiago Gala (sgala@hisitech.com, refactored to group functionality)
  */
 public class UnusedPagesPlugin
     extends AbstractReferralPlugin
@@ -41,15 +42,9 @@
         ReferenceManager refmgr = context.getEngine().getReferenceManager();
         Collection links = refmgr.findUnreferenced();
 
-        super.initialize( context, params );
-
-        TreeSet sortedSet = new TreeSet();
-
-        sortedSet.addAll( links );
-
-        String wikitext = wikitizeCollection( sortedSet, m_separator, ALL_ITEMS );
+        String wikitext = wikitizeCollection( filterCollection( links, params), params );
         
-        return makeHTML( context, wikitext );
+        return makeHTML( context, wikitext, params );
     }
 
 }


Back to Category Patches, Category Development

Add new attachment

Only authorized users are allowed to upload new attachments.
« This particular version was published on 23-Feb-2003 22:13 by SantiagoGala.