View Javadoc
1   /*
2    * Copyright 2008 Filip Hianik
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.exolab.castor.builder.descriptors;
17  
18  import org.exolab.castor.builder.BuilderConfiguration;
19  import org.exolab.javasource.JClass;
20  import org.exolab.javasource.JConstructor;
21  import org.exolab.javasource.JSourceCode;
22  
23  /**
24   * A class which defines the necessary methods for creating the JDO-specific
25   * descriptor source files.
26   * 
27   * @see DescriptorJClass
28   * @see DescriptorSourceFactory
29   * 
30   * @author Filip Hianik
31   * @since 1.2.1
32   * 
33   */
34  public final class JDODescriptorJClass extends JClass {
35  
36      /** 
37       * JDODescriptors extend this base class.
38       */
39      private static final String JDO_CLASS_DESCRIPTOR = 
40          "org.exolab.castor.mapping.loader.ClassDescriptorImpl";
41  
42      /** 
43       * The type being described by the Descriptor class we'll generate.
44       */
45      private final JClass _type;
46      
47      /** 
48       * Source Builder configuration.
49       */
50      private final BuilderConfiguration _config;
51  
52      /**
53       * Constructs a JDODescriptorJClass.
54       * 
55       * @param config
56       *                Builder Configuration
57       * @param className
58       *                name of this descriptor class
59       * @param type
60       *                the type that is described by this descriptor
61       */
62      public JDODescriptorJClass(final BuilderConfiguration config,
63              final String className, final JClass type) {
64          super(className);
65          this._config = config;
66          this._type = type;
67          init();
68      }
69  
70      /**
71       * Initializes this JDODescriptorJClass with the required methods.
72       */
73      private void init() {
74          // Make sure that the Descriptor is extended JDOClassDescriptor even
75          // when
76          // the user has specified a super class for all the generated classes
77          String superClass = null;
78          if (_config != null) {
79              superClass = _config.getProperty(
80                      BuilderConfiguration.Property.SUPER_CLASS, null);
81          }
82  
83          // boolean extended = false;
84  
85          if (_type.getSuperClassQualifiedName() == null
86                  || _type.getSuperClassQualifiedName().equals(superClass)) {
87              setSuperClass(JDO_CLASS_DESCRIPTOR);
88          } else {
89              if (_type.getSuperClass() == null) {
90                  setSuperClass(null);
91              } else {
92                  // extended = true;
93                  // setSuperClass(getSuperClassName());
94              }
95          }
96          superClass = null;
97  
98          if (_type.getPackageName() != null
99                  && _type.getPackageName().length() > 0) {
100             addImport(_type.getName());
101         }
102 
103         // -- add default imports
104         addImports();
105         // -- add default contructor
106         addDefaultConstructor();
107     }
108 
109     /**
110      * Adds our default imports.
111      */
112     private void addImports() {
113         addImport("org.exolab.castor.jdo.engine.nature.ClassDescriptorJDONature");
114         addImport("org.castor.jdo.engine.SQLTypeInfos");
115 //        addImport("org.exolab.castor.jdo.engine.JDOFieldDescriptor");
116 //        addImport("org.exolab.castor.jdo.engine.JDOFieldDescriptorImpl");
117         addImport("org.exolab.castor.mapping.AccessMode");
118         addImport("org.exolab.castor.mapping.FieldDescriptor");
119         addImport("org.exolab.castor.mapping.FieldHandler");
120         addImport("org.exolab.castor.mapping.MappingException");
121         addImport("org.exolab.castor.mapping.loader.FieldHandlerImpl");
122         addImport("org.exolab.castor.mapping.loader.TypeInfo");
123         addImport("org.exolab.castor.mapping.xml.ClassChoice");
124         addImport("org.exolab.castor.mapping.xml.ClassMapping");
125         addImport("org.exolab.castor.mapping.xml.FieldMapping");
126         addImport("org.exolab.castor.mapping.xml.MapTo");
127         addImport("org.exolab.castor.mapping.xml.Sql");
128         addImport("org.exolab.castor.mapping.xml.types.ClassMappingAccessType");
129         addImport("org.castor.core.exception.IllegalClassDescriptorInitialization");
130         addImport("org.exolab.castor.mapping.xml.types.FieldMappingCollectionType");
131         addImport("org.exolab.castor.mapping.loader.FieldDescriptorImpl");
132         addImport("org.exolab.castor.jdo.engine.nature.FieldDescriptorJDONature");
133         addImport("java.lang.reflect.Method");
134     }
135 
136     /**
137      * Adds our default constructor.
138      */
139     private void addDefaultConstructor() {
140         addConstructor(createConstructor());
141         JConstructor cons = getConstructor(0);
142         JSourceCode jsc = cons.getSourceCode();
143         jsc.add("super();");
144         jsc.add("ClassMapping mapping = new ClassMapping();");
145         jsc.add("ClassChoice choice = new ClassChoice();");
146         jsc.add("MapTo mapTo = new MapTo();");
147     }
148 }