1 package org.apache.commons.betwixt;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.beans.BeanDescriptor;
21 import java.beans.BeanInfo;
22 import java.beans.IntrospectionException;
23 import java.beans.Introspector;
24 import java.beans.PropertyDescriptor;
25 import java.io.IOException;
26 import java.lang.reflect.Method;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.apache.commons.beanutils.DynaBean;
36 import org.apache.commons.beanutils.DynaClass;
37 import org.apache.commons.beanutils.DynaProperty;
38 import org.apache.commons.betwixt.digester.MultiMappingBeanInfoDigester;
39 import org.apache.commons.betwixt.digester.XMLBeanInfoDigester;
40 import org.apache.commons.betwixt.digester.XMLIntrospectorHelper;
41 import org.apache.commons.betwixt.expression.CollectionUpdater;
42 import org.apache.commons.betwixt.expression.EmptyExpression;
43 import org.apache.commons.betwixt.expression.IteratorExpression;
44 import org.apache.commons.betwixt.expression.MapEntryAdder;
45 import org.apache.commons.betwixt.expression.MethodUpdater;
46 import org.apache.commons.betwixt.expression.StringExpression;
47 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
48 import org.apache.commons.betwixt.registry.PolymorphicReferenceResolver;
49 import org.apache.commons.betwixt.registry.XMLBeanInfoRegistry;
50 import org.apache.commons.betwixt.strategy.ClassNormalizer;
51 import org.apache.commons.betwixt.strategy.DefaultNameMapper;
52 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
53 import org.apache.commons.betwixt.strategy.NameMapper;
54 import org.apache.commons.betwixt.strategy.PluralStemmer;
55 import org.apache.commons.betwixt.strategy.TypeBindingStrategy;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.xml.sax.InputSource;
59 import org.xml.sax.SAXException;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public class XMLIntrospector {
81
82
83
84
85 protected Log log = LogFactory.getLog( XMLIntrospector.class );
86
87
88 private XMLBeanInfoRegistry registry;
89
90
91 private XMLBeanInfoDigester digester;
92
93
94 private MultiMappingBeanInfoDigester multiMappingdigester;
95
96
97 private IntrospectionConfiguration configuration;
98
99
100
101
102
103
104
105 private PolymorphicReferenceResolver polymorphicReferenceResolver;
106
107
108 public XMLIntrospector() {
109 this(new IntrospectionConfiguration());
110 }
111
112
113
114
115
116
117
118 public XMLIntrospector(IntrospectionConfiguration configuration) {
119 setConfiguration(configuration);
120 DefaultXMLBeanInfoRegistry defaultRegistry
121 = new DefaultXMLBeanInfoRegistry();
122 setRegistry(defaultRegistry);
123 setPolymorphicReferenceResolver(defaultRegistry);
124 }
125
126
127
128
129
130
131
132
133
134 public Log getLog() {
135 return getConfiguration().getIntrospectionLog();
136 }
137
138
139
140
141
142 public void setLog(Log log) {
143 getConfiguration().setIntrospectionLog(log);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public XMLBeanInfoRegistry getRegistry() {
159 return registry;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public void setRegistry(XMLBeanInfoRegistry registry) {
180 this.registry = registry;
181 }
182
183
184
185
186
187
188
189
190
191 public IntrospectionConfiguration getConfiguration() {
192 return configuration;
193 }
194
195
196
197
198
199
200
201
202
203 public void setConfiguration(IntrospectionConfiguration configuration) {
204 this.configuration = configuration;
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218 public ClassNormalizer getClassNormalizer() {
219 return getConfiguration().getClassNormalizer();
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233 public void setClassNormalizer(ClassNormalizer classNormalizer) {
234 getConfiguration().setClassNormalizer(classNormalizer);
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public PolymorphicReferenceResolver getPolymorphicReferenceResolver() {
256 return polymorphicReferenceResolver;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 public void setPolymorphicReferenceResolver(
276 PolymorphicReferenceResolver polymorphicReferenceResolver) {
277 this.polymorphicReferenceResolver = polymorphicReferenceResolver;
278 }
279
280
281
282
283
284
285
286 public boolean isCachingEnabled() {
287 return true;
288 }
289
290
291
292
293
294
295
296 public void setCachingEnabled(boolean cachingEnabled) {
297
298 }
299
300
301
302
303
304
305
306 public boolean isAttributesForPrimitives() {
307 return getConfiguration().isAttributesForPrimitives();
308 }
309
310
311
312
313
314
315
316 public void setAttributesForPrimitives(boolean attributesForPrimitives) {
317 getConfiguration().setAttributesForPrimitives(attributesForPrimitives);
318 }
319
320
321
322
323
324
325
326 public boolean isWrapCollectionsInElement() {
327 return getConfiguration().isWrapCollectionsInElement();
328 }
329
330
331
332
333
334
335
336
337 public void setWrapCollectionsInElement(boolean wrapCollectionsInElement) {
338 getConfiguration().setWrapCollectionsInElement(wrapCollectionsInElement);
339 }
340
341
342
343
344
345
346
347 public PluralStemmer getPluralStemmer() {
348 return getConfiguration().getPluralStemmer();
349 }
350
351
352
353
354
355
356
357 public void setPluralStemmer(PluralStemmer pluralStemmer) {
358 getConfiguration().setPluralStemmer(pluralStemmer);
359 }
360
361
362
363
364
365
366
367
368 public NameMapper getNameMapper() {
369 return getElementNameMapper();
370 }
371
372
373
374
375
376
377
378 public void setNameMapper(NameMapper nameMapper) {
379 setElementNameMapper(nameMapper);
380 }
381
382
383
384
385
386
387
388
389
390 public NameMapper getElementNameMapper() {
391 return getConfiguration().getElementNameMapper();
392 }
393
394
395
396
397
398
399 public void setElementNameMapper(NameMapper nameMapper) {
400 getConfiguration().setElementNameMapper( nameMapper );
401 }
402
403
404
405
406
407
408
409
410
411 public NameMapper getAttributeNameMapper() {
412 return getConfiguration().getAttributeNameMapper();
413 }
414
415
416
417
418
419
420
421 public void setAttributeNameMapper(NameMapper nameMapper) {
422 getConfiguration().setAttributeNameMapper( nameMapper );
423 }
424
425
426
427
428
429
430
431
432 public boolean useBeanInfoSearchPath() {
433 return getConfiguration().useBeanInfoSearchPath();
434 }
435
436
437
438
439
440
441
442 public void setUseBeanInfoSearchPath(boolean useBeanInfoSearchPath) {
443 getConfiguration().setUseBeanInfoSearchPath( useBeanInfoSearchPath );
444 }
445
446
447
448
449
450
451
452
453
454 public void flushCache() {}
455
456
457
458
459
460
461
462
463
464
465 public XMLBeanInfo introspect(Object bean) throws IntrospectionException {
466 if (getLog().isDebugEnabled()) {
467 getLog().debug( "Introspecting..." );
468 getLog().debug(bean);
469 }
470
471 if ( bean instanceof DynaBean ) {
472
473 XMLBeanInfo xmlBeanInfo = findByXMLDescriptor( bean.getClass() );
474 if (xmlBeanInfo != null) {
475 return xmlBeanInfo;
476 }
477
478 return introspect( ((DynaBean) bean).getDynaClass() );
479
480 } else {
481
482 Class normalClass = getClassNormalizer().getNormalizedClass( bean );
483 return introspect( normalClass );
484 }
485 }
486
487
488
489
490
491
492
493
494
495 public XMLBeanInfo introspect(DynaClass dynaClass) {
496
497
498
499
500
501
502
503 XMLBeanInfo xmlInfo = createXMLBeanInfo( dynaClass );
504
505
506 DynaClassBeanType beanClass = new DynaClassBeanType( dynaClass );
507 populate( xmlInfo, beanClass );
508
509 return xmlInfo;
510 }
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529 public synchronized XMLBeanInfo introspect(Class aClass, InputSource source) throws IOException, SAXException {
530
531 configureDigester(aClass);
532 XMLBeanInfo result = (XMLBeanInfo) digester.parse(source);
533 return result;
534 }
535
536
537
538
539
540
541
542
543
544
545 public XMLBeanInfo introspect(Class aClass) throws IntrospectionException {
546
547 String[] searchPath = null;
548 if ( !getConfiguration().useBeanInfoSearchPath() ) {
549 try {
550 searchPath = Introspector.getBeanInfoSearchPath();
551 Introspector.setBeanInfoSearchPath(new String[] { });
552 } catch (SecurityException e) {
553
554 getLog().warn("Security manager does not allow bean info search path to be set");
555 getLog().debug("Security exception whilst setting bean info search page", e);
556 }
557 }
558
559 XMLBeanInfo xmlInfo = registry.get( aClass );
560
561 if ( xmlInfo == null ) {
562
563 if ( getLog().isDebugEnabled() ) {
564 getLog().debug( "Attempting to lookup an XML descriptor for class: " + aClass );
565 }
566
567 xmlInfo = findByXMLDescriptor( aClass );
568 if ( xmlInfo == null ) {
569 BeanInfo info;
570 if(getConfiguration().ignoreAllBeanInfo()) {
571 info = Introspector.getBeanInfo( aClass, Introspector.IGNORE_ALL_BEANINFO );
572 }
573 else {
574 info = Introspector.getBeanInfo( aClass );
575 }
576 xmlInfo = introspect( info );
577 }
578
579 if ( xmlInfo != null ) {
580 registry.put( aClass, xmlInfo );
581 }
582 } else {
583 getLog().trace( "Used cached XMLBeanInfo." );
584 }
585
586 if ( getLog().isTraceEnabled() ) {
587 getLog().trace( xmlInfo );
588 }
589 if ( !getConfiguration().useBeanInfoSearchPath() && searchPath != null) {
590 try
591 {
592
593 Introspector.setBeanInfoSearchPath( searchPath );
594 } catch (SecurityException e) {
595
596 getLog().warn("Security manager does not allow bean info search path to be set");
597 getLog().debug("Security exception whilst setting bean info search page", e);
598 }
599 }
600
601 return xmlInfo;
602 }
603
604
605
606
607
608
609
610
611
612 public XMLBeanInfo introspect(BeanInfo beanInfo) throws IntrospectionException {
613 XMLBeanInfo xmlBeanInfo = createXMLBeanInfo( beanInfo );
614 populate( xmlBeanInfo, new JavaBeanType( beanInfo ) );
615 return xmlBeanInfo;
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634 public synchronized Class[] register(InputSource source) throws IntrospectionException, IOException, SAXException {
635 Map xmlBeanInfoByClass = loadMultiMapping(source);
636 Set keySet = xmlBeanInfoByClass.keySet();
637 Class mappedClasses[] = new Class[keySet.size()];
638 int i=0;
639 for (Iterator it=keySet.iterator(); it.hasNext(); ) {
640 Class clazz = (Class) it.next();
641 mappedClasses[i++] = clazz;
642 XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) xmlBeanInfoByClass.get(clazz);
643 if (xmlBeanInfo != null) {
644 getRegistry().put(clazz, xmlBeanInfo);
645 }
646 }
647 return mappedClasses;
648 }
649
650
651
652
653
654
655
656
657
658 private synchronized Map loadMultiMapping(InputSource mapping) throws IOException, SAXException {
659
660
661 if (multiMappingdigester == null) {
662 multiMappingdigester = new MultiMappingBeanInfoDigester();
663 multiMappingdigester.setXMLIntrospector(this);
664 }
665 Map multiBeanInfoMap = (Map) multiMappingdigester.parse(mapping);
666 return multiBeanInfoMap;
667 }
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683 public void register(Class aClass, InputSource source) throws IOException, SAXException {
684 XMLBeanInfo xmlBeanInfo = introspect(aClass, source);
685 getRegistry().put(aClass, xmlBeanInfo);
686 }
687
688
689
690
691
692
693
694 private void populate(XMLBeanInfo xmlBeanInfo, BeanType bean) {
695 String name = bean.getBeanName();
696
697 ElementDescriptor elementDescriptor = new ElementDescriptor();
698 elementDescriptor.setLocalName(
699 getElementNameMapper().mapTypeToElementName( name ) );
700 elementDescriptor.setPropertyType( bean.getElementType() );
701
702 if (getLog().isTraceEnabled()) {
703 getLog().trace("Populating:" + bean);
704 }
705
706
707 if ( bean.isPrimitiveType() ) {
708 getLog().trace("Bean is primitive");
709 elementDescriptor.setTextExpression( StringExpression.getInstance() );
710
711 } else {
712
713 getLog().trace("Bean is standard type");
714
715 boolean isLoopType = bean.isLoopType();
716
717 List elements = new ArrayList();
718 List attributes = new ArrayList();
719 List contents = new ArrayList();
720
721
722 if ( !( isLoopType && isBasicCollection( bean.getClass() ) ) )
723 {
724 addProperties( bean.getProperties(), elements, attributes, contents );
725 }
726
727
728 if ( isLoopType ) {
729 getLog().trace("Bean is loop");
730 ElementDescriptor loopDescriptor = new ElementDescriptor();
731 loopDescriptor.setCollective(true);
732 loopDescriptor.setHollow(true);
733 loopDescriptor.setSingularPropertyType(Object.class);
734 loopDescriptor.setContextExpression(
735 new IteratorExpression( EmptyExpression.getInstance() )
736 );
737 loopDescriptor.setUpdater(CollectionUpdater.getInstance());
738 if ( bean.isMapType() ) {
739 loopDescriptor.setQualifiedName( "entry" );
740 }
741 elements.add( loopDescriptor );
742 }
743
744 int size = elements.size();
745 if ( size > 0 ) {
746 ElementDescriptor[] descriptors = new ElementDescriptor[size];
747 elements.toArray( descriptors );
748 elementDescriptor.setElementDescriptors( descriptors );
749 }
750 size = attributes.size();
751 if ( size > 0 ) {
752 AttributeDescriptor[] descriptors = new AttributeDescriptor[size];
753 attributes.toArray( descriptors );
754 elementDescriptor.setAttributeDescriptors( descriptors );
755 }
756 size = contents.size();
757 if ( size > 0 ) {
758 if ( size > 0 ) {
759 Descriptor[] descriptors = new Descriptor[size];
760 contents.toArray( descriptors );
761 elementDescriptor.setContentDescriptors( descriptors );
762 }
763 }
764 }
765
766 xmlBeanInfo.setElementDescriptor( elementDescriptor );
767
768
769 defaultAddMethods( elementDescriptor, bean.getElementType() );
770
771 if (getLog().isTraceEnabled()) {
772 getLog().trace("Populated descriptor:");
773 getLog().trace(elementDescriptor);
774 }
775 }
776
777
778
779
780
781
782
783
784
785
786 private boolean isBasicCollection( Class type )
787 {
788 return type.getName().startsWith( "java.util" );
789 }
790
791
792
793
794
795
796
797
798
799 protected XMLBeanInfo createXMLBeanInfo(DynaClass dynaClass) {
800
801 XMLBeanInfo beanInfo = new XMLBeanInfo(dynaClass.getClass());
802 return beanInfo;
803 }
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819 public Descriptor createDescriptor(
820 PropertyDescriptor propertyDescriptor,
821 boolean useAttributesForPrimitives
822 ) throws IntrospectionException {
823 return createXMLDescriptor( new BeanProperty( propertyDescriptor ) );
824 }
825
826
827
828
829
830
831
832
833
834
835 public Descriptor createXMLDescriptor( BeanProperty beanProperty ) {
836 return beanProperty.createXMLDescriptor( configuration );
837 }
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865 public void defaultAddMethods(
866 ElementDescriptor rootDescriptor,
867 Class beanClass ) {
868 defaultAddMethods(rootDescriptor, beanClass, false);
869 }
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896 public void defaultAddMethods( ElementDescriptor rootDescriptor, Class beanClass, boolean preservePropertyName ) {
897
898
899
900
901
902 if ( beanClass != null ) {
903 ArrayList singleParameterAdders = new ArrayList();
904 ArrayList twinParameterAdders = new ArrayList();
905
906 Method[] methods = beanClass.getMethods();
907 for ( int i = 0, size = methods.length; i < size; i++ ) {
908 Method method = methods[i];
909 String name = method.getName();
910 if ( name.startsWith( "add" )) {
911
912
913 Class[] types = method.getParameterTypes();
914 if ( types != null) {
915 if ( getLog().isTraceEnabled() ) {
916 getLog().trace("Searching for match for " + method);
917 }
918
919 switch (types.length)
920 {
921 case 1:
922 singleParameterAdders.add(method);
923 break;
924 case 2:
925 twinParameterAdders.add(method);
926 break;
927 default:
928
929 break;
930 }
931 }
932 }
933 }
934
935 Map elementsByPropertyName = makeElementDescriptorMap( rootDescriptor );
936
937 for (Iterator it=singleParameterAdders.iterator();it.hasNext();) {
938 Method singleParameterAdder = (Method) it.next();
939 setIteratorAdder(elementsByPropertyName, singleParameterAdder, preservePropertyName);
940 }
941
942 for (Iterator it=twinParameterAdders.iterator();it.hasNext();) {
943 Method twinParameterAdder = (Method) it.next();
944 setMapAdder(elementsByPropertyName, twinParameterAdder);
945 }
946
947
948
949 configureMappingDerivation( rootDescriptor );
950 }
951 }
952
953
954
955
956
957
958
959 private void configureMappingDerivation(ElementDescriptor descriptor) {
960 boolean useBindTime = getConfiguration().getMappingDerivationStrategy()
961 .useBindTimeTypeForMapping(descriptor.getPropertyType(), descriptor.getSingularPropertyType());
962 descriptor.setUseBindTimeTypeForMapping(useBindTime);
963 ElementDescriptor[] childDescriptors = descriptor.getElementDescriptors();
964 for (int i=0, size=childDescriptors.length; i<size; i++) {
965 configureMappingDerivation(childDescriptors[i]);
966 }
967 }
968
969
970
971
972
973
974 private void setIteratorAdder(
975 Map elementsByPropertyName,
976 Method singleParameterAdderMethod,
977 boolean preserveNullPropertyName) {
978
979 String adderName = singleParameterAdderMethod.getName();
980 String propertyName = Introspector.decapitalize(adderName.substring(3));
981 ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName, elementsByPropertyName);
982 if (matchingDescriptor != null) {
983
984
985 Class singularType = singleParameterAdderMethod.getParameterTypes()[0];
986 if (getLog().isTraceEnabled()) {
987 getLog().trace(adderName + "->" + propertyName);
988 }
989
990 getLog().trace("Matching collection or iteration");
991
992 matchingDescriptor.setUpdater( new MethodUpdater( singleParameterAdderMethod ) );
993 matchingDescriptor.setSingularPropertyType( singularType );
994 matchingDescriptor.setHollow(!isPrimitiveType(singularType));
995 String localName = matchingDescriptor.getLocalName();
996 if ( !preserveNullPropertyName && ( localName == null || localName.length() == 0 )) {
997 matchingDescriptor.setLocalName(
998 getConfiguration().getElementNameMapper()
999 .mapTypeToElementName( propertyName ) );
1000 }
1001
1002 if ( getLog().isDebugEnabled() ) {
1003 getLog().debug( "!! " + singleParameterAdderMethod);
1004 getLog().debug( "!! " + singularType);
1005 }
1006 }
1007 }
1008
1009
1010
1011
1012
1013
1014 private void setMapAdder(
1015 Map elementsByPropertyName,
1016 Method twinParameterAdderMethod) {
1017 String adderName = twinParameterAdderMethod.getName();
1018 String propertyName = Introspector.decapitalize(adderName.substring(3));
1019 ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName, elementsByPropertyName);
1020 assignAdder(twinParameterAdderMethod, matchingDescriptor);
1021 }
1022
1023
1024
1025
1026
1027
1028
1029 public void assignAdder(Method twinParameterAdderMethod, ElementDescriptor matchingDescriptor) {
1030 if ( matchingDescriptor != null
1031 && Map.class.isAssignableFrom( matchingDescriptor.getPropertyType() )) {
1032
1033 getLog().trace("Matching map");
1034 ElementDescriptor[] children
1035 = matchingDescriptor.getElementDescriptors();
1036
1037 if ( children.length == 0 ) {
1038 getLog().info(
1039 "'entry' descriptor is missing for map. "
1040 + "Updaters cannot be set");
1041
1042 } else {
1043 assignAdder(twinParameterAdderMethod, children);
1044 }
1045 }
1046 }
1047
1048
1049
1050
1051
1052
1053 private void assignAdder(Method twinParameterAdderMethod, ElementDescriptor[] children) {
1054 Class[] types = twinParameterAdderMethod.getParameterTypes();
1055 Class keyType = types[0];
1056 Class valueType = types[1];
1057
1058
1059
1060 MapEntryAdder adder = new MapEntryAdder(twinParameterAdderMethod);
1061 for (
1062 int n=0,
1063 noOfGrandChildren = children.length;
1064 n < noOfGrandChildren;
1065 n++ ) {
1066 if ( "key".equals( children[n].getLocalName() ) ) {
1067
1068 children[n].setUpdater( adder.getKeyUpdater() );
1069 children[n].setSingularPropertyType( keyType );
1070 if (children[n].getPropertyType() == null) {
1071 children[n].setPropertyType( valueType );
1072 }
1073 if ( isPrimitiveType(keyType) ) {
1074 children[n].setHollow(false);
1075 }
1076 if ( getLog().isTraceEnabled() ) {
1077 getLog().trace( "Key descriptor: " + children[n]);
1078 }
1079
1080 } else if ( "value".equals( children[n].getLocalName() ) ) {
1081
1082 children[n].setUpdater( adder.getValueUpdater() );
1083 children[n].setSingularPropertyType( valueType );
1084 if (children[n].getPropertyType() == null) {
1085 children[n].setPropertyType( valueType );
1086 }
1087 if ( isPrimitiveType( valueType) ) {
1088 children[n].setHollow(false);
1089 }
1090 if ( isLoopType( valueType )) {
1091
1092
1093
1094 ElementDescriptor loopDescriptor = new ElementDescriptor();
1095 loopDescriptor.setHollow(true);
1096 loopDescriptor.setSingularPropertyType( valueType );
1097 loopDescriptor.setPropertyType( valueType );
1098 children[n].addElementDescriptor(loopDescriptor);
1099 loopDescriptor.setCollective(true);
1100 }
1101 if ( getLog().isTraceEnabled() ) {
1102 getLog().trace( "Value descriptor: " + children[n]);
1103 }
1104 }
1105 }
1106 }
1107
1108
1109
1110
1111
1112
1113
1114 private ElementDescriptor getMatchForAdder(
1115 String propertyName,
1116 Map elementsByPropertyName) {
1117 ElementDescriptor matchingDescriptor = null;
1118 if (propertyName.length() > 0) {
1119 if ( getLog().isTraceEnabled() ) {
1120 getLog().trace( "findPluralDescriptor( " + propertyName
1121 + " ):root property name=" + propertyName );
1122 }
1123
1124 PluralStemmer stemmer = getPluralStemmer();
1125 matchingDescriptor = stemmer.findPluralDescriptor( propertyName, elementsByPropertyName );
1126
1127 if ( getLog().isTraceEnabled() ) {
1128 getLog().trace(
1129 "findPluralDescriptor( " + propertyName
1130 + " ):ElementDescriptor=" + matchingDescriptor );
1131 }
1132 }
1133 return matchingDescriptor;
1134 }
1135
1136
1137
1138
1139
1140
1141
1142
1143 private Map makeElementDescriptorMap( ElementDescriptor rootDescriptor ) {
1144 Map result = new HashMap();
1145 String rootPropertyName = rootDescriptor.getPropertyName();
1146 if (rootPropertyName != null) {
1147 result.put(rootPropertyName, rootDescriptor);
1148 }
1149 makeElementDescriptorMap( rootDescriptor, result );
1150 return result;
1151 }
1152
1153
1154
1155
1156
1157
1158
1159
1160 private void makeElementDescriptorMap( ElementDescriptor rootDescriptor, Map map ) {
1161 ElementDescriptor[] children = rootDescriptor.getElementDescriptors();
1162 if ( children != null ) {
1163 for ( int i = 0, size = children.length; i < size; i++ ) {
1164 ElementDescriptor child = children[i];
1165 String propertyName = child.getPropertyName();
1166 if ( propertyName != null ) {
1167 map.put( propertyName, child );
1168 }
1169 makeElementDescriptorMap( child, map );
1170 }
1171 }
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 protected PluralStemmer createPluralStemmer() {
1183 return new DefaultPluralStemmer();
1184 }
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194 protected NameMapper createNameMapper() {
1195 return new DefaultNameMapper();
1196 }
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 protected synchronized XMLBeanInfo findByXMLDescriptor( Class aClass ) {
1208
1209 String name = aClass.getName();
1210 int idx = name.lastIndexOf( '.' );
1211 if ( idx >= 0 ) {
1212 name = name.substring( idx + 1 );
1213 }
1214 name += ".betwixt";
1215
1216 URL url = aClass.getResource( name );
1217 if ( url != null ) {
1218 try {
1219 String urlText = url.toString();
1220 if ( getLog().isDebugEnabled( )) {
1221 getLog().debug( "Parsing Betwixt XML descriptor: " + urlText );
1222 }
1223
1224
1225 configureDigester(aClass);
1226 return (XMLBeanInfo) digester.parse( urlText );
1227 } catch (Exception e) {
1228 getLog().warn( "Caught exception trying to parse: " + name, e );
1229 }
1230 }
1231
1232 if ( getLog().isTraceEnabled() ) {
1233 getLog().trace( "Could not find betwixt file " + name );
1234 }
1235 return null;
1236 }
1237
1238
1239
1240
1241
1242 private synchronized void configureDigester(Class aClass) {
1243 if ( digester == null ) {
1244 digester = new XMLBeanInfoDigester();
1245 digester.setXMLIntrospector( this );
1246 }
1247
1248 if (configuration.isUseContextClassLoader()) {
1249
1250
1251
1252
1253
1254 digester.setUseContextClassLoader(true);
1255 } else {
1256
1257
1258 digester.setClassLoader(this.getClass().getClassLoader());
1259 }
1260
1261 digester.setBeanClass( aClass );
1262 }
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274 protected void addProperties(
1275 BeanInfo beanInfo,
1276 List elements,
1277 List attributes,
1278 List contents)
1279 throws
1280 IntrospectionException {
1281 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
1282 if ( descriptors != null ) {
1283 for ( int i = 0, size = descriptors.length; i < size; i++ ) {
1284 addProperty(beanInfo, descriptors[i], elements, attributes, contents);
1285 }
1286 }
1287 if (getLog().isTraceEnabled()) {
1288 getLog().trace(elements);
1289 getLog().trace(attributes);
1290 getLog().trace(contents);
1291 }
1292 }
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302 protected void addProperties(
1303 BeanProperty[] beanProperties,
1304 List elements,
1305 List attributes,
1306 List contents) {
1307 if ( beanProperties != null ) {
1308 if (getLog().isTraceEnabled()) {
1309 getLog().trace(beanProperties.length + " properties to be added");
1310 }
1311 for ( int i = 0, size = beanProperties.length; i < size; i++ ) {
1312 addProperty(beanProperties[i], elements, attributes, contents);
1313 }
1314 }
1315 if (getLog().isTraceEnabled()) {
1316 getLog().trace("After properties have been added (elements, attributes, contents):");
1317 getLog().trace(elements);
1318 getLog().trace(attributes);
1319 getLog().trace(contents);
1320 }
1321 }
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338 protected void addProperty(
1339 BeanInfo beanInfo,
1340 PropertyDescriptor propertyDescriptor,
1341 List elements,
1342 List attributes,
1343 List contents)
1344 throws
1345 IntrospectionException {
1346 addProperty( propertyDescriptor, elements, attributes, contents);
1347 }
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361 protected void addProperty(
1362 PropertyDescriptor propertyDescriptor,
1363 List elements,
1364 List attributes,
1365 List contents)
1366 throws
1367 IntrospectionException {
1368 addProperty(new BeanProperty( propertyDescriptor ), elements, attributes, contents);
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 protected void addProperty(
1383 BeanProperty beanProperty,
1384 List elements,
1385 List attributes,
1386 List contents) {
1387 Descriptor nodeDescriptor = createXMLDescriptor(beanProperty);
1388 if (nodeDescriptor == null) {
1389 return;
1390 }
1391 if (nodeDescriptor instanceof ElementDescriptor) {
1392 elements.add(nodeDescriptor);
1393 } else if (nodeDescriptor instanceof AttributeDescriptor) {
1394 attributes.add(nodeDescriptor);
1395 } else {
1396 contents.add(nodeDescriptor);
1397 }
1398 }
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410 protected void addProperties(
1411 BeanInfo beanInfo,
1412 List elements,
1413 List attributes)
1414 throws
1415 IntrospectionException {
1416 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
1417 if ( descriptors != null ) {
1418 for ( int i = 0, size = descriptors.length; i < size; i++ ) {
1419 addProperty(beanInfo, descriptors[i], elements, attributes);
1420 }
1421 }
1422 if (getLog().isTraceEnabled()) {
1423 getLog().trace(elements);
1424 getLog().trace(attributes);
1425 }
1426 }
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441 protected void addProperty(
1442 BeanInfo beanInfo,
1443 PropertyDescriptor propertyDescriptor,
1444 List elements,
1445 List attributes)
1446 throws
1447 IntrospectionException {
1448 NodeDescriptor nodeDescriptor = XMLIntrospectorHelper
1449 .createDescriptor(propertyDescriptor,
1450 isAttributesForPrimitives(),
1451 this);
1452 if (nodeDescriptor == null) {
1453 return;
1454 }
1455 if (nodeDescriptor instanceof ElementDescriptor) {
1456 elements.add(nodeDescriptor);
1457 } else {
1458 attributes.add(nodeDescriptor);
1459 }
1460 }
1461
1462
1463
1464
1465
1466
1467
1468
1469 protected XMLBeanInfo createXMLBeanInfo( BeanInfo beanInfo ) {
1470 XMLBeanInfo xmlBeanInfo = new XMLBeanInfo( beanInfo.getBeanDescriptor().getBeanClass() );
1471 return xmlBeanInfo;
1472 }
1473
1474
1475
1476
1477
1478
1479
1480 public boolean isLoopType(Class type) {
1481 return getConfiguration().isLoopType(type);
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491 public boolean isPrimitiveType(Class type) {
1492
1493
1494 TypeBindingStrategy.BindingType bindingType
1495 = configuration.getTypeBindingStrategy().bindingType( type ) ;
1496 boolean result = (bindingType.equals(TypeBindingStrategy.BindingType.PRIMITIVE));
1497 return result;
1498 }
1499
1500
1501
1502 private abstract class BeanType {
1503
1504
1505
1506
1507 public abstract String getBeanName();
1508
1509
1510
1511
1512
1513 public abstract Class getElementType();
1514
1515
1516
1517
1518
1519 public abstract boolean isPrimitiveType();
1520
1521
1522
1523
1524
1525 public abstract boolean isMapType();
1526
1527
1528
1529
1530
1531 public abstract boolean isLoopType();
1532
1533
1534
1535
1536
1537 public abstract BeanProperty[] getProperties();
1538
1539
1540
1541
1542
1543 public String toString() {
1544 return "Bean[name=" + getBeanName() + ", type=" + getElementType();
1545 }
1546 }
1547
1548
1549 private class JavaBeanType extends BeanType {
1550
1551 private BeanInfo beanInfo;
1552
1553 private Class beanClass;
1554
1555 private String name;
1556
1557 private BeanProperty[] properties;
1558
1559
1560
1561
1562
1563 public JavaBeanType(BeanInfo beanInfo) {
1564 this.beanInfo = beanInfo;
1565 BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
1566 beanClass = beanDescriptor.getBeanClass();
1567 name = beanDescriptor.getName();
1568
1569 if (beanClass.isArray()) {
1570
1571 name = "Array";
1572 }
1573
1574 }
1575
1576
1577 public Class getElementType() {
1578 return beanClass;
1579 }
1580
1581
1582 public String getBeanName() {
1583 return name;
1584 }
1585
1586
1587 public boolean isPrimitiveType() {
1588 return XMLIntrospector.this.isPrimitiveType( beanClass );
1589 }
1590
1591
1592 public boolean isLoopType() {
1593 return getConfiguration().isLoopType( beanClass );
1594 }
1595
1596
1597 public boolean isMapType() {
1598 return Map.class.isAssignableFrom( beanClass );
1599 }
1600
1601
1602 public BeanProperty[] getProperties() {
1603
1604 if ( properties == null ) {
1605 ArrayList propertyDescriptors = new ArrayList();
1606
1607 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
1608 if ( descriptors != null ) {
1609 for (int i=0, size=descriptors.length; i<size; i++) {
1610 if (!getConfiguration().getPropertySuppressionStrategy()
1611 .suppressProperty(
1612 beanClass,
1613 descriptors[i].getPropertyType(),
1614 descriptors[i].getName())) {
1615 propertyDescriptors.add( descriptors[i] );
1616 }
1617 }
1618 }
1619
1620
1621 BeanInfo[] additionals = beanInfo.getAdditionalBeanInfo();
1622 if ( additionals != null ) {
1623 for ( int i=0, outerSize=additionals.length; i<outerSize; i++ ) {
1624 BeanInfo additionalInfo = additionals[i];
1625 descriptors = additionalInfo.getPropertyDescriptors();
1626 if ( descriptors != null ) {
1627 for (int j=0, innerSize=descriptors.length; j<innerSize; j++) {
1628 if (!getConfiguration().getPropertySuppressionStrategy()
1629 .suppressProperty(
1630 beanClass,
1631 descriptors[j].getPropertyType(),
1632 descriptors[j].getName())) {
1633 propertyDescriptors.add( descriptors[j] );
1634 }
1635 }
1636 }
1637 }
1638 }
1639
1640 addAllSuperinterfaces(beanClass, propertyDescriptors);
1641
1642
1643 properties = new BeanProperty[ propertyDescriptors.size() ];
1644 int count = 0;
1645 for ( Iterator it = propertyDescriptors.iterator(); it.hasNext(); count++) {
1646 PropertyDescriptor propertyDescriptor = (PropertyDescriptor) it.next();
1647 properties[count] = new BeanProperty( propertyDescriptor );
1648 }
1649 }
1650 return properties;
1651 }
1652
1653
1654
1655
1656
1657
1658
1659
1660 private void addAllSuperinterfaces(Class clazz, ArrayList propertyDescriptors) {
1661 if (clazz.isInterface()) {
1662 Class[] superinterfaces = clazz.getInterfaces();
1663 for (int i=0, size=superinterfaces.length; i<size; i++) {
1664 try {
1665
1666 BeanInfo beanInfo;
1667 if( getConfiguration().ignoreAllBeanInfo() ) {
1668 beanInfo = Introspector.getBeanInfo( superinterfaces[i], Introspector.IGNORE_ALL_BEANINFO );
1669 }
1670 else {
1671 beanInfo = Introspector.getBeanInfo( superinterfaces[i] );
1672 }
1673 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
1674 for (int j=0, descriptorLength=descriptors.length; j<descriptorLength ; j++) {
1675 if (!getConfiguration().getPropertySuppressionStrategy()
1676 .suppressProperty(
1677 beanClass,
1678 descriptors[j].getPropertyType(),
1679 descriptors[j].getName())) {
1680 propertyDescriptors.add( descriptors[j] );
1681 }
1682 }
1683 addAllSuperinterfaces(superinterfaces[i], propertyDescriptors);
1684
1685 } catch (IntrospectionException ex) {
1686 log.info("Introspection on superinterface failed.", ex);
1687 }
1688 }
1689 }
1690 }
1691
1692 }
1693
1694
1695 private class DynaClassBeanType extends BeanType {
1696
1697 private DynaClass dynaClass;
1698
1699 private BeanProperty[] properties;
1700
1701
1702
1703
1704
1705 public DynaClassBeanType(DynaClass dynaClass) {
1706 this.dynaClass = dynaClass;
1707 DynaProperty[] dynaProperties = dynaClass.getDynaProperties();
1708 properties = new BeanProperty[dynaProperties.length];
1709 for (int i=0, size=dynaProperties.length; i<size; i++) {
1710 properties[i] = new BeanProperty(dynaProperties[i]);
1711 }
1712 }
1713
1714
1715 public String getBeanName() {
1716 return dynaClass.getName();
1717 }
1718
1719 public Class getElementType() {
1720 return DynaClass.class;
1721 }
1722
1723 public boolean isPrimitiveType() {
1724 return false;
1725 }
1726
1727 public boolean isMapType() {
1728 return false;
1729 }
1730
1731 public boolean isLoopType() {
1732 return false;
1733 }
1734
1735 public BeanProperty[] getProperties() {
1736 return properties;
1737 }
1738 }
1739 }