View Javadoc

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