webwork/src/main/webwork/config/util XMLConfigurationReader.java,NONE,1.1
[email protected] Sun, 16 Jan 2005 21:10:40 -0800
| Newsgroups | gmane.comp.java.open-symphony.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/opensymphony/webwork/src/main/webwork/config/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27114/src/main/webwork/config/util
Added Files:
XMLConfigurationReader.java
Log Message:
Allow pluggable configurations for webwork.
You can now specify your own configuration objects in
webwork.properties.
Introduced the (badly named) ConfigurationInterface object, which all
configurations have to implement.
Split out the XML reading from the XML configuration object, so that it
can be reused from other configuration objects.
Fixes WW-722
--- NEW FILE: XMLConfigurationReader.java ---
package webwork.config.util;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import webwork.config.Configuration;
public class XMLConfigurationReader
{
public static final Log log = LogFactory.getLog(XMLConfigurationReader.class);
/**
* This stores the mapping from URL -> action. It looks through the (xml) configuration file and
* adds all the commands, aliases, and views.
* <p>
* One caveat - if you are using an extension aside from '.action', the views will be added with
* '.action' here, and then replaced on retrieval. This is to warn off a recursive dependency that
* I haven't had a chance to look at yet.
*/
private final Map actionMappings;
private static final String EXTENSION_PROPERTY = "webwork.action.extension";
public XMLConfigurationReader(Element configurationRootElement)
{
actionMappings = getMappingsFromDocument(configurationRootElement);
}
public Object getActionMapping(String mappingName)
{
return actionMappings.get(replaceExtension(mappingName));
}
public Set getActionMappingNames()
{
return actionMappings.keySet();
}
private Map getMappingsFromDocument(Element element)
{
Map actionMap = new HashMap();
// Get list of actions
NodeList actions = element.getElementsByTagName("action");
// Build list of views
int length = actions.getLength();
for (int i = 0; i < length; i++)
{
Element action = (Element)actions.item(i);
String actionName = action.getAttribute("name");
String actionAlias = action.getAttribute("alias");
// Build views for this action
{
NodeList views = action.getElementsByTagName("view");
for (int j = 0; j < views.getLength(); j++)
{
Element view = (Element)views.item(j);
// This is to avoid listing "view" elements
// that are associated with the commands
// of this action
if (!view.getParentNode().equals(action))
break;
// View mappings for this action
NodeList viewMapping = view.getChildNodes();
StringBuffer mapping = new StringBuffer();
for (int k = 0; k < viewMapping.getLength(); k++)
{
Node mappingNode = viewMapping.item(k);
if (mappingNode instanceof Text)
{
mapping.append(mappingNode.getNodeValue());
}
}
String actionViewName;
if ("".equals(actionAlias))
{
if(!"".equals(actionName))
{
actionViewName = actionName+"."+view.getAttribute("name");
}
else
{
actionViewName = view.getAttribute("name");
}
}
else
{
actionViewName = actionAlias+"."+view.getAttribute("name");
log.debug("Adding action alias "+actionAlias+"="+actionName);
actionMap.put(actionAlias+".action",
actionName);
}
String actionViewMapping = mapping.toString().trim();
log.debug("Adding view mapping "+actionViewName+"="+actionViewMapping);
actionMap.put(actionViewName, actionViewMapping);
}
}
// Commands
NodeList commands = action.getElementsByTagName("command");
for (int j = 0; j < commands.getLength(); j++)
{
Element command = (Element)commands.item(j);
String commandName = command.getAttribute("name");
String commandAlias = command.getAttribute("alias");
if (!commandAlias.equals(""))
{
log.debug("Adding command alias "+commandAlias+"="+actionName+"!"+commandName);
actionMap.put(commandAlias+".action",
actionName+"!"+commandName);
}
// Build views for this action
NodeList views = command.getElementsByTagName("view");
for (int k = 0; k < views.getLength(); k++)
{
Element view = (Element)views.item(k);
// View mappings for this action
NodeList viewMapping = view.getChildNodes();
StringBuffer mapping = new StringBuffer();
for (int l = 0; l < viewMapping.getLength(); l++)
{
Node mappingNode = viewMapping.item(l);
if (mappingNode instanceof Text)
{
mapping.append(mappingNode.getNodeValue());
}
}
String commandViewName;
if (commandAlias.equals(""))
{
if (actionAlias.equals(""))
commandViewName = actionName+"!"+commandName+"."+view.getAttribute("name");
else
commandViewName = actionAlias+"!"+commandName+"."+view.getAttribute("name");
}
else
{
commandViewName = commandAlias+"."+view.getAttribute("name");
}
String commandViewMapping = mapping.toString().trim();
log.debug("Adding command view mapping "+commandViewName+"="+commandViewMapping);
actionMap.put(commandViewName, commandViewMapping);
}
}
}
return actionMap;
}
/**
* As the actions are stored in the action mapping with a '.action' extension, we need to map the current
* request to that '.action' mapping.
* <p>
* So an action 'ABC.jspa' would be lookup up in the map at 'ABC.action'.
* <p>
* The extension used is retrieved from the configuration using key 'webwork.action.extension'
* @param actionName The original action (url) to be mapped, including an extension (if any).
* @return The action name used in actionMappings - ie with a '.action' extension.
*/
private String replaceExtension(String actionName){
//this prevents a possible recursion with getting this property
if (EXTENSION_PROPERTY.equals(actionName))
return actionName;
String ext = "." + Configuration.get(EXTENSION_PROPERTY);
//replace all custom extensions with .action to retrieve view mapping from cache
if (actionName != null && !".action".equals(ext))
{
// try to find another extension
if (actionName.endsWith(ext))
actionName = actionName.substring(0, actionName.lastIndexOf(ext)) + ".action";
// otherwise look for something like .jspa? in the view mapping and replace
// with .action?
int idx = actionName.indexOf(ext + "?");
if (idx > 0)
{
actionName = actionName.substring(0, idx) + ".action?" + actionName.substring(idx + ext.length() + 1);
}
}
return actionName;
}
}
-------------------------------------------------------
The SF.Net email is sponsored by: Beat the post-holiday blues
Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek.
It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt