*/
@SuppressWarnings("unchecked")
public String execute( WikiContext context, Map<String, String> params )
throws PluginException
{
WikiEngine engine = context.getEngine();
StringBuffer res = new StringBuffer();
String clazz = params.get( PARAM_CLASS );
String includedPage = params.get( PARAM_PAGENAME );
String style = params.get( PARAM_STYLE );
String defaultstr = params.get( PARAM_DEFAULT );
int section = TextUtil.parseIntParameter(params.get( PARAM_SECTION ), -1 );
int maxlen = TextUtil.parseIntParameter(params.get( PARAM_MAXLENGTH ), -1 );
if( style == null ) style = DEFAULT_STYLE;
if( maxlen == -1 ) maxlen = Integer.MAX_VALUE;
if( includedPage != null )
{
WikiPage page = null;
try
{
String pageName = engine.getFinalPageName( includedPage );
if( pageName != null )
{
page = engine.getPage( pageName );
}
else
{
page = engine.getPage( includedPage );
}
}
catch( ProviderException e )
{
res.append( "<span class=\"error\">Page could not be found by the page provider.</span>" );
return res.toString();
}
if( page != null )
{
//
// Check for recursivity
//
List<String> previousIncludes = (List)context.getVariable( ATTR_RECURSE );
if( previousIncludes != null )
{
if( previousIncludes.contains( page.getName() ) )
{
return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!</span>";
}
}
else
{
previousIncludes = new ArrayList<String>();
}
previousIncludes.add( page.getName() );
context.setVariable( ATTR_RECURSE, previousIncludes );
//
// Check for permissions
//
AuthorizationManager mgr = engine.getAuthorizationManager();
if( !mgr.checkPermission( context.getWikiSession(),
PermissionFactory.getPagePermission( page, "view") ) )
{
res.append("<span class=\"error\">You do not have permission to view this included page.</span>");
return res.toString();
}
/**
* We want inclusion to occur within the context of
* its own page, because we need the links to be correct.
*/
WikiContext includedContext = (WikiContext) context.clone();
includedContext.setPage( page );
String pageData = engine.getPureText( page );
String moreLink = "";
if( section != -1 )
{
try
{
pageData = TextUtil.getSection( pageData, section );
}
catch( IllegalArgumentException e )
{
throw new PluginException( e.getMessage() );
}
}
if( pageData.length() > maxlen )
{
pageData = pageData.substring( 0, maxlen )+" ...";
moreLink = "<p><a href=\""+context.getURL(WikiContext.VIEW,includedPage)+"\">More...</a></p>";
}
res.append("<div style=\""+style+"\""+(clazz != null ? " class=\""+clazz+"\"" : "")+">");
res.append( engine.textToHTML( includedContext, pageData ) );
res.append( moreLink );
res.append("</div>");
//
// Remove the name from the stack; we're now done with this.