以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 XML源码及示例(仅原创和转载) 』  (http://bbs.xml.org.cn/list.asp?boardid=32)
----  jdom samples (6)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=32&rootid=&id=6537)


--  作者:jlive
--  发布时间:4/7/2004 3:30:00 PM

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

  }

}


--  作者:struggle
--  发布时间:6/15/2004 8:55:00 AM

--  
谢谢楼主!!!
学习这几个例子先!等待楼主更多好例子!

W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms