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