KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tuple > Dom4jInstantiator


1 //$Id: Dom4jInstantiator.java,v 1.2 2005/07/11 17:31:50 steveebersole Exp $
2
package org.hibernate.tuple;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import org.dom4j.Element;
9 import org.hibernate.util.XMLHelper;
10 import org.hibernate.mapping.PersistentClass;
11 import org.hibernate.mapping.Component;
12
13 /**
14  * Performs "instantiation" based on DOM4J elements.
15  */

16 public class Dom4jInstantiator implements Instantiator {
17     private final String JavaDoc nodeName;
18     private final HashSet JavaDoc isInstanceNodeNames = new HashSet JavaDoc();
19
20     public Dom4jInstantiator(Component component) {
21         this.nodeName = component.getNodeName();
22         isInstanceNodeNames.add( nodeName );
23     }
24
25     public Dom4jInstantiator(PersistentClass mappingInfo) {
26         this.nodeName = mappingInfo.getNodeName();
27         isInstanceNodeNames.add( nodeName );
28
29         if ( mappingInfo.hasSubclasses() ) {
30             Iterator JavaDoc itr = mappingInfo.getSubclassClosureIterator();
31             while ( itr.hasNext() ) {
32                 final PersistentClass subclassInfo = ( PersistentClass ) itr.next();
33                 isInstanceNodeNames.add( subclassInfo.getNodeName() );
34             }
35         }
36     }
37     
38     public Object JavaDoc instantiate(Serializable JavaDoc id) {
39         return instantiate();
40     }
41     
42     public Object JavaDoc instantiate() {
43         return XMLHelper.generateDom4jElement( nodeName );
44     }
45
46     public boolean isInstance(Object JavaDoc object) {
47         if ( object instanceof Element ) {
48             return isInstanceNodeNames.contains( ( ( Element ) object ).getName() );
49         }
50         else {
51             return false;
52         }
53     }
54 }
Popular Tags