1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /***
18 * PrintSupport.java
19 *
20 */
21
22 package org.apache.jdo.impl.model.jdo.util;
23
24 import java.util.*;
25
26 import org.apache.jdo.model.jdo.JDOArray;
27 import org.apache.jdo.model.jdo.JDOClass;
28 import org.apache.jdo.model.jdo.JDOCollection;
29 import org.apache.jdo.model.jdo.JDOExtension;
30 import org.apache.jdo.model.jdo.JDOField;
31 import org.apache.jdo.model.jdo.JDOIdentityType;
32 import org.apache.jdo.model.jdo.JDOMap;
33 import org.apache.jdo.model.jdo.JDOModel;
34 import org.apache.jdo.model.jdo.JDOReference;
35 import org.apache.jdo.model.jdo.JDORelationship;
36 import org.apache.jdo.model.jdo.NullValueTreatment;
37 import org.apache.jdo.model.jdo.PersistenceModifier;
38
39 public class PrintSupport
40 {
41 public static void printJDOModel(JDOModel jdoModel)
42 {
43 JDOClass[] jdoClasses = jdoModel.getDeclaredClasses();
44 for (int i = 0; i < jdoClasses.length; i++) {
45 printJDOClass(0, jdoClasses[i]);
46 }
47 }
48
49 public static void printJDOClass(JDOClass jdoClass)
50 {
51 printJDOClass(0, jdoClass);
52 }
53
54 public static void printJDOFields(JDOField[] jdoFields)
55 {
56 printJDOFields(0, jdoFields);
57 }
58
59 public static void printJDOField(JDOField jdoField)
60 {
61 printJDOField(0, jdoField);
62 }
63
64 public static void printJDORelationship(JDORelationship jdoRelationship)
65 {
66 printJDORelationship(0, jdoRelationship);
67 }
68
69 public static void printJDOExtensions(JDOExtension[] jdoExtensions)
70 {
71 printJDOExtensions(0, jdoExtensions);
72 }
73
74 public static void printJDOExtension(JDOExtension jdoExtension)
75 {
76 printJDOExtension(0, jdoExtension);
77 }
78
79
80
81 private static void printJDOClass(int indent, JDOClass jdoClass)
82 {
83 if (jdoClass == null)
84 return;
85
86 println(indent, "--> JDOClass ");
87 println(indent+1, "name = " + jdoClass.getName());
88 println(indent+1, "shortName = " + jdoClass.getShortName());
89 println(indent+1, "packagePrefix = " + jdoClass.getPackagePrefix());
90 println(indent+1, "identityType = " + JDOIdentityType.toString(jdoClass.getIdentityType()));
91 println(indent+1, "objectIdClass = " + jdoClass.getObjectIdClass());
92 println(indent+1, "declaredObjectIdClassName = " + jdoClass.getDeclaredObjectIdClassName());
93 println(indent+1, "requiresExtent = " + jdoClass.requiresExtent());
94 println(indent+1, "pcSuperclassName = " + jdoClass.getPersistenceCapableSuperclassName());
95 println(indent+1, "pcSuperclass = " + jdoClass.getPersistenceCapableSuperclass());
96 println(indent+1, "pcRootClass = " + jdoClass.getPersistenceCapableRootClass());
97 println(indent+1, "javaType = " + jdoClass.getJavaType());
98 println(indent+1, "declaredManagedFieldCount = " + jdoClass.getDeclaredManagedFieldCount());
99 println(indent+1, "inheritedManagedFieldCount = " + jdoClass.getInheritedManagedFieldCount());
100 println(indent+1, "managedFields = " + Arrays.asList(jdoClass.getManagedFields()));
101 println(indent+1, "managedFieldNumbers = " + asList(jdoClass.getManagedFieldNumbers()));
102 println(indent+1, "persistentFieldNumbers = " + asList(jdoClass.getPersistentFieldNumbers()));
103 println(indent+1, "primaryKeyFieldNumbers = " + asList(jdoClass.getPrimaryKeyFieldNumbers()));
104 println(indent+1, "persistentNonPKFieldNs = " + asList(jdoClass.getPersistentNonPrimaryKeyFieldNumbers()));
105 println(indent+1, "persistentRelshipFieldNs = " + asList(jdoClass.getPersistentRelationshipFieldNumbers()));
106 println(indent+1, "persistentSerializableFNs = " + asList(jdoClass.getPersistentSerializableFieldNumbers()));
107 println(indent+1, "declaredFields");
108 printJDOFields(indent+1, jdoClass.getDeclaredFields());
109 printJDOExtensions(indent+1, jdoClass.getJDOExtensions());
110 println(0, "<-- JDOClass\n ");
111 }
112
113 private static void printJDOFields(int indent, JDOField[] jdoFields) {
114 if ((jdoFields != null) && (jdoFields.length > 0)) {
115 TreeSet sorted = new TreeSet(new Comparator() {
116 public int compare(Object o1, Object o2) {
117 return (((JDOField)o1).getFieldNumber()
118 - ((JDOField)o2).getFieldNumber());
119 }
120 public boolean equals(Object obj) {
121 return obj.equals(this);
122 }
123 });
124
125 for (int i = 0; i < jdoFields.length; i++) {
126 sorted.add(jdoFields[i]);
127 }
128
129 for (Iterator i = sorted.iterator(); i.hasNext();) {
130 printJDOField(indent, (JDOField)i.next());
131 }
132 }
133 }
134
135 private static void printJDOField(int indent, JDOField jdoField)
136 {
137 if (jdoField == null)
138 return;
139 boolean isProperty = jdoField.isProperty();
140
141 if (isProperty)
142 println(indent, "--> JDOProperty");
143 else
144 println(indent, "--> JDOField");
145 println(indent+1, "name = " + jdoField.getName());
146 println(indent+1, "declaringClass = " + jdoField.getDeclaringClass().getName());
147 println(indent+1, "persistenceModifier = " + PersistenceModifier.toString(jdoField.getPersistenceModifier()));
148 println(indent+1, "primaryKey = " + jdoField.isPrimaryKey());
149 println(indent+1, "nullValue = " + NullValueTreatment.toString(jdoField.getNullValueTreatment()));
150 println(indent+1, "defaultFetchGroup = " + jdoField.isDefaultFetchGroup());
151 println(indent+1, "embedded = " + jdoField.isEmbedded());
152 println(indent+1, "type = " + jdoField.getType());
153
154 println(indent+1, "javaField = " + jdoField.getJavaField());
155 println(indent+1, "serializable = " + jdoField.isSerializable());
156 println(indent+1, "mappedByName = " + jdoField.getMappedByName());
157 println(indent+1, "fieldNumber = " + jdoField.getFieldNumber());
158 println(indent+1, "relativeFieldNumber = " + jdoField.getRelativeFieldNumber());
159 println(indent+1, "isProperty = " + jdoField.isProperty());
160 printJDORelationship(indent+1, jdoField.getRelationship());
161 printJDOExtensions(indent+1, jdoField.getJDOExtensions());
162 if (isProperty)
163 println(indent, "<-- JDOProperty ");
164 else
165 println(indent, "<-- JDOField ");
166 }
167
168 private static void printJDORelationship(int indent,
169 JDORelationship jdoRelationship)
170 {
171 if (jdoRelationship == null)
172 return;
173
174 if (jdoRelationship.isJDOReference())
175 printJDOReference(indent, (JDOReference)jdoRelationship);
176 else if (jdoRelationship.isJDOCollection())
177 printJDOCollection(indent, (JDOCollection)jdoRelationship);
178 else if (jdoRelationship.isJDOArray())
179 printJDOArray(indent, (JDOArray)jdoRelationship);
180 else if (jdoRelationship.isJDOMap())
181 printJDOMap(indent, (JDOMap)jdoRelationship);
182 }
183
184 private static void printJDORelationshipHelper(int indent, JDORelationship jdoRelationship)
185 {
186 if (jdoRelationship == null)
187 return;
188
189 JDORelationship mappedBy = jdoRelationship.getMappedBy();
190 JDORelationship inverse = jdoRelationship.getInverseRelationship();
191 println(indent+1, "declaringField = " + jdoRelationship.getDeclaringField().getName());
192 println(indent+1, "bounds = " + jdoRelationship.getLowerBound() + " / " + jdoRelationship.getUpperBound());
193 println(indent+1, "mappedBy = " + ((mappedBy==null) ? "null" :
194 mappedBy.getDeclaringField().getDeclaringClass().getName() + "." +
195 mappedBy.getDeclaringField().getName()));
196 println(indent+1, "inverseName = " + jdoRelationship.getInverseRelationshipName());
197 println(indent+1, "inverse = " + ((inverse==null) ? "null" :
198 inverse.getDeclaringField().getDeclaringClass().getName() + "." +
199 inverse.getDeclaringField().getName()));
200 }
201
202 private static void printJDOReference(int indent, JDOReference jdoReference)
203 {
204 if (jdoReference == null)
205 return;
206
207 println(indent, "--> JDOReference");
208 printJDORelationshipHelper(indent, jdoReference);
209 printJDOExtensions(indent+1, jdoReference.getJDOExtensions());
210 println(indent, "<-- JDOReference");
211 }
212
213 private static void printJDOCollection(int indent, JDOCollection jdoCollection)
214 {
215 if (jdoCollection == null)
216 return;
217
218 println(indent, "--> JDOCollection");
219 printJDORelationshipHelper(indent, jdoCollection);
220 println(indent+1, "embeddedElement = " + jdoCollection.isEmbeddedElement());
221 println(indent+1, "elementType = " + jdoCollection.getElementType());
222 println(indent+1, "elementTypeName = " + jdoCollection.getElementTypeName());
223 printJDOExtensions(indent+1, jdoCollection.getJDOExtensions());
224 println(indent, "<-- JDOCollection");
225 }
226
227 private static void printJDOArray(int indent, JDOArray jdoArray)
228 {
229 if (jdoArray == null)
230 return;
231
232 println(indent, "--> JDOArray");
233 printJDORelationshipHelper(indent, jdoArray);
234 println(indent+1, "embeddedElement = " + jdoArray.isEmbeddedElement());
235 printJDOExtensions(indent+1, jdoArray.getJDOExtensions());
236 println(indent, "<-- JDOArray");
237 }
238
239 private static void printJDOMap(int indent, JDOMap jdoMap)
240 {
241 if (jdoMap == null)
242 return;
243
244 println(indent, "--> JDOMap");
245 printJDORelationshipHelper(indent, jdoMap);
246 println(indent+1, "embeddedKey = " + jdoMap.isEmbeddedKey());
247 println(indent+1, "keyType = " + jdoMap.getKeyType());
248 println(indent+1, "keyTypeName = " + jdoMap.getKeyTypeName());
249 println(indent+1, "embeddedValue = " + jdoMap.isEmbeddedValue());
250 println(indent+1, "valueType = " + jdoMap.getValueType());
251 println(indent+1, "valueTypeName = " + jdoMap.getValueTypeName());
252 printJDOExtensions(indent+1, jdoMap.getJDOExtensions());
253 println(indent, "<-- JDOMap");
254 }
255
256 private static void printJDOExtensions(int indent, JDOExtension[] jdoExtensions)
257 {
258 if ((jdoExtensions != null) && (jdoExtensions.length > 0)) {
259 TreeSet sorted = new TreeSet(new Comparator() {
260 public int compare(Object o1, Object o2) {
261 return ((JDOExtension)o1).getKey().compareTo(
262 ((JDOExtension)o2).getKey());
263 }
264 public boolean equals(Object obj) {
265 return obj.equals(this);
266 }
267 });
268
269 for (int i = 0; i < jdoExtensions.length; i++) {
270 sorted.add(jdoExtensions[i]);
271 }
272
273 for (Iterator i = sorted.iterator(); i.hasNext();) {
274 printJDOExtension(indent, (JDOExtension)i.next());
275 }
276 }
277 }
278
279 private static void printJDOExtension(int indent, JDOExtension jdoExtension)
280 {
281 if (jdoExtension == null)
282 return;
283
284 println(indent, "--> JDOExtension");
285 println(indent+1, "vendorName = " + jdoExtension.getVendorName());
286 println(indent+1, "key = " + jdoExtension.getKey());
287 println(indent+1, "value = " + jdoExtension.getValue());
288 println(indent, "<-- JDOExtension");
289 }
290
291
292
293 static void println(int indent, String text) {
294 for (int i = 0; i < indent; i++) {
295 System.out.print(" ");
296 }
297 System.out.println(text);
298 }
299
300 public static List asList(int[] array)
301 {
302 int length = (array == null) ? 0 : array.length;
303 List list = new ArrayList(length);
304 for (int i = 0; i < length; i++) {
305 list.add(new Integer(array[i]));
306 }
307 return list;
308 }
309 }