Source code: com/xpn/xwiki/objects/BaseObject.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details, published at
15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
16 * root folder of this distribution.
17 *
18 * Created by
19 * User: Ludovic Dubost
20 * Date: 9 déc. 2003
21 * Time: 11:36:06
22 */
23 package com.xpn.xwiki.objects;
24
25
26 import com.xpn.xwiki.XWikiContext;
27 import com.xpn.xwiki.objects.classes.BaseClass;
28 import com.xpn.xwiki.objects.classes.PropertyClass;
29 import org.dom4j.Element;
30 import org.dom4j.dom.DOMElement;
31
32 import java.io.Serializable;
33 import java.util.Collection;
34 import java.util.Iterator;
35 import java.util.List;
36
37 public class BaseObject extends BaseCollection implements ObjectInterface, Serializable {
38
39 public int hashCode() {
40 String str = getName()+getClassName();
41 int nb = getNumber();
42 if (nb>0)
43 str += "_" + nb;
44 return str.hashCode();
45 }
46
47 public void setId(int id) {
48 }
49
50 public void displayHidden(StringBuffer buffer, String name, String prefix, XWikiContext context) {
51 ((PropertyClass)getxWikiClass(context).get(name)).displayHidden(buffer, name, prefix, this, context);
52 }
53
54 public void displaySearch(StringBuffer buffer, String name, String prefix, XWikiContext context) {
55 ((PropertyClass)getxWikiClass(context).get(name)).displaySearch(buffer, name, prefix, this, context);
56 }
57
58 public void displayView(StringBuffer buffer, String name, String prefix, XWikiContext context) {
59 ((PropertyClass)getxWikiClass(context).get(name)).displayView(buffer, name, prefix, this, context);
60 }
61
62 public void displayEdit(StringBuffer buffer, String name, String prefix, XWikiContext context) {
63 ((PropertyClass)getxWikiClass(context).get(name)).displayEdit(buffer, name, prefix, this, context);
64 }
65
66 public String displayHidden(String name, String prefix, XWikiContext context) {
67 StringBuffer buffer = new StringBuffer();
68 ((PropertyClass)getxWikiClass(context).get(name)).displayHidden(buffer, name, prefix, this, context);
69 return buffer.toString();
70 }
71
72 public String displaySearch(String name, String prefix, XWikiContext context) {
73 StringBuffer buffer = new StringBuffer();
74 ((PropertyClass)getxWikiClass(context).get(name)).displaySearch(buffer, name, prefix, this, context);
75 return buffer.toString();
76 }
77
78 public String displayView(String name, String prefix, XWikiContext context) {
79 StringBuffer buffer = new StringBuffer();
80 ((PropertyClass)getxWikiClass(context).get(name)).displayView(buffer, name, prefix, this, context);
81 return buffer.toString();
82 }
83
84 public String displayEdit(String name, String prefix, XWikiContext context) {
85 StringBuffer buffer = new StringBuffer();
86 ((PropertyClass)getxWikiClass(context).get(name)).displayEdit(buffer, name, prefix, this, context);
87 return buffer.toString();
88 }
89
90 public String displayHidden(String name, XWikiContext context) {
91 return displayHidden(name, "", context);
92 }
93
94 public String displaySearch(String name, XWikiContext context) {
95 return displaySearch(name, "", context);
96 }
97
98 public String displayView(String name, XWikiContext context) {
99 return displayView(name, "", context);
100 }
101
102 public String displayEdit(String name, XWikiContext context) {
103 return displayEdit(name, "", context);
104 }
105
106 public Object clone() {
107 BaseObject object = (BaseObject) super.clone();
108 return object;
109 }
110
111 public boolean equals(Object obj) {
112 if (!super.equals(obj))
113 return false;
114
115 if (getNumber()!=((BaseObject)obj).getNumber())
116 return false;
117
118 return true;
119 }
120
121 public Element toXML(BaseClass bclass) {
122 Element oel = new DOMElement("object");
123
124 // Add Class
125 if (bclass!=null) {
126 Collection fields = bclass.getFieldList();
127 if (fields.size()>0) {
128 oel.add(bclass.toXML());
129 }
130 }
131
132 Element el = new DOMElement("name");
133 el.addText(getName());
134 oel.add(el);
135
136 el = new DOMElement("number");
137 el.addText(getNumber() + "");
138 oel.add(el);
139
140 el = new DOMElement("className");
141 el.addText(getClassName());
142 oel.add(el);
143
144 Iterator it = getFieldList().iterator();
145 while (it.hasNext()) {
146 Element pel = new DOMElement("property");
147 PropertyInterface bprop = (PropertyInterface)it.next();
148 pel.add(bprop.toXML());
149 oel.add(pel);
150 }
151 return oel;
152 }
153
154 public void fromXML(Element oel) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
155 Element cel = oel.element("class");
156 BaseClass bclass = new BaseClass();
157 if (cel!=null) {
158 bclass.fromXML(cel);
159 setClassName(bclass.getName());
160 }
161
162 setName(oel.element("name").getText());
163 List list = oel.elements("property");
164 for (int i=0;i<list.size();i++) {
165 Element pcel = (Element)((Element) list.get(i)).elements().get(0);
166 String name = pcel.getName();
167 PropertyClass pclass = (PropertyClass) bclass.get(name);
168 if (pclass!=null) {
169 BaseProperty property = pclass.newPropertyfromXML(pcel);
170 property.setName(name);
171 property.setObject(this);
172 safeput(name, property);
173 }
174 }
175 }
176
177 }