⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actionparser.java

📁 webwork study w ebwork study
💻 JAVA
字号:
package jaction.xml;

import java.io.*;		//File
import java.util.*;		//HashMap

import javax.xml.parsers.*;		//DocumentBuilderFactory DocumentBuilder

import org.w3c.dom.*;		//Document Node Element NodeList NamedNodeMap
import org.w3c.dom.traversal.*;		//NodeIterator NodeFilter DocumentTraversal

import jaction.utility.*;
import jaction.workspace.JactionConfigResource;



/**
 *
 * 注册事件(action xml) 解析类
 * @author yanger
 * @version 1.0
 * @since 1.1 $weigang 2003-4-22
 */
public class ActionParser extends XMLParser{
	
	/**
	 * xml file path
	 */
	public static final String JACTION_CONFIG=JactionConfigResource.getMessage("jaction.action");
    
	/**
     * xml文件的Document
     */
    public static Document document=null;
	//////////////////////////////////////////////////////////////
	//	action node name
	//////////////////////////////////////////////////////////////
	/**
	 * 动作集合标志
	 */
	public static final String ACTION_MAPPING="action-mapping";
	/**
	 * 动作名称
	 */
	public static final String ACTION_FCODE="name";
	/**
	 *业务类标志
	 */
	public static final String ACTION_BEAN="bean";
	/**
	 * 动作标识
	 */
	public static final String ACTION_ID="id";
	/**
	 * 跳转标志
	 */
	public static final String ACTION_FORWARD="forward";
	/**
	 * 跳转方法名称
	 */
	public static final String ACTION_METHOD="method";
	/**
	 * 输入页面
	 */
	public static final String ACTION_INPUT = "input";
	/**
	 * 是否校验
	 */
	public static final String ACTION_VALIDATE = "validate";
	/**
	 * 跳转注释
	 */
	public static final String ACTION_RMK = "rmk";
	/**
	 * 跳转标识
	 */
	public static final String FORWARD_ID="id";
	/**
	 * 跳转页面
	 */
	public static final String FORWARD_PAGE="page";
	//////////////////////////////////////////////////////////////
	//	page node 
	//////////////////////////////////////////////////////////////
	/**
	 * 页面集合标志
	 */
	public static final String PAGE_MAPPING="page-mapping";
	/**
	 * 页面
	 */
	public static final String PAGE = "page";
	/**
	 * 页面标识
	 */
	public static final String PAGE_ID="id";
	/**
	 * 页面路径
	 */
	public static final String PAGE_PATH="path";
	/**
	 * 页面动作
	 */
	public static final String PAGE_ACTION="action";
	/**
	 * 页面注释
	 */
	public static final String PAGE_RMK="rmk";

