1 /**
2 *3 */
4 packagecom.linker.util;5
6 importjava.io.File;7 importjava.io.IOException;8 importjava.util.ArrayList;9 importjava.util.Iterator;10 importjava.util.List;11 importjava.util.Map;12 importjava.util.Set;13
14 importjavax.xml.parsers.DocumentBuilder;15 importjavax.xml.parsers.DocumentBuilderFactory;16 importjavax.xml.parsers.ParserConfigurationException;17
18 importorg.w3c.dom.Document;19 importorg.w3c.dom.Element;20 importorg.w3c.dom.Node;21 importorg.w3c.dom.NodeList;22 importorg.xml.sax.SAXException;23
24 /**
25 * Title:Xmlparse4FormVerify26 * description: 读取xml中配置的表单验证信息27 * company: linker28 *@authorzhanjp29 * @date 2016年2月22日 上午9:33:5930 */
31 public classXmlparse4FormVerify {32
33 private static DocumentBuilder db = null;34
35 private static DocumentBuilderFactory dbf = null;36
37 private static Document dt = null;38
39 public staticXmlparse4FormVerify xp;40
41
42 /**
43 * 初始化DocumentBuilder等信息44 *@return
45 */
46 publicXmlparse4FormVerify getInstance(){47 if(xp==null){48 xp = newXmlparse4FormVerify();49 }50 returnxp;51 }52
53 static{54 try{55 //返回documentBuilderFactory
56 dbf =DocumentBuilderFactory.newInstance();57
58 db =dbf.newDocumentBuilder();59 String basePath = new Xmlparse4FormVerify().getClass().getClassLoader().getResource("/").getPath();60 File f = new File(basePath + "formVerify.xml");61 //获取xml文件的dom对象
62 dt =db.parse(f);63 } catch(ParserConfigurationException e) {64 e.printStackTrace();65 System.out.println("class:Xmlparse4FormVerify>>documentBuilder对象初始化失败。");66 } catch (SAXException |IOException e) {67 //TODO Auto-generated catch block
68 e.printStackTrace();69 System.out.println("class:Xmlparse4FormVerify>>解析formVerify.xml文档失败");70 }71 }72
73 /**
74 * 根据表单ID和表单参数进行校验75 *@paramformId 表单ID76 *@parampMap 表单参数77 *@returnflag-0:成功,1:失败; msg-消息78 *@throwsException79 */
80 public static String verifyForm(String formId,Map pMap)throwsException{81 if(dt==null){82 throw new Exception("未能正确初始化。");83 }84 String errorInfo = null;85 //开始解析xml文档86 //获取xml文件跟节点
87 Element element =dt.getDocumentElement();88 //获取根节点下的子节点列表
89 NodeList cList =element.getChildNodes();90 List forms = new ArrayList<>();91 for (int i = 0; i < cList.getLength(); i++) {92 Node tNode =cList.item(i);93 if(tNode.getNodeName().equals("form")){94 forms.add(tNode);95 }96 }97 for (int i = 0; i < forms.size(); i++) {98 Node node1 =forms.get(i);99 String nodeId = node1.getAttributes().getNamedItem("id").getNodeValue();100 //System.out.println("节点id:" + nodeId+","+"节点name"+node1.getNodeName());
101 if(nodeId.equals(formId)){102 NodeList cList2 =node1.getChildNodes();103 //开始校验
104 Set keys =pMap.keySet();105 Iterator iter =keys.iterator();106 while(iter.hasNext()) {107 String key =iter.next();108 for (int j = 0; j < cList2.getLength(); j++) {109 Node node =cList2.item(j);110 //System.out.println("clist2,node:"+node.getNodeName());
111 if(node.getNodeName().equals(key)){112 String value =pMap.get(key);113 String preText = "";114 String rule = "";115 NodeList cList3 =node.getChildNodes();116
117 for(int m = 0;m < cList3.getLength(); m++){118 if(cList3.item(m).getNodeName()=="text"){119 preText =cList3.item(m).getTextContent();120 }else if(cList3.item(m).getNodeName()=="rule"){121 rule =cList3.item(m).getTextContent();122 }123 }124 //TODO 校验
125 errorInfo = newVerifyRegularUtil().verify(value, preText, rule);126 if(errorInfo!=null){127 returnerrorInfo;128 }129 }130 }131
132 }133 break;134 }135 }136 returnerrorInfo;137 }138 }