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

📄 parse.java

📁 对jsp页面中有多个查询条件时
💻 JAVA
字号:
package org.storm.xwguan.parse;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;
import org.dom4j.io.*;
import org.storm.xwguan.exception.BlurryBoundException;
import org.storm.xwguan.exception.BlurryContionException;
import org.storm.xwguan.exception.BoundIsFirstException;
import org.storm.xwguan.exception.ConditionsNameNullEmptyException;
import org.storm.xwguan.exception.NameNullEmptyException;
import org.storm.xwguan.exception.TableNameNullEmptyException;
import org.storm.xwguan.exception.TypeException;
import org.storm.xwguan.util.ClassUtils;
import org.storm.xwguan.util.Constants;

public class Parse {

	private InputStream in;
	private SAXReader reader;
	private Document doc;
	private Element root;
	private Element fields;
	private Element conditions;

	private String tableName;// 数据库表的名字
	private String pojoName; // 表对应的pojo名字
	private List<FieldsMapping> fieldsList; // 保存字段的描述信息
	private List<ConditionsMapping> conditionsList;// 保存额外查询条件的描述信息

	public Parse() {

	}

	public Parse(String xmlPath) throws Exception {
		in = ClassUtils.getResourceAsStream(xmlPath);
		init();
		parseXML();
	}

	private void init() throws Exception {
		reader = new SAXReader();
		doc = reader.read(in);

		root = doc.getRootElement();
		fields = root.element(Constants.FIELDS);
		conditions = root.element(Constants.CONDITIONS);

		fieldsList = new ArrayList<FieldsMapping>();
		conditionsList = new ArrayList<ConditionsMapping>();
	}

	private void initialTableName() throws TableNameNullEmptyException {
		tableName = root.elementText(Constants.TABLE_NAME);
		if (tableName == null || tableName.trim().equals(Constants.K15)) {
			// 如果配置文件中没有设置数据库表的名字,抛出异常
			throw new TableNameNullEmptyException();
		}
	}

	private void check(int type, boolean isBound, boolean isFirst,
			boolean isBlurry) throws BlurryContionException,
			BlurryBoundException, TypeException, BoundIsFirstException {
		switch (type) {
		case Constants.INTEGER:
		case Constants.FLOAT:
		case Constants.DOUBLE:
			if (isBlurry == true) {
				// 若模糊查询字段为int,float,double型,抛出异常
				throw new BlurryContionException();
			} else if (isBlurry == true && isBound == true) {
				// 不能同时为范围查询条件和模糊查询条件
				throw new BlurryBoundException();
			} else if (isBound == false && isFirst == true) {
				// 非范围查询时,isFirst不能为true
				throw new BoundIsFirstException();
			}
			break;
		case Constants.STRING:
			if (isBlurry == true && isBound == true) {
				throw new BlurryBoundException();
			} else if (isBound == false && isFirst == true) {
				throw new BoundIsFirstException();
			}
			break;

		default:
			// 字段的类型如果不为int,float,double,String,抛出异常
			throw new TypeException();
		}
	}

