You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2019/12/20 18:20:24 UTC
[jspwiki] 02/10: JSPWIKI-120: move getVariable(..) method from
WikiEngine to VariableManager
This is an automated email from the ASF dual-hosted git repository.
juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 56087e3d287c475fc36eb8ccd3e5c232ded5d8f0
Author: juanpablo <ju...@apache.org>
AuthorDate: Fri Dec 20 16:43:03 2019 +0100
JSPWIKI-120: move getVariable(..) method from WikiEngine to VariableManager
---
.../main/java/org/apache/wiki/VariableManager.java | 79 +++++++++------------
.../src/main/java/org/apache/wiki/WikiContext.java | 2 +-
.../src/main/java/org/apache/wiki/WikiEngine.java | 31 ++------
.../main/java/org/apache/wiki/plugin/IfPlugin.java | 38 +++++-----
.../main/java/org/apache/wiki/rss/RSS20Feed.java | 35 +++++----
.../java/org/apache/wiki/rss/RSSGenerator.java | 82 ++++++++++------------
.../src/main/webapp/templates/210/DiffTab.jsp | 2 +-
.../src/main/webapp/templates/default/DiffTab.jsp | 2 +-
8 files changed, 111 insertions(+), 160 deletions(-)
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/VariableManager.java b/jspwiki-main/src/main/java/org/apache/wiki/VariableManager.java
index 86e166d..71a6ba5 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/VariableManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/VariableManager.java
@@ -93,59 +93,40 @@ public class VariableManager
}
/**
- * This method does in-place expansion of any variables. However,
- * the expansion is not done twice, that is, a variable containing text $variable
- * will not be expanded.
+ * This method does in-place expansion of any variables. However, the expansion is not done twice, that is,
+ * a variable containing text $variable will not be expanded.
* <P>
- * The variables should be in the same format ({$variablename} as in the web
- * pages.
+ * The variables should be in the same format ({$variablename} as in the web pages.
*
* @param context The WikiContext of the current page.
* @param source The source string.
* @return The source string with variables expanded.
*/
// FIXME: somewhat slow.
- public String expandVariables( WikiContext context, String source ) {
- StringBuilder result = new StringBuilder();
-
- for( int i = 0; i < source.length(); i++ )
- {
- if( source.charAt(i) == '{' )
- {
- if( i < source.length()-2 && source.charAt(i+1) == '$' )
- {
- int end = source.indexOf( '}', i );
-
- if( end != -1 )
- {
- String varname = source.substring( i+2, end );
+ public String expandVariables( final WikiContext context, final String source ) {
+ final StringBuilder result = new StringBuilder();
+ for( int i = 0; i < source.length(); i++ ) {
+ if( source.charAt(i) == '{' ) {
+ if( i < source.length()-2 && source.charAt(i+1) == '$' ) {
+ final int end = source.indexOf( '}', i );
+
+ if( end != -1 ) {
+ final String varname = source.substring( i+2, end );
String value;
- try
- {
+ try {
value = getValue( context, varname );
- }
- catch( NoSuchVariableException e )
- {
- value = e.getMessage();
- }
- catch( IllegalArgumentException e )
- {
+ } catch( final NoSuchVariableException | IllegalArgumentException e ) {
value = e.getMessage();
}
result.append( value );
i = end;
- continue;
}
- }
- else
- {
+ } else {
result.append( '{' );
}
- }
- else
- {
+ } else {
result.append( source.charAt(i) );
}
}
@@ -154,28 +135,36 @@ public class VariableManager
}
/**
- * Returns the value of a named variable. See {@link #getValue(WikiContext, String)}.
- * The only difference is that this method does not throw an exception, but it
- * returns the given default value instead.
+ * Returns the value of a named variable. See {@link #getValue(WikiContext, String)}. The only difference is that
+ * this method does not throw an exception, but it returns the given default value instead.
*
* @param context WikiContext
* @param varName The name of the variable
* @param defValue A default value.
* @return The variable value, or if not found, the default value.
*/
- public String getValue( WikiContext context, String varName, String defValue )
- {
- try
- {
+ public String getValue( final WikiContext context, final String varName, final String defValue ) {
+ try {
return getValue( context, varName );
- }
- catch( NoSuchVariableException e )
- {
+ } catch( final NoSuchVariableException e ) {
return defValue;
}
}
/**
+ * Shortcut to getValue(). However, this method does not throw a NoSuchVariableException, but returns null
+ * in case the variable does not exist.
+ *
+ * @param context WikiContext to look the variable in
+ * @param name Name of the variable to look for
+ * @return Variable value, or null, if there is no such variable.
+ * @since 2.2 on WikiEngine, moved to VariableManager on 2.11.0
+ */
+ public String getVariable( final WikiContext context, final String name ) {
+ return getValue( context, name, null );
+ }
+
+ /**
* Returns a value of the named variable. The resolving order is
* <ol>
* <li>Known "constant" name, such as "pagename", etc. This is so
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java
index 41a1fef..7a3029a 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java
@@ -615,7 +615,7 @@ public class WikiContext implements Cloneable, Command {
* @return An URL to the given context and page.
*/
public String getURL( final String context, final String page, final String params ) {
- final boolean absolute = "absolute".equals(m_engine.getVariable( this, WikiEngine.PROP_REFSTYLE ));
+ final boolean absolute = "absolute".equals(m_engine.getVariableManager().getVariable( this, WikiEngine.PROP_REFSTYLE ));
// FIXME: is rather slow
return m_engine.getURL( context, page, params, absolute );
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
index 3258915..5be6d67 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
@@ -1650,21 +1650,18 @@ public class WikiEngine {
*
* @return Set of WikiPage objects.
*/
-
- // FIXME: Should really get a Date object and do proper comparisons. This is terribly wasteful.
- public Set< WikiPage > getRecentChanges()
- {
+ public Set< WikiPage > getRecentChanges() {
try {
- Collection<WikiPage> pages = m_pageManager.getAllPages();
- Collection<Attachment> atts = m_attachmentManager.getAllAttachments();
+ final Collection< WikiPage > pages = m_pageManager.getAllPages();
+ final Collection< Attachment > atts = m_attachmentManager.getAllAttachments();
- TreeSet<WikiPage> sortedPages = new TreeSet<>( new PageTimeComparator() );
+ final TreeSet< WikiPage > sortedPages = new TreeSet<>( new PageTimeComparator() );
sortedPages.addAll( pages );
sortedPages.addAll( atts );
return sortedPages;
- } catch( ProviderException e ) {
+ } catch( final ProviderException e ) {
log.error( "Unable to fetch all pages: ",e);
return null;
}
@@ -1815,24 +1812,6 @@ public class WikiEngine {
}
/**
- * Shortcut to getVariableManager().getValue(). However, this method does not
- * throw a NoSuchVariableException, but returns null in case the variable does
- * not exist.
- *
- * @param context WikiContext to look the variable in
- * @param name Name of the variable to look for
- * @return Variable value, or null, if there is no such variable.
- * @since 2.2
- */
- public String getVariable( WikiContext context, String name ) {
- try {
- return m_variableManager.getValue( context, name );
- } catch( NoSuchVariableException e ) {
- return null;
- }
- }
-
- /**
* Returns the current PageManager which is responsible for storing
* and managing WikiPages.
*
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/IfPlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/IfPlugin.java
index 71490fb..2275b94 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/IfPlugin.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/IfPlugin.java
@@ -139,8 +139,7 @@ public class IfPlugin implements WikiPlugin
/**
* {@inheritDoc}
*/
- public String execute(WikiContext context, Map<String, String> params) throws PluginException
- {
+ public String execute( final WikiContext context, final Map< String, String > params ) throws PluginException {
return ifInclude( context,params )
? context.getEngine().textToHTML( context, params.get( DefaultPluginManager.PARAM_BODY ) )
: "" ;
@@ -158,34 +157,29 @@ public class IfPlugin implements WikiPlugin
* @throws PluginException If something goes wrong
* @return True, if the condition holds.
*/
- public static boolean ifInclude( WikiContext context, Map<String, String> params ) throws PluginException
- {
- boolean include = false;
-
- String group = params.get( PARAM_GROUP );
- String user = params.get( PARAM_USER );
- String ip = params.get( PARAM_IP );
- String page = params.get( PARAM_PAGE );
- String contains = params.get( PARAM_CONTAINS );
- String var = params.get( PARAM_VAR );
- String is = params.get( PARAM_IS );
- String exists = params.get( PARAM_EXISTS );
-
- include |= checkGroup(context, group);
+ public static boolean ifInclude( final WikiContext context, final Map< String, String > params ) throws PluginException {
+ final String group = params.get( PARAM_GROUP );
+ final String user = params.get( PARAM_USER );
+ final String ip = params.get( PARAM_IP );
+ final String page = params.get( PARAM_PAGE );
+ final String contains = params.get( PARAM_CONTAINS );
+ final String var = params.get( PARAM_VAR );
+ final String is = params.get( PARAM_IS );
+ final String exists = params.get( PARAM_EXISTS );
+
+ boolean include = checkGroup( context, group );
include |= checkUser(context, user);
include |= checkIP(context, ip);
- if( page != null )
- {
- String content = context.getEngine().getPureText(page, WikiProvider.LATEST_VERSION).trim();
+ if( page != null ) {
+ final String content = context.getEngine().getPureText(page, WikiProvider.LATEST_VERSION).trim();
include |= checkContains(content,contains);
include |= checkIs(content,is);
include |= checkExists(context,page,exists);
}
- if( var != null )
- {
- String content = context.getEngine().getVariable(context, var);
+ if( var != null ) {
+ final String content = context.getEngine().getVariableManager().getVariable(context, var);
include |= checkContains(content,contains);
include |= checkIs(content,is);
include |= checkVarExists(content,exists);
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java
index b3b09f5..ba69b7b 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java
@@ -18,22 +18,24 @@
*/
package org.apache.wiki.rss;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.text.SimpleDateFormat;
-import java.util.*;
-
-import javax.servlet.ServletContext;
-
-import org.jdom2.Element;
-import org.jdom2.output.Format;
-import org.jdom2.output.XMLOutputter;
import org.apache.wiki.Release;
import org.apache.wiki.WikiContext;
import org.apache.wiki.WikiEngine;
import org.apache.wiki.WikiPage;
import org.apache.wiki.api.exceptions.ProviderException;
import org.apache.wiki.attachment.Attachment;
+import org.jdom2.Element;
+import org.jdom2.output.Format;
+import org.jdom2.output.XMLOutputter;
+
+import javax.servlet.ServletContext;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Iterator;
+import java.util.List;
/**
* Represents an RSS 2.0 feed (with enclosures). This feed provides no
@@ -150,10 +152,10 @@ public class RSS20Feed extends Feed
channel.addContent( new Element("language").setText(getChannelLanguage()));
channel.addContent( new Element("generator").setText("JSPWiki "+Release.VERSTR));
- String mail = engine.getVariable(m_wikiContext,RSSGenerator.PROP_RSS_AUTHOREMAIL);
+ String mail = engine.getVariableManager().getVariable(m_wikiContext,RSSGenerator.PROP_RSS_AUTHOREMAIL);
if( mail != null )
{
- String editor = engine.getVariable( m_wikiContext,RSSGenerator.PROP_RSS_AUTHOR );
+ String editor = engine.getVariableManager().getVariable( m_wikiContext,RSSGenerator.PROP_RSS_AUTHOR );
if( editor != null )
mail = mail + " ("+editor+")";
@@ -174,15 +176,12 @@ public class RSS20Feed extends Feed
output.setFormat( Format.getPrettyFormat() );
- try
- {
- StringWriter res = new StringWriter();
+ try {
+ final StringWriter res = new StringWriter();
output.output( root, res );
return res.toString();
- }
- catch( IOException e )
- {
+ } catch( final IOException e ) {
return null;
}
}
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java
index 383bafa..058d9e6 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSGenerator.java
@@ -421,14 +421,14 @@ public class RSSGenerator {
{
feed.setChannelTitle( m_engine.getApplicationName()+": "+wikiContext.getPage().getName() );
feed.setFeedURL( wikiContext.getViewURL( wikiContext.getPage().getName() ) );
- String language = m_engine.getVariable( wikiContext, PROP_CHANNEL_LANGUAGE );
+ String language = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_LANGUAGE );
if( language != null )
feed.setChannelLanguage( language );
else
feed.setChannelLanguage( m_channelLanguage );
- String channelDescription = m_engine.getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION );
+ String channelDescription = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION );
if( channelDescription != null )
{
@@ -490,39 +490,39 @@ public class RSSGenerator {
* @return A String of valid RSS or Atom.
* @throws ProviderException If reading of pages was not possible.
*/
- protected String generateBlogRSS( WikiContext wikiContext, List< WikiPage > changed, Feed feed )
- throws ProviderException
- {
- if( log.isDebugEnabled() ) log.debug("Generating RSS for blog, size="+changed.size());
+ protected String generateBlogRSS( WikiContext wikiContext, List< WikiPage > changed, Feed feed ) {
+ if( log.isDebugEnabled() ) {
+ log.debug( "Generating RSS for blog, size=" + changed.size() );
+ }
- String ctitle = m_engine.getVariable( wikiContext, PROP_CHANNEL_TITLE );
+ String ctitle = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_TITLE );
- if( ctitle != null )
+ if( ctitle != null ) {
feed.setChannelTitle( ctitle );
- else
- feed.setChannelTitle( m_engine.getApplicationName()+":"+wikiContext.getPage().getName() );
+ } else {
+ feed.setChannelTitle( m_engine.getApplicationName() + ":" + wikiContext.getPage().getName() );
+ }
feed.setFeedURL( wikiContext.getViewURL( wikiContext.getPage().getName() ) );
- String language = m_engine.getVariable( wikiContext, PROP_CHANNEL_LANGUAGE );
+ String language = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_LANGUAGE );
- if( language != null )
+ if( language != null ) {
feed.setChannelLanguage( language );
- else
+ } else {
feed.setChannelLanguage( m_channelLanguage );
+ }
- String channelDescription = m_engine.getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION );
+ String channelDescription = m_engine.getVariableManager().getVariable( wikiContext, PROP_CHANNEL_DESCRIPTION );
- if( channelDescription != null )
- {
+ if( channelDescription != null ) {
feed.setChannelDescription( channelDescription );
}
Collections.sort( changed, new PageTimeComparator() );
int items = 0;
- for( Iterator< WikiPage > i = changed.iterator(); i.hasNext() && items < 15; items++ )
- {
+ for( Iterator< WikiPage > i = changed.iterator(); i.hasNext() && items < 15; items++ ) {
WikiPage page = i.next();
Entry e = new Entry();
@@ -531,19 +531,10 @@ public class RSSGenerator {
String url;
- if( page instanceof Attachment )
- {
- url = m_engine.getURL( WikiContext.ATTACH,
- page.getName(),
- null,
- true );
- }
- else
- {
- url = m_engine.getURL( WikiContext.VIEW,
- page.getName(),
- null,
- true );
+ if( page instanceof Attachment ) {
+ url = m_engine.getURL( WikiContext.ATTACH, page.getName(),null,true );
+ } else {
+ url = m_engine.getURL( WikiContext.VIEW, page.getName(),null, true );
}
e.setURL( url );
@@ -557,15 +548,18 @@ public class RSSGenerator {
String title = "";
int firstLine = pageText.indexOf('\n');
- if( firstLine > 0 )
- {
+ if( firstLine > 0 ) {
title = pageText.substring( 0, firstLine ).trim();
}
- if( title.length() == 0 ) title = page.getName();
+ if( title.length() == 0 ) {
+ title = page.getName();
+ }
// Remove wiki formatting
- while( title.startsWith("!") ) title = title.substring(1);
+ while( title.startsWith("!") ) {
+ title = title.substring(1);
+ }
e.setTitle( title );
@@ -573,28 +567,24 @@ public class RSSGenerator {
// Description
//
- if( firstLine > 0 )
- {
+ if( firstLine > 0 ) {
int maxlen = pageText.length();
if( maxlen > MAX_CHARACTERS ) maxlen = MAX_CHARACTERS;
- if( maxlen > 0 )
- {
+ if( maxlen > 0 ) {
pageText = m_engine.textToHTML( wikiContext,
pageText.substring( firstLine+1,
maxlen ).trim() );
- if( maxlen == MAX_CHARACTERS ) pageText += "...";
+ if( maxlen == MAX_CHARACTERS ) {
+ pageText += "...";
+ }
e.setContent( pageText );
- }
- else
- {
+ } else {
e.setContent( title );
}
- }
- else
- {
+ } else {
e.setContent( title );
}
diff --git a/jspwiki-war/src/main/webapp/templates/210/DiffTab.jsp b/jspwiki-war/src/main/webapp/templates/210/DiffTab.jsp
index 17d7f56..f55b68f 100644
--- a/jspwiki-war/src/main/webapp/templates/210/DiffTab.jsp
+++ b/jspwiki-war/src/main/webapp/templates/210/DiffTab.jsp
@@ -31,7 +31,7 @@
WikiContext c = WikiContext.findContext( pageContext );
List history = c.getEngine().getVersionHistory(c.getPage().getName());
pageContext.setAttribute( "history", history );
- pageContext.setAttribute( "diffprovider", c.getEngine().getVariable(c,"jspwiki.diffProvider"));
+ pageContext.setAttribute( "diffprovider", c.getEngine().getVariableManager().getVariable(c,"jspwiki.diffProvider"));
%>
<wiki:PageExists>
diff --git a/jspwiki-war/src/main/webapp/templates/default/DiffTab.jsp b/jspwiki-war/src/main/webapp/templates/default/DiffTab.jsp
index f7986dc..1f7972b 100644
--- a/jspwiki-war/src/main/webapp/templates/default/DiffTab.jsp
+++ b/jspwiki-war/src/main/webapp/templates/default/DiffTab.jsp
@@ -30,7 +30,7 @@
WikiContext c = WikiContext.findContext( pageContext );
%>
<c:set var="history" value="<%= c.getEngine().getVersionHistory(c.getPage().getName()) %>" />
-<c:set var="diffprovider" value='<%= c.getEngine().getVariable(c,"jspwiki.diffProvider") %>' />
+<c:set var="diffprovider" value='<%= c.getEngine().getVariableManager().getVariable(c,"jspwiki.diffProvider") %>' />
<wiki:PageExists>
<form action="<wiki:Link jsp='Diff.jsp' format='url' />"
class="diffbody form-inline"