KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > property > Dom4jAccessor


1 // $Id: Dom4jAccessor.java,v 1.15 2005/07/16 22:20:47 oneovthafew Exp $
2
package org.hibernate.property;
3
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.dom4j.Attribute;
8 import org.dom4j.Element;
9 import org.dom4j.Node;
10 import org.hibernate.HibernateException;
11 import org.hibernate.MappingException;
12 import org.hibernate.PropertyNotFoundException;
13 import org.hibernate.engine.SessionFactoryImplementor;
14 import org.hibernate.engine.SessionImplementor;
15 import org.hibernate.type.CollectionType;
16 import org.hibernate.type.Type;
17
18 /**
19  * Responsible for accessing property values represented as a dom4j Element
20  * or Attribute.
21  *
22  * @author Steve Ebersole
23  */

24 public class Dom4jAccessor implements PropertyAccessor {
25     private String JavaDoc nodeName;
26     private Type propertyType;
27     private final SessionFactoryImplementor factory;
28
29     public Dom4jAccessor(String JavaDoc nodeName, Type propertyType, SessionFactoryImplementor factory) {
30         this.factory = factory;
31         this.nodeName = nodeName;
32         this.propertyType = propertyType;
33         
34     }
35
36     /**
37      * Create a "getter" for the named attribute
38      */

39     public Getter getGetter(Class JavaDoc theClass, String JavaDoc propertyName)
40     throws PropertyNotFoundException {
41         if (nodeName==null) {
42             throw new MappingException("no node name for property: " + propertyName);
43         }
44         if ( ".".equals(nodeName) ) {
45             return new TextGetter(propertyType, factory);
46         }
47         else if ( nodeName.indexOf('/')>-1 ) {
48             return new ElementAttributeGetter(nodeName, propertyType, factory);
49         }
50         else if ( nodeName.indexOf('@')>-1 ) {
51             return new AttributeGetter(nodeName, propertyType, factory);
52         }
53         else {
54             return new ElementGetter(nodeName, propertyType, factory);
55         }
56     }
57
58     /**
59      * Create a "setter" for the named attribute
60      */

61     public Setter getSetter(Class JavaDoc theClass, String JavaDoc propertyName)
62     throws PropertyNotFoundException {
63         if (nodeName==null) {
64             throw new MappingException("no node name for property: " + propertyName);
65         }
66         if ( ".".equals(nodeName) ) {
67             return new TextSetter(propertyType);
68         }
69         else if ( nodeName.indexOf('/')>-1 ) {
70             return new ElementAttributeSetter(nodeName, propertyType);
71         }
72         else if ( nodeName.indexOf('@')>-1 ) {
73             return new AttributeSetter(nodeName, propertyType);
74         }
75         else {
76             return new ElementSetter(nodeName, propertyType);
77         }
78     }
79
80     /**
81      * Defines the strategy for getting property values out of a dom4j Node.
82      */

83     public abstract static class Dom4jGetter implements Getter {
84         protected final Type propertyType;
85         protected final SessionFactoryImplementor factory;
86         
87         Dom4jGetter(Type propertyType, SessionFactoryImplementor factory) {
88             this.propertyType = propertyType;
89             this.factory = factory;
90         }
91         
92         public Object JavaDoc getForInsert(Object JavaDoc owner, Map JavaDoc mergeMap, SessionImplementor session)
93         throws HibernateException {
94             return get( owner );
95         }
96
97         /**
98          * Get the declared Java type
99          */

100         public Class JavaDoc getReturnType() {
101             return Object JavaDoc.class;
102         }
103
104         /**
105          * Optional operation (return null)
106          */

107         public String JavaDoc getMethodName() {
108             return null;
109         }
110
111         /**
112          * Optional operation (return null)
113          */

114         public Method JavaDoc getMethod() {
115             return null;
116         }
117     }
118
119     public abstract static class Dom4jSetter implements Setter {
120         protected final Type propertyType;
121
122         Dom4jSetter(Type propertyType) {
123             this.propertyType = propertyType;
124         }
125         
126         /**
127          * Optional operation (return null)
128          */

129         public String JavaDoc getMethodName() {
130             return null;
131         }
132
133         /**
134          * Optional operation (return null)
135          */

136         public Method JavaDoc getMethod() {
137             return null;
138         }
139     }
140     
141     /**
142      * For nodes like <tt>"."</tt>
143      * @author Gavin King
144      */

145     public static class TextGetter extends Dom4jGetter {
146         
147         TextGetter(Type propertyType, SessionFactoryImplementor factory) {
148             super(propertyType, factory);
149         }
150
151         public Object JavaDoc get(Object JavaDoc owner) throws HibernateException {
152             Element ownerElement = (Element) owner;
153             return propertyType.fromXMLNode(ownerElement, factory);
154         }
155         
156     }
157     
158     /**
159      * For nodes like <tt>"@bar"</tt>
160      * @author Gavin King
161      */

162     public static class AttributeGetter extends Dom4jGetter {
163         private final String JavaDoc attributeName;
164         
165         AttributeGetter(String JavaDoc name, Type propertyType, SessionFactoryImplementor factory) {
166             super(propertyType, factory);
167             attributeName = name.substring(1);
168         }
169
170         public Object JavaDoc get(Object JavaDoc owner) throws HibernateException {
171             Element ownerElement = (Element) owner;
172             Node attribute = ownerElement.attribute(attributeName);
173             return attribute==null ? null : propertyType.fromXMLNode(attribute, factory);
174         }
175         
176     }
177
178     /**
179      * For nodes like <tt>"foo"</tt>
180      * @author Gavin King
181      */

182     public static class ElementGetter extends Dom4jGetter {
183         private final String JavaDoc elementName;
184         
185         ElementGetter(String JavaDoc name, Type propertyType, SessionFactoryImplementor factory) {
186             super(propertyType, factory);
187             elementName = name;
188         }
189
190         public Object JavaDoc get(Object JavaDoc owner) throws HibernateException {
191             Element ownerElement = (Element) owner;
192             Node element = ownerElement.element(elementName);
193             return element==null ? null : propertyType.fromXMLNode(element, factory);
194         }
195         
196     }
197     
198     /**
199      * For nodes like <tt>"foo/@bar"</tt>
200      * @author Gavin King
201      */

202     public static class ElementAttributeGetter extends Dom4jGetter {
203         private final String JavaDoc elementName;
204         private final String JavaDoc attributeName;
205         
206         ElementAttributeGetter(String JavaDoc name, Type propertyType, SessionFactoryImplementor factory) {
207             super(propertyType, factory);
208             elementName = name.substring( 0, name.indexOf('/') );
209             attributeName = name.substring( name.indexOf('/')+2 );
210         }
211
212         public Object JavaDoc get(Object JavaDoc owner) throws HibernateException {
213             Element ownerElement = (Element) owner;
214             
215             Element element = ownerElement.element(elementName);
216             
217             if ( element==null ) {
218                 return null;
219             }
220             else {
221                 Attribute attribute = element.attribute(attributeName);
222                 if (attribute==null) {
223                     return null;
224                 }
225                 else {
226                     return propertyType.fromXMLNode(attribute, factory);
227                 }
228             }
229         }
230     }
231     
232     
233     /**
234      * For nodes like <tt>"."</tt>
235      * @author Gavin King
236      */

237     public static class TextSetter extends Dom4jSetter {
238         
239         TextSetter(Type propertyType) {
240             super(propertyType);
241         }
242
243         public void set(Object JavaDoc target, Object JavaDoc value, SessionFactoryImplementor factory)
244         throws HibernateException {
245             Element owner = ( Element ) target;
246             if ( !propertyType.isXMLElement() ) { //kinda ugly, but needed for collections with a "." node mapping
247
if (value==null) {
248                     owner.setText(null); //is this ok?
249
}
250                 else {
251                     propertyType.setToXMLNode(owner, value, factory);
252                 }
253             }
254         }
255
256     }
257     
258     /**
259      * For nodes like <tt>"@bar"</tt>
260      * @author Gavin King
261      */

262     public static class AttributeSetter extends Dom4jSetter {
263         private final String JavaDoc attributeName;
264         
265         AttributeSetter(String JavaDoc name, Type propertyType) {
266             super(propertyType);
267             attributeName = name.substring(1);
268         }
269
270         public void set(Object JavaDoc target, Object JavaDoc value, SessionFactoryImplementor factory)
271         throws HibernateException {
272             Element owner = ( Element ) target;
273             Attribute attribute = owner.attribute(attributeName);
274             if (value==null) {
275                 if (attribute!=null) attribute.detach();
276             }
277             else {
278                 if (attribute==null) {
279                     owner.addAttribute(attributeName, "null");
280                     attribute = owner.attribute(attributeName);
281                 }
282                 propertyType.setToXMLNode(attribute, value, factory);
283             }
284         }
285
286     }
287     
288     /**
289      * For nodes like <tt>"foo"</tt>
290      * @author Gavin King
291      */

292     public static class ElementSetter extends Dom4jSetter {
293         private final String JavaDoc elementName;
294         
295         ElementSetter(String JavaDoc name, Type propertyType) {
296             super(propertyType);
297             elementName = name;
298         }
299
300         public void set(Object JavaDoc target, Object JavaDoc value, SessionFactoryImplementor factory)
301         throws HibernateException {
302             if (value!=CollectionType.UNFETCHED_COLLECTION) {
303                 Element owner = ( Element ) target;
304                 Element existing = owner.element(elementName);
305                 if (existing!=null) existing.detach();
306                 if (value!=null) {
307                     Element element = owner.addElement(elementName);
308                     propertyType.setToXMLNode(element, value, factory);
309                 }
310             }
311         }
312
313     }
314     
315     /**
316      * For nodes like <tt>"foo/@bar"</tt>
317      * @author Gavin King
318      */

319     public static class ElementAttributeSetter extends Dom4jSetter {
320         private final String JavaDoc elementName;
321         private final String JavaDoc attributeName;
322         
323         ElementAttributeSetter(String JavaDoc name, Type propertyType) {
324             super(propertyType);
325             elementName = name.substring( 0, name.indexOf('/') );
326             attributeName = name.substring( name.indexOf('/')+2 );
327         }
328
329         public void set(Object JavaDoc target, Object JavaDoc value, SessionFactoryImplementor factory)
330         throws HibernateException {
331             Element owner = ( Element ) target;
332             Element element = owner.element(elementName);
333             if (value==null) {
334                 if (element!=null) element.detach();
335             }
336             else {
337                 Attribute attribute;
338                 if (element==null) {
339                     element = owner.addElement(elementName);
340                     attribute = null;
341                 }
342                 else {
343                     attribute = element.attribute(attributeName);
344                 }
345                 
346                 if (attribute==null) {
347                     element.addAttribute(attributeName, "null");
348                     attribute = element.attribute(attributeName);
349                 }
350                 propertyType.setToXMLNode(attribute, value, factory);
351             }
352         }
353
354     }
355     
356
357 }
358
Popular Tags