KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > hibernate > HibernateSubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.hibernate;
6
7 import java.text.MessageFormat JavaDoc;
8
9 import xjavadoc.XClass;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XmlSubTask;
13 import xdoclet.util.Translator;
14
15 /**
16  * This task generates Hibernate xml mapping file for a given class. Supports Hibernate 1.1, 2.0, 2.1 & 3.0.
17  *
18  * @author Sébastien Guimont (sebastieng@sympatico.ca)
19  * @author <a HREF="mailto:dchannon at users.sourceforge.net">David Channon</a>
20  * @created August 9th, 2002
21  * @version $Revision: 1.12 $
22  * @ant.element name="hibernate" display-name="Hibernate Mapping File"
23  * parent="xdoclet.modules.hibernate.HibernateDocletTask"
24  * @xdoclet.merge-file file="hibernate-properties.xml" relates-to="{0}.hbm.xml" description="An XML unparsed entity
25  * containing additional property mappings for all classes."
26  * @xdoclet.merge-file file="hibernate-properties-{0}.xml" relates-to="{0}.hbm.xml" description="An XML unparsed
27  * entity containing additional property mappings for a class."
28  */

29 public class HibernateSubTask
30      extends XmlSubTask
31 {
32     public final static String JavaDoc DEFAULT_HIBERNATE_CLASS_PATTERN = "{0}";
33
34     //~ Instance/static variables ......................................................................................
35

36     private final static String JavaDoc HIBERNATE_PUBLICID_11 = "-//Hibernate/Hibernate Mapping DTD 1.1//EN";
37
38     private final static String JavaDoc HIBERNATE_SYSTEMID_11 = "http://hibernate.sourceforge.net/hibernate-mapping-1.1.dtd";
39
40     private final static String JavaDoc DTD_FILE_NAME_11 = "resources/hibernate-mapping_1_1.dtd";
41
42     private final static String JavaDoc HIBERNATE_PUBLICID_20 = "-//Hibernate/Hibernate Mapping DTD 2.0//EN";
43
44     private final static String JavaDoc HIBERNATE_SYSTEMID_20 = "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";
45
46     private final static String JavaDoc DTD_FILE_NAME_20 = "resources/hibernate-mapping_2_0.dtd";
47
48     private final static String JavaDoc HIBERNATE_PUBLICID_30 = "-//Hibernate/Hibernate Mapping DTD 3.0//EN";
49
50     private final static String JavaDoc HIBERNATE_SYSTEMID_30 = "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";
51
52     private final static String JavaDoc DTD_FILE_NAME_30 = "resources/hibernate-mapping_3_0.dtd";
53
54     /**
55      * Default template to use for hibernate files.
56      */

57     private static String JavaDoc DEFAULT_HIBERNATE_TEMPLATE_FILE = "resources/hibernate.xdt";
58
59     /**
60      * Pattern for generation of hibernate files.
61      */

62     private static String JavaDoc GENERATED_HIBERNATE_FILE_NAME = "{0}.hbm.xml";
63
64     /**
65      * Defaults to Hibernate 1.1.
66      */

67     private String JavaDoc hibernateVersion = HibernateVersion.HIBERNATE_1_1;
68
69     //~ Constructors ...................................................................................................
70

71     /**
72      * Constructor for the HibernateSubTask object.
73      */

74     public HibernateSubTask()
75     {
76         setHavingClassTag("hibernate.class");
77         setTemplateURL(getClass().getResource(DEFAULT_HIBERNATE_TEMPLATE_FILE));
78         setDestinationFile(GENERATED_HIBERNATE_FILE_NAME);
79     }
80
81     public String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
82     {
83         return super.getGeneratedFileName(clazz);
84     }
85
86     public String JavaDoc getMappingURL(XClass clazz)
87     {
88         return MessageFormat.format(getDestinationFile(), new Object JavaDoc[]{clazz.getTransformedQualifiedName().replace('.', '/'), ""});
89     }
90
91     /**
92      * Get the Hibernate version.
93      *
94      * @return
95      */

96     public String JavaDoc getVersion()
97     {
98         return hibernateVersion;
99     }
100
101     /**
102      * Sets the hibernate version to use. Legal values are "1.1", "2.0", "2.1" and "3.0".
103      *
104      * @param version
105      * @ant.not-required No. Default is "1.1".
106      */

107     public void setVersion(HibernateVersion version)
108     {
109         hibernateVersion = version.getValue();
110     }
111
112     /**
113      * Generate Mapping file (*.hbm.xml).
114      *
115      * @exception XDocletException
116      */

117     public void execute() throws XDocletException
118     {
119
120         if (hibernateVersion.equals(HibernateVersion.HIBERNATE_1_1)) {
121             setPublicId(HIBERNATE_PUBLICID_11);
122             setSystemId(HIBERNATE_SYSTEMID_11);
123             setDtdURL(getClass().getResource(DTD_FILE_NAME_11));
124         }
125         if (hibernateVersion.equals(HibernateVersion.HIBERNATE_2_0) ||
126             hibernateVersion.equals(HibernateVersion.HIBERNATE_2_1)) {
127             setPublicId(HIBERNATE_PUBLICID_20);
128             setSystemId(HIBERNATE_SYSTEMID_20);
129             setDtdURL(getClass().getResource(DTD_FILE_NAME_20));
130         }
131         else {
132             setPublicId(HIBERNATE_PUBLICID_30);
133             setSystemId(HIBERNATE_SYSTEMID_30);
134             setDtdURL(getClass().getResource(DTD_FILE_NAME_30));
135         }
136         startProcess();
137     }
138
139     //~ Methods ........................................................................................................
140

141     /**
142      * Called when the engine is started
143      *
144      * @exception XDocletException Thrown in case of problem
145      */

146     protected void engineStarted()
147          throws XDocletException
148     {
149         System.out.println(Translator.getString(XDocletModulesHibernateMessages.class,
150             XDocletModulesHibernateMessages.GENERATING_HIBERNATE_FOR,
151             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
152     }
153
154     /**
155      * Since we want to support static inner classes, we set this to true.
156      *
157      * @return <code>true</code>
158      */

159     protected boolean processInnerClasses()
160     {
161         return true;
162     }
163
164     /**
165      * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
166      * @created February 23, 2003
167      */

168     public static class HibernateVersion extends org.apache.tools.ant.types.EnumeratedAttribute
169     {
170         public final static String JavaDoc HIBERNATE_1_1 = "1.1";
171         public final static String JavaDoc HIBERNATE_2_0 = "2.0";
172         public final static String JavaDoc HIBERNATE_2_1 = "2.1";
173         public final static String JavaDoc HIBERNATE_3_0 = "3.0";
174
175         /**
176          * Gets the Values attribute of the HibernateVersion object.
177          *
178          * @return The Values value
179          */

180         public java.lang.String JavaDoc[] getValues()
181         {
182             return (new java.lang.String JavaDoc[]{
183                 HIBERNATE_1_1, HIBERNATE_2_0, HIBERNATE_2_1, HIBERNATE_3_0
184                 });
185         }
186     }
187 }
188
Popular Tags