View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  /*
18   * File:           JDOHandlerImpl.java
19   * Date:           July 3, 2001  2:16 PM
20   *
21   * @author  michael
22   * @version generated by FFJ XML module
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", //NOI18N
78          JDOHandlerImpl.class.getClassLoader());
79  
80      /*** Logger */
81      private static Log logger = LogFactory.getFactory().getInstance(
82          "org.apache.jdo.impl.model.jdo.xml"); // NOI18N
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>"); //NOI18N
102         // push current JDOModel on context stack
103         context.push(model);
104     }
105     
106     /***
107      * 
108      */
109     public void end_jdo() 
110     {
111         if (logger.isTraceEnabled()) 
112             logger.trace("  </jdo>"); //NOI18N
113         // remove JDOModel fom context stack
114         context.pop();
115     }
116     
117     /***
118      * 
119      */
120     publicong> void start_package(final Attributes meta) 
121         throws SAXException 
122     {
123         boolean trace = logger.isTraceEnabled();
124         if (trace) 
125             logger.trace("  <package>"); //NOI18N
126         JDOPackage jdoPackage = null;
127         try {
128             // get JDOModel from context stack
129             JDOModel model = (JDOModel)context.peek();
130             String packageName = meta.getValue("", "name"); //NOI18N
131             >if ((packageName == null) || packageName.length() == 0)
132                 packageName = ""; //NOI18N
133             if (trace)
134                 logger.trace("    name = " + packageName); //NOI18N
135             jdoPackage = model.createJDOPackage(packageName);
136         }
137         catch (ModelException ex) {
138             SAXException e = 
139                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
140             if (trace)
141                 logger.trace("Throwing exception in " + //NOI18N
142                              "JDOHandlerImpl.start_package:", e); //NOI18N
143             throw e;
144         }
145         
146         // push current JDOPackage on context stack
147         context.push(jdoPackage);
148     }
149     
150     /***
151      * 
152      */
153     publicong> void end_package() 
154     {
155         if (logger.isTraceEnabled()) 
156             logger.trace("  </package>"); //NOI18N
157         // remove JDOPackage fom context stack
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>"); //NOI18N
170         JDOClass jdoClass = null;
171         try {
172             // get JDOPackage from context stack
173             //String packageName = (String)context.peek();
174             JDOPackage jdoPackage = (JDOPackage)context.peek();
175             String packageName = jdoPackage.getName();
176             String className = meta.getValue("", "name"); //NOI18N
177             if ((packageName != null) && (packageName.length() > 0))
178                 className = packageName + "." + className; //NOI18N
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 " + //NOI18N
185                         className + ", skipping class element"); //NOI18N
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); //NOI18N
193                 if ("name".equals(name)) { //NOI18N
194                     // name is already set during create => do nothing 
195                 }
196                 else if ("identity-type".equals(name)) { //NOI18N
197                     jdoClass.setIdentityType(
198                         JDOIdentityType.toJDOIdentityType(value));
199                 }
200                 else if ("objectid-class".equals(name)) { //NOI18N
201                     jdoClass.setDeclaredObjectIdClassName(value);
202                 }
203                 else if ("requires-extent".equals(name)) { //NOI18N
204                     jdoClass.setRequiresExtent(
205                         Boolean.valueOf(value).booleanValue());
206                 }
207                 else if ("persistence-capable-superclass".equals(name)) { //NOI18N
208                     // Do not overwrite existing entry
209                     // TBD check old and new entry for consistency
210                     if (jdoClass.getPersistenceCapableSuperclassName() == null) {
211                         jdoClass.setPersistenceCapableSuperclassName(value);
212                     }
213                 }
214                 else {
215                     /* JDO2 metadata not yet fully supported => 
216                        do not throw exception now
217                     SAXException e = new SAXException(
218                         msg.msg("EXC_UnknownAttribute", "<class>", //NOI18N
219                                 name, value)); 
220                     if (trace)
221                         logger.trace("Throwing exception in " +  //NOI18N
222                                      "JDOHandlerImpl.start_class:", e); //NOI18N
223                     throw e;
224                     */
225                     if (trace)
226                         logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
227                                              "<class>", name, value)); //NOI18N
228                 }
229             }
230         }
231         catch (ModelException ex) {
232             SAXException e = 
233                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
234             if (trace)
235                 logger.trace("Throwing exception in " +  //NOI18N
236                              "JDOHandlerImpl.start_class:", e); //NOI18N
237             throw e;
238         }
239         // store current jdoClass in handledJDOClasses
240         handledJDOClasses.add(jdoClass);
241 
242         // push current JDOClass object on context stack
243         context.push(jdoClass);
244     }
245     
246     /***
247      * 
248      */
249     public void end_class()  
250     {
251         if (logger.isTraceEnabled()) 
252             logger.trace("  </class>"); //NOI18N
253         if (skipXMLElements) {
254             // set flag to false to allow next class entry to be populated
255             skipXMLElements = false;
256         }
257         else {
258             // remove JDOClass fom context stack
259             JDOClass jdoClass = (JDOClass)context.pop();
260             // set jdoClass' xmlMetadataLoaded flag
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>"); //NOI18N
276         JDOField jdoField = null;
277         try {
278             // get the current JDOClass from context stack
279             JDOClass jdoClass = (JDOClass)context.peek();
280             String fieldName = meta.getValue("", "name"); //NOI18N
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); //NOI18N
287                 if ("name".equals(name)) { //NOI18N
288                     // name is already set during create => do nothing 
289                 }
290                 else if ("persistence-modifier".equals(name)) { //NOI18N
291                     int modifier = 
292                         PersistenceModifier.toPersistenceModifier(value);
293                     jdoField.setPersistenceModifier(modifier);
294                 }
295                 else if ("primary-key".equals(name)) { //NOI18N
296                     jdoField.setPrimaryKey(
297                         Boolean.valueOf(value).booleanValue());
298                 }
299                 else if ("null-value".equals(name)) { //NOI18N
300                     jdoField.setNullValueTreatment(
301                         NullValueTreatment.toNullValueTreatment(value));
302                 }
303                 else if ("default-fetch-group".equals(name)) { //NOI18N
304                     jdoField.setDefaultFetchGroup(
305                         Boolean.valueOf(value).booleanValue());
306                 }
307                 else if ("embedded".equals(name)) { //NOI18N
308                     jdoField.setEmbedded(
309                         Boolean.valueOf(value).booleanValue());
310                 }
311                 else {
312                     /* JDO2 metadata not yet fully supported => 
313                        do not throw exception now
314                     SAXException e = new SAXException(
315                         msg.msg("EXC_UnknownAttribute", "<field>", //NOI18N
316                                 name, value)); 
317                     if (trace)
318                         logger.trace("Throwing exception in " + //NOI18N
319                                      "JDOHandlerImpl.start_field:", e); //NOI18N
320                     throw e;
321                     */
322                     if (trace)
323                         logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
324                                              "<field>", name, value)); //NOI18N
325                 }
326             }
327         }
328         catch (ModelException ex) {
329             SAXException e = 
330                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
331             if (trace)
332                 logger.trace("Throwing exception in " + //NOI18N
333                              "JDOHandlerImpl.start_field:", e); //NOI18N
334             throw e;
335         }
336         
337         // push current JDOField on context stack
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>"); //NOI18N
350         // remove JDOField from context stack
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>"); //NOI18N
366         JDOCollection jdoCollection = null;
367         try {
368             // get the current JDOField from context stack
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); //NOI18N
376                 if ("element-type".equals(name)) { //NOI18N
377                     jdoCollection.setElementTypeName(value);
378                 }
379                 else if ("embedded-element".equals(name)) { //NOI18N
380                     jdoCollection.setEmbeddedElement(
381                         Boolean.valueOf(value).booleanValue());
382                 }
383                 else {
384                     /* JDO2 metadata not yet fully supported => 
385                        do not throw exception now
386                     SAXException e = new SAXException(
387                         msg.msg("EXC_UnknownAttribute", "<collection>", //NOI18N
388                                 name, value)); 
389                     if (trace)
390                         logger.trace("Throwing exception in " + //NOI18N
391                                      "JDOHandlerImpl.start_collection:", e); //NOI18N
392                     throw e;
393                     */
394                     if (trace)
395                         logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
396                                              "<collection>", name, value)); //NOI18N
397                 }
398             }
399         }
400         catch (ModelException ex) {
401             SAXException e = 
402                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
403             if (trace)
404                 logger.trace("Throwing exception in " + //NOI18N
405                              "JDOHandlerImpl.start_collection:", e); //NOI18N
406             throw e;
407         }
408         
409         // push current JDOCollection on context stack
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>"); //NOI18N
422         // remove JDOCollection from context stack
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>"); //NOI18N
438         JDOArray jdoArray = null;
439         try {
440             // get the current JDOField from context stack
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); //NOI18N
448                 if ("embedded-element".equals(name)) { //NOI18N
449                     jdoArray.setEmbeddedElement(
450                         Boolean.valueOf(value).booleanValue());
451                 }
452                 else {
453                     /* JDO2 metadata not yet fully supported => 
454                        do not throw exception now
455                     SAXException e = new SAXException(
456                         msg.msg("EXC_UnknownAttribute", "<array>", //NOI18N
457                                 name, value)); 
458                     if (trace)
459                         logger.trace("Throwing exception in " + //NOI18N
460                                      "JDOHandlerImpl.start_array:", e); //NOI18N
461                     throw e;
462                     */
463                     if (trace)
464                         logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
465                                              "<array>", name, value)); //NOI18N
466                 }
467             }
468         }
469         catch (ModelException ex) {
470             SAXException e = 
471                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
472             if (trace)
473                 logger.trace("Throwing exception in " + //NOI18N
474                              "JDOHandlerImpl.start_array:", e); //NOI18N
475             throw e;
476         }
477         
478         // push current JDOArray on context stack
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>"); //NOI18N
491         // remove JDOArray from context stack
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>"); //NOI18N
506         JDOMap jdoMap = null;
507         try {
508             // get the current JDOField from context stack
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); //NOI18N
516                 if ("key-type".equals(name)) { //NOI18N
517                     jdoMap.setKeyTypeName(value);
518                 }
519                 else if ("embedded-key".equals(name)) { //NOI18N
520                     jdoMap.setEmbeddedKey(
521                         Boolean.valueOf(value).booleanValue());
522                 }
523                 else if ("value-type".equals(name)) { //NOI18N
524                     jdoMap.setValueTypeName(value);
525                 }
526                 else if ("embedded-value".equals(name)) { //NOI18N
527                     jdoMap.setEmbeddedValue(
528                         Boolean.valueOf(value).booleanValue());
529                 }
530                 else {
531                     /* JDO2 metadata not yet fully supported => 
532                        do not throw exception now
533                     SAXException e = new SAXException(
534                         msg.msg("EXC_UnknownAttribute", "<map>", //NOI18N
535                                 name, value)); 
536                     if (trace)
537                         logger.trace("Throwing exception in " + //NOI18N
538                                      "JDOHandlerImpl.start_map:", e); //NOI18N
539                     throw e;
540                     */
541                     if (trace)
542                         logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
543                                              "<map>", name, value)); //NOI18N
544                 }
545             }
546         }
547         catch (ModelException ex) {
548             SAXException e = 
549                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
550             if (trace)
551                 logger.trace("Throwing exception in " + //NOI18N
552                              "JDOHandlerImpl.start_map:", e); //NOI18N
553             throw e;
554         }
555         
556         // push current JDOMap on context stack
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>"); //NOI18N
569         // remove JDOMap from context stack
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>"); //NOI18N
585         JDOExtension jdoExtension = null;
586         try {
587             // get the current JDOElement from context stack
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); //NOI18N
596                 if ("vendor-name".equals(name)) { //NOI18N
597                     jdoExtension.setVendorName(value);
598                 }
599                 else if ("key".equals(name)) { //NOI18N
600                     jdoExtension.setKey(value);
601                 }
602                 else if ("value".equals(name)) { //NOI18N
603                     jdoExtension.setValue(value);
604                 }
605                 else {
606                     /* JDO2 metadata not yet fully supported => 
607                        do not throw exception now
608                     SAXException e = new SAXException(
609                         msg.msg("EXC_UnknownAttribute", "<extension>", //NOI18N
610                                 name, value));
611                     if (trace)
612                         logger.trace("Throwing exception in " + //NOI18N
613                                      "JDOHandlerImpl.start_extension:", e); //NOI18N
614                     throw e;
615                     */
616                     if (trace)
617                         logger.trace(msg.msg("EXC_UnknownAttribute", //NOI18N
618                                              "<extension>", name, value)); //NOI18N
619                 }
620             }
621         }
622         catch (ModelException ex) {
623             SAXException e = 
624                 new SAXException(msg.msg("EXC_ModelException"), ex); //NOI18N
625             if (trace)
626                 logger.trace("Throwing exception in " + //NOI18N
627                              "JDOHandlerImpl.start_extension:", e); //NOI18N
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>"); //NOI18N
641         // start did not push anything => do nothing
642     }
643     
644     /***
645      *
646      */
647     public Collection handledJDOClasses()
648     {
649         return handledJDOClasses;
650     }
651 
652 }