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 25 public class ActionControllerUtil { 26 27 31 public final static String defaultConfigFile = "WEB-INF/actions.xml"; 32 33 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 73 public static void parseFile(HashMap dest, ActionData global_forwards, String filePath, String fileName) throws ServletException { 74 75 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 try { 90 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 92 dbf.setNamespaceAware(true); 93 dbf.setValidating(false); 94 dbf.setIgnoringComments(true); 95 96 DocumentBuilder db = dbf.newDocumentBuilder(); 98 Document doc = db.parse(new File(configFile)); 99 100 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); if (mparam != null) { 116 actd.putParameter("mparam", mparam); } 118 if (type == null) { 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); } 128 } 129 dest.put(path, actd); 130 } 131 132 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 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 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); } 176 return; 177 } 178 179 180 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); } 196 return; 197 } 198 199 } 200 | Popular Tags |