View Javadoc
1   package org.exolab.castor.builder.factory;
2   
3   import org.castor.xml.JavaNaming;
4   import org.exolab.castor.builder.SGTypes;
5   import org.exolab.castor.builder.info.FieldInfo;
6   import org.exolab.castor.builder.info.nature.XMLInfoNature;
7   import org.exolab.javasource.JMethod;
8   import org.exolab.javasource.JParameter;
9   import org.exolab.javasource.JSourceCode;
10  import org.exolab.javasource.JType;
11  
12  /**
13   * This class translates a fieldInfo describing an identity into 
14   * the suitable getter<7setter methods.
15   */
16  public class IdentityMemberAndAccessorFactory extends FieldMemberAndAccessorFactory {
17  
18      /**
19       * Creates the IdentityMemberAndAccessorFactory.
20       * @param naming the javaNaming to use
21       */
22      public IdentityMemberAndAccessorFactory(final JavaNaming naming) {
23          super(naming);
24      }
25  
26      /**
27       * Creats the getter/setter and getReferenceId methods.
28       * 
29       * @param fieldInfo the fieldInfo to translate
30       * @return the created methods
31       */
32      public JMethod[] createAccessMethods(final FieldInfo fieldInfo) {
33              String mname = fieldInfo.getMethodSuffix();
34              JType jType = new XMLInfoNature(fieldInfo).getSchemaType().getJType();
35  
36              JMethod[] methods = new JMethod[3];
37              methods[0] = makeGetMethod(fieldInfo, mname, jType); // -- create get method
38              methods[1] = makeSetMethod(fieldInfo, mname, jType); // -- create set method
39              methods[2] = makeGetReferenceIdMethod(fieldInfo); // -- create getReferenceId
40                                                       // (from Referable Interface)
41  
42              return methods;
43          } // -- createAccessMethods
44  
45          /**
46           * Creates the getter method.
47           * 
48           * @param fieldInfo the fieldInfo to translate
49           * @param mname the name of this field
50           * @param jType the type of this field
51           * @return the getter method for this identity
52           */
53          private JMethod makeGetMethod(final FieldInfo fieldInfo, 
54                  final String mname, final JType jType) {
55              JMethod method = new JMethod("get" + mname, jType,
56                      "the value of field '" + mname + "'.");
57              JSourceCode jsc = method.getSourceCode();
58              jsc.add("return this.");
59              jsc.append(fieldInfo.getName());
60              jsc.append(";");
61              return method;
62          }
63  
64          /**
65           * Creates the setter method.
66           * 
67           * @param fieldInfo the fieldInfo to translate
68           * @param mname the name of this field
69           * @param jType the type of this field
70           * @return the setter method for this identity
71           */
72          private JMethod makeSetMethod(final FieldInfo fieldInfo, 
73                  final String mname, final JType jType) {
74              JMethod method = new JMethod("set" + mname);
75              method.addParameter(new JParameter(jType, fieldInfo.getName()));
76              JSourceCode jsc = method.getSourceCode();
77              jsc.add("this.");
78              jsc.append(fieldInfo.getName());
79              jsc.append(" = ");
80              jsc.append(fieldInfo.getName());
81              jsc.append(";");
82  
83              //-- add resolver registration
84              //jsc.add("if (idResolver != null) ");
85              //jsc.indent();
86              //jsc.add("idResolver.addResolvable(");
87              //jsc.append(fieldInfo.getName());
88              //jsc.append(", this);");
89              //jsc.unindent();
90  
91              return method;
92          }
93  
94          /**
95           * Creates the getReferenceId method.
96           * @param fieldInfo the fieldInfo to translate
97           * @return the getReferenceId method.
98           */
99          private JMethod makeGetReferenceIdMethod(final FieldInfo fieldInfo) {
100             JMethod method = new JMethod("getReferenceId", SGTypes.STRING,
101                     "the reference ID");
102             JSourceCode jsc = method.getSourceCode();
103             jsc.add("return this.");
104             jsc.append(fieldInfo.getName());
105             jsc.append(";");
106             return method;
107         }
108 
109 
110 }