1 package org.exolab.castor.mapping.loader;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 import org.exolab.castor.mapping.ClassDescriptor;
7 import org.exolab.castor.mapping.FieldDescriptor;
8 import org.exolab.castor.mapping.MappingException;
9
10 public class ClassDescriptorHelper {
11
12 /**
13 * Get all the {@link FieldDescriptor}s for non-identity fields, including
14 * all the fields in base classes.
15 *
16 * @param classDescriptor
17 * ClassMapping instance.
18 * @return An array.
19 * @throws MappingException
20 */
21 public static FieldDescriptor[] getFullFields(final ClassDescriptor classDescriptor)
22 throws MappingException {
23 ClassDescriptor baseClassDescriptor = classDescriptor.getExtends();
24 ArrayList<FieldDescriptor> fullFields = new ArrayList<FieldDescriptor>();
25
26 if (baseClassDescriptor != null) {
27 ClassDescriptor origin = baseClassDescriptor;
28
29 while (origin.getExtends() != null) {
30 origin = origin.getExtends();
31 }
32
33 // recursive call to obtain full fields for base class.
34 FieldDescriptor[] fullBaseFieldDescriptors = getFullFields(baseClassDescriptor);
35 FieldDescriptor[] currentFields = classDescriptor.getFields();
36
37 // add all base field descriptors
38 for (FieldDescriptor baseFieldDescriptor : fullBaseFieldDescriptors) {
39 fullFields.add(baseFieldDescriptor);
40 }
41 // add all fields of the current class
42 for (FieldDescriptor currentFieldDescriptor : currentFields) {
43 fullFields.add(currentFieldDescriptor);
44 }
45 } else {
46 FieldDescriptor[] fieldDescriptors = ((ClassDescriptorImpl) classDescriptor).getFields();
47 fullFields.addAll(Arrays.asList(fieldDescriptors));
48 }
49
50 return fullFields.toArray(new FieldDescriptor[fullFields.size()]);
51 }
52
53
54 /**
55 * Get the all the id fields of a class
56 * If the class, C, is a dependent class, then
57 * the depended class', D, id fields will be
58 * appended at the back and returned.
59 * If the class is an extended class, the id
60 * fields of the extended class will be returned.
61 */
62 public static FieldDescriptor[] getIdFields(final ClassDescriptor classDescriptor)
63 throws MappingException {
64
65 // start with the extended class
66 ClassDescriptor base = classDescriptor;
67 while (base.getExtends() != null) {
68 base = base.getExtends();
69 }
70
71 // fmDepended = null;
72
73 FieldDescriptor[] identities = ((ClassDescriptorImpl) base).getIdentities();
74
75 if (identities == null || identities.length == 0) {
76 throw new MappingException("Identity is null!");
77 }
78
79 // //INBESTIGATE[WG]: what's the use fo this code
80 // fmIds = new FieldMapping[identities.length];
81 // fmBase = base.getClassChoice().getFieldMapping();
82 // for (int i = 0; i < fmBase.length; i++) {
83 // for (int k = 0; k < identities.length; k++) {
84 // if (fmBase[i].getName().equals(identities[k])) {
85 // fmIds[k] = fmBase[i];
86 // break;
87 // }
88 // }
89 // }
90 // if (fmDepended == null) {
91 return identities;
92 // }
93
94 //TODO[INVESTIGATE]: look at this dead code
95 // // join depend ids and class id
96 // fmResult = new FieldMapping[fmDepended.length + identities.length];
97 // System.arraycopy(fmIds, 0, fmResult, 0, fmIds.length);
98 // System.arraycopy(fmDepended, 0, fmResult, fmIds.length,
99 // fmDepended.length);
100 // return fmIds;
101 }
102
103 }
104
105