	/**
	 * action 节点
	 */
	protected static Node action = null;
	/**
	 * page 节点
	 */
	protected static Node page = null;
	/**
	 * 初始化环境
	 */
	static{
		ActionParser.init();
		try{
			action = getNode(ACTION_MAPPING);
			page = getNode(PAGE_MAPPING);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
     * 构造函数
     */
    public ActionParser(){
  	}

	/**
     * 将提供的XML文件解析成DOM文档(初始化)
     * @throws Exception
     */
  	public static void init() {
    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	try {
           	DocumentBuilder builder = factory.newDocumentBuilder();
            File f = new File(JACTION_CONFIG);
           	document = builder.parse(f);
   		}catch (Exception e) {
			System.out.println("controler.init():"+e);
	   	}
  	}
	/**
     * 得到相对应的一个主节点的节点树
     * @return 一个主节点的节点树
     * @throws Exception
     */
   	private static NodeList getRootNodelist() throws Exception{
    	Element element = document.getDocumentElement();
        NodeList nodelist = element.getChildNodes();
        return nodelist;
  	}

	///---------------------------------------------------------------->>public method
	/**
	 * 根据actionName、forwardId得到对应的action的列表
	 * @param actionName 动作名称
	 * @param forwardId 逻辑标识
	 * @return HashMap 逻辑中可用的动作集合
	 * @exception Exception 
	 * @deprecated 1.4版本以后无需定义该动作集合
	 */
	public static HashMap getActionMap(String actionName, String forwardId) throws Exception{
		HashMap hashMap = new HashMap();
    	Node actionNode = getSubNode(action, ACTION_FCODE, actionName);
    	Node forwardNode = getSubNode(actionNode, FORWARD_ID, forwardId);
		String pageId = getAttributeOfNode(forwardNode, FORWARD_PAGE);
		
    	Node pageNode = getSubNode(page, PAGE_ID, pageId);
		NodeList nodeList = pageNode.getChildNodes();
		Node isNode = null;		//page-mapping/page/action node
		String key = null;		//key of hashMap
		String value = null;	//value of hashMap
    	for(int i=0;i<nodeList.getLength();i++){
			isNode = nodeList.item(i);
			if(isNode.hasAttributes()){
				key = getAttributeOfNode(isNode, ACTION_ID);
				value = getAttributeOfNode(getSubNode(action, ACTION_ID, key), ACTION_FCODE);
				FileUtil.log("key="+key+" value="+value);
				hashMap.put(key, value);
			}
    	}
    	return hashMap;
	}


	/**
	 * 根据actionName得到对应的Bean的路径
	 * @param actionName 动作名称
	 * @return String 事件相应处理bean名称
	 * @exception Exception
	 */
	public static String getBeanName(String actionName) throws Exception{
    	Node actionNode = getSubNode(action, ACTION_FCODE, actionName);
    	String beanName = getAttributeOfNode(actionNode, ACTION_BEAN);
    	return beanName;
	}
	

	/**
	 * 根据actionName、forwardId得到对应的jsp的路径
	 * @param actionName 动作名称
	 * @param forwordId 逻辑跳转标志
	 * @return String 页面的http访问URL
	 * @exception Exception
	 */
    public static String getPathOfPageNode(String actionName, String forwardId) throws Exception{
    	Node actionNode = getSubNode(action, ACTION_FCODE, actionName);
    	Node forwardNode = getSubNode(actionNode, FORWARD_ID, forwardId);
		
		String pageId = getAttributeOfNode(forwardNode, FORWARD_PAGE);
    	Node pageNode = getSubNode(page, PAGE_ID, pageId);
		
    	String pagePath = getAttributeOfNode(pageNode, PAGE_PATH);
    	return pagePath;
    }

	/**
	 * 根据actionName得到对应的method名称
	 * @param actionName 动作名称
	 * @return String 动作处理的方法名称
	 * @exception Exception
	 * 
	 */
	public static String getMethod(String actionName) throws Exception{
    	Node actionNode = getSubNode(action, ACTION_FCODE, actionName);
		
    	String methodName = getAttributeOfNode(actionNode, ACTION_METHOD);
    	return methodName;
	}

	/**
	 * 根据acitonName得到请求页面项
	 * @param actionName 动作名称
	 * @return String 输入项
	 * @exception Exception
	 * @deprecated 1.4以后去掉该属性
	 */
	public static String getInput(String actionName) throws Exception{
    	Node actionNode = getSubNode(action, ACTION_FCODE, actionName);
    	String input = getAttributeOfNode(actionNode, ACTION_INPUT);
    	return input;
	}
	/**
	 * 根据acitonName ,得到validate的属性,如果没有该属性,返回null
	 * @param actionName
	 * @return 得到validate的属性值,如果没有该属性,返回null
	 * @exception 异常
	 * @Since 1.4.3.0 $yanger 2003-5-2 8:28:07
	 */
	public static String getValidateValueOfActionAttr(String actionName) throws Exception{
    	Node actionNode = getSubNode(action, ACTION_FCODE, actionName);
		
    	String validate = getAttributeOfNode(actionNode, ACTION_VALIDATE);
    	return validate;
	}
	/**
	 * 查找pageNode
	 * @param pageid 
	 * @return pageNode
	 * @Since 1.4.3.0 $yanger 2003-5-2 8:30:39
	 */
	 public static Node findPageNode(String pageId) throws Exception{
		Node pageNode = getSubNode(page, PAGE_ID, pageId);
		
		return pageNode;
	 }
	 /**
	 * 得到page path
	 * @param pageid 
	 * @return pagepath
	 * @Since 1.4.3.0 $yanger 2003-5-2 8:28:20
	 */
	 public static String getPathValueOfPageAttr(String pageId) throws Exception{
		Node pageNode = findPageNode(pageId);
		
		String pagePath = getAttributeOfNode(pageNode, PAGE_PATH);
		
		return pagePath;
	 }



	/**
	 * 根据page id得到相应的page map
	 * @param pageId 页面标志
	 * @return HashMap 
	 * @deprecated 机制取消
	 */
	public static HashMap getPageMap(String pageId) throws Exception{
		HashMap hash = new HashMap();
		Node isNode = null;
		String isPageId = null;
		NodeList nodeList = page.getChildNodes();
		for(int i=0;i<nodeList.getLength();i++){
			isNode = nodeList.item(i);
			if(isNode.hasAttributes()){
				isPageId = getAttributeOfNode(isNode, PAGE_ID);
				if(pageId == null || pageId.equals("") || (isPageId.indexOf(pageId) != -1)){
					PageObject pageObject = new PageObject(isPageId);
					hash.put(isPageId, pageObject);
				}
			}
		}
		return hash;
	}

	/**
	 * 根据action name 得到相应的action map
	 * @param actionName
	 * @return HashMap
	 * @exception Exception
 	 * @deprecated 机制取消
	 */
	public static HashMap getActionMap(String actionFcode) throws Exception{
		HashMap hash = new HashMap();
		Node isNode = null;
		String isActionFcode = null;
		NodeList nodeList = action.getChildNodes();
		for(int i=0;i<nodeList.getLength();i++){
			isNode = nodeList.item(i);
			if(isNode.hasAttributes()){
				isActionFcode = getAttributeOfNode(isNode, ACTION_FCODE);
				if(actionFcode == null || actionFcode.equals("") || (isActionFcode.indexOf(actionFcode) != -1)){
					ActionObject actionObject = new ActionObject(isActionFcode);
					hash.put(isActionFcode, actionObject);
				}
			}
		}
		return hash;
	}


///////////////////////////////protected///////////////////////////////////////	
	/**
   	 * 通过id得到节点
   	 * @param nodeId node name
   	 * @return Node
	 */
	protected static Node getNode(String nodeName) throws Exception{
		Node isNode = null;
    	NodeList nodelist = getRootNodelist();
		//没有子节点
		if(nodelist.getLength()==0){
			return null;
		}
    	for(int i=0;i<nodelist.getLength();i++) {
      		isNode = nodelist.item(i);
      		if(isNode.getNodeName().equals(nodeName)){
            	break;
	        }
    	}
		return isNode;
	}

	/**
	 * 通过子节点id的值得到子节点
	 * @param parentNode 父节点
	 * @param attributeName 属性名称
	 * @param attributeValue 属性值
	 * @return Node 子节点
	 * @exception Exception
	 */
	protected static Node getSubNode(Node parentNode, String attributeName,
								String attributeValue ) throws Exception{
		Node isNode = null;
		NamedNodeMap nameNodeMap = null;
		NodeList nodeList = parentNode.getChildNodes();
		//没有子节点
		if(nodeList.getLength()==0){
			return null;
		}
		for(int i=0;i<nodeList.getLength();i++){
			isNode = nodeList.item(i);
			if(isNode.hasAttributes()){
				nameNodeMap = isNode.getAttributes();
				if(nameNodeMap.getNamedItem(attributeName).getNodeValue().equals(attributeValue)){
					break;
				}
			}
		}
		return isNode;
	}

    /**
     * 在节点中根据属性的名字得到属性相对应的值
 	 * @param Node 节点
	 * @param attributeName 属性名称
	 * @return String 属性相对应的值
	 * @exception Exception
     */
	protected static String getAttributeOfNode(Node node, String attributeName) throws Exception{
		if(node==null)return null;
		NamedNodeMap namedNodeMap = node.getAttributes();
		if(namedNodeMap==null)return null;
        Node isNode = namedNodeMap.getNamedItem(attributeName);
        if(isNode == null){
			return null;
        }
       	return  isNode.getNodeValue();
    }

/////////////////////////////////////////////////////////////////////////////
	/**
	 * main method
	 */
	public static void main(String [] args) throws Exception{
		System.out.println(getPathOfPageNode("helloworld", "hw"));
		System.out.println(getBeanName("helloworld"));
		getActionMap("helloworld", "hw");
	}

     /**
	  * 生成String
	  * @param root dom
	  */
     public static void  outputString(Document root){
        NodeIterator iterator = ((DocumentTraversal)root).createNodeIterator(root, NodeFilter.SHOW_ELEMENT, null, true);
        System.out.println("Root = " + iterator.getRoot());
        Node node = iterator.nextNode();
        while(node != null) {
          System.out.println(node);
          node = iterator.nextNode();
        }

        iterator = ((DocumentTraversal)root).createNodeIterator(
        root.getFirstChild().getFirstChild().getNextSibling(),
        NodeFilter.SHOW_ELEMENT, null, true);
        System.out.println("\nRoot = " + iterator.getRoot());
        node = iterator.nextNode();
        while(node != null) {
          System.out.println(node);
          node = iterator.nextNode();
        }
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -