?? gdparsexml.java
字號:
package com.gd.mvc.util;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class GdParseXml {
private Document document;//獲取指定Xml的document
private String path;//獲取從頁面傳來的訪問路徑
private String action;//獲取從頁面傳來的動作
private String forwardJsp;//獲取Xml設定的返回的頁面
private String forwardJspPath;//獲取Xml設定的返回頁面的path
private String methodForwardJsp;//獲取Xml中方法對應的forward
private String actionName; //獲取Xml中訪問路徑對應的actionName
private List voId = new ArrayList();//獲取Xml中設定的voId
private List voClass = new ArrayList();//獲取Xml中設定的voClass
private List voType = new ArrayList();//獲取Xml中設定的voType
private List voValidate = new ArrayList();//獲取Xml中設定的voValidate
private List voTable = new ArrayList();//獲取Xml中設定的voTable
private String method;//獲取Xml中對應的方法
private String prefix;//獲取Xml中設定的頁面的前綴
private String suffix;//獲取Xml中設定的頁面后綴
private String extendsAction;//獲取Xml中設定的是否繼承GdAction
//從文件讀取XML,輸入文件名,返回XML文檔
public void read(String fileName) throws MalformedURLException, DocumentException {
SAXReader reader = new SAXReader();
document = reader.read(new File(fileName));
}
//獲取根節點
public Element getRootElement(Document doc){
return doc.getRootElement();
}
/**
* 用來解析此架構的xml檔,產生對應的參數,即可在bean中設定VO,也可以在Page中設定VO
* @return void
*/
public void parseXml() {
List listPathAttribute = document.selectNodes("/nancy-config/page/@path" ); //獲取所有page的path屬性
List listPageElement = document.selectNodes("/nancy-config/page" );//獲取所有的page
Iterator iteratorListPathAttribute = listPathAttribute.iterator();
Iterator iteratorPageElement = listPageElement.iterator();
int m = 0;//存放VO的key
int i = 0;//用于VO的key值遞增
//假如Xml中有設定page和page的path屬性
while(iteratorListPathAttribute.hasNext() && iteratorPageElement.hasNext()) {
Attribute attribute = (Attribute)iteratorListPathAttribute.next();//獲取第一個path屬性
Element element = (Element)iteratorPageElement.next();//獲取第一個page下面的元素
Iterator iteratorForward = element.elementIterator("forward");//獲取第一個page下面的forward元素
Iterator iteratorValueObject = element.elementIterator("valueObject");//獲取第一個page下面的valueObject元素
Iterator iteratorAction = element.elementIterator("action");//獲取第一個page下面的action元素
if(attribute.getValue().trim().equals(path)) {//如果獲得的path和xml配置的一樣
actionName = element.attributeValue("actionName");//獲取該頁面即path對應的actionName
while(iteratorForward.hasNext()) {//就取得該path下所有要返回的頁面
Element elementForward = (Element)iteratorForward.next();
forwardJsp = elementForward.attributeValue("name");//獲取forwardJsp的名稱
forwardJspPath = elementForward.attributeValue("path");//獲取forwardJsp的的路徑
}
while(iteratorValueObject.hasNext()) {//獲得valueObject的屬性
m = i++;//用于VO的key值遞增
Element elementValueObject = (Element)iteratorValueObject.next();
voId.add(m, elementValueObject.attributeValue("id"));//獲取VO的id
voClass.add(m, elementValueObject.attributeValue("class"));//獲取VO的class
voType.add(m, elementValueObject.attributeValue("type"));//獲取該VO是單筆還是多筆
voValidate.add(m, elementValueObject.attributeValue("validate"));//獲取該VO是否驗證
voTable.add(m, elementValueObject.attributeValue("table"));//獲取VO對應的table
}
while(iteratorAction.hasNext()) {
Element elementAction = (Element)iteratorAction.next();
String actionFlag = elementAction.attributeValue("action").trim();
if (actionFlag.equals(action)) {//如果action與頁面上的相同
method = elementAction.attributeValue("method");//就獲取該action相對應的method
methodForwardJsp = elementAction.attributeValue("forward");//就獲取該action相對應的forward
break;
}
}
break;
}
}
List listBeanAttribute = document.selectNodes("/nancy-config/bean/@id" );//獲取所有bean的id屬性
List listBeanElement = document.selectNodes("/nancy-config/bean" );//獲取所有的bean
Iterator iteratorBeanPathAttribute = listBeanAttribute.iterator();
Iterator iteratorBeanElement = listBeanElement.iterator();
//假如Xml中有設定bean和bean的id屬性
while(iteratorBeanPathAttribute.hasNext() && iteratorBeanElement.hasNext()) {
Attribute attribute = (Attribute)iteratorBeanPathAttribute.next();
Element element = (Element)iteratorBeanElement.next();
if(attribute.getValue().equals("viewResolver")) {//假如有id為viewResolver的bean
Iterator iteratorPrefix = element.elementIterator("prefix");//獲取id為viewResolver的bean下的prefix元素
Iterator iteratorSuffix = element.elementIterator("suffix");//獲取id為viewResolver的bean下的suffix元素
Element elementPrefix = (Element)iteratorPrefix.next();
Element elementSuffix = (Element)iteratorSuffix.next();
prefix = elementPrefix.getText();//獲得viewResolver指定的前綴prefix
suffix = elementSuffix.getText();//獲得viewResolver指定的后綴suffix
}
if(attribute.getValue().equals("actionResolver")) {//假如有id為actionResolver的bean
Iterator iteratorExtends = element.elementIterator("extends");
Element elementExtends = (Element)iteratorExtends.next();
extendsAction = elementExtends.getText();//獲得actionResolver指定的繼承GdAction
}
if(attribute.getValue().equals("valueObjects")) {//假如有id為valueObjects的bean
Iterator iteratorValueObject = element.elementIterator("valueObject");
while(iteratorValueObject.hasNext()) {//獲得valueObject的屬性
m = i++;
Element elementValueObject = (Element)iteratorValueObject.next();
voId.add(m, elementValueObject.attributeValue("id"));//獲取VO的id
voClass.add(m, elementValueObject.attributeValue("class"));//獲取VO的class
voType.add(m, elementValueObject.attributeValue("type"));//獲取該VO是單筆還是多筆
voValidate.add(m, elementValueObject.attributeValue("validate"));//獲取該VO是否驗證
voTable.add(m, elementValueObject.attributeValue("table"));//獲取VO對應的table
}
}
}
}
/**獲取action
* @return the action
*/
public String getAction() {
return action;
}
/**設定action
* @param action the action to set
*/
public void setAction(String action) {
this.action = action;
}
/**獲取actionName
* @return the actionName
*/
public String getActionName() {
return actionName;
}
/**設定actionName
* @param actionName the actionName to set
*/
public void setActionName(String actionName) {
this.actionName = actionName;
}
/**獲取document
* @return the document
*/
public Document getDocument() {
return document;
}
/**設定document
* @param document the document to set
*/
public void setDocument(Document document) {
this.document = document;
}
/**獲取forwardJsp
* @return the forwardJsp
*/
public String getForwardJsp() {
return forwardJsp;
}
/**設定forwardJsp
* @param forwardJsp the forwardJsp to set
*/
public void setForwardJsp(String forwardJsp) {
this.forwardJsp = forwardJsp;
}
/**獲取forwardJspPath
* @return the forwardJspPath
*/
public String getForwardJspPath() {
return forwardJspPath;
}
/**設定forwardJspPath
* @param forwardJspPath the forwardJspPath to set
*/
public void setForwardJspPath(String forwardJspPath) {
this.forwardJspPath = forwardJspPath;
}
/**獲取method
* @return the method
*/
public String getMethod() {
return method;
}
/**設定method
* @param method the method to set
*/
public void setMethod(String method) {
this.method = method;
}
/**獲取methodForwardJsp
* @return the methodForwardJsp
*/
public String getMethodForwardJsp() {
return methodForwardJsp;
}
/**設定methodForwardJsp
* @param methodForwardJsp the methodForwardJsp to set
*/
public void setMethodForwardJsp(String methodForwardJsp) {
this.methodForwardJsp = methodForwardJsp;
}
/**獲取path
* @return the path
*/
public String getPath() {
return path;
}
/**設定path
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
/**獲取prefix
* @return the prefix
*/
public String getPrefix() {
return prefix;
}
/**設定prefix
* @param prefix the prefix to set
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
/**獲取suffix
* @return the suffix
*/
public String getSuffix() {
return suffix;
}
/**設定suffix
* @param suffix the suffix to set
*/
public void setSuffix(String suffix) {
this.suffix = suffix;
}
/**獲取voClass
* @return the voClass
*/
public List getVoClass() {
return voClass;
}
/**設定voClass
* @param voClass the voClass to set
*/
public void setVoClass(List voClass) {
this.voClass = voClass;
}
/**獲取voId
* @return the voId
*/
public List getVoId() {
return voId;
}
/**設定voId
* @param voId the voId to set
*/
public void setVoId(List voId) {
this.voId = voId;
}
/**獲取voType
* @return the voType
*/
public List getVoType() {
return voType;
}
/**設定voType
* @param voType the voType to set
*/
public void setVoType(List voType) {
this.voType = voType;
}
/**獲取extendsAction
* @return the extendsAction
*/
public String getExtendsAction() {
return extendsAction;
}
/**設定extendsAction
* @param extendsAction the extendsAction to set
*/
public void setExtendsAction(String extendsAction) {
this.extendsAction = extendsAction;
}
/**獲取voValidate
* @return the voValidate
*/
public List getVoValidate() {
return voValidate;
}
/**設定voValidate
* @param voValidate the voValidate to set
*/
public void setVoValidate(List voValidate) {
this.voValidate = voValidate;
}
/**獲取voTable
* @return the voTable
*/
public List getVoTable() {
return voTable;
}
/**設定voTable
* @param voTable the voTable to set
*/
public void setVoTable(List voTable) {
this.voTable = voTable;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -