[cvs]
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/velocity
ExpressoTool.java
JCorporate Ltd
jcorp at jcorp2.servlets.net
Sat May 29 09:51:33 PDT 2004
Update of /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/velocity
In directory jcorp2.servlets.net:/tmp/cvs-serv1245
Added Files:
ExpressoTool.java
Log Message:
Initial checkin of velocity support
--- NEW FILE: ExpressoTool.java ---
package com.jcorporate.expresso.ext.velocity;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import com.jcorporate.expresso.core.controller.Block;
import com.jcorporate.expresso.core.controller.ControllerElement;
import com.jcorporate.expresso.core.controller.ControllerException;
import com.jcorporate.expresso.core.controller.ControllerRequest;
import com.jcorporate.expresso.core.controller.ControllerResponse;
import com.jcorporate.expresso.core.db.DBException;
import com.jcorporate.expresso.core.i18n.Messages;
import com.jcorporate.expresso.services.dbobj.SchemaList;
/**
* A generic object that allows Velocity pages to be written more user friendly.
*
* @author David Lloyd
* @version $Revision: 1.1 $ on $Date: 2004/05/29 16:51:31 $
*
* $Log: ExpressoTool.java,v $
* Revision 1.1 2004/05/29 16:51:31 dlloyd
* Initial checkin of velocity support
*
*
*/
public class ExpressoTool {
/**
* Return a string in the messages bundles. The current schema stack is seached for the
* correct translation. This is a wrapper for Messages.getString(stack,locale,code,args).
*
* @param response The controllerResponse request attribute that contains the schema stack.
* @param code The string code being searched for.
* @param args Any replacement arguments; can be null.
* @return The code translation if found, otherwise the code itself.
* @throws ControllerException
*/
public String getString(ControllerResponse response, String code, List args) throws ControllerException {
Object[] argsArray;
if (args == null)
argsArray = new Object[0];
else
argsArray = args.toArray();
return Messages.getString( response.getSchemaStack(),
response.getLocale(),
code,
argsArray);
}
/**
* Find a <code>ControllerElement</code> in the response identified by a path.
* This path is somewhat similar to the paths used in the taglibs. The differences
* are that a leading '/' means start from the controller response and a '@' means
* to return the string which is the attribute by the name that follows. Also, if
* the path item name cannot be found and the name starts with one of 'BLOCK:',
* 'OUTPUT:', 'INPUT:', or 'TRANSITION:', then the prefix is removed and the search
* is made again limited to the type of object specified.
*
* <p><pre>
* An example use in Velocity:
* #exp_element( $myelement "lists/groups/tests" )
* returns the element named 'tests'
*
* #exp_element( $myelement "lists/BLOCK:groups/tests" )
* returns the same element named 'tests' as above but useful if
* an object named groups exists in transitions (for example).
*
* #exp_element( $myelement "lists/groups/@style" )
* returns the string for attribute 'style' in 'groups'
* </pre>
* @param response The controllerResponse request attribute.
* @param path The path to the element or attribute to be returned.
* @return The found object or null.
* @throws ControllerException
*/
public Object getElement(ControllerResponse response, String path) throws ControllerException {
if (path == null || path.length() == 0)
return null;
if (path.charAt(0) == '/')
if (path.length() == 1)
return null;
else
path = path.substring(1);
Object element = null;
StringTokenizer tok = new StringTokenizer(path, "/");
if (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();
boolean isAttribute = false;
if (token.length() > 1) {
if (token.charAt(0) == '@') {
isAttribute = true;
token = token.substring(1);
}
}
if (token.length() != 0) {
if (isAttribute) {
return response.getAttribute(token);
} else {
element = response.getNestedMap().get(token);
if (element == null) {
if (token.startsWith("BLOCK:")) {
element = response.getBlock(token.substring(6));
} else if (token.startsWith("OUTPUT:")) {
element = response.getBlock(token.substring(7));
} else if (token.startsWith("INPUT:")) {
element = response.getBlock(token.substring(6));
} else if (token.startsWith("TRANSITION:")) {
element = response.getBlock(token.substring(11));
}
}
}
}
if (tok.hasMoreTokens()) {
element = getElementFrom(response, (ControllerElement)element, tok);
}
}
return element;
}
/**
* Find a <code>ControllerElement</code> in the response identified by a path.
* This path is somewhat similar to the paths used in the taglibs. The differences
* are that a leading '/' means start from the controller response and a '@' means
* to return the string which is the attribute by the name that follows. Also, if
* the path item name cannot be found and the name starts with one of 'BLOCK:',
* 'OUTPUT:', 'INPUT:', or 'TRANSITION:', then the prefix is removed and the search
* is made again limited to the type of object specified. If the from parameter is null
* or the path starts with a '/' then this function behaves the same as
* getElement(response, path).
*
* <p><pre>
* An example use in Velocity:
* #exp_elementFrom( $myelement $fromElement "lists/groups/tests" )
* returns the element named 'tests'
*
* #exp_element( $myelement $fromElement "lists/BLOCK:groups/tests" )
* returns the same element named 'tests' as above but useful if
* an object named groups exists in transitions (for example).
*
* #exp_element( $myelement $fromElement "lists/groups/@style" )
* returns the string for attribute 'style' in 'groups'
* </pre>
* @param response The controllerResponse request attribute.
* @param path The path to the element or attribute to be returned.
* @param from The ControllerElement to start the search from.
* @return The found object or null.
* @throws ControllerException
*/
public Object getElementFrom(ControllerResponse response, ControllerElement from, String path) throws ControllerException {
if (from == null)
return getElement(response, path);
if (path == null || path.length() == 0)
return from;
if (path.charAt(0) == '/')
return getElement(response, path);
StringTokenizer tok = new StringTokenizer(path, "/");
Object element = getElementFrom(response, from, tok);
return element;
}
private Object getElementFrom(ControllerResponse response, ControllerElement from, StringTokenizer tok) throws ControllerException {
ControllerElement element = null;
while (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();
boolean isAttribute = false;
if (token.length() > 1) {
if (token.charAt(0) == '@') {
isAttribute = true;
token = token.substring(1);
}
}
if (token.length() != 0) {
if (isAttribute) {
return from.getAttribute(token);
} else {
element = (ControllerElement) from.getNestedMap().get(token);
if (element == null && (from instanceof Block)) {
if (token.startsWith("BLOCK:")) {
element = ((Block)from).getBlock(token.substring(6));
} else if (token.startsWith("OUTPUT:")) {
element = ((Block)from).getBlock(token.substring(7));
} else if (token.startsWith("INPUT:")) {
element = ((Block)from).getBlock(token.substring(6));
} else if (token.startsWith("TRANSITION:")) {
element = ((Block)from).getBlock(token.substring(11));
}
}
if (element == null) {
return null;
} else {
from = element;
}
}
}
}
return from;
}
/**
* Get a value from the setup table. A simple wrapper for Setup.getValue(dataContext, key).
*
* @param dataContext The data context for the setup table.
* @param key The key whose value is to be retrieved.
* @return The found value or null
* @see com.jcorporate.expresso.services.dbobj.Setup
* @throws DBException
*/
public String getSetupValue(String dataContext, String key) throws DBException {
return com.jcorporate.expresso.services.dbobj.Setup.getValue(dataContext, key);
}
/**
* Get the ConfigManager singleton.
*
* @return The ConfigManager singleton.
*/
public com.jcorporate.expresso.core.misc.ConfigManager getConfigManager() {
return com.jcorporate.expresso.core.misc.ConfigManager.getInstance();
}
/**
* Returns the list of installed schemas. This would commonly be used to support the
* current expresso tradition of listin the installed components in the left menu.
*
* @param dataContext The data context for SchemaList (typically 'default').
* @return The SchemaList.
* @throws DBException
*/
public List getSchemaList(String dataContext) throws DBException {
SchemaList sl = new SchemaList(SchemaList.SYSTEM_ACCOUNT);
sl.setDBName(dataContext);
return sl.searchAndRetrieveList();
}
}
More information about the cvs
mailing list