Author: agilliland
Date: Thu Jan 12 21:26:55 2006
New Revision: 368600
URL: http://svn.apache.org/viewcvs?rev=368600&view=rev
Log:
code reformatting.
Modified:
incubator/roller/trunk/src/org/roller/presentation/velocity/ContextLoader.java
Modified:
incubator/roller/trunk/src/org/roller/presentation/velocity/ContextLoader.java
URL:
http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/velocity/ContextLoader.java?rev=368600&r1=368599&r2=368600&view=diff
==============================================================================
---
incubator/roller/trunk/src/org/roller/presentation/velocity/ContextLoader.java
(original)
+++
incubator/roller/trunk/src/org/roller/presentation/velocity/ContextLoader.java
Thu Jan 12 21:26:55 2006
@@ -9,12 +9,10 @@
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
-
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.RequestUtils;
@@ -46,47 +44,48 @@
import org.roller.util.StringUtils;
import org.roller.util.Utilities;
+
/**
* Load Velocity Context with Roller objects, values, and custom plugins.
- *
+ *
* @author llavandowska
* @author David M Johnson
*/
-public class ContextLoader
-{
+public class ContextLoader {
+
+ private static Log mLogger = LogFactory.getLog(ContextLoader.class);
+
+ private static final String TOOLBOX_KEY =
+ "org.roller.presentation.velocity.toolbox";
+
+ private static final String TOOLBOX_MANAGER_KEY =
+ "org.roller.presentation.velocity.toolboxManager";
+
private RollerRequest mRollerReq = null;
- private static Log mLogger =
- LogFactory.getFactory().getInstance(ContextLoader.class);
-
- //------------------------------------------------------------------------
/**
* Setup the a Velocity context by loading it with objects, values, and
* RollerPagePlugins needed for Roller page execution.
*/
- public static void setupContext( Context ctx,
- RollerRequest rreq, HttpServletResponse response )
- throws RollerException
- {
+ public static void setupContext(Context ctx, RollerRequest rreq,
HttpServletResponse response )
+ throws RollerException {
+
mLogger.debug("setupContext( ctx = "+ctx+")");
HttpServletRequest request = rreq.getRequest();
RollerContext rollerCtx = RollerContext.getRollerContext( request );
- try
- {
- // Add page model object to context
- String pageModelClassName =
- RollerConfig.getProperty("velocity.pagemodel.classname");
+ try {
+ // Add page model object to context
+ String pageModelClassName =
+ RollerConfig.getProperty("velocity.pagemodel.classname");
Class pageModelClass = Class.forName(pageModelClassName);
PageModel pageModel = (PageModel)pageModelClass.newInstance();
pageModel.init(rreq);
ctx.put("pageModel", pageModel );
ctx.put("pages", pageModel.getPages());
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
throw new RollerException("ERROR creating Page Model",e);
}
@@ -94,99 +93,92 @@
PageHelper pageHelper = new PageHelper(request, response, ctx);
Roller roller = RollerFactory.getRoller();
ctx.put("pageHelper", pageHelper);
-
- // Load standard Roller objects and values into the context
+
+ // Load standard Roller objects and values into the context
String handle = loadWebsiteValues(ctx, rreq, rollerCtx );
- loadWeblogValues( ctx, rreq, rollerCtx, handle );
- loadPathValues( ctx, rreq, rollerCtx, handle );
- loadRssValues( ctx, rreq, handle );
- loadUtilityObjects( ctx, rreq, rollerCtx, handle );
+ loadWeblogValues( ctx, rreq, rollerCtx, handle );
+ loadPathValues( ctx, rreq, rollerCtx, handle );
+ loadRssValues( ctx, rreq, handle );
+ loadUtilityObjects( ctx, rreq, rollerCtx, handle );
loadRequestParamKeys(ctx);
loadStatusMessage( ctx, rreq );
// If single entry is specified, load comments too
- if ( rreq.getWeblogEntry() != null )
- {
+ if ( rreq.getWeblogEntry() != null ) {
loadCommentValues( ctx, rreq, rollerCtx );
}
// add Velocity Toolbox tools to context
- loadToolboxContext(request, response, ctx);
+ loadToolboxContext(request, response, ctx);
}
- //------------------------------------------------------------------------
/**
* If there is an ERROR or STATUS message in the session,
* place it into the Context for rendering later.
- *
+ *
* @param rreq
*/
- private static void loadStatusMessage(Context ctx, RollerRequest rreq)
- {
+ private static void loadStatusMessage(Context ctx, RollerRequest rreq) {
+
mLogger.debug("Loading status message");
HttpSession session = rreq.getRequest().getSession(false);
String msg = null;
if (session != null)
msg = (String)session.getAttribute(RollerSession.ERROR_MESSAGE);
- if (msg != null)
- {
+ if (msg != null) {
ctx.put("errorMessage", msg);
session.removeAttribute(RollerSession.ERROR_MESSAGE);
}
-
+
if (session != null)
msg = (String)session.getAttribute(RollerSession.STATUS_MESSAGE);
- if (msg != null)
- {
+ if (msg != null) {
ctx.put("statusMessage", msg);
session.removeAttribute(RollerSession.STATUS_MESSAGE);
}
}
- //------------------------------------------------------------------------
-
+
/**
* @param ctx
* @param rreq
* @param rollerCtx
* @param userName
*/
- private static void loadWeblogValues(
- Context ctx, RollerRequest rreq, RollerContext rollerCtx, String handle)
- throws RollerException
- {
+ private static void loadWeblogValues(Context ctx, RollerRequest rreq,
+ RollerContext rollerCtx, String handle)
+ throws RollerException {
+
mLogger.debug("Loading weblog values");
// if there is an "_entry" page, only load it once
- WebsiteData website =
-
RollerFactory.getRoller().getUserManager().getWebsiteByHandle(handle);
- if (website != null)
- {
+ WebsiteData website =
+
RollerFactory.getRoller().getUserManager().getWebsiteByHandle(handle);
+ if (website != null) {
/* alternative display pages - customization */
Template entryPage = website.getPageByName("_entry");
- if (entryPage != null)
- {
+ if (entryPage != null) {
ctx.put("entryPage", TemplateWrapper.wrap(entryPage));
}
Template descPage = website.getPageByName("_desc");
- if (descPage != null)
- {
+ if (descPage != null) {
ctx.put("descPage", TemplateWrapper.wrap(descPage));
}
}
- boolean commentsEnabled =
- RollerRuntimeConfig.getBooleanProperty("users.comments.enabled");
- boolean trackbacksEnabled =
- RollerRuntimeConfig.getBooleanProperty("users.trackbacks.enabled");
+ boolean commentsEnabled =
+
RollerRuntimeConfig.getBooleanProperty("users.comments.enabled");
+ boolean trackbacksEnabled =
+
RollerRuntimeConfig.getBooleanProperty("users.trackbacks.enabled");
ctx.put("commentsEnabled", new Boolean(commentsEnabled) );
ctx.put("trackbacksEnabled", new Boolean(trackbacksEnabled) );
}
-
- private static String figureResourcePath( RollerRequest rreq )
- {
+
+
+ private static String figureResourcePath(RollerRequest rreq) {
+
String uploadurl = null;
try {
uploadurl =
RollerFactory.getRoller().getFileManager().getUploadUrl();
@@ -195,74 +187,66 @@
return uploadurl;
}
- //------------------------------------------------------------------------
- protected static void loadCommentValues(
- Context ctx, RollerRequest rreq, RollerContext rollerCtx )
- throws RollerException
- {
+ protected static void loadCommentValues(Context ctx, RollerRequest rreq,
RollerContext rollerCtx )
+ throws RollerException {
+
mLogger.debug("Loading comment values");
HttpServletRequest request = rreq.getRequest();
- String escapeHtml =
- RollerRuntimeConfig.getProperty("users.comments.escapehtml");
- String autoFormat =
- RollerRuntimeConfig.getProperty("users.comments.autoformat");
+ String escapeHtml =
+ RollerRuntimeConfig.getProperty("users.comments.escapehtml");
+ String autoFormat =
+ RollerRuntimeConfig.getProperty("users.comments.autoformat");
// Add comments related values to context
ctx.put("isCommentPage", Boolean.TRUE);
ctx.put("escapeHtml", new Boolean(escapeHtml) );
- ctx.put("autoformat", new Boolean(autoFormat) );
+ ctx.put("autoformat", new Boolean(autoFormat) );
// Make sure comment form object is available in context
- CommentFormEx commentForm =
+ CommentFormEx commentForm =
(CommentFormEx) request.getAttribute("commentForm");
- if ( commentForm == null )
- {
+ if ( commentForm == null ) {
commentForm = new CommentFormEx();
-
+
// Set fields to spaces to please Velocity
commentForm.setName("");
commentForm.setEmail("");
commentForm.setUrl("");
commentForm.setContent("");
- }
- ctx.put("commentForm",commentForm);
-
- // Either put a preview comment in to context
- if ( request.getAttribute("previewComments")!=null )
- {
+ }
+ ctx.put("commentForm",commentForm);
+
+ // Either put a preview comment in to context
+ if ( request.getAttribute("previewComments")!=null ) {
ArrayList list = new ArrayList();
CommentData cd = new CommentData();
commentForm.copyTo(cd, request.getLocale());
list.add(CommentDataWrapper.wrap(cd));
- ctx.put("previewComments",list);
+ ctx.put("previewComments",list);
}
WeblogEntryData entry = rreq.getWeblogEntry();
if (entry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
ctx.put("entry", WeblogEntryDataWrapper.wrap(entry));
}
- }
-
- //------------------------------------------------------------------------
+ }
+
- protected static void loadPathValues(
- Context ctx, RollerRequest rreq, RollerContext rollerCtx, String
userName)
- throws RollerException
- {
+ protected static void loadPathValues(Context ctx, RollerRequest rreq,
+ RollerContext rollerCtx, String
userName)
+ throws RollerException {
+
mLogger.debug("Loading path values");
HttpServletRequest request = rreq.getRequest();
String url = null;
- if ( userName != null && !userName.equals("zzz_none_zzz"))
- {
- url = Utilities.escapeHTML(
- rollerCtx.getAbsoluteContextUrl(request)+"/page/"+userName);
- }
- else
- {
+ if ( userName != null && !userName.equals("zzz_none_zzz")) {
+ url = Utilities.escapeHTML(
+
rollerCtx.getAbsoluteContextUrl(request)+"/page/"+userName);
+ } else {
url=
Utilities.escapeHTML(rollerCtx.getAbsoluteContextUrl(request));
}
ctx.put("websiteURL", url);
@@ -271,21 +255,17 @@
ctx.put("ctxPath", request.getContextPath() );
ctx.put("uploadPath", ContextLoader.figureResourcePath( rreq ) );
- try
- {
+ try {
URL absUrl = RequestUtils.absoluteURL(request, "/");
ctx.put("host", absUrl.getHost());
- }
- catch (MalformedURLException e)
- {
+ } catch (MalformedURLException e) {
throw new RollerException(e);
}
}
-
- //------------------------------------------------------------------------
- protected static void loadRequestParamKeys(Context ctx)
- {
+
+ protected static void loadRequestParamKeys(Context ctx) {
+
mLogger.debug("Loading request param keys");
// Since Velocity *requires* accessor methods, these values from
@@ -307,60 +287,52 @@
ctx.put("WEBLOGDAY_KEY", RollerRequest.WEBLOGDAY_KEY);
ctx.put("WEBLOGCOMMENTID_KEY", RollerRequest.WEBLOGCOMMENTID_KEY);
}
-
- //------------------------------------------------------------------------
- protected static void loadRssValues(
- Context ctx, RollerRequest rreq, String handle) throws RollerException
- {
+
+ protected static void loadRssValues(Context ctx, RollerRequest rreq,
String handle)
+ throws RollerException {
+
mLogger.debug("Loading rss values");
HttpServletRequest request = rreq.getRequest();
int entryLength = -1;
String sExcerpts = request.getParameter("excerpts");
- if ( sExcerpts!=null && sExcerpts.equalsIgnoreCase("true"))
- {
+ if ( sExcerpts!=null && sExcerpts.equalsIgnoreCase("true")) {
entryLength = 150;
}
ctx.put("entryLength", new Integer(entryLength));
int entryCount = 15;
String sCount = request.getParameter("count");
- if ( sCount!=null && sExcerpts!=null && sExcerpts.trim().equals(""))
- {
- try
- {
+ if ( sCount!=null && sExcerpts!=null && sExcerpts.trim().equals("")) {
+ try {
entryCount = Integer.parseInt(sCount);
- }
- catch (NumberFormatException e)
- {
+ } catch (NumberFormatException e) {
mLogger.warn("Improperly formatted count parameter");
}
if ( entryCount > 50 ) entryCount = 50;
if ( entryCount < 0 ) entryCount = 15;
}
ctx.put("entryCount", new Integer(entryCount));
-
+
String catname = null;
String catPath = null;
- if ( rreq.getWeblogCategory() != null )
- {
+ if ( rreq.getWeblogCategory() != null ) {
catname = rreq.getWeblogCategory().getName();
catPath = rreq.getWeblogCategory().getPath();
- }
+ }
ctx.put("catname", (catname!=null) ? catname : "");
ctx.put("catPath", (catPath != null) ? catPath : "");
ctx.put("updateTime", request.getAttribute("updateTime"));
ctx.put("now", new Date());
}
-
- //------------------------------------------------------------------------
- protected static void loadUtilityObjects(
- Context ctx, RollerRequest rreq, RollerContext rollerCtx, String
handle)
- throws RollerException
- {
+
+ protected static void loadUtilityObjects(Context ctx, RollerRequest rreq,
+ RollerContext rollerCtx, String
handle)
+ throws RollerException {
+
mLogger.debug("Loading utility objects");
// date formatter for macro's
@@ -369,21 +341,20 @@
Locale viewLocale = (Locale) ctx.get("viewLocale");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", viewLocale);
WebsiteData website = rreq.getWebsite();
- if (website != null)
- {
+ if (website != null) {
sdf.setTimeZone(website.getTimeZoneInstance());
}
// add formatter to context
ctx.put("dateFormatter", sdf );
-
+
// Note: in the macro's, the formats are taken from the
ResourceBundles.
// Only the plainFormat is specified here, because it is used to render
// the Entry Day link.
ctx.put("plainFormat", "yyyyMMdd");
-
+
ctx.put("page", TemplateWrapper.wrap(rreq.getPage()));
ctx.put("utilities", new Utilities() );
- ctx.put("stringUtils", new StringUtils() );
+ ctx.put("stringUtils", new StringUtils() );
ctx.put("rollerVersion", rollerCtx.getRollerVersion() );
ctx.put("rollerBuildTime", rollerCtx.getRollerBuildTime() );
ctx.put("rollerBuildUser", rollerCtx.getRollerBuildUser() );
@@ -392,30 +363,24 @@
ctx.put("requestParameters", rreq.getRequest().getParameterMap());
}
- //------------------------------------------------------------------------
- protected static String loadWebsiteValues(
- Context ctx, RollerRequest rreq, RollerContext rollerCtx )
- throws RollerException
- {
+ protected static String loadWebsiteValues(Context ctx, RollerRequest rreq,
RollerContext rollerCtx )
+ throws RollerException {
+
String handle = null;
WebsiteData website = null;
Roller mRoller = RollerFactory.getRoller();
Map props = mRoller.getPropertiesManager().getProperties();
- if ( rreq.getRequest().getAttribute(RollerRequest.OWNING_WEBSITE) !=
null)
- {
+ if ( rreq.getRequest().getAttribute(RollerRequest.OWNING_WEBSITE) !=
null) {
website = (WebsiteData)
- rreq.getRequest().getAttribute(RollerRequest.OWNING_WEBSITE);
- }
- else if ( rreq.getWebsite() != null )
- {
+ rreq.getRequest().getAttribute(RollerRequest.OWNING_WEBSITE);
+ } else if ( rreq.getWebsite() != null ) {
website = rreq.getWebsite();
}
- if ( website != null )
- {
+ if ( website != null ) {
handle = website.getHandle();
ctx.put("userName", handle);
ctx.put("fullName", website.getName() );
@@ -425,13 +390,11 @@
// setup Locale for future rendering
ctx.put("locale", website.getLocaleInstance());
-
+
// setup Timezone for future rendering
ctx.put("timezone", website.getTimeZoneInstance());
ctx.put("timeZone", website.getTimeZoneInstance());
- }
- else
- {
+ } else {
website = new WebsiteData();
website.setName(((RollerPropertyData)props.get("site.name")).getValue());
website.setAllowComments(Boolean.FALSE);
@@ -440,49 +403,42 @@
ctx.put("userName", handle );
ctx.put("fullName","zzz_none_zzz");
ctx.put("emailAddress",
- ((RollerPropertyData)props.get("site.adminemail")).getValue());
+
((RollerPropertyData)props.get("site.adminemail")).getValue());
ctx.put("locale", Locale.getDefault());
ctx.put("timezone", TimeZone.getDefault());
ctx.put("timeZone", TimeZone.getDefault());
}
ctx.put("website", WebsiteDataWrapper.wrap(website) );
-
+
String siteName =
((RollerPropertyData)props.get("site.name")).getValue();
if ("Roller-based Site".equals(siteName)) siteName = "Main";
- ctx.put("siteName", siteName);
-
+ ctx.put("siteName", siteName);
+
String siteShortName =
((RollerPropertyData)props.get("site.shortName")).getValue();
- ctx.put("siteShortName", siteShortName);
-
+ ctx.put("siteShortName", siteShortName);
+
// add language of the session (using locale of viewer set by Struts)
ctx.put(
- "viewLocale",
- LanguageUtil.getViewLocale(rreq.getRequest()));
+ "viewLocale",
+ LanguageUtil.getViewLocale(rreq.getRequest()));
mLogger.debug("context viewLocale = "+ctx.get( "viewLocale"));
-
+
return handle;
}
- //------------------------------------------------------------------------
-
- private static final String TOOLBOX_KEY =
- "org.roller.presentation.velocity.toolbox";
-
- private static final String TOOLBOX_MANAGER_KEY =
- "org.roller.presentation.velocity.toolboxManager";
-
- public static ToolboxContext loadToolboxContext(
- HttpServletRequest request, HttpServletResponse response,
Context ctx)
- {
+
+ public static ToolboxContext loadToolboxContext(HttpServletRequest
request,
+ HttpServletResponse
response,
+ Context ctx) {
+
mLogger.debug("Loading toolbox context");
ServletContext servletContext = RollerContext.getServletContext();
-
+
// get the toolbox manager
- ServletToolboxManager toolboxManager =
-
(ServletToolboxManager)servletContext.getAttribute(TOOLBOX_MANAGER_KEY);
- if (toolboxManager==null)
- {
+ ServletToolboxManager toolboxManager =
+
(ServletToolboxManager)servletContext.getAttribute(TOOLBOX_MANAGER_KEY);
+ if (toolboxManager==null) {
String file = RollerConfig.getProperty("velocity.toolbox.file");
mLogger.debug("Creating new toolboxContext using config-file:
"+file);
toolboxManager = ServletToolboxManager.getInstance(servletContext,
file);
@@ -490,17 +446,16 @@
}
// load a toolbox context
- ChainedContext chainedContext =
- new ChainedContext(ctx, request, response, servletContext);
- ToolboxContext toolboxContext =
- toolboxManager.getToolboxContext(chainedContext);
-
- if (toolboxContext != null)
- {
+ ChainedContext chainedContext =
+ new ChainedContext(ctx, request, response, servletContext);
+ ToolboxContext toolboxContext =
+ toolboxManager.getToolboxContext(chainedContext);
+
+ if (toolboxContext != null) {
// add MessageTool to VelocityContext
ctx.put("text", toolboxContext.internalGet("text"));
- /*
+ /*
Object[] keys = toolboxContext.internalGetKeys();
for (int i=0;i<keys.length;i++) {
String key = (String)keys[i];
@@ -509,9 +464,10 @@
System.out.println("tool = "+tool);
ctx.put(key, tool);
}
- */
+ */
}
- return toolboxContext;
+ return toolboxContext;
}
+
}