?? jactionexceptionparser.java
字號:
package jaction.xml;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import jaction.utility.*;
/**
*
* <p>Title: JactionExceptionParser.java</p>
* <p>Description: Jaction Exception xml 解析類</p>
* <p>Copyright: JAction Group 2003</p>
* @author DingWei
* @version 1.1
*/
public class JactionExceptionParser extends XMLParser{
/**
* 異常配置文件
*/
public static MessageResources resource = MessageResources.getMessageResources("jaction.JactionConfig");
public static final String EXCEPTION_FILE = resource.getMessage("jaction.exception");
/**
* 異常集合標示
*/
public static final String JACTION_EXCEPTION="jactionexception";
/**
* 異常集合
*/
public static final String EXCEPTION_LIST="exception-list";
/**
* 異常集合
*/
public static final String EXCEPTION="exception";
/**
* 異常類型
*/
public static final String EXCEPTION_TYPE="exception-type";
/**
* 默認異常標示
*/
public static final String JACTION_DEFAULT="default";
/**
* 默認異常
*/
public static final String DEFAULT_EXCEPTION="exception";
/**
* 默認異常的類型名稱
*/
public static final String DEFAULT_EXCEPTION_TYPE_NAME="type-name";
/**
* 屬性的code
*/
public static final String JACTION_CODE="code";
/**
* 屬性的類型
*/
public static final String JACTION_TYPE="type";
/**
* 屬性的名稱
*/
public static final String JACTION_NAME="name";
/**
* 異常信息標示
*/
public static final String EXCEPTION_MESSAGE="message";
/**
* 異常信息說明
*/
public static final String EXCEPTION_DECLARATION="declaration";
static Document document=null;
static File file=new File(EXCEPTION_FILE);
public static String message="";
public static String declaration="";
public static String typeName="";
public static String typeValue="";
/**
* 返回異常信息
* @return
*/
public static String getMessage(){
return message;
}
/**
* 返回異常信息的說明
* @return
*/
public static String getDeclaration(){
return declaration;
}
/**
* 返回異常信息的類型名稱
* @return
*/
public static String getTypeValue(){
return typeValue;
}
/**
* 返回異常信息的類型名稱
* @return
*/
public static String getTypeName(){
return typeName;
}
/**
* 讀取xml形成dom,并得到靜態的根節點樹
* @return 靜態的根節點樹
* @throws Exception
*/
private static NodeList getMainNodeList() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(file);
Element element = document.getDocumentElement();
NodeList nodelist = element.getChildNodes();
return nodelist;
}
/**
* 得到異常集合
* @return 得到異常列表的節點
* @throws Exception
*/
private static Node getExceptionListNode() throws Exception{
Node isNode=null;
NodeList nodeList=getMainNodeList();
for(int i=0;i<nodeList.getLength();i++){
//得到jactionexception下的子節點
isNode = nodeList.item(i);
//判斷是否exception-list
if (isNode.getNodeName().equals(EXCEPTION_LIST)) {
break;
}
else {
continue;
}
}
return isNode;
}
/**
* 初始化
*/
public void init(String code,String type) throws Exception{
NodeList nodeList = getExceptionListNode().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasAttributes()) {
NamedNodeMap nodeMap = node.getAttributes();
Node codeNode = nodeMap.getNamedItem(JACTION_CODE);
Node typeNode = nodeMap.getNamedItem(JACTION_TYPE);
//判斷是那個異常信息
if (codeNode.getNodeValue().equals(code) && typeNode.getNodeValue().equals(type)) {
//得到該節點的子節點的信息
NodeList childNodeList = node.getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
Node childNode = childNodeList.item(j);
//得到異常信息
if (childNode.getNodeName().equals(EXCEPTION_MESSAGE)) {
if(childNode.hasChildNodes()){
message = childNode.getFirstChild().getNodeValue();
}else{
message="";
}
}
//得到信息說明
if (childNode.getNodeName().equals(EXCEPTION_DECLARATION)) {
if(childNode.hasChildNodes()){
declaration = childNode.getFirstChild().getNodeValue();
}else{
declaration="";
}
}
}
//得到信息類型名稱
typeName = getExceptionTypeOfName(type);
break;
}
else {
//得到異常信息
message = getDefaultExceptionMessage();
//得到信息說明
declaration = getDefaultExceptionDeclaration();
//得到信息類型名稱
typeName = getDefaultExceptionTypeName();
}
}
}
}
/**
* 進行初始化,對dom進行解析,并對message,declaration,typename這三個成員變量進行付值
* @param code 異常信息的標識
* @throws Exception
*/
public void init(String code) throws Exception{
NodeList nodeList = getExceptionListNode().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasAttributes()) {
NamedNodeMap nodeMap = node.getAttributes();
Node codeNode = nodeMap.getNamedItem(JACTION_CODE);
Node typeNode = nodeMap.getNamedItem(JACTION_TYPE);
//判斷是那個異常信息
if (codeNode.getNodeValue().equals(code)) {
//得到該節點的子節點的信息
NodeList childNodeList = node.getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
Node childNode = childNodeList.item(j);
//得到異常信息
if (childNode.getNodeName().equals(EXCEPTION_MESSAGE)) {
if(childNode.hasChildNodes()){
message = childNode.getFirstChild().getNodeValue();
}else{
message="";
}
}
//得到信息說明
if (childNode.getNodeName().equals(EXCEPTION_DECLARATION)) {
if(childNode.hasChildNodes()){
declaration = childNode.getFirstChild().getNodeValue();
}else{
declaration="";
}
}
}
//得到改code的類型
typeValue=typeNode.getNodeValue();
//得到信息類型名稱
typeName = getExceptionTypeOfName(typeValue);
break;
}
else {
//得到異常信息
message = getDefaultExceptionMessage();
//得到信息說明
declaration = getDefaultExceptionDeclaration();
//得到信息類型名稱
typeName = getDefaultExceptionTypeName();
}
}
}
}
/**
* 得到異常類型集合
* @return 異常類型的節點
* @throws Exception
*/
public static Node getExceptionTypeNode() throws Exception{
Node isNode=null;
NodeList nodeList=getMainNodeList();
for(int i=0;i<nodeList.getLength();i++){
//得到jactionexception下的子節點
isNode = nodeList.item(i);
//判斷是否exception-type
if (isNode.getNodeName().equals(EXCEPTION_TYPE)) {
break;
}
else {
continue;
}
}
return isNode;
}
/**
* 得到異常類型的名稱
* @param typeCode 異常類型的標識
* @return 異常類型的中文名稱
* @throws Exception
*/
public static String getExceptionTypeOfName(String typeCode) throws Exception{
Node isNode=null;
String exceptionTypeName=null;
NodeList nodeList=getExceptionTypeNode().getChildNodes();
for(int i=0;i<nodeList.getLength();i++){
isNode=nodeList.item(i);
if(isNode.hasAttributes()){
NamedNodeMap nodeMap=isNode.getAttributes();
Node knode = nodeMap.getNamedItem(JACTION_CODE);
if (knode.getNodeValue().equals(typeCode)) {
knode = nodeMap.getNamedItem(JACTION_NAME);
exceptionTypeName = knode.getNodeValue();
}
}
}
return exceptionTypeName;
}
/**
* 得到默認異常標示節點
* @return 默認異常的節點
* @throws Exception
*/
private static Node getDefaultNode() throws Exception{
Node isNode=null;
NodeList nodeList=getMainNodeList();
for(int i=0;i<nodeList.getLength();i++){
//得到jactionexception下的子節點
isNode = nodeList.item(i);
//判斷是否default
if (isNode.getNodeName().equals(JACTION_DEFAULT)) {
break;
}
else {
continue;
}
}
return isNode;
}
/**
* 得到默認異常集合
* @return 默認異常信息的節點
* @throws Exception
*/
private static Node getDefaultExceptionNode() throws Exception{
Node isNode=null;
NodeList nodeList=getDefaultNode().getChildNodes();
for(int i=0;i<nodeList.getLength();i++){
isNode=nodeList.item(i);
if(isNode.getNodeName().equals(DEFAULT_EXCEPTION)){
break;
}else{
continue;
}
}
return isNode;
}
/**
* 得到默認的異常信息
* @return 默認的異常信息
* @throws Exception
*/
public static String getDefaultExceptionMessage() throws Exception{
Node isNode=null;
String defaultMessage=null;
NodeList nodeList=getDefaultExceptionNode().getChildNodes();
for(int i=0;i<nodeList.getLength();i++){
isNode=nodeList.item(i);
if(isNode.getNodeName().equals(EXCEPTION_MESSAGE)){
if(nodeList.item(i).hasChildNodes()){
defaultMessage=nodeList.item(i).getFirstChild().getNodeValue();
}else{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -