?? actionparser.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 + -