📄 jactionexceptionparser.java.bak
字号:
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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -