001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.xml.dom;
024:
025: import java.lang.reflect.Field;
026:
027: import org.w3c.dom.Element;
028:
029: import biz.hammurapi.RuntimeException;
030: import biz.hammurapi.convert.CompositeConverter;
031:
032: /**
033: * @author Pavel Vlasov
034: * @version $Revision: 1.4 $
035: */
036: public class XmlBeanBase implements DomSerializable {
037:
038: public void toDom(Element holder) {
039: holder.setAttribute("type", getClass().getName());
040: for (Class clazz = getClass(); !clazz.equals(XmlBeanBase.class); clazz = clazz
041: .getSuperclass()) {
042: try {
043: Field[] fields = clazz.getDeclaredFields();
044: for (int i = 0, j = fields.length; i < j; i++) {
045: boolean accessible = fields[i].isAccessible();
046: fields[i].setAccessible(true);
047: Object fieldValue = fields[i].get(this );
048: fields[i].setAccessible(accessible);
049: if (fieldValue != null) {
050: CompositeDomSerializer.getThreadInstance()
051: .toDomSerializable(fieldValue).toDom(
052: AbstractDomObject.addElement(
053: holder, fields[i]
054: .getName()));
055: }
056: }
057: } catch (IllegalAccessException iae) {
058: throw new RuntimeException("Cannot access field: "
059: + iae, iae);
060: }
061: }
062: }
063:
064: public String toString() {
065: StringBuffer ret = new StringBuffer(getClass().getName());
066: ret.append("[");
067: for (Class clazz = getClass(); !clazz.equals(XmlBeanBase.class); clazz = clazz
068: .getSuperclass()) {
069: try {
070: Field[] fields = clazz.getDeclaredFields();
071: for (int i = 0, j = fields.length; i < j; i++) {
072: if (i > 0) {
073: ret.append(", ");
074: }
075: ret.append(fields[i].getName());
076: ret.append("=");
077: boolean accessible = fields[i].isAccessible();
078: fields[i].setAccessible(true);
079: Object fieldValue = fields[i].get(this );
080: fields[i].setAccessible(accessible);
081: if (fieldValue == null) {
082: ret.append("(null)");
083: } else {
084: ret.append(fieldValue);
085: }
086: }
087: } catch (IllegalAccessException iae) {
088: throw new RuntimeException("Cannot access field: "
089: + iae, iae);
090: }
091: }
092: ret.append("]");
093: return ret.toString();
094: }
095:
096: // /**
097: // * @param rs
098: // * @throws SQLException
099: // */
100: // protected static void checkObject(Object o, String column, String targetType) throws SQLException {
101: // System.out.println(column+" -> ("+targetType+") "+(o==null ? "null" : o.getClass().getName()));
102: // }
103:
104: /**
105: * Converts one type to another if possible
106: * @return Converted object.
107: */
108: public static Object convert(Object arg, String targetClass) {
109: try {
110: return CompositeConverter.getDefaultConverter().convert(
111: arg, Class.forName(targetClass), false);
112: } catch (ClassNotFoundException e) {
113: throw new ClassCastException("Target class not found: "
114: + targetClass);
115: }
116: }
117:
118: /**
119: * Two objects are considered equal and all their fields are equal.
120: */
121: public boolean equals(Object otherBean) {
122: if (otherBean == null) {
123: return false;
124: } else if (getClass().equals(otherBean.getClass())) {
125: Field[] fields = getClass().getFields();
126: for (int i = 0, j = fields.length; i < j; i++) {
127: try {
128: Object fieldValue = fields[i].get(this );
129: Object otherFieldValue = fields[i].get(otherBean);
130: if (fieldValue == null) {
131: if (otherFieldValue != null) {
132: return false;
133: }
134: } else {
135: if (otherFieldValue == null) {
136: return false;
137: } else if (!fieldValue.equals(otherFieldValue)) {
138: return false;
139: }
140: }
141: } catch (IllegalAccessException e) {
142: throw new RuntimeException(
143: "Cannot compare objects", e);
144: }
145: }
146: return true;
147: } else {
148: return false;
149: }
150: }
151:
152: public int hashCode() {
153: int ret = 0;
154: Field[] fields = getClass().getFields();
155: for (int i = 0, j = fields.length; i < j; i++) {
156: try {
157: ret ^= fields[i].getName().hashCode();
158: Object fieldValue = fields[i].get(this );
159: if (fieldValue != null) {
160: ret ^= fieldValue.hashCode();
161: }
162: } catch (IllegalAccessException e) {
163: throw new RuntimeException(
164: "Cannot calculate hash code", e);
165: }
166: }
167: return ret;
168: }
169: }
|