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