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

📄 xmlproperties.java

📁 java实现的可配置的工作流引擎,采用jsp+javabean实现
💻 JAVA
字号:
package com.hongsoft.agile.xmlParser;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.hongsoft.agile.Logger;

/**
 * 用x.y.z的形式来获取值
 *
 * 本代码参考了www.jdon.org
 */
public class XMLProperties {

	public File file;
	public Document doc;
	/**
	cache
     */
	protected Map propertyCache = new HashMap();	

	/**
	 * Creates a new XMLProperties object.
	 *
	 * @parm file 全路径
	 */
	public XMLProperties(String file) throws XMLParserException {
		this.file = new File(file);
		try {
			SAXBuilder builder = new SAXBuilder();
			XmlFilterNull format = new XmlFilterNull();
			builder.setXMLFilter(format);
			doc = builder.build(new File(file));
		}
		catch (Exception e) {
		    Logger.getBusinessLogger().error(e);
		    throw new XMLParserException("解析XML文件"+file+"失败!");
		}
	}
	/**
		 * Returns the value of the x.y.z attribute
		 *
		 * @param name the name of the property to get.
		 * @return the value of the specified property.
		 */
		public String getAttribute(String name) {
			if (propertyCache.containsKey(name)) {
				return (String)propertyCache.get(name);
			}

			String[] propName = parsePropertyName(name);
			Element element = doc.getRootElement();
			for (int i = 0; i < propName.length-1; i++) {
				element = element.getChild(propName[i]);
				if (element == null) {
						return null;
				}
			}
			String value = element.getAttributeValue(propName[propName.length-1]);
			//modifyed by Paul Gu,at 2004/7/24 16:43 
			//原先没有处理null值。
			if (value==null||"".equals(value)) {
			//if ("".equals(value)) {
				return null;
			}
			else {
				value = value.trim();
				propertyCache.put(name, value);
				return value;
			}
		}

	/**
	 * Returns the value of the x.y.z
	 *
	 * @param name the name of the property to get.
	 * @return the value of the specified property.
	 */
	public String getProperty(String name) {
		if (propertyCache.containsKey(name)) {
			return (String)propertyCache.get(name);
		}

		String[] propName = parsePropertyName(name);
		Element element = doc.getRootElement();
		for (int i = 0; i < propName.length; i++) {
			element = element.getChild(propName[i]);
			if (element == null) {
					return null;
			}
		}
		String value = element.getText();
		
		//modified by Paul Gu,at 2004/7/24 19:29
		//先前的对于<a><!--this is desc..--></a>这种情况不能解析,此时value="tab\r\n",类似这样的格式
		//if ("".equals(value)) {
		if (null==value||value.trim().equals("")){
			return null;
		}
		else {		
			value = value.trim();
			propertyCache.put(name, value);
			return value;
		}
	}
	   /**
		 * Return all numeric children property names of a parent property as a String array,
		 * or an empty array if the if there are no children. For example, given
		 * the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then
		 * the child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and
		 * <tt>C</tt>.
		 *
		 * @param parent the name of the parent property.
		 * @return all child property values for the given parent.
		 */
		public String [] getNumericChildrenProperties(String parent) {
			String[] propName = parsePropertyName(parent);
			Element element = doc.getRootElement();
			for (int i = 0; i < propName.length; i++) {
				element = element.getChild(propName[i]);
				if (element == null) {
					return new String [] { };
				}
			}
			List children = element.getChildren();
			int childCount = children.size();
			//children2 is the list of numeric
			List children2=new ArrayList();
			for (int i=0; i<childCount; i++) {
				String str=((Element)children.get(i)).getName();
				boolean isNumeric=true;
				for(int j=0;j<str.length();j++)
				{
					if(str.charAt(j)<'0'||str.charAt(j)>'9')
					    isNumeric=false;
				}
				if(isNumeric==true)
				   children2.add(str);

			}
			return (String[])children2.toArray();
		}

