/*
JSPWiki - a JSP-based WikiWiki clone.
Copyright (C) 2001-2005 Janne Jalkanen (Janne.Jalkanen@iki.fi)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.ecyrd.jspwiki.plugin;
import org.apache.log4j.Logger;
import com.ecyrd.jspwiki.*;
import java.util.*;
/**
* Plugin for displaying an index of the wiki's links, to or from pages that
* do and don't exist. If the parameter dir is "in", provides the
* referredBy list rather than the default refersTo list.
* Uses the ReferenceManager.
*
* Parameters:
*
*
* - dir="in|out"
* - The direction of the link index, inbound ("in") or outbound ("out").
* The default is "out".
* - nolinktext="[text string]"
* - When a page has no links, this is the descriptive text.
* The default is "no links".
* - page="[page]"
* - When this parameter is absent, a link index of all wiki pages is
* returned. If a page parameter is supplied, only the links of that
* page are returned. More than one page can be specified when
* separated by whitespace. There is no default.
*
*
* This produces a definition (<dl>) list, where the term
* (<dt>) is the page name and the definition
* (<dd>) is the list of links. The <dl>)
* element has a class attribute whose value is "linkindex". If
* the page parameter is provided and the page does not exist,
* there will be a single term whose value is "[page] does not exist!".
*
*
* Also see: LinkIndexPlugin
*
*
* @author Murray Altheim
* @version JSPWiki 2.2.28
*/
public class LinkIndexPlugin
extends AbstractReferralPlugin
{
public static final String PARAM_DIR = "dir";
public static final String PARAM_PAGE = "page";
public static final String PARAM_NO_LINK_TEXT = "nolinktext";
/** The default text used to indicate that there are no links for a given page. */
public static String NO_LINK_TEXT = "no links";
/** The default text used to indicate that the page specified as a parameter does not exist. */
public static String DOES_NOT_EXIST = "does not exist!";
private static Logger log = Logger.getLogger( LinkIndexPlugin.class );
private static final String // reusable strings
DLo = "", DLc = "
\n",
DTo = "", DTao = "",
DTc = "\n", DTac = "\n",
DDo = " ", DDc = "\n", Comma = ", ",
Io = "", Ic = "",
Bo = "", Bc = "";
public String execute( WikiContext context, Map params )
throws PluginException
{
WikiEngine eng = context.getEngine();
ReferenceManager refmgr = eng.getReferenceManager();
try {
String nlt = (String)params.get(PARAM_NO_LINK_TEXT);
String noLinkText = ( nlt != null ? nlt : NO_LINK_TEXT );
boolean omitLinkless = ( nlt != null && nlt.equalsIgnoreCase("OMIT") );
// obtain Map based on page and direction parameters
String dir = (String)params.get(PARAM_DIR);
Map map = ( dir != null && ((String)dir).equals("in") )
? refmgr.getReferredBy()
: refmgr.getRefersTo();
Collection uncreated = refmgr.findUncreated();
String pageparam = (String)params.get(PARAM_PAGE);
boolean multipage = pageparam != null
&& ( ( pageparam.indexOf(' ') != -1
|| pageparam.indexOf('\t') != -1
|| pageparam.indexOf('\n') != -1 ) );
TreeSet keys = new TreeSet();
if ( pageparam == null )
{
keys.addAll(map.keySet());
}
else if ( pageparam.trim().length() == 0 )
{
throw new PluginException("no page specified in 'page' parameter"); // I18N
}
else if ( !multipage && !context.getEngine().pageExists(pageparam) )
{
return DLo + DTo + pageparam + DTc + DDo + Bo + DOES_NOT_EXIST + Bc + DTc + DLc;
}
else
{
if ( multipage )
{
StringTokenizer stok = new StringTokenizer(pageparam);
String token = null;
while ( stok.hasMoreTokens() )
{
token = stok.nextToken().trim();
if ( token.length() > 0 )
{
keys.add(token);
}
}
}
else
{
keys.add(pageparam);
}
}
// begin generating content
StringBuffer out = new StringBuffer();
out.append(DLo);
Iterator it = keys.iterator();
while( it.hasNext() )
{
String key = (String)it.next();
Collection col = (Collection)map.get(key);
if ( !context.getEngine().pageExists(key) ) // page doesn't exist ........
{
out.append(DTo + key + DTc);
out.append(DDo + Bo + DOES_NOT_EXIST + Bc + DDc);
}
else if ( col.isEmpty() ) // page can't be found in map ..................
{
if ( !omitLinkless )
{
out.append( DTao
+ context.getEngine().getURL( WikiContext.VIEW, key, null, false )
+ DTm + key + DTac );
// out.append(DTo + key + DTc); // link-less
out.append(DDo + Io + noLinkText + Ic + DDc);
}
}
else // process set of pages .............................................
{
out.append( DTao
+ context.getEngine().getURL( WikiContext.VIEW, key, null, false )
+ DTm + key + DTac );
out.append(DDo);
Iterator it2 = col.iterator();
while ( it2.hasNext() )
{
String page = (String)it2.next();
out.append( uncreated.contains(page)
? Bo + page + Bc
: page );
if ( it2.hasNext() ) out.append(Comma);
}
out.append(DDc);
}
}
out.append(DLc);
super.initialize( context, params );
return out.toString();
}
catch ( PluginException pe )
{
return "error processing LinkIndexPlugin: "
+ pe.getMessage() + ""; // I18N
}
}
} // end com.ecyrd.jspwiki.plugin.LinkIndexPlugin