1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.apache.jdo.impl.model.jdo.xml;
26
27 import java.util.*;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import org.apache.jdo.impl.model.jdo.JDOClassImplDynamic;
33 import org.apache.jdo.model.ModelException;
34 import org.apache.jdo.model.jdo.JDOArray;
35 import org.apache.jdo.model.jdo.JDOClass;
36 import org.apache.jdo.model.jdo.JDOCollection;
37 import org.apache.jdo.model.jdo.JDOElement;
38 import org.apache.jdo.model.jdo.JDOExtension;
39 import org.apache.jdo.model.jdo.JDOField;
40 import org.apache.jdo.model.jdo.JDOIdentityType;
41 import org.apache.jdo.model.jdo.JDOMap;
42 import org.apache.jdo.model.jdo.JDOModel;
43 import org.apache.jdo.model.jdo.JDOPackage;
44 import org.apache.jdo.model.jdo.NullValueTreatment;
45 import org.apache.jdo.model.jdo.PersistenceModifier;
46 import org.apache.jdo.util.I18NHelper;
47 import org.xml.sax.*;
48
49 /***
50 *
51 * TBD:
52 * <ul>
53 * <li> Reading persistence capable superclass entry: check already existing
54 * superclass entry for consistency
55 * <li> Only populate requested class info, not the entire .jdo file
56 */
57 public class JDOHandlerImpl
58 implements JDOHandler
59 {
60 /*** */
61 private final JDOModel model;
62
63 /*** */
64 private final Stack context;
65
66 /***
67 * Flag indicating that the current entries should no be loaded,
68 * because JDO metadata has been loaded before for the current
69 * persistence capable class.
70 */
71 private boolean skipXMLElements;
72
73 /*** */
74 private final Collection handledJDOClasses;
75
76 /*** I18N support. */
77 private static final I18NHelper msg = I18NHelper.getInstance(
78 "org.apache.jdo.impl.model.jdo.Bundle",
79 JDOHandlerImpl.class.getClassLoader());
80
81 /*** Logger */
82 private static Log logger = LogFactory.getFactory().getInstance(
83 "org.apache.jdo.impl.model.jdo.xml");
84
85 /***
86 *
87 */
88 public JDOHandlerImpl (JDOModel model)
89 {
90 this.model = model;
91 this.context = new Stack();
92 this.skipXMLElements = false;
93 this.handledJDOClasses = new HashSet();
94 }
95
96 /***
97 *
98 */
99 public void start_jdo(final Attributes meta)
100 {
101 if (logger.isTraceEnabled())
102 logger.trace(" <jdo>");
103
104 context.push(model);
105 }
106
107 /***
108 *
109 */
110 public void end_jdo()
111 {
112 if (logger.isTraceEnabled())
113 logger.trace(" </jdo>");
114
115 context.pop();
116 }
117
118 /***
119 *
120 */
121 public void start_package(final Attributes meta)/package-summary.html">ong> void start_package(final Attributes meta)
122 throws SAXException
123 {
124 boolean trace = logger.isTraceEnabled();
125 if (trace)
126 logger.trace(" <package>");
127 JDOPackage jdoPackage = null;
128 try {
129
130 JDOModel model = (JDOModel)context.peek();
131 String packageName = meta.getValue("", "name");
132 >if ((packageName == null) || packageName.length() == 0)
133 packageName = "";
134 if (trace)
135 logger.trace(" name = " + packageName);
136 jdoPackage = model.createJDOPackage(packageName);
137 }
138 catch (ModelException ex) {
139 SAXException e =
140 new SAXException(msg.msg("EXC_ModelException"), ex);
141 if (trace)
142 logger.trace("Throwing exception in " +
143 "JDOHandlerImpl.start_package:", e);
144 throw e;
145 }
146
147
148 context.push(jdoPackage);
149 }
150
151 /***
152 *
153 */
154 public void end_package()/package-summary.html">ong> void end_package()
155 {
156 if (logger.isTraceEnabled())
157 logger.trace(" </package>");
158
159 context.pop();
160 }
161
162 /***
163 *
164 */
165 public void start_class(final Attributes meta)
166 throws SAXException
167 {
168 boolean trace = logger.isTraceEnabled();
169 if (trace)
170 logger.trace(" <class>");
171 JDOClass jdoClass = null;
172 try {
173
174
175 JDOPackage jdoPackage = (JDOPackage)context.peek();
176 String packageName = jdoPackage.getName();
177 String className = meta.getValue("", "name");
178 if ((packageName != null) && (packageName.length() > 0))
179 className = packageName + "." + className;
180 jdoClass = model.createJDOClass(className, false);
181 skipXMLElements = jdoClass.isXMLMetadataLoaded();
182 if (skipXMLElements) {
183 if (trace)
184 logger.trace(
185 " JDO metadata already loaded for class " +
186 className + ", skipping class element");
187 return;
188 }
189 for ( int i = 0; i < meta.getLength(); i++ ) {
190 String name = meta.getLocalName(i);
191 String value = meta.getValue(i);
192 if (trace)
193 logger.trace(" " + name + " = " + value);
194 if ("name".equals(name)) {
195
196 }
197 else if ("identity-type".equals(name)) {
198 jdoClass.setIdentityType(
199 JDOIdentityType.toJDOIdentityType(value));
200 }
201 else if ("objectid-class".equals(name)) {
202 jdoClass.setDeclaredObjectIdClassName(value);
203 }
204 else if ("requires-extent".equals(name)) {
205 jdoClass.setRequiresExtent(
206 Boolean.valueOf(value).booleanValue());
207 }
208 else if ("persistence-capable-superclass".equals(name)) {
209
210
211 if (jdoClass.getPersistenceCapableSuperclassName() == null) {
212 jdoClass.setPersistenceCapableSuperclassName(value);
213 }
214 }
215 else {
216
217
218
219
220
221
222
223
224
225
226 if (trace)
227 logger.trace(msg.msg("EXC_UnknownAttribute",
228 "<class>", name, value));
229 }
230 }
231 }
232 catch (ModelException ex) {
233 SAXException e =
234 new SAXException(msg.msg("EXC_ModelException"), ex);
235 if (trace)
236 logger.trace("Throwing exception in " +
237 "JDOHandlerImpl.start_class:", e);
238 throw e;
239 }
240
241 handledJDOClasses.add(jdoClass);
242
243
244 context.push(jdoClass);
245 }
246
247 /***
248 *
249 */
250 public void end_class()
251 {
252 if (logger.isTraceEnabled())
253 logger.trace(" </class>");
254 if (skipXMLElements) {
255
256 skipXMLElements = false;
257 }
258 else {
259
260 JDOClass jdoClass = (JDOClass)context.pop();
261
262 jdoClass.setXMLMetadataLoaded();
263 }
264 }
265
266 /***
267 *
268 */
269 public void start_field(final Attributes meta)
270 throws SAXException
271 {
272 if (skipXMLElements)
273 return;
274 boolean trace = logger.isTraceEnabled();
275 if (trace)
276 logger.trace(" <field>");
277 JDOField jdoField = null;
278 try {
279
280 JDOClass jdoClass = (JDOClass)context.peek();
281 String fieldName = meta.getValue("", "name");
282 jdoField = jdoClass.createJDOField(fieldName);
283 for (int i = 0; i < meta.getLength(); i++ ) {
284 String name = meta.getLocalName(i);
285 String value = meta.getValue(i);
286 if (trace)
287 logger.trace(" " + name + " = " + value);
288 if ("name".equals(name)) {
289
290 }
291 else if ("persistence-modifier".equals(name)) {
292 int modifier =
293 PersistenceModifier.toPersistenceModifier(value);
294 jdoField.setPersistenceModifier(modifier);
295 }
296 else if ("primary-key".equals(name)) {
297 jdoField.setPrimaryKey(
298 Boolean.valueOf(value).booleanValue());
299 }
300 else if ("null-value".equals(name)) {
301 jdoField.setNullValueTreatment(
302 NullValueTreatment.toNullValueTreatment(value));
303 }
304 else if ("default-fetch-group".equals(name)) {
305 jdoField.setDefaultFetchGroup(
306 Boolean.valueOf(value).booleanValue());
307 }
308 else if ("embedded".equals(name)) {
309 jdoField.setEmbedded(
310 Boolean.valueOf(value).booleanValue());
311 }
312 else {
313
314
315
316
317
318
319
320
321
322
323 if (trace)
324 logger.trace(msg.msg("EXC_UnknownAttribute",
325 "<field>", name, value));
326 }
327 }
328 }
329 catch (ModelException ex) {
330 SAXException e =
331 new SAXException(msg.msg("EXC_ModelException"), ex);
332 if (trace)
333 logger.trace("Throwing exception in " +
334 "JDOHandlerImpl.start_field:", e);
335 throw e;
336 }
337
338
339 context.push(jdoField);
340 }
341
342 /***
343 *
344 */
345 public void end_field()
346 {
347 if (skipXMLElements)
348 return;
349 if (logger.isTraceEnabled())
350 logger.trace(" </field>");
351
352 context.pop();
353 }
354
355 /***
356 *
357 */
358 public void start_collection(final Attributes meta)
359 throws SAXException
360 {
361 if (skipXMLElements)
362 return;
363
364 boolean trace = logger.isTraceEnabled();
365 if (trace)
366 logger.trace(" <collection>");
367 JDOCollection jdoCollection = null;
368 try {
369
370 JDOField jdoField = (JDOField)context.peek();
371 jdoCollection = jdoField.createJDOCollection();
372 for (int i = 0; i < meta.getLength(); i++ ) {
373 String name = meta.getLocalName(i);
374 String value = meta.getValue(i);
375 if (trace)
376 logger.trace(" " + name + " = " + value);
377 if ("element-type".equals(name)) {
378 jdoCollection.setElementTypeName(value);
379 }
380 else if ("embedded-element".equals(name)) {
381 jdoCollection.setEmbeddedElement(
382 Boolean.valueOf(value).booleanValue());
383 }
384 else {
385
386
387
388
389
390
391
392
393
394
395 if (trace)
396 logger.trace(msg.msg("EXC_UnknownAttribute",
397 "<collection>", name, value));
398 }
399 }
400 }
401 catch (ModelException ex) {
402 SAXException e =
403 new SAXException(msg.msg("EXC_ModelException"), ex);
404 if (trace)
405 logger.trace("Throwing exception in " +
406 "JDOHandlerImpl.start_collection:", e);
407 throw e;
408 }
409
410
411 context.push(jdoCollection);
412 }
413
414 /***
415 *
416 */
417 public void end_collection()
418 {
419 if (skipXMLElements)
420 return;
421 if (logger.isTraceEnabled())
422 logger.trace(" </collection>");
423
424 context.pop();
425 }
426
427 /***
428 *
429 */
430 public void start_array(final Attributes meta)
431 throws SAXException
432 {
433 if (skipXMLElements)
434 return;
435
436 boolean trace = logger.isTraceEnabled();
437 if (trace)
438 logger.trace(" <array>");
439 JDOArray jdoArray = null;
440 try {
441
442 JDOField jdoField = (JDOField)context.peek();
443 jdoArray = jdoField.createJDOArray();
444 for (int i = 0; i < meta.getLength(); i++ ) {
445 String name = meta.getLocalName(i);
446 String value = meta.getValue(i);
447 if (trace)
448 logger.trace(" " + name + " = " + value);
449 if ("embedded-element".equals(name)) {
450 jdoArray.setEmbeddedElement(
451 Boolean.valueOf(value).booleanValue());
452 }
453 else {
454
455
456
457
458
459
460
461
462
463
464 if (trace)
465 logger.trace(msg.msg("EXC_UnknownAttribute",
466 "<array>", name, value));
467 }
468 }
469 }
470 catch (ModelException ex) {
471 SAXException e =
472 new SAXException(msg.msg("EXC_ModelException"), ex);
473 if (trace)
474 logger.trace("Throwing exception in " +
475 "JDOHandlerImpl.start_array:", e);
476 throw e;
477 }
478
479
480 context.push(jdoArray);
481 }
482
483 /***
484 *
485 */
486 public void end_array()
487 {
488 if (skipXMLElements)
489 return;
490 if (logger.isTraceEnabled())
491 logger.trace(" </array>");
492
493 context.pop();
494 }
495
496 /***
497 *
498 */
499 public void start_map(final Attributes meta)
500 throws SAXException
501 {
502 if (skipXMLElements)
503 return;
504 boolean trace = logger.isTraceEnabled();
505 if (trace)
506 logger.trace(" <map>");
507 JDOMap jdoMap = null;
508 try {
509
510 JDOField jdoField = (JDOField)context.peek();
511 jdoMap = jdoField.createJDOMap();
512 for (int i = 0; i < meta.getLength(); i++ ) {
513 String name = meta.getLocalName(i);
514 String value = meta.getValue(i);
515 if (trace)
516 logger.trace(" " + name + " = " + value);
517 if ("key-type".equals(name)) {
518 jdoMap.setKeyTypeName(value);
519 }
520 else if ("embedded-key".equals(name)) {
521 jdoMap.setEmbeddedKey(
522 Boolean.valueOf(value).booleanValue());
523 }
524 else if ("value-type".equals(name)) {
525 jdoMap.setValueTypeName(value);
526 }
527 else if ("embedded-value".equals(name)) {
528 jdoMap.setEmbeddedValue(
529 Boolean.valueOf(value).booleanValue());
530 }
531 else {
532
533
534
535
536
537
538
539
540
541
542 if (trace)
543 logger.trace(msg.msg("EXC_UnknownAttribute",
544 "<map>", name, value));
545 }
546 }
547 }
548 catch (ModelException ex) {
549 SAXException e =
550 new SAXException(msg.msg("EXC_ModelException"), ex);
551 if (trace)
552 logger.trace("Throwing exception in " +
553 "JDOHandlerImpl.start_map:", e);
554 throw e;
555 }
556
557
558 context.push(jdoMap);
559 }
560
561 /***
562 *
563 */
564 public void end_map()
565 {
566 if (skipXMLElements)
567 return;
568 if (logger.isTraceEnabled())
569 logger.trace(" </map>");
570
571 context.pop();
572 }
573
574 /***
575 *
576 */
577 public void start_extension(final Attributes meta)
578 throws SAXException
579 {
580 if (skipXMLElements)
581 return;
582 boolean trace = logger.isTraceEnabled();
583
584 if (trace)
585 logger.trace(" <extension>");
586 JDOExtension jdoExtension = null;
587 try {
588
589 JDOElement jdoElement = (JDOElement)context.peek();
590 jdoExtension = jdoElement.createJDOExtension();
591 for ( int i = 0; i < meta.getLength(); i++ ) {
592 String name = meta.getLocalName(i);
593 String value = meta.getValue(i);
594
595 if (trace)
596 logger.trace(" " + name + " = " + value);
597 if ("vendor-name".equals(name)) {
598 jdoExtension.setVendorName(value);
599 }
600 else if ("key".equals(name)) {
601 jdoExtension.setKey(value);
602 }
603 else if ("value".equals(name)) {
604 jdoExtension.setValue(value);
605 }
606 else {
607
608
609
610
611
612
613
614
615
616
617 if (trace)
618 logger.trace(msg.msg("EXC_UnknownAttribute",
619 "<extension>", name, value));
620 }
621 }
622 }
623 catch (ModelException ex) {
624 SAXException e =
625 new SAXException(msg.msg("EXC_ModelException"), ex);
626 if (trace)
627 logger.trace("Throwing exception in " +
628 "JDOHandlerImpl.start_extension:", e);
629 throw e;
630 }
631 }
632
633 /***
634 *
635 */
636 public void end_extension()
637 {
638 if (skipXMLElements)
639 return;
640 if (logger.isTraceEnabled())
641 logger.trace(" </extension>");
642
643 }
644
645 /***
646 *
647 */
648 public Collection handledJDOClasses()
649 {
650 return handledJDOClasses;
651 }
652
653 }