	/**
	 * 将配置文件中所有对字段描述信息解析出并保存到List<FieldsMapping>中。
	 * 
	 * @throws BlurryContionException
	 * @throws BlurryBoundException
	 * @throws NameNullEmptyException
	 * @throws TypeException
	 * @throws BoundIsFirstException
	 */
	private void initialFieldsInfo() throws NameNullEmptyException,
			TypeException, BlurryContionException, BlurryBoundException,
			BoundIsFirstException {
		for (Iterator<?> it = fields.elementIterator(Constants.FIELD); it
				.hasNext();) {

			Element foo = (Element) it.next();

			String fieldType1 = foo.elementText(Constants.TYPE);

			Integer fieldType = new Integer(-1);
			if (fieldType1 != null) {
				try {
					// 字段类型如果不为int型,抛出异常
					fieldType = Integer.parseInt(fieldType1);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			} else {
				// 如果没有type选项,则默认为String型
				fieldType = new Integer(4);
			}

			String isBound1 = foo.elementText(Constants.IS_BOUND);
			boolean isBound;
			if (isBound1 != null) {
				isBound = Boolean.parseBoolean(isBound1);
			} else {
				isBound = false;
			}

			String isFirst1 = foo.elementText(Constants.IS_FIRST);
			boolean isFirst;
			if (isFirst1 != null) {
				isFirst = Boolean.parseBoolean(isFirst1);
			} else {
				isFirst = false;
			}

			String isBlurry1 = foo.elementText(Constants.IS_BLURRY);
			boolean isBlurry;
			if (isBlurry1 != null) {
				isBlurry = Boolean.parseBoolean(isBlurry1);
			} else {
				isBlurry = false;
			}

			check(fieldType, isBound, isFirst, isBlurry);

			String formName = foo.elementText(Constants.FORM_NAME);
			if (formName == null || formName.trim().equals(Constants.K15)) {
				// 配置文件中没有对应form中的名字时,抛出异常
				throw new NameNullEmptyException();

			}
			String tableFieldName = foo.elementText(Constants.TABLE_FIELD_NAME);

			if (tableFieldName == null
					|| tableFieldName.trim().equals(Constants.K15)) {
				// 配置文件中没有该字段在数据库表中对应的名字时,抛出异常
				throw new NameNullEmptyException();

			}

			FieldsMapping fields = new FieldsMapping(fieldType, isBound,
					isFirst, isBlurry, formName, tableFieldName);
			fieldsList.add(fields);
		}
	}

	/**
	 * 将配置文件中所有对额外查询条件的描述信息解析出并保存到List<ConditionsMapping>中。
	 * 
	 * @throws ConditionsNameNullEmptyException
	 * @throws TypeException
	 * @throws BoundIsFirstException
	 * @throws BlurryBoundException
	 * @throws BlurryContionException
	 * @throws NameNullEmptyException 
	 */
	private void initialConditionsInfo()
			throws ConditionsNameNullEmptyException, TypeException,
			BlurryContionException, BlurryBoundException, BoundIsFirstException, NameNullEmptyException {
		for (Iterator<?> it = conditions.elementIterator(Constants.CONDITION); it
				.hasNext();) {
			Element foo = (Element) it.next();

			String conditionType1 = foo.elementText(Constants.TYPE);

			Integer conditionType = new Integer(-1);
			if (conditionType1 != null) {
				try {
					conditionType = Integer.parseInt(conditionType1);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			} else {
				conditionType = new Integer(4);// 默认值为4
			}

			String name = foo.elementText(Constants.NAME);
			if (name == null || name.trim().equals(Constants.K15)) {

				throw new ConditionsNameNullEmptyException();

			}
			String tableFieldName = foo.elementText(Constants.TABLE_FIELD_NAME);

			if (tableFieldName == null
					|| tableFieldName.trim().equals(Constants.K15)) {
				// 配置文件中没有该字段在数据库表中对应的名字时,抛出异常
				throw new NameNullEmptyException();

			}
			String value = foo.elementText(Constants.VALUE);
			if (value == null || value.trim().equals(Constants.K15)) {

				throw new ConditionsNameNullEmptyException();

			}
			String isBound1 = foo.elementText(Constants.IS_BOUND);
			boolean isBound;
			if (isBound1 != null) {
				isBound = Boolean.parseBoolean(isBound1);
			} else {
				isBound = false;
			}

			String isFirst1 = foo.elementText(Constants.IS_FIRST);
			boolean isFirst;
			if (isFirst1 != null) {
				isFirst = Boolean.parseBoolean(isFirst1);
			} else {
				isFirst = false;
			}

			String isBlurry1 = foo.elementText(Constants.IS_BLURRY);
			boolean isBlurry;
			if (isBlurry1 != null) {
				isBlurry = Boolean.parseBoolean(isBlurry1);
			} else {
				isBlurry = false;
			}
			
			check(conditionType, isBound, isFirst, isBlurry);

			ConditionsMapping conditions = new ConditionsMapping(conditionType,
					isBound, isFirst, isBlurry, name, tableFieldName, value);
			conditionsList.add(conditions);
		}
	}

	private void parseXML() throws NameNullEmptyException,
			ConditionsNameNullEmptyException, TableNameNullEmptyException,
			TypeException, BlurryContionException, BlurryBoundException,
			BoundIsFirstException {
		initialTableName();
		initialFieldsInfo();
		initialConditionsInfo();
	}

	public List<FieldsMapping> getFieldsList() {
		return fieldsList;
	}

	public List<ConditionsMapping> getConditionsList() {
		return conditionsList;
	}

	public String getTableName() {
		return tableName;
	}

	public String getPojoName() {
		String firstLetter = tableName.substring(0, 1).toUpperCase();
		pojoName = firstLetter + tableName.substring(1);
		return pojoName;
	}
}

⌨️ 快捷键说明

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