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

📄 node.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
字号:
/**
 * @author 沈东良 <a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 * 2007-1-19 下午03:55:00
 */
package net.sf.oxmled.model;

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

import org.apache.commons.lang.builder.EqualsBuilder;

/**
 * @author 沈东良 <a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 *         2007-1-19 下午03:55:00 节点模型,每一个节点的数据集合
 * OXmlEd的任何节点。与具体的XML操作组件无关,是轻量级的。
 * 
 */
public class Node implements INode {
	/**
	 * 文档Document的编码
	 */
	private String encoding = "utf-8";

	/*
	 * 默认元素不是根!
	 */
	private boolean root = false;

	/**
	 * 节点的名称,是QName,如<map></map>这个节点。它的name就是map
	 */
	private String name = null;
	/**
	 * 节点元素的内容
	 */
	private String text = "";

	/**
	 * 属性集合 Hashtable<String String>
	 */
	private Map<String, String> attributes = new Hashtable<String, String>();
	/**
	 * 儿子元素集合
	 */
	private List<INode> childNodes = new ArrayList<INode>();
	/**
	 * 父节点
	 */
	private INode parentNode = null;

	/**
	 * 返回文档
	 */
	/**
	 * 得到这个转化成的DOM4j 的Document。 public Document getDocument(){ //public static
	 * Document createDocument(Element rootElement) /** 需要得到根元素, 已经笛归调用了
	 * 
	 * Document
	 * document=DocumentHelper.createDocument(this.convertToAssembledRootElement());
	 * /** 编码格式
	 * 
	 * document.setXMLEncoding(this.getEncoding()); // return document; }
	 * 
	 */

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#clone(net.sf.oxmled.model.INode)
	 */
	public void clone(INode source) {
		this.setName(source.getName());
		this.setText(source.getText());
		this.setRoot(source.isRoot());
		this.setEncoding(source.getEncoding());
		this.setChildNodes(source.getChildNodes());
		this.setAttributes(source.getAttributes());
		this.setParentNode(source.getParentNode());
		
	}
	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#clone()
	 */
	public INode clone(){
		INode result=new Node();
		result.setName(this.getName());
		result.setText(this.getText());
		result.setRoot(this.isRoot());
		result.setEncoding(this.getEncoding());
		result.setChildNodes(this.getChildNodes());
		result.setAttributes(this.getAttributes());
		result.setParentNode(this.getParentNode());
		
		
		return result;
	}

	/**
	 * 转换Node节点为 Dom4j的Element
	 * 
	 * public Element convertToElement(){ //节点的名字,不可能为空! //
	 * if(this.getName()==null){ // throw new
	 * PropertiesIsNullException("节点的名字不能为空!");
	 *  // }else{ Element element=new DefaultElement(this.getName()); // }
	 * element.addText(this.getText()); // Map attributes=; /** 读取所有属性
	 * 
	 * 
	 * 
	 * Iterator itKey= this.getAttributes().keySet().iterator(); //Iterator
	 * itValue=this.getAttributes().values().iterator(); while(itKey.hasNext()){
	 * String key=(String)itKey.next(); String
	 * value=this.getAttributes().get(key); element.addAttribute(key, value);
	 * 
	 *  }
	 * 
	 * 
	 * return element; }
	 *  }
	 */

	/**
	 * 未完成 把整个树,全部转换成element,并且链接好
	 * 
	 * @return public Element convertToAssembledRootElement(){ //节点的名字,不可能为空! //
	 *         if(this.getName()==null){ // throw new
	 *         PropertiesIsNullException("节点的名字不能为空!"); // }else{ Element
	 *         rootElement=this.convertToElement();
	 * 
	 * /** PUBLIC Element convertLoopElement
	 * 
	 * 
	 * this.loopElement(rootElement, this);
	 * 
	 * return rootElement; }
	 */

	/**
	 * 
	 * @param parentElement
	 * @param parentNode
	 * @return public Element loopElement(Element parentElement,Node
	 *         parentNode){ /** 对父亲节点的儿子节点,加到父亲元素上
	 * 
	 * 
	 * 
	 * Iterator it=parentNode.getChildNodes().iterator(); while(it.hasNext()){
	 * Node tmpChildNode=(Node)it.next(); Element
	 * tmpChildElement=tmpChildNode.convertToElement();
	 * parentElement.add(tmpChildElement); /** 递归,加到用户父亲元素上。
	 * 
	 * this.loopElement(tmpChildElement, tmpChildNode); }
	 * 
	 * 
	 * return parentElement; }
	 */

