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