	/**
	 * Return all children property names of a parent property as a String array,
	 * or an empty array if the if there are no children. For example, given
	 * the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then
	 * the child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and
	 * <tt>C</tt>.
	 *
	 * @param parent the name of the parent property.
	 * @return all child property values for the given parent.
	 */
	public String [] getChildrenProperties(String parent) {
		String[] propName = parsePropertyName(parent);
		Element element = doc.getRootElement();
		for (int i = 0; i < propName.length; i++) {
			element = element.getChild(propName[i]);
			if (element == null) {
				return new String [] { };
			}
		}
		List children = element.getChildren();
		int childCount = children.size();
		String [] childrenNames = new String[childCount];
		for (int i=0; i<childCount; i++) {
			childrenNames[i] = ((Element)children.get(i)).getName();
		}
		return childrenNames;
	}

	/**
	 * 设定值
	 *
	 * @param name the name of the property to set.
	 * @param value the new value for the property.
	 */
	public void setProperty(String name, String value) {
		propertyCache.put(name, value);

		String[] propName = parsePropertyName(name);
		Element element = doc.getRootElement();
		for (int i=0; i<propName.length; i++) {
			if (element.getChild(propName[i]) == null) {
				element.addContent(new Element(propName[i]));
			}
			element = element.getChild(propName[i]);
		}
		element.setText(value);
		saveProperties();
	}

	/**
	 * Deletes the specified property.
	 *
	 * @param name the property to delete.
	 */
	public void deleteProperty(String name) {
		String[] propName = parsePropertyName(name);
		Element element = doc.getRootElement();
		for (int i=0; i<propName.length-1; i++) {
			element = element.getChild(propName[i]);
			// Can't find the property so return.
			if (element == null) {
				return;
			}
		}
		// Found the correct element to remove, so remove it...
		element.removeChild(propName[propName.length-1]);
		// .. then write to disk.
		saveProperties();
	}

	/**
	 * Saves the properties to disk as an XML document. A temporary file is
	 * used during the writing process for maximum safety.
	 *
	 */
	public synchronized void saveProperties() {
		OutputStream out = null;
		boolean error = false;
		File tempFile = null;
		try {
			tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
			XMLOutputter outputter = new XMLOutputter("    ", true);
			out = new BufferedOutputStream(new FileOutputStream(tempFile));
			outputter.output(doc, out);
		}
		catch (Exception e) {
			e.printStackTrace();
			error = true;
		}
		finally {
			try {  out.close();  }
			catch (Exception e) {
				e.printStackTrace();
				error = true;
			}
		}
		if (!error) {
			file.delete();
			tempFile.renameTo(file);
		}
	}

	/**
	 * Returns an array representation of the given property.
	 * properties are always in the format "prop.name.is.this" which would be
	 * represented as an array of four Strings.
	 *
	 * @param name the name of the  property.
	 * @return an array representation of the given property.
	 * 
	 */
	/*private*/ String[] parsePropertyName(String name) {
		int size = 1;
		for (int i=0; i<name.length(); i++) {
			if (name.charAt(i) == '.') {
				size++;
			}
		}
		String[] propName = new String[size];
		// Use a StringTokenizer to tokenize the property name.
		StringTokenizer tokenizer = new StringTokenizer(name, ".");
		int i = 0;
		while (tokenizer.hasMoreTokens()) {
			propName[i] = tokenizer.nextToken();
			i++;
		}
		return propName;
	}
	
	/**
	 * @author Paul Gu
	 * @created-time 2004/7/24 15:57
	 * Returns the value of the x.y.z attribute List
	 *
	 * @param name the name of the property to get.
	 * @return the attributes of the specified property.
	 */
	public List getAttributes(String name) {
	    String attributes = "?attributes";
		if (propertyCache.containsKey(name+attributes)) {
			return (List)propertyCache.get(name);
		}

		String[] propName = parsePropertyName(name);
		Element element = doc.getRootElement();
		for (int i = 0; i < propName.length; i++) {
			element = element.getChild(propName[i]);
			if (element == null) {
					return null;
			}
		}
		List value = element.getAttributes();
		if (null==value) {
			return null;
		}
		else {
			propertyCache.put(name+attributes, value);
			return value;
		}	    
	}
    /**
     * @author Paul Gu
     *@created-time 7-27-2004 14:21
     * 
     * @return Map Returns the propertyCache.
     */
    public Map getPropertyCache() {
        return propertyCache;
    }
    /**
     * @author Paul Gu
     *@created-time 7-27-2004 14:21
     *
     * @return Document Returns the doc.
     */
    public Document getDoc() {
        return doc;
    }	
}

⌨️ 快捷键说明

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