KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > ActionControllerUtil


1 package jodd.servlet;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.servlet.ServletException;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.TransformerException;
14
15 import jodd.util.XMLUtil;
16
17 import org.apache.xpath.XPathAPI;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Node;
20 import org.xml.sax.SAXException;
21
22 /**
23  * ActionController helper class.
24  */

25 public class ActionControllerUtil {
26
27     /**
28      * Default relative address of main configuration xml file that defines
29      * actions.
30      */

31     public final static String defaultConfigFile = "WEB-INF/actions.xml";
32
33     /**
34      * Adds all forwards and params subtags.
35      *
36      * @param dest destination action data
37      * @param srcNode source node where to read from
38      */

39     private static void addActionData(ActionData dest, Node srcNode) {
40         List nodes = XMLUtil.getNodes(srcNode.getChildNodes());
41         for (int j = 0; j < nodes.size(); j++) {
42             Node node = (Node) nodes.get(j);
43             if (node.getNodeName().equals("forward")) {
44                 addForward(dest, node);
45                 continue;
46             }
47             if (node.getNodeName().equals("param")) {
48                 addParameter(dest, node);
49                 continue;
50             }
51         }
52     }
53
54     /**
55      * Parses actions configuration xml file and loads a hashmap with processed
56      * data. Target HashMap will be used by ActionController for redirecting the
57      * requests.
58      * <p>
59      *
60      * This method is recursive, i.e. configuration file may contains imports of
61      * other configuration files. This may be used to split one big configuration
62      * file to many shorter ones. The result will be the same.
63      *
64      * @param dest target hashmap
65      * @param global_forwards
66      * target global forwards ActionData
67      * @param filePath local root-path of the folder that contains all config files. usually it
68      * is the local path of the application web folder.
69      * @param fileName relative path to the exact actions xml configuration file.
70      *
71      * @exception ServletException
72      */

73     public static void parseFile(HashMap dest, ActionData global_forwards, String filePath, String fileName) throws ServletException {
74
75         // build file path for configuration file
76
if (fileName == null) {
77             fileName = defaultConfigFile;
78         }
79         String configFile = filePath;
80         if (filePath.endsWith("/") == false) {
81             configFile += "/";
82         }
83         if (fileName.startsWith("/")) {
84             fileName = fileName.substring(1);
85         }
86         configFile += fileName;
87
88         // read configuration XML
89
try {
90             // create a DocumentBuilderFactory and configure it
91
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
92             dbf.setNamespaceAware(true);
93             dbf.setValidating(false);
94             dbf.setIgnoringComments(true);
95
96             // create a DocumentBuilder and parse the input file to get a Document object
97
DocumentBuilder db = dbf.newDocumentBuilder();
98             Document doc = db.parse(new File(configFile));
99
100             // read all actions and action-mappins
101
List nodelist = XMLUtil.getNodes(doc, "/actions/action");
102
103             for (int i = 0; i < nodelist.size(); i++) {
104                 Node action_node = (Node) nodelist.get(i);
105                 Map attrs = XMLUtil.getNodeAttributes(action_node);
106                 String path = (String) attrs.get("path");
107                 String type = (String) attrs.get("type");
108                 String method = (String) attrs.get("method");
109                 String mapping = (String) attrs.get("mapping");
110                 String mparam = (String) attrs.get("mparam");
111
112                 ActionData actd = new ActionData(path, type, method);
113                 
114                 addActionData(actd, action_node); // add action data
115
if (mparam != null) {
116                     actd.putParameter("mparam", mparam); // add optional mparam attribute
117
}
118                 if (type == null) { // action-mapping specified
119
Node tempNode = XPathAPI.selectSingleNode(doc, "/actions/action-mapping[@name='" + mapping + "']");
120                     if (tempNode != null) {
121                         attrs = XMLUtil.getNodeAttributes(tempNode);
122                         type = (String) attrs.get("type");
123                         method = (String) attrs.get("method");
124                         actd.setType(type);
125                         actd.setMethod(method);
126                         addActionData(actd, tempNode); // add action-mapping data
127
}
128                 }
129                 dest.put(path, actd);
130             }
131
132             // read all global forwards
133
nodelist = XMLUtil.getNodes(doc, "/actions/global-forwards/forward");
134             for (int i = 0; i < nodelist.size(); i++) {
135                 addForward(global_forwards, (Node)nodelist.get(i));
136             }
137
138             // process all included files
139
nodelist = XMLUtil.getNodes(doc, "/actions/include");
140             for (int i = 0; i < nodelist.size(); i++) {
141                 Map attrs = XMLUtil.getNodeAttributes((Node)nodelist.get(i));
142                 parseFile(dest, global_forwards, filePath, (String) attrs.get("file"));
143             }
144         } catch (ParserConfigurationException pcex) {
145             throw new ServletException("parser configuration error" + pcex.toString());
146         } catch (SAXException saxex) {
147             throw new ServletException("xml parse error" + saxex.toString());
148         } catch (TransformerException tex) {
149             throw new ServletException("xml transforming error" + tex.toString());
150         } catch (IOException ioex) {
151             throw new ServletException("io error" + ioex.toString());
152         }
153     }
154
155
156     /**
157      * Adds forward information into the ActionDate object, from a XML node.
158      *
159      * @param acd
160      * @param forward_node
161      */

162     public static void addForward(ActionData acd, Node forward_node) {
163         if (forward_node == null) {
164             return;
165         }
166         Map attrs = XMLUtil.getNodeAttributes(forward_node);
167         String f_name = (String) attrs.get("name");
168         String f_path = (String) attrs.get("path");
169         String f_rdrc = (String) attrs.get("redirect");
170         if (f_rdrc == null) {
171             f_rdrc = "false";
172         }
173         if (f_name != null) {
174             acd.putForwardPath(f_name, f_path, f_rdrc); // add forward info to action data
175
}
176         return;
177     }
178
179
180     /**
181      * Adds parameter information into the ActionDate object, from a XML node.
182      *
183      * @param acd
184      * @param parameter_node
185      */

186     public static void addParameter(ActionData acd, Node parameter_node) {
187         if (parameter_node == null) {
188             return;
189         }
190         Map attrs = XMLUtil.getNodeAttributes(parameter_node);
191         String f_name = (String) attrs.get("name");
192         String f_value = (String) attrs.get("value");
193         if (f_name != null) {
194             acd.putParameter(f_name, f_value); // add parameter info to action data
195
}
196         return;
197     }
198
199 }
200
Popular Tags