📄 actioncontrollerutil.java
字号:
package jodd.servlet;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import jodd.util.XMLUtil;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* ActionController helper class.
*/
public class ActionControllerUtil {
/**
* Default relative address of main configuration xml file that defines
* actions.
*/
public final static String defaultConfigFile = "WEB-INF/actions.xml";
/**
* Adds all forwards and params subtags.
*
* @param dest destination action data
* @param srcNode source node where to read from
*/
private static void addActionData(ActionData dest, Node srcNode) {
List nodes = XMLUtil.getNodes(srcNode.getChildNodes());
for (int j = 0; j < nodes.size(); j++) {
Node node = (Node) nodes.get(j);
if (node.getNodeName().equals("forward")) {
addForward(dest, node);
continue;
}
if (node.getNodeName().equals("param")) {
addParameter(dest, node);
continue;
}
}
}
/**
* Parses actions configuration xml file and loads a hashmap with processed
* data. Target HashMap will be used by ActionController for redirecting the
* requests.
* <p>
*
* This method is recursive, i.e. configuration file may contains imports of
* other configuration files. This may be used to split one big configuration
* file to many shorter ones. The result will be the same.
*
* @param dest target hashmap
* @param global_forwards
* target global forwards ActionData
* @param filePath local root-path of the folder that contains all config files. usually it
* is the local path of the application web folder.
* @param fileName relative path to the exact actions xml configuration file.
*
* @exception ServletException
*/
public static void parseFile(HashMap dest, ActionData global_forwards, String filePath, String fileName) throws ServletException {
// build file path for configuration file
if (fileName == null) {
fileName = defaultConfigFile;
}
String configFile = filePath;
if (filePath.endsWith("/") == false) {
configFile += "/";
}
if (fileName.startsWith("/")) {
fileName = fileName.substring(1);
}
configFile += fileName;
// read configuration XML
try {
// create a DocumentBuilderFactory and configure it
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
dbf.setIgnoringComments(true);
// create a DocumentBuilder and parse the input file to get a Document object
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(configFile));
// read all actions and action-mappins
List nodelist = XMLUtil.getNodes(doc, "/actions/action");
for (int i = 0; i < nodelist.size(); i++) {
Node action_node = (Node) nodelist.get(i);
Map attrs = XMLUtil.getNodeAttributes(action_node);
String path = (String) attrs.get("path");
String type = (String) attrs.get("type");
String method = (String) attrs.get("method");
String mapping = (String) attrs.get("mapping");
String mparam = (String) attrs.get("mparam");
ActionData actd = new ActionData(path, type, method);
addActionData(actd, action_node); // add action data
if (mparam != null) {
actd.putParameter("mparam", mparam); // add optional mparam attribute
}
if (type == null) { // action-mapping specified
Node tempNode = XPathAPI.selectSingleNode(doc, "/actions/action-mapping[@name='" + mapping + "']");
if (tempNode != null) {
attrs = XMLUtil.getNodeAttributes(tempNode);
type = (String) attrs.get("type");
method = (String) attrs.get("method");
actd.setType(type);
actd.setMethod(method);
addActionData(actd, tempNode); // add action-mapping data
}
}
dest.put(path, actd);
}
// read all global forwards
nodelist = XMLUtil.getNodes(doc, "/actions/global-forwards/forward");
for (int i = 0; i < nodelist.size(); i++) {
addForward(global_forwards, (Node)nodelist.get(i));
}
// process all included files
nodelist = XMLUtil.getNodes(doc, "/actions/include");
for (int i = 0; i < nodelist.size(); i++) {
Map attrs = XMLUtil.getNodeAttributes((Node)nodelist.get(i));
parseFile(dest, global_forwards, filePath, (String) attrs.get("file"));
}
} catch (ParserConfigurationException pcex) {
throw new ServletException("parser configuration error" + pcex.toString());
} catch (SAXException saxex) {
throw new ServletException("xml parse error" + saxex.toString());
} catch (TransformerException tex) {
throw new ServletException("xml transforming error" + tex.toString());
} catch (IOException ioex) {
throw new ServletException("io error" + ioex.toString());
}
}
/**
* Adds forward information into the ActionDate object, from a XML node.
*
* @param acd
* @param forward_node
*/
public static void addForward(ActionData acd, Node forward_node) {
if (forward_node == null) {
return;
}
Map attrs = XMLUtil.getNodeAttributes(forward_node);
String f_name = (String) attrs.get("name");
String f_path = (String) attrs.get("path");
String f_rdrc = (String) attrs.get("redirect");
if (f_rdrc == null) {
f_rdrc = "false";
}
if (f_name != null) {
acd.putForwardPath(f_name, f_path, f_rdrc); // add forward info to action data
}
return;
}
/**
* Adds parameter information into the ActionDate object, from a XML node.
*
* @param acd
* @param parameter_node
*/
public static void addParameter(ActionData acd, Node parameter_node) {
if (parameter_node == null) {
return;
}
Map attrs = XMLUtil.getNodeAttributes(parameter_node);
String f_name = (String) attrs.get("name");
String f_value = (String) attrs.get("value");
if (f_name != null) {
acd.putParameter(f_name, f_value); // add parameter info to action data
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -