新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> XML网站展示,XML源代码,XML编程示例。 本版仅接受原创、转贴、网站展示,具体的技术交流请前往各相关版块。
    [返回] 计算机科学论坛XML.ORG.CN讨论区 - XML技术『 XML源码及示例(仅原创和转载) 』 → jdom samples (6) 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3086 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: jdom samples (6) 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     jlive 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:6
      积分:85
      注册:2004/2/17

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jlive发送一个短消息 把jlive加入好友 查看jlive的个人资料 搜索jlive在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jlive的博客楼主
    发贴心情 jdom samples (6)

    接上:

    package com.sz.lgyw.right;

    import java.io.*;
    import java.sql.*;

    import javax.naming.*;
    import java.util.*;

    import org.jdom.*;
    import org.jdom.output.*;
    import org.jdom.contrib.input.ResultSetBuilder;
    import org.jdom.JDOMException;

    import com.sz.lgyw.ejb.util.ContextFactory;
    import com.sz.lgyw.util.DestroyDbResource;
    import com.sz.lgyw.util.DataSourceConn;

    public class UserRightDocuments {
      private ResultSet rs;
      private PreparedStatement psmt;
      private Connection conn;

      private boolean debug = true;
      private String rootName;
      private String rowName;
      private String namespace, url;
      private DataSourceConn ds;

      private String select_sql_1 =
          "select url,modulename,righttype from t_check_right order by modulename";

      private String select_sql_2 =
          "select userid, funcid, func_view, func_add, func_delete,"
          + "func_modify, func_query, func_print, func_conf,"
          + "func_unconf from t_user_right order by userid, funcid";

      public UserRightDocuments() {
        ds = new DataSourceConn();
        //conn = ds.getConnection();
        ContextFactory f = ContextFactory.getInstance();
        /*
         try {

          rootName = (String) f.getInitialContext().lookup("java:/env/rootName");
          rowName = (String) f.getInitialContext().lookup("java:/env/rowName");
          namespace = (String) f.getInitialContext().lookup("java:/env/namespace");
          url = (String) f.getInitialContext().lookup("java:/env/url");

        }
        catch (NamingException ne) {
          System.err.println("namingException  Err: " + ne.getMessage());
        }
       */
        rootName= "config";
        rowName="row";
      }

      public Document getUrlDocument() {
        ResultSetBuilder builder_url = null;
        Document doc_url = null;
        psmt = ds.getPrepareStatement(select_sql_1);
        try {
          rs = psmt.executeQuery();
          // Create a ResultSetBuilder
          builder_url = new ResultSetBuilder(rs);
        }
        catch (SQLException sqlE) {
          sqlE.printStackTrace();
        }
        if (rootName != null) { //url-config
          builder_url.setRootName(rootName);
        }

        if (rowName != null) { //row-result
          builder_url.setRowName(rowName);
        }

        if (namespace != null && url != null) {
          builder_url.setNamespace(Namespace.getNamespace(namespace, url));
        }

        builder_url.setAsAttribute("url");
        builder_url.setAsElement("modulename", "moduleId");
        builder_url.setAsElement("righttype", "action");

        // builder_url.setAsAttribute(1,"url");
        //builder_url.setAsElement(2,"funcid");
        try {
          doc_url = builder_url.build();
        }
        catch (JDOMException jdomE) {
          jdomE.printStackTrace();
        }

        // Convert the result to XML (as String)
        XMLOutputter outputter = new XMLOutputter("  ", true);
       // ByteArrayOutputStream output = new ByteArrayOutputStream();
        //outputter.output(doc, output);
        DestroyDbResource.destroy(rs);
        DestroyDbResource.destroy(psmt);
        // return output.toString();
        try{
          outputter.output(doc_url, System.out);
        }catch(IOException ioe){
          System.out.println("getUrlDocument --> outputter.output(doc_url, System.out)**********************");
        }
        return doc_url;
      }

      public Document getRightDocument() {
        Document doc_right = null;
        try {
          psmt = ds.getPrepareStatement(select_sql_2);
          // Create a ResultSetBuilder
          rs = psmt.executeQuery();
        }
        catch (SQLException sqlE) {
          sqlE.printStackTrace();
        }
        ResultSetBuilder builder_right = new ResultSetBuilder(rs);
        if (rootName != null) {
          builder_right.setRootName(rootName);
        }

        if (rowName != null) {
          builder_right.setRowName(rowName);
        }

        if (namespace != null && url != null) {
          builder_right.setNamespace(Namespace.getNamespace(namespace, url));
        }

        // userid, funcid, func_view, func_add,
        // func_delete, func_modify,
        // func_query, func_print, func_conf, func_unconf

        builder_right.setAsAttribute("userid", "userId");
        builder_right.setAsAttribute("funcid", "moduleId");
        builder_right.setAsElement("func_view","func_view");
        builder_right.setAsElement("func_add","func_add");
        builder_right.setAsElement("func_delete", "func_delete");
        builder_right.setAsElement("func_modify", "func_modify");
        builder_right.setAsElement("func_query", "func_query");
        builder_right.setAsElement("func_print", "func_print");
        try {
          doc_right = builder_right.build();
        }
        catch (JDOMException jdomE) {
          jdomE.printStackTrace();
        }

        // Convert the result to XML (as String)
        XMLOutputter outputter = new XMLOutputter("  ", true);
        // ByteArrayOutputStream output = new ByteArrayOutputStream();
        try{
          outputter.output(doc_right, System.out);
        }catch(IOException ioe){
          System.out.println("getUrlDocument --> outputter.output(doc_url, System.out)**********************");
        }

        DestroyDbResource.destroy(rs);
        DestroyDbResource.destroy(psmt);

        // return output.toString();
        return doc_right;
      }

      public void log(String name, String mes) {
        if (debug)
          System.out.println(" * * * * " + name + "-- -" + mes + " * * * ");
      }

      public static void main(String[] args){
        UserRightDocuments ud  = new UserRightDocuments();
        ud.getUrlDocument();
        ud.getRightDocument();
      }
    }

    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();

      }

    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/4/7 15:30:00
     
     struggle 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:9
      积分:93
      门派:XML.ORG.CN
      注册:2004/5/27

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给struggle发送一个短消息 把struggle加入好友 查看struggle的个人资料 搜索struggle在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看struggle的博客2
    发贴心情 
    谢谢楼主!!!
    学习这几个例子先!等待楼主更多好例子!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/6/15 8:55:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XML源码及示例(仅原创和转载) 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/8/6 20:46:41

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    93.750ms