KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > metadata > XsdAnnotation


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.metadata;
23
24 import java.io.StringReader JavaDoc;
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.jboss.xb.binding.Constants;
28 import org.jboss.xb.binding.GenericObjectModelFactory;
29 import org.jboss.xb.binding.JBossXBException;
30 import org.jboss.xb.binding.JBossXBRuntimeException;
31 import org.jboss.xb.binding.Unmarshaller;
32 import org.jboss.xb.binding.UnmarshallerFactory;
33 import org.jboss.xb.binding.UnmarshallingContext;
34 import org.jboss.logging.Logger;
35 import org.xml.sax.Attributes JavaDoc;
36
37 /**
38  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
39  * @version <tt>$Revision: 1958 $</tt>
40  */

41 public class XsdAnnotation
42    extends XsdElement
43 {
44    private static final Logger log = Logger.getLogger(XsdAnnotation.class);
45
46    public XsdAnnotation(QName JavaDoc qName)
47    {
48       super(qName);
49    }
50
51    public static final XsdAnnotation unmarshal(String JavaDoc annotation)
52    {
53       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
54       unmarshaller.mapFactoryToNamespace(JaxbObjectModelFactory.INSTANCE, Constants.NS_JAXB);
55       unmarshaller.mapFactoryToNamespace(JbxbObjectModelFactory.INSTANCE, Constants.NS_JBXB);
56
57       try
58       {
59          return (XsdAnnotation)unmarshaller.unmarshal(new StringReader JavaDoc(annotation),
60             XsdObjectModelFactory.INSTANCE,
61             (Object JavaDoc)null
62          );
63       }
64       catch(JBossXBException e)
65       {
66          throw new JBossXBRuntimeException("Failed to parse annotation string: " + annotation + ": " + e.getMessage(),
67             e
68          );
69       }
70    }
71
72    public XsdAppInfo getAppInfo()
73    {
74       return (XsdAppInfo)getChild(XsdAppInfo.QNAME);
75    }
76
77    // Inner
78

79    private static abstract class AbstractGOMF
80       implements GenericObjectModelFactory
81    {
82       public Object JavaDoc newChild(Object JavaDoc parent,
83                              UnmarshallingContext ctx,
84                              String JavaDoc namespaceURI,
85                              String JavaDoc localName,
86                              Attributes JavaDoc attrs)
87       {
88          return null;
89       }
90
91       public void addChild(Object JavaDoc parent,
92                            Object JavaDoc child,
93                            UnmarshallingContext ctx,
94                            String JavaDoc namespaceURI,
95                            String JavaDoc localName)
96       {
97          XsdElement p = (XsdElement)parent;
98          XsdElement c = (XsdElement)child;
99          p.addChild(c);
100       }
101
102       public void setValue(Object JavaDoc o, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
103       {
104          XsdElement e = (XsdElement)o;
105          e.setData(value);
106       }
107
108       public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
109       {
110          return root;
111       }
112
113       }
114
115    private static final class XsdObjectModelFactory
116       extends AbstractGOMF
117    {
118       public static final GenericObjectModelFactory INSTANCE = new XsdObjectModelFactory();
119
120       public Object JavaDoc newChild(Object JavaDoc parent,
121                              UnmarshallingContext ctx,
122                              String JavaDoc namespaceURI,
123                              String JavaDoc localName,
124                              Attributes JavaDoc attrs)
125       {
126          XsdElement element = null;
127          if("appinfo".equals(localName))
128          {
129             element = new XsdAppInfo();
130             for(int i = 0; i < attrs.getLength(); ++i)
131             {
132                element.addAttribute(new QName JavaDoc(attrs.getURI(i), attrs.getLocalName(i)), attrs.getValue(i));
133             }
134          }
135          return element;
136       }
137
138       public void addChild(Object JavaDoc parent,
139                            Object JavaDoc child,
140                            UnmarshallingContext ctx,
141                            String JavaDoc namespaceURI,
142                            String JavaDoc localName)
143       {
144          if(parent instanceof XsdAppInfo)
145          {
146             XsdAppInfo appInfo = (XsdAppInfo)parent;
147             if(child instanceof ClassMetaData)
148             {
149                appInfo.setClassMetaData((ClassMetaData)child);
150             }
151             else if(child instanceof PropertyMetaData)
152             {
153                appInfo.setPropertyMetaData((PropertyMetaData)child);
154             }
155             else if(child instanceof SchemaMetaData)
156             {
157                appInfo.setSchemaMetaData((SchemaMetaData)child);
158             }
159             else if(child instanceof ValueMetaData)
160             {
161                appInfo.setValueMetaData((ValueMetaData)child);
162             }
163             else if(child instanceof CharactersMetaData)
164             {
165                appInfo.setCharactersMetaData((CharactersMetaData)child);
166             }
167          }
168          else
169          {
170             super.addChild(parent, child, ctx, namespaceURI, localName);
171          }
172       }
173
174       public Object JavaDoc newRoot(Object JavaDoc root,
175                             UnmarshallingContext ctx,
176                             String JavaDoc namespaceURI,
177                             String JavaDoc localName,
178                             Attributes JavaDoc attrs)
179       {
180          return new XsdAnnotation(new QName JavaDoc(namespaceURI, localName));
181       }
182    }
183
184    private static final class JaxbObjectModelFactory
185       implements GenericObjectModelFactory
186    {
187       public static final GenericObjectModelFactory INSTANCE = new JaxbObjectModelFactory();
188
189       public Object JavaDoc newChild(Object JavaDoc parent,
190                              UnmarshallingContext ctx,
191                              String JavaDoc namespaceURI,
192                              String JavaDoc localName,
193                              Attributes JavaDoc attrs)
194       {
195          Object JavaDoc element = null;
196          if("package".equals(localName))
197          {
198             element = new PackageMetaData();
199             setAttributes(element, attrs, new AttributeSetter()
200             {
201                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
202                {
203                   if("name".equals(localName))
204                   {
205                      ((PackageMetaData)o).setName(value);
206                   }
207                }
208             }
209             );
210          }
211          else if("javaType".equals(localName))
212          {
213             ValueMetaData valueMetaData = new ValueMetaData();
214             setAttributes(valueMetaData, attrs, new AttributeSetter()
215             {
216                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
217                {
218                   if("parseMethod".equals(localName))
219                   {
220                      ((ValueMetaData)o).setUnmarshalMethod(value);
221                   }
222                   else if("printMethod".equals(localName))
223                   {
224                      ((ValueMetaData)o).setMarshalMethod(value);
225                   }
226                }
227             }
228             );
229
230             // todo review this...
231
XsdAppInfo appInfo = (XsdAppInfo)parent;
232             appInfo.setValueMetaData(valueMetaData);
233          }
234
235          return element;
236       }
237
238       public void addChild(Object JavaDoc parent,
239                            Object JavaDoc child,
240                            UnmarshallingContext ctx,
241                            String JavaDoc namespaceURI,
242                            String JavaDoc localName)
243       {
244          if(parent instanceof SchemaMetaData)
245          {
246             SchemaMetaData schemaMetaData = (SchemaMetaData)parent;
247             if(child instanceof PackageMetaData)
248             {
249                schemaMetaData.setPackage((PackageMetaData)child);
250             }
251             else
252             {
253                schemaMetaData.addValue((ValueMetaData)child);
254             }
255          }
256       }
257
258       public void setValue(Object JavaDoc o, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
259       {
260       }
261
262       public Object JavaDoc newRoot(Object JavaDoc root,
263                             UnmarshallingContext ctx,
264                             String JavaDoc namespaceURI,
265                             String JavaDoc localName,
266                             Attributes JavaDoc attrs)
267       {
268          Object JavaDoc element = null;
269          if("schemaBindings".equals(localName))
270          {
271             element = new SchemaMetaData();
272          }
273          else if("property".equals(localName))
274          {
275             PropertyMetaData property = new PropertyMetaData();
276             setAttributes(property, attrs, new AttributeSetter()
277             {
278                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
279                {
280                   if("name".equals(localName))
281                   {
282                      ((PropertyMetaData)o).setName(value);
283                   }
284                   else if("collectionType".equals(localName))
285                   {
286                      ((PropertyMetaData)o).setCollectionType(value);
287                   }
288                }
289             }
290             );
291             //element = property;
292
XsdAppInfo appInfo = (XsdAppInfo)root;
293             appInfo.setPropertyMetaData(property);
294             // return null;
295
}
296          else if("class".equals(localName))
297          {
298             element = new ClassMetaData();
299             setAttributes(element, attrs, new AttributeSetter()
300             {
301                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
302                {
303                   if("implClass".equals(localName))
304                   {
305                      ((ClassMetaData)o).setImpl(value);
306                   }
307                }
308             }
309             );
310          }
311          else if("javaType".equals(localName))
312          {
313             element = new ValueMetaData();
314             setAttributes(element, attrs, new AttributeSetter()
315             {
316                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
317                {
318                   if("printMethod".equals(localName))
319                   {
320                      ((ValueMetaData)o).setMarshalMethod(value);
321                   }
322                   else if("parseMethod".equals(localName))
323                   {
324                      ((ValueMetaData)o).setUnmarshalMethod(value);
325                   }
326                }
327             }
328             );
329          }
330
331          return element;
332       }
333
334       public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
335       {
336          return root;
337       }
338
339       private void setAttributes(Object JavaDoc o, Attributes JavaDoc attrs, AttributeSetter attrSetter)
340       {
341          for(int i = 0; i < attrs.getLength(); ++i)
342          {
343             attrSetter.setAttribute(o, attrs.getURI(i), attrs.getLocalName(i), attrs.getValue(i));
344          }
345       }
346    }
347
348    private static final class JbxbObjectModelFactory
349       implements GenericObjectModelFactory
350    {
351       public static final JbxbObjectModelFactory INSTANCE = new JbxbObjectModelFactory();
352
353       public Object JavaDoc newChild(Object JavaDoc parent,
354                              UnmarshallingContext ctx,
355                              String JavaDoc namespaceURI,
356                              String JavaDoc localName,
357                              Attributes JavaDoc attrs)
358       {
359          Object JavaDoc child = null;
360          // schemaBindings/package
361
if("package".equals(localName))
362          {
363             child = new PackageMetaData();
364             setAttributes(child, attrs, new AttributeSetter()
365             {
366                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
367                {
368                   if("name".equals(localName))
369                   {
370                      ((PackageMetaData)o).setName(value);
371                   }
372                }
373             }
374             );
375          }
376          else if("value".equals(localName))
377          {
378             child = new ValueMetaData();
379             setAttributes(child, attrs, new AttributeSetter()
380             {
381                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
382                {
383                   if("marshalMethod".equals(localName))
384                   {
385                      ((ValueMetaData)o).setMarshalMethod(value);
386                   }
387                   else if("unmarshalMethod".equals(localName))
388                   {
389                      ((ValueMetaData)o).setUnmarshalMethod(value);
390                   }
391                }
392             }
393             );
394          }
395          else if("property".equals(localName))
396          {
397             PropertyMetaData property = new PropertyMetaData();
398             setAttributes(property, attrs, new AttributeSetter()
399             {
400                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
401                {
402                   if("name".equals(localName))
403                   {
404                      ((PropertyMetaData)o).setName(value);
405                   }
406                   else if("collectionType".equals(localName))
407                   {
408                      ((PropertyMetaData)o).setCollectionType(value);
409                   }
410                }
411             }
412             );
413
414             if(parent instanceof XsdAppInfo)
415             {
416                ((XsdAppInfo)parent).setPropertyMetaData(property);
417             }
418             else
419             {
420                ((CharactersMetaData)parent).setProperty(property);
421             }
422          }
423          else if("mapEntryKey".equals(localName))
424          {
425             if(parent instanceof XsdAppInfo)
426             {
427                ((XsdAppInfo)parent).setMapEntryKey(true);
428             }
429             else
430             {
431                ((CharactersMetaData)parent).setMapEntryKey(true);
432             }
433          }
434          else if("mapEntryValue".equals(localName))
435          {
436             if(parent instanceof XsdAppInfo)
437             {
438                ((XsdAppInfo)parent).setMapEntryValue(true);
439             }
440             else
441             {
442                ((CharactersMetaData)parent).setMapEntryValue(true);
443             }
444          }
445          else if("skip".equals(localName))
446          {
447             XsdAppInfo appInfo = (XsdAppInfo)parent;
448             appInfo.setSkip(true);
449          }
450          else
451          {
452             // Log a warning for any unexpected elements
453
if( "ignoreUnresolvedFieldOrClass".equals(localName) == false
454                && "replacePropertyRefs".equals(localName) == false )
455             {
456                log.warn("newChild: " + localName);
457             }
458          }
459          return child;
460       }
461
462       public void addChild(Object JavaDoc parent,
463                            Object JavaDoc child,
464                            UnmarshallingContext ctx,
465                            String JavaDoc namespaceURI,
466                            String JavaDoc localName)
467       {
468          if(child instanceof PackageMetaData)
469          {
470             SchemaMetaData schema = (SchemaMetaData)parent;
471             schema.setPackage((PackageMetaData)child);
472          }
473          else if(child instanceof ValueMetaData)
474          {
475             ValueMetaData valueMetaData = (ValueMetaData)child;
476             if(parent instanceof XsdAppInfo)
477             {
478                ((XsdAppInfo)parent).setValueMetaData(valueMetaData);
479             }
480             else
481             {
482                ((CharactersMetaData)parent).setValue(valueMetaData);
483             }
484          }
485          else if(child instanceof CharactersMetaData)
486          {
487             CharactersMetaData charMD = (CharactersMetaData)child;
488             ((XsdAppInfo)parent).setCharactersMetaData(charMD);
489          }
490          else
491          {
492             log.warn("addChild: " + localName + "=" + child);
493          }
494       }
495
496       public void setValue(Object JavaDoc o, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
497       {
498          // schemaBindings/ignoreUnresolvedFieldOrClass
499
if( "ignoreUnresolvedFieldOrClass".equals(localName) )
500          {
501             SchemaMetaData schema = (SchemaMetaData) o;
502             Boolean JavaDoc flag = Boolean.valueOf(value);
503             schema.setIgnoreUnresolvedFieldOrClass(flag.booleanValue());
504          }
505          // schemaBindings/replacePropertyRefs
506
else if( "replacePropertyRefs".equals(localName) )
507          {
508             SchemaMetaData schema = (SchemaMetaData) o;
509             Boolean JavaDoc flag = Boolean.valueOf(value);
510             schema.setReplacePropertyRefs(flag.booleanValue());
511          }
512          else
513          {
514             log.warn("setValue: " + localName + "=" + value);
515          }
516       }
517
518       public Object JavaDoc newRoot(Object JavaDoc root,
519                             UnmarshallingContext ctx,
520                             String JavaDoc namespaceURI,
521                             String JavaDoc localName,
522                             Attributes JavaDoc attrs)
523       {
524          Object JavaDoc element = null;
525          if("schemaBindings".equals(localName))
526          {
527             element = new SchemaMetaData();
528          }
529          // Legacy schema element name change to schemaBindings for jaxb consistency
530
else if("schema".equals(localName))
531          {
532             element = new SchemaMetaData();
533          }
534          else if("value".equals(localName))
535          {
536             element = new ValueMetaData();
537             setAttributes(element, attrs, new AttributeSetter()
538             {
539                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
540                {
541                   if("marshalMethod".equals(localName))
542                   {
543                      ((ValueMetaData)o).setMarshalMethod(value);
544                   }
545                   else if("unmarshalMethod".equals(localName))
546                   {
547                      ((ValueMetaData)o).setUnmarshalMethod(value);
548                   }
549                }
550             }
551             );
552          }
553          else if("class".equals(localName))
554          {
555             element = new ClassMetaData();
556             setAttributes(element, attrs, new AttributeSetter()
557             {
558                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
559                {
560                   if("impl".equals(localName))
561                   {
562                      ((ClassMetaData)o).setImpl(value);
563                   }
564                }
565             }
566             );
567          }
568          else if("property".equals(localName))
569          {
570             PropertyMetaData property = new PropertyMetaData();
571             setAttributes(property, attrs, new AttributeSetter()
572             {
573                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
574                {
575                   if("name".equals(localName))
576                   {
577                      ((PropertyMetaData)o).setName(value);
578                   }
579                   else if("collectionType".equals(localName))
580                   {
581                      ((PropertyMetaData)o).setCollectionType(value);
582                   }
583                }
584             }
585             );
586             XsdAppInfo appInfo = (XsdAppInfo)root;
587             appInfo.setPropertyMetaData(property);
588          }
589          else if("putMethod".equals(localName))
590          {
591             PutMethodMetaData putMethod = new PutMethodMetaData();
592             setAttributes(putMethod, attrs, new AttributeSetter()
593             {
594                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
595                {
596                   if("name".equals(localName))
597                   {
598                      ((PutMethodMetaData)o).setName(value);
599                   }
600                   else if("keyType".equals(localName))
601                   {
602                      ((PutMethodMetaData)o).setKeyType(value);
603                   }
604                   else if("valueType".equals(localName))
605                   {
606                      ((PutMethodMetaData)o).setValueType(value);
607                   }
608                }
609             }
610             );
611             XsdAppInfo appInfo = (XsdAppInfo)root;
612             appInfo.setPutMethodMetaData(putMethod);
613          }
614          else if("addMethod".equals(localName))
615          {
616             AddMethodMetaData addMethod = new AddMethodMetaData();
617             setAttributes(addMethod, attrs, new AttributeSetter()
618             {
619                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
620                {
621                   if("name".equals(localName))
622                   {
623                      ((AddMethodMetaData)o).setMethodName(value);
624                   }
625                   else if("valueType".equals(localName))
626                   {
627                      if("child".equals(value))
628                      {
629                         ((AddMethodMetaData)o).setChildType(true);
630                      }
631                      else
632                      {
633                         ((AddMethodMetaData)o).setValueType(value);
634                      }
635                   }
636                }
637             }
638             );
639             XsdAppInfo appInfo = (XsdAppInfo)root;
640             appInfo.setAddMethodMetaData(addMethod);
641          }
642          else if("mapEntry".equals(localName))
643          {
644             MapEntryMetaData mapEntry = new MapEntryMetaData();
645             setAttributes(mapEntry, attrs, new AttributeSetter()
646             {
647                public void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value)
648                {
649                   if("impl".equals(localName))
650                   {
651                      ((MapEntryMetaData)o).setImpl(value);
652                   }
653                   else if("getKeyMethod".equals(localName))
654                   {
655                      ((MapEntryMetaData)o).setGetKeyMethod(value);
656                   }
657                   else if("setKeyMethod".equals(localName))
658                   {
659                      ((MapEntryMetaData)o).setSetKeyMethod(value);
660                   }
661                   else if("getValueMethod".equals(localName))
662                   {
663                      ((MapEntryMetaData)o).setGetValueMethod(value);
664                   }
665                   else if("setValueMethod".equals(localName))
666                   {
667                      ((MapEntryMetaData)o).setSetValueMethod(value);
668                   }
669                   else if("valueType".equals(localName))
670                   {
671                      ((MapEntryMetaData)o).setValueType(value);
672                   }
673                   else if("nonNullValue".equals(localName))
674                   {
675                      boolean b = Boolean.valueOf(value).booleanValue();
676                      ((MapEntryMetaData)o).setNonNullValue(b);
677                   }
678                }
679             }
680             );
681             XsdAppInfo appInfo = (XsdAppInfo)root;
682             appInfo.setMapEntryMetaData(mapEntry);
683          }
684          else if("mapEntryKey".equals(localName))
685          {
686             XsdAppInfo appInfo = (XsdAppInfo)root;
687             appInfo.setMapEntryKey(true);
688          }
689          else if("mapEntryValue".equals(localName))
690          {
691             XsdAppInfo appInfo = (XsdAppInfo)root;
692             appInfo.setMapEntryValue(true);
693          }
694          else if("characters".equals(localName))
695          {
696             element = new CharactersMetaData();
697          }
698          else if("skip".equals(localName))
699          {
700             XsdAppInfo appInfo = (XsdAppInfo)root;
701             appInfo.setSkip(true);
702          }
703          else
704          {
705             log.warn("Unexpected jbxb annotation: ns=" + namespaceURI + ", localName=" + localName);
706          }
707          return element;
708       }
709
710       public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
711       {
712          return root;
713       }
714
715       // Private
716

717       private void setAttributes(Object JavaDoc o, Attributes JavaDoc attrs, AttributeSetter attrSetter)
718       {
719          for(int i = 0; i < attrs.getLength(); ++i)
720          {
721             attrSetter.setAttribute(o, attrs.getURI(i), attrs.getLocalName(i), attrs.getValue(i));
722          }
723       }
724    }
725
726    interface AttributeSetter
727    {
728       void setAttribute(Object JavaDoc o, String JavaDoc nsUri, String JavaDoc localName, String JavaDoc value);
729    }
730 }
731
Popular Tags