All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nervousync.commons.beans.xml.BaseElement Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nervousync.commons.beans.xml;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nervousync.commons.core.Globals;
import com.nervousync.exceptions.xml.XmlException;
import com.nervousync.utils.BeanUtils;
import com.nervousync.utils.ReflectionUtils;
import com.nervousync.utils.XmlUtils;

/**
 * Base element define, all xml object define must extends this class
 * @author Steven Wee	[email protected]
 * @version $Revision: 1.0 $ $Date: Sep 23, 2010, 2010 1:22:51 PM $
 */
public class BaseElement implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 239544550914272242L;
	
	protected transient final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public BaseElement() {
		
	}
	
	/**
	 * Parse xml string and setting datas to this object
	 * @param xmlObj	XML string will be parsed
	 */
	public void parseXml(Object xmlObj) {
		BaseElement parseObj = XmlUtils.convertToObject(xmlObj, this.getClass());
		BeanUtils.copyProperties(parseObj, this);
	}
	
	/**
	 * Convert Object to XML String By Nervousync XML Util
	 * @return XML String
	 */
	public String toString() throws XmlException {
		return this.toString(null);
	}

	/**
	 * Convert Object to XML String By Nervousync XML Util 
	 * Expain all empty element
	 * 
	 * @param indent 
	 * @param expainEmptyElement expain empty element status
	 * @return XML String
	 */
	public String toString(String indent) throws XmlException {
		return this.toString(indent, null);
	}
	
	/**
	 * Convert Object to XML String By Nervousync XML Util 
	 * Expain all empty element
	 * 
	 * @param indent 
	 * @param expainEmptyElement expain empty element status
	 * @return XML String
	 */
	public String toString(String indent, String encoding) throws XmlException {
		return this.toString(indent, encoding, Globals.DEFAULT_VALUE_BOOLEAN);
	}
	
	/**
	 * Convert Object to XML String By Nervousync XML Util 
	 * Expain all empty element
	 * 
	 * @param indent 
	 * @param expainEmptyElement expain empty element status
	 * @return XML String
	 */
	public String toString(String indent, String encoding, boolean expandEmptyElements) throws XmlException {
		return XmlUtils.convertToXml(this, indent, encoding, expandEmptyElements);
	}

	@Override
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		
		if (this == o) {
			return true;
		}
		
		if (!o.getClass().equals(this.getClass())) {
			return false;
		}
		
		Field[] fields = this.getClass().getDeclaredFields();
		
		try {
			for (Field field : fields) {
				String fieldName = field.getName();
				String methodName = null;
				
				if (field.getType().equals(boolean.class)) {
					methodName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				} else {
					methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				}
				
				if ("serialVersionUID".equals(fieldName)) {
					methodName = "getSerialversionuid";
				}
				
				Method getMethod = ReflectionUtils.findMethod(this.getClass(), methodName, new Class[]{});
				
				Object origValue = getMethod.invoke(this, new Object[]{});
				Object destValue = getMethod.invoke(o, new Object[]{});
				
				if (origValue != null ? !origValue.equals(destValue) : destValue != null) {
					return false;
				}
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	@Override
	public int hashCode() {
		Field[] fields = this.getClass().getDeclaredFields();
		
		int result = 0;

		try {
			for (Field field : fields) {
				String fieldName = field.getName();
				String methodName = null;
				
				if (field.getType().equals(boolean.class)) {
					methodName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				} else {
					methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				}
				
				if ("serialVersionUID".equals(fieldName)) {
					methodName = "getSerialversionuid";
				}
				
				Method getMethod = ReflectionUtils.findMethod(this.getClass(), methodName, new Class[]{});
				
				Object origValue = getMethod.invoke(this, new Object[]{});
				
				result = 29 * result + (origValue != null ? origValue.hashCode() : 0);
			}
		} catch (Exception e) {
			return 0;
		}
		
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy