element.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 267 行

JAVA
267
字号
/**
 *    $Id:Element.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdom;


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

import com.jfimagine.utils.commonutil.CommonUtil;
 
 /**
 * Element class.  A class used to represent an XML element.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.01 $
 */  
 public class Element{
        
        //element tag
        private String m_tag="";
        
        //element value
        private String m_value="";
        
        //child element list.
        private List m_children	=new ArrayList();
        
        
        //****************Constructor ****************************
        
     	/** Constructor */
     	public Element(){
	} 

     	/** Constructor */
     	public Element(String tag){
     		setTag(tag);
	} 

     	/** Constructor */
     	public Element(String tag, String value){
     		setTag(tag);
     		setValue(value);
	} 


     	/** Constructor */
     	public Element(String tag, int value){
     		setTag(tag);
     		setValue(value);
	} 

     	/** Constructor */
     	public Element(String tag, double value){
     		setTag(tag);
     		setValue(value);
	} 

     	/** Constructor */
     	public Element(String tag, java.util.Date value){
     		setTag(tag);
     		setValue(value);
	} 

     	/** Constructor */
     	public Element(String tag, boolean value){
     		setTag(tag);
     		setValue(value);
	} 


        //****************setter, getter ****************************
        
        /**
         *  Get tag string.
         *  @return tag string
         */
        public String getTag(){
        	return m_tag;
	}

        /**
         *  Set tag string.
         *  @param tag The tag string
         */
        public void setTag(String tag){
        	if (tag==null) tag="";
        	m_tag	=tag;
	}


        /**
         *  Get value in string.
         *  @return value in string.
         */
        public String getValue(){
        	return m_value;
	}

        /**
         *  Get value in string.
         *  @param value Value in string.
         */
        public void setValue(String value){
        	if (value==null) value="";
        	m_value	=value;
	}


        /**
         *  Set value in int.
         *  @param value Value in int.
         */
        public void setValue(int value){
        	m_value	=CommonUtil.i2s(value);
	}


        /**
         *  Set value in double.
         *  @param value Value in double.
         */
        public void setValue(double value){
        	m_value	=CommonUtil.f2s(value);
	}

        /**
         *  Set value in java.util.Date.
         *  @param value Value in java.util.Date.
         */
        public void setValue(java.util.Date value){
        	m_value	=CommonUtil.d2s(value);
	}


        /**
         *  Set value in boolean.
         *  @param value Value in boolean.
         */
        public void setValue(boolean value){
        	m_value	=CommonUtil.i2s(CommonUtil.b2i(value));
	}

        //****************  static get method  *********************

        /**
         *  Get value in string.
         *  @param element An element to get value.
         *  @return value in string.
         */
        public static String getStringValue(Element element){
        	if (element==null)
        		return "";
        	else
        		return element.getValue();
	}


        /**
         *  Get value in integer
         *  @return value in integer
         */
        public static int getIntValue(Element element){
        	if (element==null)
        		return 0;
        	else
        		return CommonUtil.s2i(element.getValue());
	}


        /**
         *  Get value in double
         *  @return value in double
         */
        public static double getDoubleValue(Element element){
        	if (element==null)
        		return 0;
        	else
        		return CommonUtil.s2f(element.getValue());
	}

        /**
         *  Get value in java.util.Date
         *  @return value in java.util.Date
         */
        public static java.util.Date getDateValue(Element element){
        	if (element==null)
        		return null;
        	else
        		return CommonUtil.s2d(element.getValue());
	}
        
        /**
         *  Get value in boolean
         *  @return value in boolean
         */
        public static boolean getBooleanValue(Element element){
        	if (element==null)
        		return false;
        	else
                	return CommonUtil.i2b(CommonUtil.s2i(element.getValue()));
        } 
        //****************child elements ****************************

        /**
         *  Get child elements
         *  @return the child elements.
         */
        public List getChildren(){
        	return m_children;
	}
 
        /**
         *  Set child elements
         *  @param children new child elements.
         */
        public void setChildren(List children){
        	m_children	=new ArrayList(children);
	}

        /**
         *  Get count of child elements
         *  @return the children count
         */
        public int getChildrenCount(){
        	return m_children.size();
	}

        /**
         *  If this element has children
         *  @return True if has children, false otherwise.
         */
        public boolean hasChildren(){
        	return m_children.size()>0;
	}
 
        /**
         *  add child element
         *  @param child A new child element.
         */
        public void addChild(Element child){
        	m_children.add(child);
	}

        /**
         *  get child element by tag.
         *  @return child found.
         */
        public Element getChild(String tag){
        	Iterator it	=m_children.iterator();
        	while (it!=null && it.hasNext()){
        		Element child	=(Element)it.next();
        		if (child.getTag().equals(tag))
        			return child;
        	}
        	return null;
	}

         

	
}

⌨️ 快捷键说明

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