	/**
	 * 根节点可以没有名字
	 * 
	 * @return
	 * 
	 * public static Node getProcessDefinition(){ Node processDefinition=new
	 * Node("process-definition");
	 * processDefinition.getAttributes().put("xmlns", "");
	 * processDefinition.setRoot(true); return processDefinition; } /**
	 * 
	 * @param name
	 * @return
	 * 
	 * public static Node getProcessDefinition(String name){ Node
	 * processDefinition=Node.getProcessDefinition();
	 * processDefinition.getAttributes().put("name", name);
	 * processDefinition.setRoot(true); return processDefinition; }
	 * 
	 * 
	 * /**
	 * 
	 * @param name
	 *            jpdl的任何节点,都必须要有名字
	 * @return
	 * 
	 * public static Node getStartState(String name){ Node startState=new
	 * Node("start-state"); startState.getAttributes().put("name", name); return
	 * startState; }
	 */

	/**
	 * 必须要提供name,才能够使用,否则缺少name,则无法表示一个xml的元素。
	 */
	public Node() {
		super();

	}

	/**
	 * 
	 * @param name
	 */
	public Node(String name) {

		this.setName(name);

	}

	/**
	 * 
	 * @param name
	 * @param text
	 */
	public Node(String name, String text) {

		this.setName(name);
		this.setText(text);

	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getName()
	 */
	public String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setName(java.lang.String)
	 */
	public void setName(String name) {

		this.name = name;
		// this.fireStateChanged();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * 
		 */

	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getAttributes()
	 */
	public Map<String, String> getAttributes() {
		return attributes;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setAttributes(java.util.Map)
	 */
	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
		// this.fireStateChanged();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getChildNodes()
	 */
	public List<INode> getChildNodes() {
		return childNodes;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setChildNodes(java.util.List)
	 */
	public void setChildNodes(List<INode> childNodes) {
		this.childNodes = childNodes;
		// this.fireStateChanged();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getParentNode()
	 */
	public INode getParentNode() {
		return parentNode;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setParentNode(net.sf.oxmled.model.INode)
	 */
	public void setParentNode(INode parentNode) {
		this.parentNode = parentNode;
		// this.fireStateChanged();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getText()
	 */
	public String getText() {
		return text;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setText(java.lang.String)
	 */
	public void setText(String text) {
		this.text = text;
		// this.fireStateChanged();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#isRoot()
	 */
	public boolean isRoot() {
		return root;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setRoot(boolean)
	 */
	public void setRoot(boolean root) {
		this.root = root;
		// this.fireStateChanged();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getEncoding()
	 */
	public String getEncoding() {
		return encoding;
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#setEncoding(java.lang.String)
	 */
	public void setEncoding(String encoding) {
		this.encoding = encoding;
		// this.fireStateChanged();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#toString()
	 */
	public String toString() {
		String result="";
		result=result+"<"+this.getName();
		Iterator itAttributes= this.getAttributes().entrySet().iterator();
		while(itAttributes.hasNext()){
			Map.Entry<String,String> tmp= (Map.Entry<String,String>)itAttributes.next();
			result=result+" "+tmp.getKey()+"=\""+tmp.getValue()+"\"";
			
		}
		result=result+"> \n"+this.getText()+"\n";
		/**
		 * 子节点,递归
		 * 
		 */
		Iterator  itChildNodes =this.getChildNodes().iterator();
			while(itChildNodes.hasNext()){
				
				INode  tmp=(INode) itChildNodes.next();
				result=result+tmp.toString();
			}
			result=result+"</"+this.getName() + ">\n";
			
			return result;
		

	}
	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getChild(int)
	 */
	public INode getChild(int index){
		return this.getChildNodes().get(index);
		
	}
	
    /* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getChildCount()
	 */
	public int getChildCount(){
		
		return this.getChildNodes().size();
	}

	/* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getIndexOfChild(net.sf.oxmled.model.INode)
	 */
	public int getIndexOfChild(INode child){
		return this.getChildNodes().indexOf(child);
	}
	
    /* (non-Javadoc)
	 * @see net.sf.oxmled.model.INode#getRoot()
	 */
	public INode getRoot() {
		/*
		 * 
		 */
		if (this.isRoot()) {
			return this;
		}

		INode parent = this.getParentNode();

		while (!parent.isRoot()) {
			if (this.getParentNode() != null) {
				parent = this.getParentNode();
			} else {
				return null;

			}
		}

		return parent;
	}
	@Override
	public List<INode> descendant() {
		/*
		*所有后代,递归
		*
		*/
		List<INode> descendant=new ArrayList<INode>();
		this.loopChildNodes(this,descendant);
		
		return descendant;
	}
	/**
	 * 
	 * @param parentNode 节点,需要找出所有的儿子节点
	 * @param target 目标List
	 */
	private void loopChildNodes(INode parentNode,List<INode> target){
		target.addAll(parentNode.getChildNodes());
		Iterator<INode> it=parentNode.getChildNodes().iterator();
		while(it.hasNext()){
			
			this.loopChildNodes(it.next(), target);
			
		}
		
	}
	
	
	@Override
	public List<INode> getNodes(INode node) {
		/*
		*
		*/
		List<INode> result=new ArrayList<INode>();
		Iterator<INode> it=this.descendant().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(tmp.equals(node)){
				result.add(tmp);				
			}
			
			
		}
		
		return result;
	}
	@Override
	public List<INode> getNodes(String name) {
		/*
		*
		*/
		List<INode> result=new ArrayList<INode>();
		Iterator<INode> it=this.descendant().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(name.equals(tmp.getName())){
				result.add(tmp);				
			}
			
			
		}
		
		return result;
		
	}
	@Override
	public boolean childContains(INode node) {
		/*
		*
		*/
		Iterator<INode> it=this.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(tmp.equals(node)){
				return true;
			}
			
		}
		
		
		return false;
	}
	@Override
	public boolean childContains(String name) {
		/*
		*
		*/
		Iterator<INode> it=this.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(name.equals(tmp.getName())){
				return true;
			}
			
		}
		
		
		return false;
	}
	@Override
	public List<INode> getChildNodes(INode node) {
		/*
		*
		*/
		List<INode> result=new ArrayList<INode>();
		Iterator<INode> it= this.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(tmp.equals(node)){
				result.add(tmp);
			}
			
		}
		
		
		return result;
	}
	@Override
	public List<INode> getChildNodes(String name) {
		/*
		*
		*/
		List<INode> result=new ArrayList<INode>();
		Iterator<INode> it= this.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(name.equals(tmp.getName())){
				result.add(tmp);				
			}
		}
		
		return result;
	}
	@Override
	public INode getSingleChildNode(INode node) {
		/*
		*
		*/
	
		Iterator<INode> it= this.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(tmp.equals(node)){
				return tmp;
			}
			
		}
		return null;
		
		
	}
	@Override
	public INode getSingleChildNode(String name) {
		/*
		*
		*/
		Iterator<INode> it= this.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if(name.equals(tmp.getName())){
				return tmp;			
			}
		}
		
		return null;
	}
	
	@Override	
	public boolean equals(Object obj){
		/**
		 * 比较是否相等包括其儿子,不比较没有意义的东西
		 * 
		 */
		if(obj==null){
			return false;
		}
		if ((obj instanceof Node) == false) {
		     return false;
		   }
		if(this==obj){
			return true;
		}
		Node rhs=(Node)obj;
		if(!this.getName().equals(rhs.getName()) || !this.getText().equals(rhs.getText()) || !this.getAttributes().entrySet().containsAll(rhs.getAttributes().entrySet()) || !rhs.getAttributes().entrySet().containsAll(this.getAttributes().entrySet()) || !this.getEncoding().equals(rhs.getEncoding()) ||  this.isRoot()!=rhs.isRoot()){
			return false;
		}
		if(this.getParentNode() ==null && rhs.getParentNode()!=null){
			return false;
		}
		if(this.getParentNode() !=null && rhs.getParentNode()==null){
			return false;
		}
		if(this.getParentNode() !=null && rhs.getParentNode()!=null){
           if(!this.getParentNode().equals(rhs.getParentNode())){
        	   return false;
           }

		}
		
		
		Iterator<INode> it=rhs.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmp=it.next();
			if((tmp instanceof Node)== false){
				return false;
			}
			Node tmpChild=(Node)tmp;
			//递归
			if(!this.childContains(tmpChild)){
				return false;
			}
			
		}
		return true;	
		
		
		
	//	return new EqualsBuilder().appendSuper(super.equals(obj)).append(this.getAttributes(), rhs.getAttributes()).append(this.getChildNodes(), rhs.getChildNodes()).append(this.getEncoding(), rhs.getEncoding()).append(this.isRoot(), rhs.isRoot()).append(this.getName(), rhs.getName()).append(this.getText(), rhs.getText()).append(this.getParentNode(), rhs.getParentNode()).isEquals();
	
	
	
	
	}
	
	
  @Override	
  public int hashCode(){
	  /*
	   * 相等的对象hashCode应该相同。 由于Map和List的hashcode可能不同,所以未引入
	  return new org.apache.commons.lang.builder.HashCodeBuilder(1,999).
	  append(this.getAttributes()).append(this.getChildNodes()).
	  append(this.getEncoding()).append(this.getParentNode()).append(this.getName()).append(this.getText()).append(this.isRoot()).toHashCode();
*/
	  return new org.apache.commons.lang.builder.HashCodeBuilder(1,999).append(this.getEncoding()).append(this.getParentNode()).append(this.getName()).append(this.getText()).append(this.isRoot()).toHashCode();
  }
  
}

⌨️ 快捷键说明

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