You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ma...@apache.org on 2003/10/30 01:03:37 UTC
cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts TilesTool.java
marino 2003/10/29 16:03:37
Modified: src/java/org/apache/velocity/tools/struts TilesTool.java
Log:
updated to extend ImportSupport
Revision Changes Path
1.2 +64 -124 jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/TilesTool.java
Index: TilesTool.java
===================================================================
RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/TilesTool.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
+++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
@@ -61,10 +61,6 @@
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
-import java.io.StringWriter;
-import org.apache.velocity.app.Velocity;
-import org.apache.velocity.Template;
-
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ComponentDefinition;
import org.apache.struts.tiles.AttributeDefinition;
@@ -76,10 +72,11 @@
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.Controller;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.tools.view.ImportSupport;
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ViewTool;
-
/**
* <p>Title: TilesTool</p>
* <p>Description: A tool to use struts-tiles with Velocity</p>
@@ -94,13 +91,10 @@
* @author <a href="mailto:marinoj@centrum.is">Marino A. Jonsson</a>
* @version $Revision$ $Date$
*/
-public class TilesTool implements ViewTool
-{
+public class TilesTool extends ImportSupport
+ implements ViewTool {
protected ViewContext context;
- protected ServletContext application;
- protected HttpServletRequest request;
- protected HttpServletResponse response;
/**
* A stack to hold ComponentContexts while nested tile-definitions
@@ -108,7 +102,6 @@
*/
protected Stack contextStack;
-
/******************************* Constructors ****************************/
/**
@@ -116,54 +109,47 @@
*/
public TilesTool() {}
-
/**
* Initializes this tool.
*
* @param obj the current ViewContext
* @throws IllegalArgumentException if the param is not a ViewContext
*/
- public void init(Object obj)
- {
- if (!(obj instanceof ViewContext))
- {
+ public void init(Object obj) {
+ if (! (obj instanceof ViewContext)) {
throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
}
- this.context = (ViewContext)obj;
+ this.context = (ViewContext) obj;
this.request = context.getRequest();
this.response = context.getResponse();
this.application = context.getServletContext();
}
-
/***************************** View Helpers ******************************/
/**
* Fetches a named attribute value from the current tiles-context.
*
- * <p>This is functionally equivalent to
+ * <p>This is functionally equivalent to
* <code><tiles:getAsString name="title" /></code>.</p>
*
* @param name the name of the tiles-attribute to fetch
* @return the attribute value as String
*/
- public String getString(String name)
- {
+ public String getString(String name) {
ComponentContext context = ComponentContext.getContext(request);
Object attrValue = context.getAttribute(name);
- if (attrValue == null)
- {
+ if (attrValue == null) {
return null;
}
return attrValue.toString();
}
-
/**
* <p>A generic tiles insert function</p>
*
- * <p>This is functionally equivalent to
+ * <p>This is functionally equivalent to
* <code><tiles:insert attribute="menu" /></code>.</p>
*
* @param attr - can be any of the following:
@@ -175,49 +161,48 @@
* @return the rendered template or value as a String
* @throws Exception on failure
*/
- public String get(Object attr) throws Exception
- {
- ComponentContext currentContext = ComponentContext.getContext(request);
- Object attrValue = currentContext.getAttribute(attr.toString());
- if (attrValue != null)
- {
- return processObjectValue(attrValue);
+ public String get(Object attr) throws Exception {
+ try {
+ ComponentContext currentContext = ComponentContext.getContext(request);
+ Object attrValue = currentContext.getAttribute(attr.toString());
+ if (attrValue != null) {
+ return processObjectValue(attrValue);
+ }
+ return processAsDefinitionOrURL(attr.toString());
+ }
+ catch(Exception e) {
+ Velocity.error("Exeption while rendering Tile " + attr.toString() + ": " + e.getMessage());
+ return null;
}
- return processAsDefinitionOrURL(attr.toString());
}
-
/************************** Protected Methods ****************************/
/**
* Process an object retrieved as a bean or attribute.
*
* @param value - Object can be a typed attribute, a String, or anything
- * else. If typed attribute, use associated type. Otherwise, apply
+ * else. If typed attribute, use associated type. Otherwise, apply
* toString() on object, and use returned string as a name.
- * @throws Exception - Throws by underlying nested call to
+ * @throws Exception - Throws by underlying nested call to
* processDefinitionName()
* @return the fully processed value as String
*/
- protected String processObjectValue(Object value) throws Exception
- {
+ protected String processObjectValue(Object value) throws Exception {
/* First, check if value is one of the Typed Attribute */
- if (value instanceof AttributeDefinition)
- {
+ if (value instanceof AttributeDefinition) {
/* We have a type => return appropriate IncludeType */
- return processTypedAttribute((AttributeDefinition)value);
+ return processTypedAttribute( (AttributeDefinition) value);
}
- else if (value instanceof ComponentDefinition)
- {
- return processDefinition((ComponentDefinition)value);
+ else if (value instanceof ComponentDefinition) {
+ return processDefinition( (ComponentDefinition) value);
}
/* Value must denote a valid String */
return processAsDefinitionOrURL(value.toString());
}
-
/**
* Process typed attribute according to its type.
*
@@ -226,25 +211,21 @@
* @throws Exception - Throws by underlying nested call to processDefinitionName()
*/
protected String processTypedAttribute(AttributeDefinition value) throws Exception {
- if (value instanceof DirectStringAttribute)
- {
- return (String)value.getValue();
+ if (value instanceof DirectStringAttribute) {
+ return (String) value.getValue();
}
- else if (value instanceof DefinitionAttribute)
- {
- return processDefinition((ComponentDefinition)value.getValue());
+ else if (value instanceof DefinitionAttribute) {
+ return processDefinition( (ComponentDefinition) value.getValue());
}
- else if (value instanceof DefinitionNameAttribute)
- {
- return processAsDefinitionOrURL((String)value.getValue());
+ else if (value instanceof DefinitionNameAttribute) {
+ return processAsDefinitionOrURL( (String) value.getValue());
}
/* else if( value instanceof PathAttribute ) */
- return doInsert((String)value.getValue(), null, null);
+ return doInsert( (String) value.getValue(), null, null);
}
-
/**
* Try to process name as a definition, or as an URL if not found.
*
@@ -253,24 +234,20 @@
* @throws Exception
*/
protected String processAsDefinitionOrURL(String name) throws Exception {
- try
- {
+ try {
ComponentDefinition definition =
TilesUtil.getDefinition(name, request, application);
- if (definition != null)
- {
+ if (definition != null) {
return processDefinition(definition);
}
}
- catch (DefinitionsFactoryException ex)
- {
+ catch (DefinitionsFactoryException ex) {
/* silently failed, because we can choose to not define a factory. */
}
/* no definition found, try as url */
return processUrl(name);
}
-
/**
* End of Process for definition.
*
@@ -278,12 +255,10 @@
* @return the fully processed definition.
* @throws Exception from InstantiationException Can't create requested controller
*/
- protected String processDefinition(ComponentDefinition definition) throws Exception
- {
+ protected String processDefinition(ComponentDefinition definition) throws Exception {
Controller controller = null;
- try
- {
+ try {
controller = definition.getOrCreateController();
String role = definition.getRole();
@@ -294,13 +269,11 @@
role,
controller);
}
- catch (InstantiationException ex)
- {
+ catch (InstantiationException ex) {
throw new Exception(ex.getMessage());
}
}
-
/**
* Processes an url
*
@@ -308,12 +281,10 @@
* @return the rendered template as String.
* @throws Exception
*/
- protected String processUrl(String url) throws Exception
- {
+ protected String processUrl(String url) throws Exception {
return doInsert(url, null, null);
}
-
/**
* Use this if there is no nested tile.
*
@@ -323,10 +294,8 @@
* @return the rendered template as String.
* @throws Exception
*/
- protected String doInsert(String page, String role, Controller controller) throws Exception
- {
- if (role != null && !request.isUserInRole(role))
- {
+ protected String doInsert(String page, String role, Controller controller) throws Exception {
+ if (role != null && !request.isUserInRole(role)) {
return null;
}
@@ -334,7 +303,6 @@
return doInsert(subCompContext, page, role, controller);
}
-
/**
* Use this if there is a nested tile.
*
@@ -345,13 +313,11 @@
* @return the rendered template as String.
* @throws Exception
*/
- protected String doInsert(Map attributes,
- String page,
- String role,
- Controller controller) throws Exception
- {
- if (role != null && !request.isUserInRole(role))
- {
+ protected String doInsert(Map attributes,
+ String page,
+ String role,
+ Controller controller) throws Exception {
+ if (role != null && !request.isUserInRole(role)) {
return null;
}
@@ -359,7 +325,6 @@
return doInsert(subCompContext, page, role, controller);
}
-
/**
* An extension of the other two doInsert functions
*
@@ -371,71 +336,46 @@
* @return the rendered template as String.
* @throws Exception
*/
- protected String doInsert(ComponentContext subCompContext,
- String page,
+ protected String doInsert(ComponentContext subCompContext,
+ String page,
String role,
- Controller controller) throws Exception
- {
+ Controller controller) throws Exception {
pushTilesContext();
- try
- {
+ try {
ComponentContext.setContext(subCompContext, request);
/* Call controller if any */
- if (controller != null)
- {
+ if (controller != null) {
controller.perform(subCompContext,
request,
response,
application);
}
- return parse(page);
+ return this.acquireString(page);
}
- finally
- {
+ finally {
popTilesContext();
}
}
-
/**
* <p>pushes the current tiles context onto the context-stack.
* preserving the context is necessary so that a sub-context can be
* put into request scope and lower level tiles can be rendered</p>
*/
- protected void pushTilesContext()
- {
- if (contextStack == null)
- {
+ protected void pushTilesContext() {
+ if (contextStack == null) {
contextStack = new Stack();
}
contextStack.push(ComponentContext.getContext(request));
}
-
/**
* <p>pops the tiles sub-context off the context-stack after the lower level
* tiles have been rendered</p>
*/
- protected void popTilesContext()
- {
- ComponentContext.setContext((ComponentContext)contextStack.pop(), request);
- }
-
-
- /**
- * <p>Renders a template</p>
- *
- * @param templateName - name of template to be rendered
- * @throws Exception if it fails
- * @return the rendered template as a String
- */
- protected String parse(String templateName) throws Exception
- {
- StringWriter sw = new StringWriter();
- Template template = Velocity.getTemplate(templateName);
- template.merge(context.getVelocityContext(), sw);
- return sw.toString();
+ protected void popTilesContext() {
+ ComponentContext.setContext( (ComponentContext) contextStack.pop(), request);
}
-}
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts
TilesTool.java
Posted by Daniel Rall <dl...@finemaltcoding.com>.
Great. If you're unfamiliar with the coding guidelines for a project, making
sure you don't diverge from the existing style of a file will never get you into
hot water. Examing a diff of your change before committing ought to help with
tool-induced changes like auto formatting, and is a best practice for a final
pass at catching mistakes.
Marinó A. Jónsson wrote:
> thanks man, I'll adjust my formatting settings accordingly.
>
>
>>-----Original Message-----
>>From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>>Sent: 30. október 2003 16:04
>>To: Velocity Developers List
>>Subject: Re: cvs commit:
>>jakarta-velocity-tools/src/java/org/apache/velocity/tools/stru
>>ts TilesTool.java
>>
>>
>>http://jakarta.apache.org/velocity/code-standards.html
>>
>>On Thursday, October 30, 2003, at 09:57 AM, Marinó A. Jónsson wrote:
>>
>>
>>>sorry guys, didn't mean to bug ya :)
>>>
>>>it isn't as if I manually reformatted the source - I just used the
>>>autoformatting features in the modified sections ... force
>>
>>of habit I
>>
>>>guess. if you could point me to some basic formatting
>>
>>guidelines for
>>
>>>Velocity I'd
>>>be much abliged.
>>>
>>>cheers,
>>>Marinó
>>>
>>>
>>>>-----Original Message-----
>>>>From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>>>>Sent: 30. október 2003 11:22
>>>>To: Velocity Developers List
>>>>Subject: Re: cvs commit:
>>>>jakarta-velocity-tools/src/java/org/apache/velocity/tools/stru
>>>>ts TilesTool.java
>>>>
>>>>
>>>>
>>>>On Thursday, October 30, 2003, at 12:13 AM, Daniel L. Rall wrote:
>>>>
>>>>
>>>>>Why did you reformat the source file? Generally speaking, making
>>>>>changes that fit in with the existing style is preferred.
>>>>
>>>>Yes please.
>>>>
>>>>
>>>>>- Dan
>>>>>
>>>>>
>>>>>marino@apache.org wrote:
>>>>>
>>>>>>marino 2003/10/29 16:03:37
>>>>>> Modified: src/java/org/apache/velocity/tools/struts
>>>>>>TilesTool.java
>>>>>> Log:
>>>>>> updated to extend ImportSupport
>>>>>> Revision Changes Path
>>>>>> 1.2 +64 -124
>>>>>>jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/
>>>>>>TilesTool.java
>>>>>> Index: TilesTool.java
>>>>>>
>>>>
>>>>===================================================================
>>>>
>>>>>> RCS file:
>>>>>>
>>>>
>>/home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/
>>
>>>>>>struts/TilesTool.java,v
>>>>>> retrieving revision 1.1
>>>>>> retrieving revision 1.2
>>>>>> diff -u -r1.1 -r1.2
>>>>>> --- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
>>>>>> +++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
>>>>>> @@ -61,10 +61,6 @@
>>>>>> import javax.servlet.http.HttpSession;
>>>>>> import javax.servlet.ServletContext;
>>>>>> -import java.io.StringWriter;
>>>>>> -import org.apache.velocity.app.Velocity;
>>>>>> -import org.apache.velocity.Template;
>>>>>> -
>>>>>> import org.apache.struts.tiles.ComponentContext;
>>>>>> import org.apache.struts.tiles.ComponentDefinition;
>>>>>> import org.apache.struts.tiles.AttributeDefinition;
>>>>>> @@ -76,10 +72,11 @@
>>>>>> import org.apache.struts.tiles.DefinitionsFactoryException;
>>>>>> import org.apache.struts.tiles.Controller;
>>>>>> +import org.apache.velocity.app.Velocity;
>>>>>> +import org.apache.velocity.tools.view.ImportSupport;
>>>>>> import org.apache.velocity.tools.view.context.ViewContext;
>>>>>> import org.apache.velocity.tools.view.tools.ViewTool;
>>>>>> -
>>>>>> /**
>>>>>> * <p>Title: TilesTool</p>
>>>>>> * <p>Description: A tool to use struts-tiles with
>>
>>Velocity</p>
>>
>>>>>> @@ -94,13 +91,10 @@
>>>>>> * @author <a href="mailto:marinoj@centrum.is">Marino A.
>>>>>>Jonsson</a>
>>>>>> * @version $Revision$ $Date$
>>>>>> */
>>>>>> -public class TilesTool implements ViewTool
>>>>>> -{
>>>>>> +public class TilesTool extends ImportSupport
>>>>>> + implements ViewTool {
>>>>>> protected ViewContext context;
>>>>>> - protected ServletContext application;
>>>>>> - protected HttpServletRequest request;
>>>>>> - protected HttpServletResponse response;
>>>>>> /**
>>>>>> * A stack to hold ComponentContexts while nested
>>>>>>tile-definitions
>>>>>> @@ -108,7 +102,6 @@
>>>>>> */
>>>>>> protected Stack contextStack;
>>>>>> -
>>>>>> /******************************* Constructors
>>>>>>****************************/
>>>>>> /**
>>>>>> @@ -116,54 +109,47 @@
>>>>>> */
>>>>>> public TilesTool() {}
>>>>>> -
>>>>>> /**
>>>>>> * Initializes this tool.
>>>>>> *
>>>>>> * @param obj the current ViewContext
>>>>>> * @throws IllegalArgumentException if the param is not a
>>>>>>ViewContext
>>>>>> */
>>>>>> - public void init(Object obj)
>>>>>> - {
>>>>>> - if (!(obj instanceof ViewContext))
>>>>>> - {
>>>>>> + public void init(Object obj) {
>>>>>> + if (! (obj instanceof ViewContext)) {
>>>>>> throw new IllegalArgumentException("Tool
>>>>
>>>>can only be
>>>>
>>>>>>initialized with a ViewContext");
>>>>>> }
>>>>>> - this.context = (ViewContext)obj;
>>>>>> + this.context = (ViewContext) obj;
>>>>>> this.request = context.getRequest();
>>>>>> this.response = context.getResponse();
>>>>>> this.application = context.getServletContext();
>>>>>> }
>>>>>> -
>>>>>> /***************************** View Helpers
>>>>>>******************************/
>>>>>> /**
>>>>>> * Fetches a named attribute value from the current
>>>>>>tiles-context.
>>>>>> *
>>>>>> - * <p>This is functionally equivalent to + *
>>>>
>>>><p>This is
>>>>
>>>>>>functionally equivalent to
>>>>>> * <code><tiles:getAsString name="title" /></code>.</p>
>>>>>> *
>>>>>> * @param name the name of the tiles-attribute to fetch
>>>>>> * @return the attribute value as String
>>>>>> */
>>>>>> - public String getString(String name)
>>>>>> - {
>>>>>> + public String getString(String name) {
>>>>>> ComponentContext context =
>>>>>>ComponentContext.getContext(request);
>>>>>> Object attrValue = context.getAttribute(name);
>>>>>> - if (attrValue == null)
>>>>>> - {
>>>>>> + if (attrValue == null) {
>>>>>> return null;
>>>>>> }
>>>>>> return attrValue.toString();
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * <p>A generic tiles insert function</p>
>>>>>> *
>>>>>> - * <p>This is functionally equivalent to + *
>>>>
>>>><p>This is
>>>>
>>>>>>functionally equivalent to
>>>>>> * <code><tiles:insert attribute="menu" /></code>.</p>
>>>>>> *
>>>>>> * @param attr - can be any of the following:
>>>>>> @@ -175,49 +161,48 @@
>>>>>> * @return the rendered template or value as a String
>>>>>> * @throws Exception on failure
>>>>>> */
>>>>>> - public String get(Object attr) throws Exception
>>>>>> - {
>>>>>> - ComponentContext currentContext =
>>>>>>ComponentContext.getContext(request);
>>>>>> - Object attrValue =
>>>>>>currentContext.getAttribute(attr.toString());
>>>>>> - if (attrValue != null)
>>>>>> - {
>>>>>> - return processObjectValue(attrValue);
>>>>>> + public String get(Object attr) throws Exception {
>>>>>> + try {
>>>>>> + ComponentContext currentContext =
>>>>>>ComponentContext.getContext(request);
>>>>>> + Object attrValue =
>>>>>>currentContext.getAttribute(attr.toString());
>>>>>> + if (attrValue != null) {
>>>>>> + return processObjectValue(attrValue);
>>>>>> + }
>>>>>> + return processAsDefinitionOrURL(attr.toString());
>>>>>> + }
>>>>>> + catch(Exception e) {
>>>>>> + Velocity.error("Exeption while rendering Tile " +
>>>>>>attr.toString() + ": " + e.getMessage());
>>>>>> + return null;
>>>>>> }
>>>>>> - return processAsDefinitionOrURL(attr.toString());
>>>>>> }
>>>>>> -
>>>>>> /************************** Protected Methods
>>>>>>****************************/
>>>>>> /**
>>>>>> * Process an object retrieved as a bean or attribute.
>>>>>> *
>>>>>> * @param value - Object can be a typed attribute,
>>>>
>>>>a String,
>>>>
>>>>>>or anything
>>>>>> - * else. If typed attribute, use associated type.
>>>>>>Otherwise, apply + * else. If typed attribute, use
>>>>>>associated type. Otherwise, apply
>>>>>> * toString() on object, and use returned
>>>>
>>>>string as a
>>>>
>>>>>>name.
>>>>>> - * @throws Exception - Throws by underlying nested
>>>>
>>>>call to +
>>>>
>>>>>> * @throws Exception - Throws by underlying nested call to
>>>>>> * processDefinitionName()
>>>>>> * @return the fully processed value as String
>>>>>> */
>>>>>> - protected String processObjectValue(Object value) throws
>>>>>>Exception
>>>>>> - {
>>>>>> + protected String processObjectValue(Object value) throws
>>>>>>Exception {
>>>>>> /* First, check if value is one of the Typed
>>>>
>>>>Attribute */
>>>>
>>>>>> - if (value instanceof AttributeDefinition)
>>>>>> - {
>>>>>> + if (value instanceof AttributeDefinition) {
>>>>>> /* We have a type => return appropriate
>>>>
>>>>IncludeType */
>>>>
>>>>>> - return
>>>>>>processTypedAttribute((AttributeDefinition)value);
>>>>>> + return processTypedAttribute(
>>>>
>>>>(AttributeDefinition)
>>>>
>>>>>>value);
>>>>>> }
>>>>>> - else if (value instanceof ComponentDefinition)
>>>>>> - {
>>>>>> - return
>>>>
>>>>processDefinition((ComponentDefinition)value);
>>>>
>>>>>> + else if (value instanceof ComponentDefinition) {
>>>>>> + return processDefinition(
>>>>
>>>>(ComponentDefinition) value);
>>>>
>>>>>> }
>>>>>> /* Value must denote a valid String */
>>>>>> return processAsDefinitionOrURL(value.toString());
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * Process typed attribute according to its type.
>>>>>> *
>>>>>> @@ -226,25 +211,21 @@
>>>>>> * @throws Exception - Throws by underlying nested call to
>>>>>>processDefinitionName()
>>>>>> */
>>>>>> protected String processTypedAttribute(AttributeDefinition
>>>>>>value) throws Exception {
>>>>>> - if (value instanceof DirectStringAttribute)
>>>>>> - {
>>>>>> - return (String)value.getValue();
>>>>>> + if (value instanceof DirectStringAttribute) {
>>>>>> + return (String) value.getValue();
>>>>>> }
>>>>>> - else if (value instanceof DefinitionAttribute)
>>>>>> - {
>>>>>> - return
>>>>>>processDefinition((ComponentDefinition)value.getValue());
>>>>>> + else if (value instanceof DefinitionAttribute) {
>>>>>> + return processDefinition( (ComponentDefinition)
>>>>>>value.getValue());
>>>>>> }
>>>>>> - else if (value instanceof DefinitionNameAttribute)
>>>>>> - {
>>>>>> - return
>>>>>>processAsDefinitionOrURL((String)value.getValue());
>>>>>> + else if (value instanceof DefinitionNameAttribute) {
>>>>>> + return processAsDefinitionOrURL( (String)
>>>>>>value.getValue());
>>>>>> }
>>>>>> /* else if( value instanceof PathAttribute ) */
>>>>>> - return doInsert((String)value.getValue(), null, null);
>>>>>> + return doInsert( (String) value.getValue(),
>>
>>null, null);
>>
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * Try to process name as a definition, or as an
>>>>
>>>>URL if not
>>>>
>>>>>>found.
>>>>>> *
>>>>>> @@ -253,24 +234,20 @@
>>>>>> * @throws Exception
>>>>>> */
>>>>>> protected String processAsDefinitionOrURL(String
>>>>
>>>>name) throws
>>>>
>>>>>>Exception {
>>>>>> - try
>>>>>> - {
>>>>>> + try {
>>>>>> ComponentDefinition definition =
>>>>>> TilesUtil.getDefinition(name, request,
>>>>>>application);
>>>>>> - if (definition != null)
>>>>>> - {
>>>>>> + if (definition != null) {
>>>>>> return processDefinition(definition);
>>>>>> }
>>>>>> }
>>>>>> - catch (DefinitionsFactoryException ex)
>>>>>> - {
>>>>>> + catch (DefinitionsFactoryException ex) {
>>>>>> /* silently failed, because we can choose to not
>>>>>>define a factory. */
>>>>>> }
>>>>>> /* no definition found, try as url */
>>>>>> return processUrl(name);
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * End of Process for definition.
>>>>>> *
>>>>>> @@ -278,12 +255,10 @@
>>>>>> * @return the fully processed definition.
>>>>>> * @throws Exception from InstantiationException
>>>>
>>>>Can't create
>>>>
>>>>>>requested controller
>>>>>> */
>>>>>> - protected String processDefinition(ComponentDefinition
>>>>>>definition) throws Exception
>>>>>> - {
>>>>>> + protected String processDefinition(ComponentDefinition
>>>>>>definition) throws Exception {
>>>>>> Controller controller = null;
>>>>>> - try
>>>>>> - {
>>>>>> + try {
>>>>>> controller = definition.getOrCreateController();
>>>>>> String role = definition.getRole();
>>>>>> @@ -294,13 +269,11 @@
>>>>>> role,
>>>>>> controller);
>>>>>> }
>>>>>> - catch (InstantiationException ex)
>>>>>> - {
>>>>>> + catch (InstantiationException ex) {
>>>>>> throw new Exception(ex.getMessage());
>>>>>> }
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * Processes an url
>>>>>> *
>>>>>> @@ -308,12 +281,10 @@
>>>>>> * @return the rendered template as String.
>>>>>> * @throws Exception
>>>>>> */
>>>>>> - protected String processUrl(String url) throws Exception
>>>>>> - {
>>>>>> + protected String processUrl(String url) throws Exception {
>>>>>> return doInsert(url, null, null);
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * Use this if there is no nested tile.
>>>>>> *
>>>>>> @@ -323,10 +294,8 @@
>>>>>> * @return the rendered template as String.
>>>>>> * @throws Exception
>>>>>> */
>>>>>> - protected String doInsert(String page, String role,
>>>>
>>>>Controller
>>>>
>>>>>>controller) throws Exception
>>>>>> - {
>>>>>> - if (role != null && !request.isUserInRole(role))
>>>>>> - {
>>>>>> + protected String doInsert(String page, String role,
>>>>
>>>>Controller
>>>>
>>>>>>controller) throws Exception {
>>>>>> + if (role != null && !request.isUserInRole(role)) {
>>>>>> return null;
>>>>>> }
>>>>>> @@ -334,7 +303,6 @@
>>>>>> return doInsert(subCompContext, page, role,
>>
>>controller);
>>
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * Use this if there is a nested tile.
>>>>>> *
>>>>>> @@ -345,13 +313,11 @@
>>>>>> * @return the rendered template as String.
>>>>>> * @throws Exception
>>>>>> */
>>>>>> - protected String doInsert(Map attributes, -
>>>>
>>>>>> String page, - String
>>>>>>role, - Controller
>>>>
>>>>controller) throws
>>>>
>>>>>>Exception
>>>>>> - {
>>>>>> - if (role != null && !request.isUserInRole(role))
>>>>>> - {
>>>>>> + protected String doInsert(Map attributes,
>>>>>> + String page,
>>>>>> + String role,
>>>>>> + Controller controller) throws
>>>>>>Exception {
>>>>>> + if (role != null && !request.isUserInRole(role)) {
>>>>>> return null;
>>>>>> }
>>>>>> @@ -359,7 +325,6 @@
>>>>>> return doInsert(subCompContext, page, role,
>>
>>controller);
>>
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * An extension of the other two doInsert functions
>>>>>> *
>>>>>> @@ -371,71 +336,46 @@
>>>>>> * @return the rendered template as String.
>>>>>> * @throws Exception
>>>>>> */
>>>>>> - protected String doInsert(ComponentContext
>>>>
>>>>subCompContext, -
>>>>
>>>>>> String page, + protected String
>>>>>>doInsert(ComponentContext subCompContext,
>>>>>> + String page,
>>>>>> String role,
>>>>>> - Controller controller) throws
>>>>>>Exception
>>>>>> - {
>>>>>> + Controller controller) throws
>>>>>>Exception {
>>>>>> pushTilesContext();
>>>>>> - try
>>>>>> - {
>>>>>> + try {
>>>>>> ComponentContext.setContext(subCompContext,
>>>>
>>>>request);
>>>>
>>>>>> /* Call controller if any */
>>>>>> - if (controller != null)
>>>>>> - {
>>>>>> + if (controller != null) {
>>>>>> controller.perform(subCompContext,
>>>>>> request,
>>>>>> response,
>>>>>> application);
>>>>>> }
>>>>>> - return parse(page);
>>>>>> + return this.acquireString(page);
>>>>>> }
>>>>>> - finally
>>>>>> - {
>>>>>> + finally {
>>>>>> popTilesContext();
>>>>>> }
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * <p>pushes the current tiles context onto the
>>>>
>>>>context-stack.
>>>>
>>>>>> * preserving the context is necessary so that a
>>>>
>>>>sub-context
>>>>
>>>>>>can be
>>>>>> * put into request scope and lower level tiles can be
>>>>>>rendered</p>
>>>>>> */
>>>>>> - protected void pushTilesContext()
>>>>>> - {
>>>>>> - if (contextStack == null)
>>>>>> - {
>>>>>> + protected void pushTilesContext() {
>>>>>> + if (contextStack == null) {
>>>>>> contextStack = new Stack();
>>>>>> }
>>>>>>
>>
>>contextStack.push(ComponentContext.getContext(request));
>>
>>>>>> }
>>>>>> -
>>>>>> /**
>>>>>> * <p>pops the tiles sub-context off the
>>>>
>>>>context-stack after
>>>>
>>>>>>the lower level
>>>>>> * tiles have been rendered</p>
>>>>>> */
>>>>>> - protected void popTilesContext()
>>>>>> - {
>>>>>> -
>>>>>>ComponentContext.setContext((ComponentContext)contextStack.pop(),
>>>>>>request);
>>>>>> - }
>>>>>> -
>>>>>> -
>>>>>> - /**
>>>>>> - * <p>Renders a template</p>
>>>>>> - *
>>>>>> - * @param templateName - name of template to be rendered
>>>>>> - * @throws Exception if it fails
>>>>>> - * @return the rendered template as a String
>>>>>> - */
>>>>>> - protected String parse(String templateName)
>>
>>throws Exception
>>
>>>>>> - {
>>>>>> - StringWriter sw = new StringWriter();
>>>>>> - Template template =
>>
>>Velocity.getTemplate(templateName);
>>
>>>>>> - template.merge(context.getVelocityContext(), sw);
>>>>>> - return sw.toString();
>>>>>> + protected void popTilesContext() {
>>>>>> + ComponentContext.setContext( (ComponentContext)
>>>>>>contextStack.pop(), request);
>>>>>> }
>>>>>> -}
>>>>>> +}
>>>>>> \ No newline at end of file
>>>>>
>>>>>
>>>>>
>>---------------------------------------------------------------------
>>
>>>>>To unsubscribe, e-mail:
>>
>>velocity-dev-unsubscribe@jakarta.apache.org
>>
>>>>>For additional commands, e-mail:
>>>>
>>>>velocity-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
RE: cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts TilesTool.java
Posted by "Marinó A. Jónsson" <ma...@centrum.is>.
thanks man, I'll adjust my formatting settings accordingly.
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: 30. október 2003 16:04
> To: Velocity Developers List
> Subject: Re: cvs commit:
> jakarta-velocity-tools/src/java/org/apache/velocity/tools/stru
> ts TilesTool.java
>
>
> http://jakarta.apache.org/velocity/code-standards.html
>
> On Thursday, October 30, 2003, at 09:57 AM, Marinó A. Jónsson wrote:
>
> > sorry guys, didn't mean to bug ya :)
> >
> > it isn't as if I manually reformatted the source - I just used the
> > autoformatting features in the modified sections ... force
> of habit I
> > guess. if you could point me to some basic formatting
> guidelines for
> > Velocity I'd
> > be much abliged.
> >
> > cheers,
> > Marinó
> >
> >> -----Original Message-----
> >> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> >> Sent: 30. október 2003 11:22
> >> To: Velocity Developers List
> >> Subject: Re: cvs commit:
> >> jakarta-velocity-tools/src/java/org/apache/velocity/tools/stru
> >> ts TilesTool.java
> >>
> >>
> >>
> >> On Thursday, October 30, 2003, at 12:13 AM, Daniel L. Rall wrote:
> >>
> >>> Why did you reformat the source file? Generally speaking, making
> >>> changes that fit in with the existing style is preferred.
> >>
> >> Yes please.
> >>
> >>>
> >>> - Dan
> >>>
> >>>
> >>> marino@apache.org wrote:
> >>>> marino 2003/10/29 16:03:37
> >>>> Modified: src/java/org/apache/velocity/tools/struts
> >>>> TilesTool.java
> >>>> Log:
> >>>> updated to extend ImportSupport
> >>>> Revision Changes Path
> >>>> 1.2 +64 -124
> >>>> jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/
> >>>> TilesTool.java
> >>>> Index: TilesTool.java
> >>>>
> >> ===================================================================
> >>>> RCS file:
> >>>>
> >>
> /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/
> >>>> struts/TilesTool.java,v
> >>>> retrieving revision 1.1
> >>>> retrieving revision 1.2
> >>>> diff -u -r1.1 -r1.2
> >>>> --- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
> >>>> +++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
> >>>> @@ -61,10 +61,6 @@
> >>>> import javax.servlet.http.HttpSession;
> >>>> import javax.servlet.ServletContext;
> >>>> -import java.io.StringWriter;
> >>>> -import org.apache.velocity.app.Velocity;
> >>>> -import org.apache.velocity.Template;
> >>>> -
> >>>> import org.apache.struts.tiles.ComponentContext;
> >>>> import org.apache.struts.tiles.ComponentDefinition;
> >>>> import org.apache.struts.tiles.AttributeDefinition;
> >>>> @@ -76,10 +72,11 @@
> >>>> import org.apache.struts.tiles.DefinitionsFactoryException;
> >>>> import org.apache.struts.tiles.Controller;
> >>>> +import org.apache.velocity.app.Velocity;
> >>>> +import org.apache.velocity.tools.view.ImportSupport;
> >>>> import org.apache.velocity.tools.view.context.ViewContext;
> >>>> import org.apache.velocity.tools.view.tools.ViewTool;
> >>>> -
> >>>> /**
> >>>> * <p>Title: TilesTool</p>
> >>>> * <p>Description: A tool to use struts-tiles with
> Velocity</p>
> >>>> @@ -94,13 +91,10 @@
> >>>> * @author <a href="mailto:marinoj@centrum.is">Marino A.
> >>>> Jonsson</a>
> >>>> * @version $Revision$ $Date$
> >>>> */
> >>>> -public class TilesTool implements ViewTool
> >>>> -{
> >>>> +public class TilesTool extends ImportSupport
> >>>> + implements ViewTool {
> >>>> protected ViewContext context;
> >>>> - protected ServletContext application;
> >>>> - protected HttpServletRequest request;
> >>>> - protected HttpServletResponse response;
> >>>> /**
> >>>> * A stack to hold ComponentContexts while nested
> >>>> tile-definitions
> >>>> @@ -108,7 +102,6 @@
> >>>> */
> >>>> protected Stack contextStack;
> >>>> -
> >>>> /******************************* Constructors
> >>>> ****************************/
> >>>> /**
> >>>> @@ -116,54 +109,47 @@
> >>>> */
> >>>> public TilesTool() {}
> >>>> -
> >>>> /**
> >>>> * Initializes this tool.
> >>>> *
> >>>> * @param obj the current ViewContext
> >>>> * @throws IllegalArgumentException if the param is not a
> >>>> ViewContext
> >>>> */
> >>>> - public void init(Object obj)
> >>>> - {
> >>>> - if (!(obj instanceof ViewContext))
> >>>> - {
> >>>> + public void init(Object obj) {
> >>>> + if (! (obj instanceof ViewContext)) {
> >>>> throw new IllegalArgumentException("Tool
> >> can only be
> >>>> initialized with a ViewContext");
> >>>> }
> >>>> - this.context = (ViewContext)obj;
> >>>> + this.context = (ViewContext) obj;
> >>>> this.request = context.getRequest();
> >>>> this.response = context.getResponse();
> >>>> this.application = context.getServletContext();
> >>>> }
> >>>> -
> >>>> /***************************** View Helpers
> >>>> ******************************/
> >>>> /**
> >>>> * Fetches a named attribute value from the current
> >>>> tiles-context.
> >>>> *
> >>>> - * <p>This is functionally equivalent to + *
> >> <p>This is
> >>>> functionally equivalent to
> >>>> * <code><tiles:getAsString name="title" /></code>.</p>
> >>>> *
> >>>> * @param name the name of the tiles-attribute to fetch
> >>>> * @return the attribute value as String
> >>>> */
> >>>> - public String getString(String name)
> >>>> - {
> >>>> + public String getString(String name) {
> >>>> ComponentContext context =
> >>>> ComponentContext.getContext(request);
> >>>> Object attrValue = context.getAttribute(name);
> >>>> - if (attrValue == null)
> >>>> - {
> >>>> + if (attrValue == null) {
> >>>> return null;
> >>>> }
> >>>> return attrValue.toString();
> >>>> }
> >>>> -
> >>>> /**
> >>>> * <p>A generic tiles insert function</p>
> >>>> *
> >>>> - * <p>This is functionally equivalent to + *
> >> <p>This is
> >>>> functionally equivalent to
> >>>> * <code><tiles:insert attribute="menu" /></code>.</p>
> >>>> *
> >>>> * @param attr - can be any of the following:
> >>>> @@ -175,49 +161,48 @@
> >>>> * @return the rendered template or value as a String
> >>>> * @throws Exception on failure
> >>>> */
> >>>> - public String get(Object attr) throws Exception
> >>>> - {
> >>>> - ComponentContext currentContext =
> >>>> ComponentContext.getContext(request);
> >>>> - Object attrValue =
> >>>> currentContext.getAttribute(attr.toString());
> >>>> - if (attrValue != null)
> >>>> - {
> >>>> - return processObjectValue(attrValue);
> >>>> + public String get(Object attr) throws Exception {
> >>>> + try {
> >>>> + ComponentContext currentContext =
> >>>> ComponentContext.getContext(request);
> >>>> + Object attrValue =
> >>>> currentContext.getAttribute(attr.toString());
> >>>> + if (attrValue != null) {
> >>>> + return processObjectValue(attrValue);
> >>>> + }
> >>>> + return processAsDefinitionOrURL(attr.toString());
> >>>> + }
> >>>> + catch(Exception e) {
> >>>> + Velocity.error("Exeption while rendering Tile " +
> >>>> attr.toString() + ": " + e.getMessage());
> >>>> + return null;
> >>>> }
> >>>> - return processAsDefinitionOrURL(attr.toString());
> >>>> }
> >>>> -
> >>>> /************************** Protected Methods
> >>>> ****************************/
> >>>> /**
> >>>> * Process an object retrieved as a bean or attribute.
> >>>> *
> >>>> * @param value - Object can be a typed attribute,
> >> a String,
> >>>> or anything
> >>>> - * else. If typed attribute, use associated type.
> >>>> Otherwise, apply + * else. If typed attribute, use
> >>>> associated type. Otherwise, apply
> >>>> * toString() on object, and use returned
> >> string as a
> >>>> name.
> >>>> - * @throws Exception - Throws by underlying nested
> >> call to +
> >>>> * @throws Exception - Throws by underlying nested call to
> >>>> * processDefinitionName()
> >>>> * @return the fully processed value as String
> >>>> */
> >>>> - protected String processObjectValue(Object value) throws
> >>>> Exception
> >>>> - {
> >>>> + protected String processObjectValue(Object value) throws
> >>>> Exception {
> >>>> /* First, check if value is one of the Typed
> >> Attribute */
> >>>> - if (value instanceof AttributeDefinition)
> >>>> - {
> >>>> + if (value instanceof AttributeDefinition) {
> >>>> /* We have a type => return appropriate
> >> IncludeType */
> >>>> - return
> >>>> processTypedAttribute((AttributeDefinition)value);
> >>>> + return processTypedAttribute(
> >> (AttributeDefinition)
> >>>> value);
> >>>> }
> >>>> - else if (value instanceof ComponentDefinition)
> >>>> - {
> >>>> - return
> >> processDefinition((ComponentDefinition)value);
> >>>> + else if (value instanceof ComponentDefinition) {
> >>>> + return processDefinition(
> >> (ComponentDefinition) value);
> >>>> }
> >>>> /* Value must denote a valid String */
> >>>> return processAsDefinitionOrURL(value.toString());
> >>>> }
> >>>> -
> >>>> /**
> >>>> * Process typed attribute according to its type.
> >>>> *
> >>>> @@ -226,25 +211,21 @@
> >>>> * @throws Exception - Throws by underlying nested call to
> >>>> processDefinitionName()
> >>>> */
> >>>> protected String processTypedAttribute(AttributeDefinition
> >>>> value) throws Exception {
> >>>> - if (value instanceof DirectStringAttribute)
> >>>> - {
> >>>> - return (String)value.getValue();
> >>>> + if (value instanceof DirectStringAttribute) {
> >>>> + return (String) value.getValue();
> >>>> }
> >>>> - else if (value instanceof DefinitionAttribute)
> >>>> - {
> >>>> - return
> >>>> processDefinition((ComponentDefinition)value.getValue());
> >>>> + else if (value instanceof DefinitionAttribute) {
> >>>> + return processDefinition( (ComponentDefinition)
> >>>> value.getValue());
> >>>> }
> >>>> - else if (value instanceof DefinitionNameAttribute)
> >>>> - {
> >>>> - return
> >>>> processAsDefinitionOrURL((String)value.getValue());
> >>>> + else if (value instanceof DefinitionNameAttribute) {
> >>>> + return processAsDefinitionOrURL( (String)
> >>>> value.getValue());
> >>>> }
> >>>> /* else if( value instanceof PathAttribute ) */
> >>>> - return doInsert((String)value.getValue(), null, null);
> >>>> + return doInsert( (String) value.getValue(),
> null, null);
> >>>> }
> >>>> -
> >>>> /**
> >>>> * Try to process name as a definition, or as an
> >> URL if not
> >>>> found.
> >>>> *
> >>>> @@ -253,24 +234,20 @@
> >>>> * @throws Exception
> >>>> */
> >>>> protected String processAsDefinitionOrURL(String
> >> name) throws
> >>>> Exception {
> >>>> - try
> >>>> - {
> >>>> + try {
> >>>> ComponentDefinition definition =
> >>>> TilesUtil.getDefinition(name, request,
> >>>> application);
> >>>> - if (definition != null)
> >>>> - {
> >>>> + if (definition != null) {
> >>>> return processDefinition(definition);
> >>>> }
> >>>> }
> >>>> - catch (DefinitionsFactoryException ex)
> >>>> - {
> >>>> + catch (DefinitionsFactoryException ex) {
> >>>> /* silently failed, because we can choose to not
> >>>> define a factory. */
> >>>> }
> >>>> /* no definition found, try as url */
> >>>> return processUrl(name);
> >>>> }
> >>>> -
> >>>> /**
> >>>> * End of Process for definition.
> >>>> *
> >>>> @@ -278,12 +255,10 @@
> >>>> * @return the fully processed definition.
> >>>> * @throws Exception from InstantiationException
> >> Can't create
> >>>> requested controller
> >>>> */
> >>>> - protected String processDefinition(ComponentDefinition
> >>>> definition) throws Exception
> >>>> - {
> >>>> + protected String processDefinition(ComponentDefinition
> >>>> definition) throws Exception {
> >>>> Controller controller = null;
> >>>> - try
> >>>> - {
> >>>> + try {
> >>>> controller = definition.getOrCreateController();
> >>>> String role = definition.getRole();
> >>>> @@ -294,13 +269,11 @@
> >>>> role,
> >>>> controller);
> >>>> }
> >>>> - catch (InstantiationException ex)
> >>>> - {
> >>>> + catch (InstantiationException ex) {
> >>>> throw new Exception(ex.getMessage());
> >>>> }
> >>>> }
> >>>> -
> >>>> /**
> >>>> * Processes an url
> >>>> *
> >>>> @@ -308,12 +281,10 @@
> >>>> * @return the rendered template as String.
> >>>> * @throws Exception
> >>>> */
> >>>> - protected String processUrl(String url) throws Exception
> >>>> - {
> >>>> + protected String processUrl(String url) throws Exception {
> >>>> return doInsert(url, null, null);
> >>>> }
> >>>> -
> >>>> /**
> >>>> * Use this if there is no nested tile.
> >>>> *
> >>>> @@ -323,10 +294,8 @@
> >>>> * @return the rendered template as String.
> >>>> * @throws Exception
> >>>> */
> >>>> - protected String doInsert(String page, String role,
> >> Controller
> >>>> controller) throws Exception
> >>>> - {
> >>>> - if (role != null && !request.isUserInRole(role))
> >>>> - {
> >>>> + protected String doInsert(String page, String role,
> >> Controller
> >>>> controller) throws Exception {
> >>>> + if (role != null && !request.isUserInRole(role)) {
> >>>> return null;
> >>>> }
> >>>> @@ -334,7 +303,6 @@
> >>>> return doInsert(subCompContext, page, role,
> controller);
> >>>> }
> >>>> -
> >>>> /**
> >>>> * Use this if there is a nested tile.
> >>>> *
> >>>> @@ -345,13 +313,11 @@
> >>>> * @return the rendered template as String.
> >>>> * @throws Exception
> >>>> */
> >>>> - protected String doInsert(Map attributes, -
> >>
> >>>> String page, - String
> >>>> role, - Controller
> >> controller) throws
> >>>> Exception
> >>>> - {
> >>>> - if (role != null && !request.isUserInRole(role))
> >>>> - {
> >>>> + protected String doInsert(Map attributes,
> >>>> + String page,
> >>>> + String role,
> >>>> + Controller controller) throws
> >>>> Exception {
> >>>> + if (role != null && !request.isUserInRole(role)) {
> >>>> return null;
> >>>> }
> >>>> @@ -359,7 +325,6 @@
> >>>> return doInsert(subCompContext, page, role,
> controller);
> >>>> }
> >>>> -
> >>>> /**
> >>>> * An extension of the other two doInsert functions
> >>>> *
> >>>> @@ -371,71 +336,46 @@
> >>>> * @return the rendered template as String.
> >>>> * @throws Exception
> >>>> */
> >>>> - protected String doInsert(ComponentContext
> >> subCompContext, -
> >>>> String page, + protected String
> >>>> doInsert(ComponentContext subCompContext,
> >>>> + String page,
> >>>> String role,
> >>>> - Controller controller) throws
> >>>> Exception
> >>>> - {
> >>>> + Controller controller) throws
> >>>> Exception {
> >>>> pushTilesContext();
> >>>> - try
> >>>> - {
> >>>> + try {
> >>>> ComponentContext.setContext(subCompContext,
> >> request);
> >>>> /* Call controller if any */
> >>>> - if (controller != null)
> >>>> - {
> >>>> + if (controller != null) {
> >>>> controller.perform(subCompContext,
> >>>> request,
> >>>> response,
> >>>> application);
> >>>> }
> >>>> - return parse(page);
> >>>> + return this.acquireString(page);
> >>>> }
> >>>> - finally
> >>>> - {
> >>>> + finally {
> >>>> popTilesContext();
> >>>> }
> >>>> }
> >>>> -
> >>>> /**
> >>>> * <p>pushes the current tiles context onto the
> >> context-stack.
> >>>> * preserving the context is necessary so that a
> >> sub-context
> >>>> can be
> >>>> * put into request scope and lower level tiles can be
> >>>> rendered</p>
> >>>> */
> >>>> - protected void pushTilesContext()
> >>>> - {
> >>>> - if (contextStack == null)
> >>>> - {
> >>>> + protected void pushTilesContext() {
> >>>> + if (contextStack == null) {
> >>>> contextStack = new Stack();
> >>>> }
> >>>>
> contextStack.push(ComponentContext.getContext(request));
> >>>> }
> >>>> -
> >>>> /**
> >>>> * <p>pops the tiles sub-context off the
> >> context-stack after
> >>>> the lower level
> >>>> * tiles have been rendered</p>
> >>>> */
> >>>> - protected void popTilesContext()
> >>>> - {
> >>>> -
> >>>> ComponentContext.setContext((ComponentContext)contextStack.pop(),
> >>>> request);
> >>>> - }
> >>>> -
> >>>> -
> >>>> - /**
> >>>> - * <p>Renders a template</p>
> >>>> - *
> >>>> - * @param templateName - name of template to be rendered
> >>>> - * @throws Exception if it fails
> >>>> - * @return the rendered template as a String
> >>>> - */
> >>>> - protected String parse(String templateName)
> throws Exception
> >>>> - {
> >>>> - StringWriter sw = new StringWriter();
> >>>> - Template template =
> Velocity.getTemplate(templateName);
> >>>> - template.merge(context.getVelocityContext(), sw);
> >>>> - return sw.toString();
> >>>> + protected void popTilesContext() {
> >>>> + ComponentContext.setContext( (ComponentContext)
> >>>> contextStack.pop(), request);
> >>>> }
> >>>> -}
> >>>> +}
> >>>> \ No newline at end of file
> >>>
> >>>
> >>>
> >>
> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail:
> velocity-dev-unsubscribe@jakarta.apache.org
> >>> For additional commands, e-mail:
> >> velocity-dev-help@jakarta.apache.org
> >>>
> >>>
> >> --
> >> Geir Magnusson Jr 203-247-1713(m)
> >> geirm@optonline.net
> >>
> >>
> >>
> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail:
> velocity-dev-help@jakarta.apache.org
> >>
> >>
> >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> velocity-dev-help@jakarta.apache.org
> >
> >
> --
> Geir Magnusson Jr 203-247-1713(m)
> geirm@optonline.net
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts TilesTool.java
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
http://jakarta.apache.org/velocity/code-standards.html
On Thursday, October 30, 2003, at 09:57 AM, Marinó A. Jónsson wrote:
> sorry guys, didn't mean to bug ya :)
>
> it isn't as if I manually reformatted the source - I just used the
> autoformatting features in the modified sections ... force of habit I
> guess.
> if you could point me to some basic formatting guidelines for Velocity
> I'd
> be much abliged.
>
> cheers,
> Marinó
>
>> -----Original Message-----
>> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>> Sent: 30. október 2003 11:22
>> To: Velocity Developers List
>> Subject: Re: cvs commit:
>> jakarta-velocity-tools/src/java/org/apache/velocity/tools/stru
>> ts TilesTool.java
>>
>>
>>
>> On Thursday, October 30, 2003, at 12:13 AM, Daniel L. Rall wrote:
>>
>>> Why did you reformat the source file? Generally speaking, making
>>> changes that fit in with the existing style is preferred.
>>
>> Yes please.
>>
>>>
>>> - Dan
>>>
>>>
>>> marino@apache.org wrote:
>>>> marino 2003/10/29 16:03:37
>>>> Modified: src/java/org/apache/velocity/tools/struts
>>>> TilesTool.java
>>>> Log:
>>>> updated to extend ImportSupport
>>>> Revision Changes Path
>>>> 1.2 +64 -124
>>>> jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/
>>>> TilesTool.java
>>>> Index: TilesTool.java
>>>>
>> ===================================================================
>>>> RCS file:
>>>>
>> /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/
>>>> struts/TilesTool.java,v
>>>> retrieving revision 1.1
>>>> retrieving revision 1.2
>>>> diff -u -r1.1 -r1.2
>>>> --- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
>>>> +++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
>>>> @@ -61,10 +61,6 @@
>>>> import javax.servlet.http.HttpSession;
>>>> import javax.servlet.ServletContext;
>>>> -import java.io.StringWriter;
>>>> -import org.apache.velocity.app.Velocity;
>>>> -import org.apache.velocity.Template;
>>>> -
>>>> import org.apache.struts.tiles.ComponentContext;
>>>> import org.apache.struts.tiles.ComponentDefinition;
>>>> import org.apache.struts.tiles.AttributeDefinition;
>>>> @@ -76,10 +72,11 @@
>>>> import org.apache.struts.tiles.DefinitionsFactoryException;
>>>> import org.apache.struts.tiles.Controller;
>>>> +import org.apache.velocity.app.Velocity;
>>>> +import org.apache.velocity.tools.view.ImportSupport;
>>>> import org.apache.velocity.tools.view.context.ViewContext;
>>>> import org.apache.velocity.tools.view.tools.ViewTool;
>>>> -
>>>> /**
>>>> * <p>Title: TilesTool</p>
>>>> * <p>Description: A tool to use struts-tiles with Velocity</p>
>>>> @@ -94,13 +91,10 @@
>>>> * @author <a href="mailto:marinoj@centrum.is">Marino A.
>>>> Jonsson</a>
>>>> * @version $Revision$ $Date$
>>>> */
>>>> -public class TilesTool implements ViewTool
>>>> -{
>>>> +public class TilesTool extends ImportSupport
>>>> + implements ViewTool {
>>>> protected ViewContext context;
>>>> - protected ServletContext application;
>>>> - protected HttpServletRequest request;
>>>> - protected HttpServletResponse response;
>>>> /**
>>>> * A stack to hold ComponentContexts while nested
>>>> tile-definitions
>>>> @@ -108,7 +102,6 @@
>>>> */
>>>> protected Stack contextStack;
>>>> -
>>>> /******************************* Constructors
>>>> ****************************/
>>>> /**
>>>> @@ -116,54 +109,47 @@
>>>> */
>>>> public TilesTool() {}
>>>> -
>>>> /**
>>>> * Initializes this tool.
>>>> *
>>>> * @param obj the current ViewContext
>>>> * @throws IllegalArgumentException if the param is not a
>>>> ViewContext
>>>> */
>>>> - public void init(Object obj)
>>>> - {
>>>> - if (!(obj instanceof ViewContext))
>>>> - {
>>>> + public void init(Object obj) {
>>>> + if (! (obj instanceof ViewContext)) {
>>>> throw new IllegalArgumentException("Tool
>> can only be
>>>> initialized with a ViewContext");
>>>> }
>>>> - this.context = (ViewContext)obj;
>>>> + this.context = (ViewContext) obj;
>>>> this.request = context.getRequest();
>>>> this.response = context.getResponse();
>>>> this.application = context.getServletContext();
>>>> }
>>>> -
>>>> /***************************** View Helpers
>>>> ******************************/
>>>> /**
>>>> * Fetches a named attribute value from the current
>>>> tiles-context.
>>>> *
>>>> - * <p>This is functionally equivalent to + *
>> <p>This is
>>>> functionally equivalent to
>>>> * <code><tiles:getAsString name="title" /></code>.</p>
>>>> *
>>>> * @param name the name of the tiles-attribute to fetch
>>>> * @return the attribute value as String
>>>> */
>>>> - public String getString(String name)
>>>> - {
>>>> + public String getString(String name) {
>>>> ComponentContext context =
>>>> ComponentContext.getContext(request);
>>>> Object attrValue = context.getAttribute(name);
>>>> - if (attrValue == null)
>>>> - {
>>>> + if (attrValue == null) {
>>>> return null;
>>>> }
>>>> return attrValue.toString();
>>>> }
>>>> -
>>>> /**
>>>> * <p>A generic tiles insert function</p>
>>>> *
>>>> - * <p>This is functionally equivalent to + *
>> <p>This is
>>>> functionally equivalent to
>>>> * <code><tiles:insert attribute="menu" /></code>.</p>
>>>> *
>>>> * @param attr - can be any of the following:
>>>> @@ -175,49 +161,48 @@
>>>> * @return the rendered template or value as a String
>>>> * @throws Exception on failure
>>>> */
>>>> - public String get(Object attr) throws Exception
>>>> - {
>>>> - ComponentContext currentContext =
>>>> ComponentContext.getContext(request);
>>>> - Object attrValue =
>>>> currentContext.getAttribute(attr.toString());
>>>> - if (attrValue != null)
>>>> - {
>>>> - return processObjectValue(attrValue);
>>>> + public String get(Object attr) throws Exception {
>>>> + try {
>>>> + ComponentContext currentContext =
>>>> ComponentContext.getContext(request);
>>>> + Object attrValue =
>>>> currentContext.getAttribute(attr.toString());
>>>> + if (attrValue != null) {
>>>> + return processObjectValue(attrValue);
>>>> + }
>>>> + return processAsDefinitionOrURL(attr.toString());
>>>> + }
>>>> + catch(Exception e) {
>>>> + Velocity.error("Exeption while rendering Tile " +
>>>> attr.toString() + ": " + e.getMessage());
>>>> + return null;
>>>> }
>>>> - return processAsDefinitionOrURL(attr.toString());
>>>> }
>>>> -
>>>> /************************** Protected Methods
>>>> ****************************/
>>>> /**
>>>> * Process an object retrieved as a bean or attribute.
>>>> *
>>>> * @param value - Object can be a typed attribute,
>> a String,
>>>> or anything
>>>> - * else. If typed attribute, use associated type.
>>>> Otherwise, apply + * else. If typed attribute, use
>>>> associated type. Otherwise, apply
>>>> * toString() on object, and use returned
>> string as a
>>>> name.
>>>> - * @throws Exception - Throws by underlying nested
>> call to +
>>>> * @throws Exception - Throws by underlying nested call to
>>>> * processDefinitionName()
>>>> * @return the fully processed value as String
>>>> */
>>>> - protected String processObjectValue(Object value) throws
>>>> Exception
>>>> - {
>>>> + protected String processObjectValue(Object value) throws
>>>> Exception {
>>>> /* First, check if value is one of the Typed
>> Attribute */
>>>> - if (value instanceof AttributeDefinition)
>>>> - {
>>>> + if (value instanceof AttributeDefinition) {
>>>> /* We have a type => return appropriate
>> IncludeType */
>>>> - return
>>>> processTypedAttribute((AttributeDefinition)value);
>>>> + return processTypedAttribute(
>> (AttributeDefinition)
>>>> value);
>>>> }
>>>> - else if (value instanceof ComponentDefinition)
>>>> - {
>>>> - return
>> processDefinition((ComponentDefinition)value);
>>>> + else if (value instanceof ComponentDefinition) {
>>>> + return processDefinition(
>> (ComponentDefinition) value);
>>>> }
>>>> /* Value must denote a valid String */
>>>> return processAsDefinitionOrURL(value.toString());
>>>> }
>>>> -
>>>> /**
>>>> * Process typed attribute according to its type.
>>>> *
>>>> @@ -226,25 +211,21 @@
>>>> * @throws Exception - Throws by underlying nested call to
>>>> processDefinitionName()
>>>> */
>>>> protected String processTypedAttribute(AttributeDefinition
>>>> value) throws Exception {
>>>> - if (value instanceof DirectStringAttribute)
>>>> - {
>>>> - return (String)value.getValue();
>>>> + if (value instanceof DirectStringAttribute) {
>>>> + return (String) value.getValue();
>>>> }
>>>> - else if (value instanceof DefinitionAttribute)
>>>> - {
>>>> - return
>>>> processDefinition((ComponentDefinition)value.getValue());
>>>> + else if (value instanceof DefinitionAttribute) {
>>>> + return processDefinition( (ComponentDefinition)
>>>> value.getValue());
>>>> }
>>>> - else if (value instanceof DefinitionNameAttribute)
>>>> - {
>>>> - return
>>>> processAsDefinitionOrURL((String)value.getValue());
>>>> + else if (value instanceof DefinitionNameAttribute) {
>>>> + return processAsDefinitionOrURL( (String)
>>>> value.getValue());
>>>> }
>>>> /* else if( value instanceof PathAttribute ) */
>>>> - return doInsert((String)value.getValue(), null, null);
>>>> + return doInsert( (String) value.getValue(), null, null);
>>>> }
>>>> -
>>>> /**
>>>> * Try to process name as a definition, or as an
>> URL if not
>>>> found.
>>>> *
>>>> @@ -253,24 +234,20 @@
>>>> * @throws Exception
>>>> */
>>>> protected String processAsDefinitionOrURL(String
>> name) throws
>>>> Exception {
>>>> - try
>>>> - {
>>>> + try {
>>>> ComponentDefinition definition =
>>>> TilesUtil.getDefinition(name, request,
>>>> application);
>>>> - if (definition != null)
>>>> - {
>>>> + if (definition != null) {
>>>> return processDefinition(definition);
>>>> }
>>>> }
>>>> - catch (DefinitionsFactoryException ex)
>>>> - {
>>>> + catch (DefinitionsFactoryException ex) {
>>>> /* silently failed, because we can choose to not
>>>> define a factory. */
>>>> }
>>>> /* no definition found, try as url */
>>>> return processUrl(name);
>>>> }
>>>> -
>>>> /**
>>>> * End of Process for definition.
>>>> *
>>>> @@ -278,12 +255,10 @@
>>>> * @return the fully processed definition.
>>>> * @throws Exception from InstantiationException
>> Can't create
>>>> requested controller
>>>> */
>>>> - protected String processDefinition(ComponentDefinition
>>>> definition) throws Exception
>>>> - {
>>>> + protected String processDefinition(ComponentDefinition
>>>> definition) throws Exception {
>>>> Controller controller = null;
>>>> - try
>>>> - {
>>>> + try {
>>>> controller = definition.getOrCreateController();
>>>> String role = definition.getRole();
>>>> @@ -294,13 +269,11 @@
>>>> role,
>>>> controller);
>>>> }
>>>> - catch (InstantiationException ex)
>>>> - {
>>>> + catch (InstantiationException ex) {
>>>> throw new Exception(ex.getMessage());
>>>> }
>>>> }
>>>> -
>>>> /**
>>>> * Processes an url
>>>> *
>>>> @@ -308,12 +281,10 @@
>>>> * @return the rendered template as String.
>>>> * @throws Exception
>>>> */
>>>> - protected String processUrl(String url) throws Exception
>>>> - {
>>>> + protected String processUrl(String url) throws Exception {
>>>> return doInsert(url, null, null);
>>>> }
>>>> -
>>>> /**
>>>> * Use this if there is no nested tile.
>>>> *
>>>> @@ -323,10 +294,8 @@
>>>> * @return the rendered template as String.
>>>> * @throws Exception
>>>> */
>>>> - protected String doInsert(String page, String role,
>> Controller
>>>> controller) throws Exception
>>>> - {
>>>> - if (role != null && !request.isUserInRole(role))
>>>> - {
>>>> + protected String doInsert(String page, String role,
>> Controller
>>>> controller) throws Exception {
>>>> + if (role != null && !request.isUserInRole(role)) {
>>>> return null;
>>>> }
>>>> @@ -334,7 +303,6 @@
>>>> return doInsert(subCompContext, page, role, controller);
>>>> }
>>>> -
>>>> /**
>>>> * Use this if there is a nested tile.
>>>> *
>>>> @@ -345,13 +313,11 @@
>>>> * @return the rendered template as String.
>>>> * @throws Exception
>>>> */
>>>> - protected String doInsert(Map attributes, -
>>
>>>> String page, - String
>>>> role, - Controller
>> controller) throws
>>>> Exception
>>>> - {
>>>> - if (role != null && !request.isUserInRole(role))
>>>> - {
>>>> + protected String doInsert(Map attributes,
>>>> + String page,
>>>> + String role,
>>>> + Controller controller) throws
>>>> Exception {
>>>> + if (role != null && !request.isUserInRole(role)) {
>>>> return null;
>>>> }
>>>> @@ -359,7 +325,6 @@
>>>> return doInsert(subCompContext, page, role, controller);
>>>> }
>>>> -
>>>> /**
>>>> * An extension of the other two doInsert functions
>>>> *
>>>> @@ -371,71 +336,46 @@
>>>> * @return the rendered template as String.
>>>> * @throws Exception
>>>> */
>>>> - protected String doInsert(ComponentContext
>> subCompContext, -
>>>> String page, + protected String
>>>> doInsert(ComponentContext subCompContext,
>>>> + String page,
>>>> String role,
>>>> - Controller controller) throws
>>>> Exception
>>>> - {
>>>> + Controller controller) throws
>>>> Exception {
>>>> pushTilesContext();
>>>> - try
>>>> - {
>>>> + try {
>>>> ComponentContext.setContext(subCompContext,
>> request);
>>>> /* Call controller if any */
>>>> - if (controller != null)
>>>> - {
>>>> + if (controller != null) {
>>>> controller.perform(subCompContext,
>>>> request,
>>>> response,
>>>> application);
>>>> }
>>>> - return parse(page);
>>>> + return this.acquireString(page);
>>>> }
>>>> - finally
>>>> - {
>>>> + finally {
>>>> popTilesContext();
>>>> }
>>>> }
>>>> -
>>>> /**
>>>> * <p>pushes the current tiles context onto the
>> context-stack.
>>>> * preserving the context is necessary so that a
>> sub-context
>>>> can be
>>>> * put into request scope and lower level tiles can be
>>>> rendered</p>
>>>> */
>>>> - protected void pushTilesContext()
>>>> - {
>>>> - if (contextStack == null)
>>>> - {
>>>> + protected void pushTilesContext() {
>>>> + if (contextStack == null) {
>>>> contextStack = new Stack();
>>>> }
>>>> contextStack.push(ComponentContext.getContext(request));
>>>> }
>>>> -
>>>> /**
>>>> * <p>pops the tiles sub-context off the
>> context-stack after
>>>> the lower level
>>>> * tiles have been rendered</p>
>>>> */
>>>> - protected void popTilesContext()
>>>> - {
>>>> -
>>>> ComponentContext.setContext((ComponentContext)contextStack.pop(),
>>>> request);
>>>> - }
>>>> -
>>>> -
>>>> - /**
>>>> - * <p>Renders a template</p>
>>>> - *
>>>> - * @param templateName - name of template to be rendered
>>>> - * @throws Exception if it fails
>>>> - * @return the rendered template as a String
>>>> - */
>>>> - protected String parse(String templateName) throws Exception
>>>> - {
>>>> - StringWriter sw = new StringWriter();
>>>> - Template template = Velocity.getTemplate(templateName);
>>>> - template.merge(context.getVelocityContext(), sw);
>>>> - return sw.toString();
>>>> + protected void popTilesContext() {
>>>> + ComponentContext.setContext( (ComponentContext)
>>>> contextStack.pop(), request);
>>>> }
>>>> -}
>>>> +}
>>>> \ No newline at end of file
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail:
>> velocity-dev-help@jakarta.apache.org
>>>
>>>
>> --
>> Geir Magnusson Jr 203-247-1713(m)
>> geirm@optonline.net
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
--
Geir Magnusson Jr 203-247-1713(m)
geirm@optonline.net
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
RE: cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts TilesTool.java
Posted by "Marinó A. Jónsson" <ma...@centrum.is>.
sorry guys, didn't mean to bug ya :)
it isn't as if I manually reformatted the source - I just used the
autoformatting features in the modified sections ... force of habit I guess.
if you could point me to some basic formatting guidelines for Velocity I'd
be much abliged.
cheers,
Marinó
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: 30. október 2003 11:22
> To: Velocity Developers List
> Subject: Re: cvs commit:
> jakarta-velocity-tools/src/java/org/apache/velocity/tools/stru
> ts TilesTool.java
>
>
>
> On Thursday, October 30, 2003, at 12:13 AM, Daniel L. Rall wrote:
>
> > Why did you reformat the source file? Generally speaking, making
> > changes that fit in with the existing style is preferred.
>
> Yes please.
>
> >
> > - Dan
> >
> >
> > marino@apache.org wrote:
> >> marino 2003/10/29 16:03:37
> >> Modified: src/java/org/apache/velocity/tools/struts
> >> TilesTool.java
> >> Log:
> >> updated to extend ImportSupport
> >> Revision Changes Path
> >> 1.2 +64 -124
> >> jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/
> >> TilesTool.java
> >> Index: TilesTool.java
> >>
> ===================================================================
> >> RCS file:
> >>
> /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/
> >> struts/TilesTool.java,v
> >> retrieving revision 1.1
> >> retrieving revision 1.2
> >> diff -u -r1.1 -r1.2
> >> --- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
> >> +++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
> >> @@ -61,10 +61,6 @@
> >> import javax.servlet.http.HttpSession;
> >> import javax.servlet.ServletContext;
> >> -import java.io.StringWriter;
> >> -import org.apache.velocity.app.Velocity;
> >> -import org.apache.velocity.Template;
> >> -
> >> import org.apache.struts.tiles.ComponentContext;
> >> import org.apache.struts.tiles.ComponentDefinition;
> >> import org.apache.struts.tiles.AttributeDefinition;
> >> @@ -76,10 +72,11 @@
> >> import org.apache.struts.tiles.DefinitionsFactoryException;
> >> import org.apache.struts.tiles.Controller;
> >> +import org.apache.velocity.app.Velocity;
> >> +import org.apache.velocity.tools.view.ImportSupport;
> >> import org.apache.velocity.tools.view.context.ViewContext;
> >> import org.apache.velocity.tools.view.tools.ViewTool;
> >> -
> >> /**
> >> * <p>Title: TilesTool</p>
> >> * <p>Description: A tool to use struts-tiles with Velocity</p>
> >> @@ -94,13 +91,10 @@
> >> * @author <a href="mailto:marinoj@centrum.is">Marino A.
> >> Jonsson</a>
> >> * @version $Revision$ $Date$
> >> */
> >> -public class TilesTool implements ViewTool
> >> -{
> >> +public class TilesTool extends ImportSupport
> >> + implements ViewTool {
> >> protected ViewContext context;
> >> - protected ServletContext application;
> >> - protected HttpServletRequest request;
> >> - protected HttpServletResponse response;
> >> /**
> >> * A stack to hold ComponentContexts while nested
> >> tile-definitions
> >> @@ -108,7 +102,6 @@
> >> */
> >> protected Stack contextStack;
> >> -
> >> /******************************* Constructors
> >> ****************************/
> >> /**
> >> @@ -116,54 +109,47 @@
> >> */
> >> public TilesTool() {}
> >> -
> >> /**
> >> * Initializes this tool.
> >> *
> >> * @param obj the current ViewContext
> >> * @throws IllegalArgumentException if the param is not a
> >> ViewContext
> >> */
> >> - public void init(Object obj)
> >> - {
> >> - if (!(obj instanceof ViewContext))
> >> - {
> >> + public void init(Object obj) {
> >> + if (! (obj instanceof ViewContext)) {
> >> throw new IllegalArgumentException("Tool
> can only be
> >> initialized with a ViewContext");
> >> }
> >> - this.context = (ViewContext)obj;
> >> + this.context = (ViewContext) obj;
> >> this.request = context.getRequest();
> >> this.response = context.getResponse();
> >> this.application = context.getServletContext();
> >> }
> >> -
> >> /***************************** View Helpers
> >> ******************************/
> >> /**
> >> * Fetches a named attribute value from the current
> >> tiles-context.
> >> *
> >> - * <p>This is functionally equivalent to + *
> <p>This is
> >> functionally equivalent to
> >> * <code><tiles:getAsString name="title" /></code>.</p>
> >> *
> >> * @param name the name of the tiles-attribute to fetch
> >> * @return the attribute value as String
> >> */
> >> - public String getString(String name)
> >> - {
> >> + public String getString(String name) {
> >> ComponentContext context =
> >> ComponentContext.getContext(request);
> >> Object attrValue = context.getAttribute(name);
> >> - if (attrValue == null)
> >> - {
> >> + if (attrValue == null) {
> >> return null;
> >> }
> >> return attrValue.toString();
> >> }
> >> -
> >> /**
> >> * <p>A generic tiles insert function</p>
> >> *
> >> - * <p>This is functionally equivalent to + *
> <p>This is
> >> functionally equivalent to
> >> * <code><tiles:insert attribute="menu" /></code>.</p>
> >> *
> >> * @param attr - can be any of the following:
> >> @@ -175,49 +161,48 @@
> >> * @return the rendered template or value as a String
> >> * @throws Exception on failure
> >> */
> >> - public String get(Object attr) throws Exception
> >> - {
> >> - ComponentContext currentContext =
> >> ComponentContext.getContext(request);
> >> - Object attrValue =
> >> currentContext.getAttribute(attr.toString());
> >> - if (attrValue != null)
> >> - {
> >> - return processObjectValue(attrValue);
> >> + public String get(Object attr) throws Exception {
> >> + try {
> >> + ComponentContext currentContext =
> >> ComponentContext.getContext(request);
> >> + Object attrValue =
> >> currentContext.getAttribute(attr.toString());
> >> + if (attrValue != null) {
> >> + return processObjectValue(attrValue);
> >> + }
> >> + return processAsDefinitionOrURL(attr.toString());
> >> + }
> >> + catch(Exception e) {
> >> + Velocity.error("Exeption while rendering Tile " +
> >> attr.toString() + ": " + e.getMessage());
> >> + return null;
> >> }
> >> - return processAsDefinitionOrURL(attr.toString());
> >> }
> >> -
> >> /************************** Protected Methods
> >> ****************************/
> >> /**
> >> * Process an object retrieved as a bean or attribute.
> >> *
> >> * @param value - Object can be a typed attribute,
> a String,
> >> or anything
> >> - * else. If typed attribute, use associated type.
> >> Otherwise, apply + * else. If typed attribute, use
> >> associated type. Otherwise, apply
> >> * toString() on object, and use returned
> string as a
> >> name.
> >> - * @throws Exception - Throws by underlying nested
> call to +
> >> * @throws Exception - Throws by underlying nested call to
> >> * processDefinitionName()
> >> * @return the fully processed value as String
> >> */
> >> - protected String processObjectValue(Object value) throws
> >> Exception
> >> - {
> >> + protected String processObjectValue(Object value) throws
> >> Exception {
> >> /* First, check if value is one of the Typed
> Attribute */
> >> - if (value instanceof AttributeDefinition)
> >> - {
> >> + if (value instanceof AttributeDefinition) {
> >> /* We have a type => return appropriate
> IncludeType */
> >> - return
> >> processTypedAttribute((AttributeDefinition)value);
> >> + return processTypedAttribute(
> (AttributeDefinition)
> >> value);
> >> }
> >> - else if (value instanceof ComponentDefinition)
> >> - {
> >> - return
> processDefinition((ComponentDefinition)value);
> >> + else if (value instanceof ComponentDefinition) {
> >> + return processDefinition(
> (ComponentDefinition) value);
> >> }
> >> /* Value must denote a valid String */
> >> return processAsDefinitionOrURL(value.toString());
> >> }
> >> -
> >> /**
> >> * Process typed attribute according to its type.
> >> *
> >> @@ -226,25 +211,21 @@
> >> * @throws Exception - Throws by underlying nested call to
> >> processDefinitionName()
> >> */
> >> protected String processTypedAttribute(AttributeDefinition
> >> value) throws Exception {
> >> - if (value instanceof DirectStringAttribute)
> >> - {
> >> - return (String)value.getValue();
> >> + if (value instanceof DirectStringAttribute) {
> >> + return (String) value.getValue();
> >> }
> >> - else if (value instanceof DefinitionAttribute)
> >> - {
> >> - return
> >> processDefinition((ComponentDefinition)value.getValue());
> >> + else if (value instanceof DefinitionAttribute) {
> >> + return processDefinition( (ComponentDefinition)
> >> value.getValue());
> >> }
> >> - else if (value instanceof DefinitionNameAttribute)
> >> - {
> >> - return
> >> processAsDefinitionOrURL((String)value.getValue());
> >> + else if (value instanceof DefinitionNameAttribute) {
> >> + return processAsDefinitionOrURL( (String)
> >> value.getValue());
> >> }
> >> /* else if( value instanceof PathAttribute ) */
> >> - return doInsert((String)value.getValue(), null, null);
> >> + return doInsert( (String) value.getValue(), null, null);
> >> }
> >> -
> >> /**
> >> * Try to process name as a definition, or as an
> URL if not
> >> found.
> >> *
> >> @@ -253,24 +234,20 @@
> >> * @throws Exception
> >> */
> >> protected String processAsDefinitionOrURL(String
> name) throws
> >> Exception {
> >> - try
> >> - {
> >> + try {
> >> ComponentDefinition definition =
> >> TilesUtil.getDefinition(name, request,
> >> application);
> >> - if (definition != null)
> >> - {
> >> + if (definition != null) {
> >> return processDefinition(definition);
> >> }
> >> }
> >> - catch (DefinitionsFactoryException ex)
> >> - {
> >> + catch (DefinitionsFactoryException ex) {
> >> /* silently failed, because we can choose to not
> >> define a factory. */
> >> }
> >> /* no definition found, try as url */
> >> return processUrl(name);
> >> }
> >> -
> >> /**
> >> * End of Process for definition.
> >> *
> >> @@ -278,12 +255,10 @@
> >> * @return the fully processed definition.
> >> * @throws Exception from InstantiationException
> Can't create
> >> requested controller
> >> */
> >> - protected String processDefinition(ComponentDefinition
> >> definition) throws Exception
> >> - {
> >> + protected String processDefinition(ComponentDefinition
> >> definition) throws Exception {
> >> Controller controller = null;
> >> - try
> >> - {
> >> + try {
> >> controller = definition.getOrCreateController();
> >> String role = definition.getRole();
> >> @@ -294,13 +269,11 @@
> >> role,
> >> controller);
> >> }
> >> - catch (InstantiationException ex)
> >> - {
> >> + catch (InstantiationException ex) {
> >> throw new Exception(ex.getMessage());
> >> }
> >> }
> >> -
> >> /**
> >> * Processes an url
> >> *
> >> @@ -308,12 +281,10 @@
> >> * @return the rendered template as String.
> >> * @throws Exception
> >> */
> >> - protected String processUrl(String url) throws Exception
> >> - {
> >> + protected String processUrl(String url) throws Exception {
> >> return doInsert(url, null, null);
> >> }
> >> -
> >> /**
> >> * Use this if there is no nested tile.
> >> *
> >> @@ -323,10 +294,8 @@
> >> * @return the rendered template as String.
> >> * @throws Exception
> >> */
> >> - protected String doInsert(String page, String role,
> Controller
> >> controller) throws Exception
> >> - {
> >> - if (role != null && !request.isUserInRole(role))
> >> - {
> >> + protected String doInsert(String page, String role,
> Controller
> >> controller) throws Exception {
> >> + if (role != null && !request.isUserInRole(role)) {
> >> return null;
> >> }
> >> @@ -334,7 +303,6 @@
> >> return doInsert(subCompContext, page, role, controller);
> >> }
> >> -
> >> /**
> >> * Use this if there is a nested tile.
> >> *
> >> @@ -345,13 +313,11 @@
> >> * @return the rendered template as String.
> >> * @throws Exception
> >> */
> >> - protected String doInsert(Map attributes, -
>
> >> String page, - String
> >> role, - Controller
> controller) throws
> >> Exception
> >> - {
> >> - if (role != null && !request.isUserInRole(role))
> >> - {
> >> + protected String doInsert(Map attributes,
> >> + String page,
> >> + String role,
> >> + Controller controller) throws
> >> Exception {
> >> + if (role != null && !request.isUserInRole(role)) {
> >> return null;
> >> }
> >> @@ -359,7 +325,6 @@
> >> return doInsert(subCompContext, page, role, controller);
> >> }
> >> -
> >> /**
> >> * An extension of the other two doInsert functions
> >> *
> >> @@ -371,71 +336,46 @@
> >> * @return the rendered template as String.
> >> * @throws Exception
> >> */
> >> - protected String doInsert(ComponentContext
> subCompContext, -
> >> String page, + protected String
> >> doInsert(ComponentContext subCompContext,
> >> + String page,
> >> String role,
> >> - Controller controller) throws
> >> Exception
> >> - {
> >> + Controller controller) throws
> >> Exception {
> >> pushTilesContext();
> >> - try
> >> - {
> >> + try {
> >> ComponentContext.setContext(subCompContext,
> request);
> >> /* Call controller if any */
> >> - if (controller != null)
> >> - {
> >> + if (controller != null) {
> >> controller.perform(subCompContext,
> >> request,
> >> response,
> >> application);
> >> }
> >> - return parse(page);
> >> + return this.acquireString(page);
> >> }
> >> - finally
> >> - {
> >> + finally {
> >> popTilesContext();
> >> }
> >> }
> >> -
> >> /**
> >> * <p>pushes the current tiles context onto the
> context-stack.
> >> * preserving the context is necessary so that a
> sub-context
> >> can be
> >> * put into request scope and lower level tiles can be
> >> rendered</p>
> >> */
> >> - protected void pushTilesContext()
> >> - {
> >> - if (contextStack == null)
> >> - {
> >> + protected void pushTilesContext() {
> >> + if (contextStack == null) {
> >> contextStack = new Stack();
> >> }
> >> contextStack.push(ComponentContext.getContext(request));
> >> }
> >> -
> >> /**
> >> * <p>pops the tiles sub-context off the
> context-stack after
> >> the lower level
> >> * tiles have been rendered</p>
> >> */
> >> - protected void popTilesContext()
> >> - {
> >> -
> >> ComponentContext.setContext((ComponentContext)contextStack.pop(),
> >> request);
> >> - }
> >> -
> >> -
> >> - /**
> >> - * <p>Renders a template</p>
> >> - *
> >> - * @param templateName - name of template to be rendered
> >> - * @throws Exception if it fails
> >> - * @return the rendered template as a String
> >> - */
> >> - protected String parse(String templateName) throws Exception
> >> - {
> >> - StringWriter sw = new StringWriter();
> >> - Template template = Velocity.getTemplate(templateName);
> >> - template.merge(context.getVelocityContext(), sw);
> >> - return sw.toString();
> >> + protected void popTilesContext() {
> >> + ComponentContext.setContext( (ComponentContext)
> >> contextStack.pop(), request);
> >> }
> >> -}
> >> +}
> >> \ No newline at end of file
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> velocity-dev-help@jakarta.apache.org
> >
> >
> --
> Geir Magnusson Jr 203-247-1713(m)
> geirm@optonline.net
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts TilesTool.java
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On Thursday, October 30, 2003, at 12:13 AM, Daniel L. Rall wrote:
> Why did you reformat the source file? Generally speaking, making
> changes that fit in with the existing style is preferred.
Yes please.
>
> - Dan
>
>
> marino@apache.org wrote:
>> marino 2003/10/29 16:03:37
>> Modified: src/java/org/apache/velocity/tools/struts
>> TilesTool.java
>> Log:
>> updated to extend ImportSupport
>> Revision Changes Path
>> 1.2 +64 -124
>> jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/
>> TilesTool.java
>> Index: TilesTool.java
>> ===================================================================
>> RCS file:
>> /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/
>> struts/TilesTool.java,v
>> retrieving revision 1.1
>> retrieving revision 1.2
>> diff -u -r1.1 -r1.2
>> --- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
>> +++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
>> @@ -61,10 +61,6 @@
>> import javax.servlet.http.HttpSession;
>> import javax.servlet.ServletContext;
>> -import java.io.StringWriter;
>> -import org.apache.velocity.app.Velocity;
>> -import org.apache.velocity.Template;
>> -
>> import org.apache.struts.tiles.ComponentContext;
>> import org.apache.struts.tiles.ComponentDefinition;
>> import org.apache.struts.tiles.AttributeDefinition;
>> @@ -76,10 +72,11 @@
>> import org.apache.struts.tiles.DefinitionsFactoryException;
>> import org.apache.struts.tiles.Controller;
>> +import org.apache.velocity.app.Velocity;
>> +import org.apache.velocity.tools.view.ImportSupport;
>> import org.apache.velocity.tools.view.context.ViewContext;
>> import org.apache.velocity.tools.view.tools.ViewTool;
>> -
>> /**
>> * <p>Title: TilesTool</p>
>> * <p>Description: A tool to use struts-tiles with Velocity</p>
>> @@ -94,13 +91,10 @@
>> * @author <a href="mailto:marinoj@centrum.is">Marino A.
>> Jonsson</a>
>> * @version $Revision$ $Date$
>> */
>> -public class TilesTool implements ViewTool
>> -{
>> +public class TilesTool extends ImportSupport
>> + implements ViewTool {
>> protected ViewContext context;
>> - protected ServletContext application;
>> - protected HttpServletRequest request;
>> - protected HttpServletResponse response;
>> /**
>> * A stack to hold ComponentContexts while nested
>> tile-definitions
>> @@ -108,7 +102,6 @@
>> */
>> protected Stack contextStack;
>> -
>> /******************************* Constructors
>> ****************************/
>> /**
>> @@ -116,54 +109,47 @@
>> */
>> public TilesTool() {}
>> -
>> /**
>> * Initializes this tool.
>> *
>> * @param obj the current ViewContext
>> * @throws IllegalArgumentException if the param is not a
>> ViewContext
>> */
>> - public void init(Object obj)
>> - {
>> - if (!(obj instanceof ViewContext))
>> - {
>> + public void init(Object obj) {
>> + if (! (obj instanceof ViewContext)) {
>> throw new IllegalArgumentException("Tool can only be
>> initialized with a ViewContext");
>> }
>> - this.context = (ViewContext)obj;
>> + this.context = (ViewContext) obj;
>> this.request = context.getRequest();
>> this.response = context.getResponse();
>> this.application = context.getServletContext();
>> }
>> -
>> /***************************** View Helpers
>> ******************************/
>> /**
>> * Fetches a named attribute value from the current
>> tiles-context.
>> *
>> - * <p>This is functionally equivalent to + * <p>This is
>> functionally equivalent to
>> * <code><tiles:getAsString name="title" /></code>.</p>
>> *
>> * @param name the name of the tiles-attribute to fetch
>> * @return the attribute value as String
>> */
>> - public String getString(String name)
>> - {
>> + public String getString(String name) {
>> ComponentContext context =
>> ComponentContext.getContext(request);
>> Object attrValue = context.getAttribute(name);
>> - if (attrValue == null)
>> - {
>> + if (attrValue == null) {
>> return null;
>> }
>> return attrValue.toString();
>> }
>> -
>> /**
>> * <p>A generic tiles insert function</p>
>> *
>> - * <p>This is functionally equivalent to + * <p>This is
>> functionally equivalent to
>> * <code><tiles:insert attribute="menu" /></code>.</p>
>> *
>> * @param attr - can be any of the following:
>> @@ -175,49 +161,48 @@
>> * @return the rendered template or value as a String
>> * @throws Exception on failure
>> */
>> - public String get(Object attr) throws Exception
>> - {
>> - ComponentContext currentContext =
>> ComponentContext.getContext(request);
>> - Object attrValue =
>> currentContext.getAttribute(attr.toString());
>> - if (attrValue != null)
>> - {
>> - return processObjectValue(attrValue);
>> + public String get(Object attr) throws Exception {
>> + try {
>> + ComponentContext currentContext =
>> ComponentContext.getContext(request);
>> + Object attrValue =
>> currentContext.getAttribute(attr.toString());
>> + if (attrValue != null) {
>> + return processObjectValue(attrValue);
>> + }
>> + return processAsDefinitionOrURL(attr.toString());
>> + }
>> + catch(Exception e) {
>> + Velocity.error("Exeption while rendering Tile " +
>> attr.toString() + ": " + e.getMessage());
>> + return null;
>> }
>> - return processAsDefinitionOrURL(attr.toString());
>> }
>> -
>> /************************** Protected Methods
>> ****************************/
>> /**
>> * Process an object retrieved as a bean or attribute.
>> *
>> * @param value - Object can be a typed attribute, a String,
>> or anything
>> - * else. If typed attribute, use associated type.
>> Otherwise, apply + * else. If typed attribute, use
>> associated type. Otherwise, apply
>> * toString() on object, and use returned string as a
>> name.
>> - * @throws Exception - Throws by underlying nested call to +
>> * @throws Exception - Throws by underlying nested call to
>> * processDefinitionName()
>> * @return the fully processed value as String
>> */
>> - protected String processObjectValue(Object value) throws
>> Exception
>> - {
>> + protected String processObjectValue(Object value) throws
>> Exception {
>> /* First, check if value is one of the Typed Attribute */
>> - if (value instanceof AttributeDefinition)
>> - {
>> + if (value instanceof AttributeDefinition) {
>> /* We have a type => return appropriate IncludeType */
>> - return
>> processTypedAttribute((AttributeDefinition)value);
>> + return processTypedAttribute( (AttributeDefinition)
>> value);
>> }
>> - else if (value instanceof ComponentDefinition)
>> - {
>> - return processDefinition((ComponentDefinition)value);
>> + else if (value instanceof ComponentDefinition) {
>> + return processDefinition( (ComponentDefinition) value);
>> }
>> /* Value must denote a valid String */
>> return processAsDefinitionOrURL(value.toString());
>> }
>> -
>> /**
>> * Process typed attribute according to its type.
>> *
>> @@ -226,25 +211,21 @@
>> * @throws Exception - Throws by underlying nested call to
>> processDefinitionName()
>> */
>> protected String processTypedAttribute(AttributeDefinition
>> value) throws Exception {
>> - if (value instanceof DirectStringAttribute)
>> - {
>> - return (String)value.getValue();
>> + if (value instanceof DirectStringAttribute) {
>> + return (String) value.getValue();
>> }
>> - else if (value instanceof DefinitionAttribute)
>> - {
>> - return
>> processDefinition((ComponentDefinition)value.getValue());
>> + else if (value instanceof DefinitionAttribute) {
>> + return processDefinition( (ComponentDefinition)
>> value.getValue());
>> }
>> - else if (value instanceof DefinitionNameAttribute)
>> - {
>> - return
>> processAsDefinitionOrURL((String)value.getValue());
>> + else if (value instanceof DefinitionNameAttribute) {
>> + return processAsDefinitionOrURL( (String)
>> value.getValue());
>> }
>> /* else if( value instanceof PathAttribute ) */
>> - return doInsert((String)value.getValue(), null, null);
>> + return doInsert( (String) value.getValue(), null, null);
>> }
>> -
>> /**
>> * Try to process name as a definition, or as an URL if not
>> found.
>> *
>> @@ -253,24 +234,20 @@
>> * @throws Exception
>> */
>> protected String processAsDefinitionOrURL(String name) throws
>> Exception {
>> - try
>> - {
>> + try {
>> ComponentDefinition definition =
>> TilesUtil.getDefinition(name, request,
>> application);
>> - if (definition != null)
>> - {
>> + if (definition != null) {
>> return processDefinition(definition);
>> }
>> }
>> - catch (DefinitionsFactoryException ex)
>> - {
>> + catch (DefinitionsFactoryException ex) {
>> /* silently failed, because we can choose to not
>> define a factory. */
>> }
>> /* no definition found, try as url */
>> return processUrl(name);
>> }
>> -
>> /**
>> * End of Process for definition.
>> *
>> @@ -278,12 +255,10 @@
>> * @return the fully processed definition.
>> * @throws Exception from InstantiationException Can't create
>> requested controller
>> */
>> - protected String processDefinition(ComponentDefinition
>> definition) throws Exception
>> - {
>> + protected String processDefinition(ComponentDefinition
>> definition) throws Exception {
>> Controller controller = null;
>> - try
>> - {
>> + try {
>> controller = definition.getOrCreateController();
>> String role = definition.getRole();
>> @@ -294,13 +269,11 @@
>> role,
>> controller);
>> }
>> - catch (InstantiationException ex)
>> - {
>> + catch (InstantiationException ex) {
>> throw new Exception(ex.getMessage());
>> }
>> }
>> -
>> /**
>> * Processes an url
>> *
>> @@ -308,12 +281,10 @@
>> * @return the rendered template as String.
>> * @throws Exception
>> */
>> - protected String processUrl(String url) throws Exception
>> - {
>> + protected String processUrl(String url) throws Exception {
>> return doInsert(url, null, null);
>> }
>> -
>> /**
>> * Use this if there is no nested tile.
>> *
>> @@ -323,10 +294,8 @@
>> * @return the rendered template as String.
>> * @throws Exception
>> */
>> - protected String doInsert(String page, String role, Controller
>> controller) throws Exception
>> - {
>> - if (role != null && !request.isUserInRole(role))
>> - {
>> + protected String doInsert(String page, String role, Controller
>> controller) throws Exception {
>> + if (role != null && !request.isUserInRole(role)) {
>> return null;
>> }
>> @@ -334,7 +303,6 @@
>> return doInsert(subCompContext, page, role, controller);
>> }
>> -
>> /**
>> * Use this if there is a nested tile.
>> *
>> @@ -345,13 +313,11 @@
>> * @return the rendered template as String.
>> * @throws Exception
>> */
>> - protected String doInsert(Map attributes, -
>> String page, - String
>> role, - Controller controller) throws
>> Exception
>> - {
>> - if (role != null && !request.isUserInRole(role))
>> - {
>> + protected String doInsert(Map attributes,
>> + String page,
>> + String role,
>> + Controller controller) throws
>> Exception {
>> + if (role != null && !request.isUserInRole(role)) {
>> return null;
>> }
>> @@ -359,7 +325,6 @@
>> return doInsert(subCompContext, page, role, controller);
>> }
>> -
>> /**
>> * An extension of the other two doInsert functions
>> *
>> @@ -371,71 +336,46 @@
>> * @return the rendered template as String.
>> * @throws Exception
>> */
>> - protected String doInsert(ComponentContext subCompContext, -
>> String page, + protected String
>> doInsert(ComponentContext subCompContext,
>> + String page,
>> String role,
>> - Controller controller) throws
>> Exception
>> - {
>> + Controller controller) throws
>> Exception {
>> pushTilesContext();
>> - try
>> - {
>> + try {
>> ComponentContext.setContext(subCompContext, request);
>> /* Call controller if any */
>> - if (controller != null)
>> - {
>> + if (controller != null) {
>> controller.perform(subCompContext,
>> request,
>> response,
>> application);
>> }
>> - return parse(page);
>> + return this.acquireString(page);
>> }
>> - finally
>> - {
>> + finally {
>> popTilesContext();
>> }
>> }
>> -
>> /**
>> * <p>pushes the current tiles context onto the context-stack.
>> * preserving the context is necessary so that a sub-context
>> can be
>> * put into request scope and lower level tiles can be
>> rendered</p>
>> */
>> - protected void pushTilesContext()
>> - {
>> - if (contextStack == null)
>> - {
>> + protected void pushTilesContext() {
>> + if (contextStack == null) {
>> contextStack = new Stack();
>> }
>> contextStack.push(ComponentContext.getContext(request));
>> }
>> -
>> /**
>> * <p>pops the tiles sub-context off the context-stack after
>> the lower level
>> * tiles have been rendered</p>
>> */
>> - protected void popTilesContext()
>> - {
>> -
>> ComponentContext.setContext((ComponentContext)contextStack.pop(),
>> request);
>> - }
>> -
>> -
>> - /**
>> - * <p>Renders a template</p>
>> - *
>> - * @param templateName - name of template to be rendered
>> - * @throws Exception if it fails
>> - * @return the rendered template as a String
>> - */
>> - protected String parse(String templateName) throws Exception
>> - {
>> - StringWriter sw = new StringWriter();
>> - Template template = Velocity.getTemplate(templateName);
>> - template.merge(context.getVelocityContext(), sw);
>> - return sw.toString();
>> + protected void popTilesContext() {
>> + ComponentContext.setContext( (ComponentContext)
>> contextStack.pop(), request);
>> }
>> -}
>> +}
>> \ No newline at end of file
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
--
Geir Magnusson Jr 203-247-1713(m)
geirm@optonline.net
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts
TilesTool.java
Posted by "Daniel L. Rall" <dl...@finemaltcoding.com>.
Why did you reformat the source file? Generally speaking, making changes that
fit in with the existing style is preferred.
- Dan
marino@apache.org wrote:
> marino 2003/10/29 16:03:37
>
> Modified: src/java/org/apache/velocity/tools/struts TilesTool.java
> Log:
> updated to extend ImportSupport
>
> Revision Changes Path
> 1.2 +64 -124 jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/TilesTool.java
>
> Index: TilesTool.java
> ===================================================================
> RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/TilesTool.java,v
> retrieving revision 1.1
> retrieving revision 1.2
> diff -u -r1.1 -r1.2
> --- TilesTool.java 24 Jul 2003 04:59:21 -0000 1.1
> +++ TilesTool.java 30 Oct 2003 00:03:37 -0000 1.2
> @@ -61,10 +61,6 @@
> import javax.servlet.http.HttpSession;
> import javax.servlet.ServletContext;
>
> -import java.io.StringWriter;
> -import org.apache.velocity.app.Velocity;
> -import org.apache.velocity.Template;
> -
> import org.apache.struts.tiles.ComponentContext;
> import org.apache.struts.tiles.ComponentDefinition;
> import org.apache.struts.tiles.AttributeDefinition;
> @@ -76,10 +72,11 @@
> import org.apache.struts.tiles.DefinitionsFactoryException;
> import org.apache.struts.tiles.Controller;
>
> +import org.apache.velocity.app.Velocity;
> +import org.apache.velocity.tools.view.ImportSupport;
> import org.apache.velocity.tools.view.context.ViewContext;
> import org.apache.velocity.tools.view.tools.ViewTool;
>
> -
> /**
> * <p>Title: TilesTool</p>
> * <p>Description: A tool to use struts-tiles with Velocity</p>
> @@ -94,13 +91,10 @@
> * @author <a href="mailto:marinoj@centrum.is">Marino A. Jonsson</a>
> * @version $Revision$ $Date$
> */
> -public class TilesTool implements ViewTool
> -{
> +public class TilesTool extends ImportSupport
> + implements ViewTool {
>
> protected ViewContext context;
> - protected ServletContext application;
> - protected HttpServletRequest request;
> - protected HttpServletResponse response;
>
> /**
> * A stack to hold ComponentContexts while nested tile-definitions
> @@ -108,7 +102,6 @@
> */
> protected Stack contextStack;
>
> -
> /******************************* Constructors ****************************/
>
> /**
> @@ -116,54 +109,47 @@
> */
> public TilesTool() {}
>
> -
> /**
> * Initializes this tool.
> *
> * @param obj the current ViewContext
> * @throws IllegalArgumentException if the param is not a ViewContext
> */
> - public void init(Object obj)
> - {
> - if (!(obj instanceof ViewContext))
> - {
> + public void init(Object obj) {
> + if (! (obj instanceof ViewContext)) {
> throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
> }
>
> - this.context = (ViewContext)obj;
> + this.context = (ViewContext) obj;
> this.request = context.getRequest();
> this.response = context.getResponse();
> this.application = context.getServletContext();
> }
>
> -
> /***************************** View Helpers ******************************/
>
> /**
> * Fetches a named attribute value from the current tiles-context.
> *
> - * <p>This is functionally equivalent to
> + * <p>This is functionally equivalent to
> * <code><tiles:getAsString name="title" /></code>.</p>
> *
> * @param name the name of the tiles-attribute to fetch
> * @return the attribute value as String
> */
> - public String getString(String name)
> - {
> + public String getString(String name) {
> ComponentContext context = ComponentContext.getContext(request);
> Object attrValue = context.getAttribute(name);
> - if (attrValue == null)
> - {
> + if (attrValue == null) {
> return null;
> }
> return attrValue.toString();
> }
>
> -
> /**
> * <p>A generic tiles insert function</p>
> *
> - * <p>This is functionally equivalent to
> + * <p>This is functionally equivalent to
> * <code><tiles:insert attribute="menu" /></code>.</p>
> *
> * @param attr - can be any of the following:
> @@ -175,49 +161,48 @@
> * @return the rendered template or value as a String
> * @throws Exception on failure
> */
> - public String get(Object attr) throws Exception
> - {
> - ComponentContext currentContext = ComponentContext.getContext(request);
> - Object attrValue = currentContext.getAttribute(attr.toString());
> - if (attrValue != null)
> - {
> - return processObjectValue(attrValue);
> + public String get(Object attr) throws Exception {
> + try {
> + ComponentContext currentContext = ComponentContext.getContext(request);
> + Object attrValue = currentContext.getAttribute(attr.toString());
> + if (attrValue != null) {
> + return processObjectValue(attrValue);
> + }
> + return processAsDefinitionOrURL(attr.toString());
> + }
> + catch(Exception e) {
> + Velocity.error("Exeption while rendering Tile " + attr.toString() + ": " + e.getMessage());
> + return null;
> }
> - return processAsDefinitionOrURL(attr.toString());
> }
>
> -
> /************************** Protected Methods ****************************/
>
> /**
> * Process an object retrieved as a bean or attribute.
> *
> * @param value - Object can be a typed attribute, a String, or anything
> - * else. If typed attribute, use associated type. Otherwise, apply
> + * else. If typed attribute, use associated type. Otherwise, apply
> * toString() on object, and use returned string as a name.
> - * @throws Exception - Throws by underlying nested call to
> + * @throws Exception - Throws by underlying nested call to
> * processDefinitionName()
> * @return the fully processed value as String
> */
> - protected String processObjectValue(Object value) throws Exception
> - {
> + protected String processObjectValue(Object value) throws Exception {
> /* First, check if value is one of the Typed Attribute */
> - if (value instanceof AttributeDefinition)
> - {
> + if (value instanceof AttributeDefinition) {
> /* We have a type => return appropriate IncludeType */
> - return processTypedAttribute((AttributeDefinition)value);
> + return processTypedAttribute( (AttributeDefinition) value);
>
> }
> - else if (value instanceof ComponentDefinition)
> - {
> - return processDefinition((ComponentDefinition)value);
> + else if (value instanceof ComponentDefinition) {
> + return processDefinition( (ComponentDefinition) value);
> }
>
> /* Value must denote a valid String */
> return processAsDefinitionOrURL(value.toString());
> }
>
> -
> /**
> * Process typed attribute according to its type.
> *
> @@ -226,25 +211,21 @@
> * @throws Exception - Throws by underlying nested call to processDefinitionName()
> */
> protected String processTypedAttribute(AttributeDefinition value) throws Exception {
> - if (value instanceof DirectStringAttribute)
> - {
> - return (String)value.getValue();
> + if (value instanceof DirectStringAttribute) {
> + return (String) value.getValue();
>
> }
> - else if (value instanceof DefinitionAttribute)
> - {
> - return processDefinition((ComponentDefinition)value.getValue());
> + else if (value instanceof DefinitionAttribute) {
> + return processDefinition( (ComponentDefinition) value.getValue());
>
> }
> - else if (value instanceof DefinitionNameAttribute)
> - {
> - return processAsDefinitionOrURL((String)value.getValue());
> + else if (value instanceof DefinitionNameAttribute) {
> + return processAsDefinitionOrURL( (String) value.getValue());
> }
> /* else if( value instanceof PathAttribute ) */
> - return doInsert((String)value.getValue(), null, null);
> + return doInsert( (String) value.getValue(), null, null);
> }
>
> -
> /**
> * Try to process name as a definition, or as an URL if not found.
> *
> @@ -253,24 +234,20 @@
> * @throws Exception
> */
> protected String processAsDefinitionOrURL(String name) throws Exception {
> - try
> - {
> + try {
> ComponentDefinition definition =
> TilesUtil.getDefinition(name, request, application);
> - if (definition != null)
> - {
> + if (definition != null) {
> return processDefinition(definition);
> }
> }
> - catch (DefinitionsFactoryException ex)
> - {
> + catch (DefinitionsFactoryException ex) {
> /* silently failed, because we can choose to not define a factory. */
> }
> /* no definition found, try as url */
> return processUrl(name);
> }
>
> -
> /**
> * End of Process for definition.
> *
> @@ -278,12 +255,10 @@
> * @return the fully processed definition.
> * @throws Exception from InstantiationException Can't create requested controller
> */
> - protected String processDefinition(ComponentDefinition definition) throws Exception
> - {
> + protected String processDefinition(ComponentDefinition definition) throws Exception {
> Controller controller = null;
>
> - try
> - {
> + try {
> controller = definition.getOrCreateController();
>
> String role = definition.getRole();
> @@ -294,13 +269,11 @@
> role,
> controller);
> }
> - catch (InstantiationException ex)
> - {
> + catch (InstantiationException ex) {
> throw new Exception(ex.getMessage());
> }
> }
>
> -
> /**
> * Processes an url
> *
> @@ -308,12 +281,10 @@
> * @return the rendered template as String.
> * @throws Exception
> */
> - protected String processUrl(String url) throws Exception
> - {
> + protected String processUrl(String url) throws Exception {
> return doInsert(url, null, null);
> }
>
> -
> /**
> * Use this if there is no nested tile.
> *
> @@ -323,10 +294,8 @@
> * @return the rendered template as String.
> * @throws Exception
> */
> - protected String doInsert(String page, String role, Controller controller) throws Exception
> - {
> - if (role != null && !request.isUserInRole(role))
> - {
> + protected String doInsert(String page, String role, Controller controller) throws Exception {
> + if (role != null && !request.isUserInRole(role)) {
> return null;
> }
>
> @@ -334,7 +303,6 @@
> return doInsert(subCompContext, page, role, controller);
> }
>
> -
> /**
> * Use this if there is a nested tile.
> *
> @@ -345,13 +313,11 @@
> * @return the rendered template as String.
> * @throws Exception
> */
> - protected String doInsert(Map attributes,
> - String page,
> - String role,
> - Controller controller) throws Exception
> - {
> - if (role != null && !request.isUserInRole(role))
> - {
> + protected String doInsert(Map attributes,
> + String page,
> + String role,
> + Controller controller) throws Exception {
> + if (role != null && !request.isUserInRole(role)) {
> return null;
> }
>
> @@ -359,7 +325,6 @@
> return doInsert(subCompContext, page, role, controller);
> }
>
> -
> /**
> * An extension of the other two doInsert functions
> *
> @@ -371,71 +336,46 @@
> * @return the rendered template as String.
> * @throws Exception
> */
> - protected String doInsert(ComponentContext subCompContext,
> - String page,
> + protected String doInsert(ComponentContext subCompContext,
> + String page,
> String role,
> - Controller controller) throws Exception
> - {
> + Controller controller) throws Exception {
> pushTilesContext();
> - try
> - {
> + try {
> ComponentContext.setContext(subCompContext, request);
>
> /* Call controller if any */
> - if (controller != null)
> - {
> + if (controller != null) {
> controller.perform(subCompContext,
> request,
> response,
> application);
> }
> - return parse(page);
> + return this.acquireString(page);
> }
> - finally
> - {
> + finally {
> popTilesContext();
> }
> }
>
> -
> /**
> * <p>pushes the current tiles context onto the context-stack.
> * preserving the context is necessary so that a sub-context can be
> * put into request scope and lower level tiles can be rendered</p>
> */
> - protected void pushTilesContext()
> - {
> - if (contextStack == null)
> - {
> + protected void pushTilesContext() {
> + if (contextStack == null) {
> contextStack = new Stack();
> }
> contextStack.push(ComponentContext.getContext(request));
> }
>
> -
> /**
> * <p>pops the tiles sub-context off the context-stack after the lower level
> * tiles have been rendered</p>
> */
> - protected void popTilesContext()
> - {
> - ComponentContext.setContext((ComponentContext)contextStack.pop(), request);
> - }
> -
> -
> - /**
> - * <p>Renders a template</p>
> - *
> - * @param templateName - name of template to be rendered
> - * @throws Exception if it fails
> - * @return the rendered template as a String
> - */
> - protected String parse(String templateName) throws Exception
> - {
> - StringWriter sw = new StringWriter();
> - Template template = Velocity.getTemplate(templateName);
> - template.merge(context.getVelocityContext(), sw);
> - return sw.toString();
> + protected void popTilesContext() {
> + ComponentContext.setContext( (ComponentContext) contextStack.pop(), request);
> }
>
> -}
> +}
> \ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org