-- 作者:jlive
-- 发布时间:4/7/2004 3:26:00 PM
-- jdom samples (5)
用例: a) build_url document: <?xml version="1.0" encoding="gb2312" ?> <!DOCTYPE url-config SYSTEM "url-config.dtd"> <url-config> <row_result url=”test.go”> <moduleID>001</moduleID> <action>add</action> </row_result> <row_result url=”test2.do”> <moduleID>002</moduleID> <action>add</action> </row_result> ………………………………………….. </url-config> b) build_right document <?xml version="1.0" encoding="gb2312" ?> <!DOCTYPE right-config SYSTEM "right-config.dtd"> <right-config> <!--userid, funcid, func_view, func_add, func_delete, func_modify, func_query, func_print, func_conf, func_unconf -- > <row_result userID="javatest" moduleID="F001"> <view>Y</view> <add>Y</add> <delete>Y</delete> <modify>Y</modify> <query>Y</query> <print>Y</print> <conf>Y</conf> <unconf>Y</unconf> <other>N</other> </row_result> <row_result userID="javatest" moduleID="F002"> <view>Y</view> <add>N</add> <delete>N</delete> <modify>N</modify> <query>Y</query> <print>Y</print> <conf>N</conf> <unconf>N</unconf> <other>N</other> </row_result> ……………………………………………… </right-config> 生产工厂: package com.sz.lgyw.right; import org.jdom.*; public class RightFactory { private static RightFactory factory = null; private UserRightDocuments userRightDocuments; private Document urlDocument; private Document rightDocument; private RightFactory() { userRightDocuments = new UserRightDocuments(); urlDocument = userRightDocuments.getUrlDocument(); rightDocument = userRightDocuments.getRightDocument(); } public static RightFactory getInstance() { if (factory == null) { factory = new RightFactory(); } return factory; } public Document getURLDocument(){ return this.urlDocument; } public Document getRightDocument(){ return this.rightDocument; } } 访问: package com.sz.lgyw.right; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletRequest; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jaxen.XPath; import org.jaxen.XPathSyntaxException; import org.jaxen.JaxenException; import org.jaxen.jdom.JDOMXPath; import org.jaxen.SimpleVariableContext; import java.util.HashMap; import java.util.List; import java.util.Iterator; import java.util.Vector; import com.sz.lgyw.util.Log; import java.util.*; public class VisitRightDocument { private RightFactory f; private Document doc; private Element element_right, element_url; private XPath xpath_url, xpath_right; private String url_xpath = "/config/row[@URL=$var_url]"; private String right_xpath = "/config/row[@userId=$var_userid][@moduleId=$var_moduleid]"; private String[] ma = null; public VisitRightDocument() { try { xpath_url = new JDOMXPath(url_xpath); xpath_right = new JDOMXPath(right_xpath); } catch (JaxenException je) { System.out.println( "getModuleIDAndAction(HttpServletRequest request) err:" + je.getMessage()); } } public boolean isCanAction(HttpServletRequest request) { return true; } public boolean isCanAction() { String userid = "00001"; // for use debug... String privilege = "N"; boolean bl = false; // ma = getModuleIDAndAction(request); ma = getModuleIDAndAction(); if (ma.length == 2) { try { doc = f.getInstance().getRightDocument(); SimpleVariableContext varContext = new SimpleVariableContext(); /* varContext.setVariableValue("var_userid", request.getSession(false). getAttribute("userid")); */ varContext.setVariableValue("var_userid", userid); varContext.setVariableValue("var_moduleid", ma[0]); xpath_right.setVariableContext(varContext); element_right = (Element) xpath_right.selectSingleNode(doc); privilege = element_right.getChild(ma[1]).getTextTrim(); if (privilege.equalsIgnoreCase("Y")) { bl = true; } } catch (JaxenException je) { System.out.println( "getModuleIDAndAction(HttpServletRequest request) err:" + je.getMessage()); } } else { System.out.println("can't get moduleid and action "); } return bl; } // private String[] getModuleIDAndAction(HttpServletRequest request) { private String[] getModuleIDAndAction() { String url = null; String ma1[] = { "", ""}; try { //url = request.getRequestURI(); url = "special/index.jsp"; //for using test!!! doc = f.getInstance().getURLDocument(); SimpleVariableContext varContext = new SimpleVariableContext(); varContext.setVariableValue("var_url", url); xpath_url.setVariableContext(varContext); element_url = (Element) xpath_url.selectSingleNode(doc); if (element_url != null) { /* Log.log("element_url", element_url.getName()); Log.log("element_url has child ? **** ", Boolean.valueOf(element_url.hasChildren()).toString()); */ if (element_url.getChild("moduleId") != null) ma1[0] = element_url.getChild("moduleId").getTextTrim(); if (element_url.getChild("action") != null) ma1[1] = ( (Element) element_url.getChild("action")).getTextTrim(); /* for (Iterator i = element_url.getChildren().iterator(); i.hasNext(); ) { Element e = (Element) i.next(); if(e == element_url.getChild("moduleId")){ Log.log(e.getName(), e.getText(),element_url.getChild("moduleId").getName()); } } System.out.println(element_url.getChild("moduleId").getTextTrim()); ma1[0] = element_url.getChild("moduleId").getTextTrim(); */ Log.log("getModuleIDAndAction", "ma1[0]:" + ma1[0], "ma1[1]:" + ma1[1]); } } catch (JaxenException je) { System.out.println( "getModuleIDAndAction(HttpServletRequest request) err:" + je.getMessage()); return null; } return ma1; } public String getModuleId() { return ma[0]; } /* get action key and value list by request url */ public HashMap getActionMap(HttpServletRequest request) { return null; } public HashMap getActionMap() { String userid = "00001"; // for use debug... String url = null; String moduleId = null; String privilege = null; moduleId = ma[0]; Element action = null; HashMap actionHash = new HashMap(); try { doc = f.getInstance().getRightDocument(); SimpleVariableContext varContext = new SimpleVariableContext(); /* varContext.setVariableValue("var_userid", request.getSession(). getAttribute("userid")); */ varContext.setVariableValue("var_userid", userid); varContext.setVariableValue("var_moduleid", ma[0]); xpath_right.setVariableContext(varContext); element_right = (Element) xpath_right.selectSingleNode(doc); List list = element_right.getChildren(); for (Iterator actions = list.iterator(); actions.hasNext(); ) { action = (Element) actions.next(); // ex: add--Y actionHash.put(action.getName(), action.getTextTrim()); Log.log("getActionMap", action.getName(), action.getTextTrim()); } } catch (JaxenException je) { System.out.println( "getModuleIDAndAction(HttpServletRequest request) err:" + je.getMessage()); return null; } return actionHash; } /* get reflict url key and value by moduleid */ public HashMap getURLsByModuleId(HttpServletRequest request) { HashMap test = new HashMap(); System.out.println("---------------------"); return test; } public HashMap getURLsByModuleId() { HashMap urlHash = new HashMap(); List nodesList = null; //String xpath_modules = "/config/row/moduleId[.=$var_module]/following-sibling::action"; String xpath_modules = "/config/row[moduleId=$var_module]"; // "/config/row[@URL=$var_url]"; Element element = null; try { doc = f.getInstance().getURLDocument(); JDOMXPath xpath = new JDOMXPath(xpath_modules); SimpleVariableContext varContext = new SimpleVariableContext(); //varContext.setVariableValue("var_module", request.getAttribute("moduleId")); varContext.setVariableValue("var_module", ma[0]); xpath.setVariableContext(varContext); nodesList = xpath.selectNodes(doc); // while (urls.hasNext()) { for(Iterator urls = nodesList.iterator();urls.hasNext();){ element = (Element) urls.next(); // EX:add -- add.jsp //<row URL="***"> // <moduleId>...</moduleId> // <actin>...</action> //</row> // put(action.getTextTrim(),row.getAttribute("URL")); if(!urlHash.containsKey(element.getChild("action").getTextTrim())){ urlHash.put(element.getChild("action").getTextTrim(), element.getAttribute("URL").getValue()); Log.log("getURLsByModuleId", element.getChild("action").getTextTrim(), element.getAttribute("URL").getValue()); } } } catch (JaxenException je) { System.out.println( "getModuleIDAndAction(HttpServletRequest request) err:" + je.getMessage()); return null; } return urlHash; } public static void main(String args[]) { VisitRightDocument vd = new VisitRightDocument(); //vd.getModuleIDAndAction(); //vd.getActionMap(request); System.out.println("*****isCanAction()****** : " + vd.isCanAction()); vd.getActionMap(); System.out.println(" ************************************ "); vd.getURLsByModuleId(); } }     
|