1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.xml.util;
17
18 import org.exolab.castor.mapping.ClassDescriptor;
19 import org.exolab.castor.mapping.FieldDescriptor;
20 import org.exolab.castor.mapping.FieldHandler;
21 import org.exolab.castor.mapping.MapItem;
22 import org.exolab.castor.mapping.MappingException;
23 import org.exolab.castor.mapping.loader.FieldDescriptorImpl;
24 import org.exolab.castor.mapping.loader.FieldHandlerImpl;
25 import org.exolab.castor.xml.FieldValidator;
26 import org.exolab.castor.xml.NodeType;
27 import org.exolab.castor.xml.XMLClassDescriptor;
28 import org.exolab.castor.xml.XMLFieldDescriptor;
29 import org.exolab.castor.xml.descriptors.CoreDescriptors;
30 import org.exolab.castor.xml.handlers.DateFieldHandler;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.StringTokenizer;
35 import java.util.Properties;
36
37
38
39
40
41
42
43
44
45
46
47
48 public class XMLFieldDescriptorImpl extends FieldDescriptorImpl implements XMLFieldDescriptor {
49 private static final String WILD_CARD = "*";
50
51 private static final String NULL_CLASS_ERR
52 = "The 'type' argument passed to the constructor of "
53 + "XMLFieldDescriptorImpl may not be null.";
54
55 private static final String NULL_NAME_ERR
56 = "The 'fieldName' argument passed to the constructor of "
57 + "XMLFieldDescriptorImpl may not be null.";
58
59
60
61
62
63
64 private int _argIndex = -1;
65
66
67 private boolean _container = false;
68
69
70
71
72 private boolean _incremental = false;
73
74
75 public boolean _isReference = false;
76
77 private boolean _isWild = false;
78
79
80 private boolean _mapped = false;
81
82 private String[] _matches = null;
83
84
85 private boolean _nillable = false;
86
87
88 private NodeType _nodeType = null;
89
90
91 private String _nsPrefix = null;
92
93
94 private String _nsURI = null;
95
96
97 private Properties _xmlProperties = null;
98
99
100 private String _schemaType = null;
101
102
103 private String _componentType = null;
104
105
106 private String _qNamePrefix = null;
107
108
109 private boolean _useParentClassNamespace = false;
110
111 private FieldValidator _validator = null;
112
113
114
115
116
117 private String _xmlName = null;
118
119
120
121
122 private String _xmlPath = null;
123
124 private List<String> _substitutes;
125
126
127
128
129
130 private boolean _derivedFromXSList;
131
132
133
134
135
136
137 public XMLFieldDescriptorImpl(final Class<?> fieldType, final String fieldName,
138 final String xmlName, final NodeType nodeType) {
139 _matches = new String[0];
140
141 if (fieldName == null) {
142 throw new IllegalArgumentException(NULL_NAME_ERR);
143 }
144 if (fieldType == null) {
145 throw new IllegalArgumentException(NULL_CLASS_ERR);
146 }
147
148 setFieldName(fieldName);
149
150 if (fieldType == org.exolab.castor.types.AnyNode.class) {
151
152
153 setFieldType(java.lang.Object.class);
154 } else {
155 setFieldType(fieldType);
156 }
157
158 _nodeType = ((nodeType == null) ? NodeType.Attribute : nodeType );
159
160
161 setXMLName(xmlName);
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175 public XMLFieldDescriptorImpl(final FieldDescriptor fieldDesc, final String xmlName,
176 final NodeType nodeType, final NodeType primitiveNodeType) throws MappingException {
177 _matches = new String[0];
178
179 if (fieldDesc instanceof XMLFieldDescriptor) {
180 setContainingClassDescriptor(fieldDesc.getContainingClassDescriptor());
181 }
182
183 setFieldName(fieldDesc.getFieldName());
184
185 if (fieldDesc.getFieldType() == org.exolab.castor.types.AnyNode.class) {
186
187
188 setFieldType(java.lang.Object.class);
189 } else {
190 setFieldType(fieldDesc.getFieldType());
191 }
192
193 ClassDescriptor cd = fieldDesc.getClassDescriptor();
194 if (cd != null) {
195 if (cd instanceof XMLClassDescriptor) {
196 setClassDescriptor(cd);
197 } else {
198 setClassDescriptor(new XMLClassDescriptorAdapter(cd, null, primitiveNodeType));
199 }
200 }
201
202 setHandler(fieldDesc.getHandler());
203
204
205
206 if (getFieldType() != null) {
207 if (java.util.Date.class.isAssignableFrom(getFieldType())) {
208 if (getHandler() instanceof FieldHandlerImpl) {
209 setHandler(new DateFieldHandler(getHandler()));
210 }
211 }
212 }
213
214 setTransient(fieldDesc.isTransient());
215 setImmutable(fieldDesc.isImmutable());
216 setRequired(fieldDesc.isRequired());
217 setMultivalued(fieldDesc.isMultivalued());
218
219
220 if (xmlName == null) {
221 setXMLName(getFieldName());
222 } else {
223 setXMLName(xmlName);
224 }
225
226 if (nodeType == null) {
227 if (isMultivalued()) {
228 _nodeType = NodeType.Element;
229 } else {
230 _nodeType = NodeType.Attribute;
231 }
232 } else {
233 _nodeType = nodeType;
234 }
235
236 if (isRequired()) {
237 _validator = new FieldValidator();
238 _validator.setMinOccurs(1);
239 _validator.setDescriptor(this);
240 }
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public void setConstructorArgumentIndex(final int index) {
256 if (_nodeType != NodeType.Attribute) {
257 String err = "constructor arguments only valid for attribute mapped fields.";
258 throw new IllegalStateException(err);
259 }
260 _argIndex = index;
261 }
262
263
264
265
266
267 public int getConstructorArgumentIndex() {
268 return _argIndex;
269 }
270
271
272
273
274
275 public boolean isConstructorArgument() {
276 return (_argIndex >= 0);
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 public void setLocationPath(final String path) {
317
318 _xmlPath = path;
319 }
320
321
322
323
324
325 public String getLocationPath() {
326 return _xmlPath;
327 }
328
329
330
331
332
333
334
335 public void setNameSpacePrefix(final String nsPrefix) {
336 _nsPrefix = nsPrefix;
337 }
338
339
340
341
342
343 public String getNameSpacePrefix() {
344 return _nsPrefix;
345 }
346
347
348
349
350
351
352 public void setUseParentsNamespace(final boolean useParentsNamespace) {
353 _useParentClassNamespace = useParentsNamespace;
354 }
355
356
357
358
359
360
361
362 public void setNameSpaceURI(final String nsURI) {
363 _nsURI = nsURI;
364 }
365
366
367
368
369
370 public String getNameSpaceURI() {
371 ClassDescriptor parent = getContainingClassDescriptor();
372 if ((_nsURI == null) && (parent != null) && _useParentClassNamespace) {
373 Class<?> type = getFieldType();
374 boolean test = ! (isAnyNode(type));
375 if ((_nodeType == NodeType.Element) && test) {
376 if (parent instanceof XMLClassDescriptor) {
377 return ((XMLClassDescriptor) parent).getNameSpaceURI();
378 }
379 }
380 }
381 return _nsURI;
382 }
383
384
385
386
387
388
389 public void setNodeType(final NodeType nodeType) {
390 _nodeType = ((nodeType == null) ? NodeType.Attribute : nodeType);
391 }
392
393
394
395
396
397 public NodeType getNodeType() {
398 return _nodeType;
399 }
400
401
402
403
404
405
406
407
408 public void setXMLProperty(final String propertyName, final String value) {
409 if (propertyName == null) {
410 String err = "The argument 'propertyName' must not be null.";
411 throw new IllegalArgumentException(err);
412 }
413
414 if (_xmlProperties == null) {
415 _xmlProperties = new Properties();
416 }
417
418 if (value == null) {
419 _xmlProperties.remove(propertyName);
420 } else {
421 _xmlProperties.put(propertyName, value);
422 }
423 }
424
425
426
427
428
429 public String getXMLProperty(final String propertyName) {
430 if ((_xmlProperties == null) || (propertyName == null)) {
431 return null;
432 }
433 return _xmlProperties.getProperty(propertyName);
434 }
435
436
437
438
439
440
441
442 public void setSchemaType(final String schemaType) {
443 _schemaType =schemaType;
444 }
445
446
447
448
449
450
451
452 public void setComponentType(final String componentType) {
453 _componentType = componentType;
454 }
455
456
457
458
459
460 public String getSchemaType() {
461 return _schemaType;
462 }
463
464
465
466
467
468 public String getComponentType() {
469 return _componentType;
470 }
471
472 public void setValidator(final FieldValidator validator) {
473 if (_validator != null) {
474 _validator.setDescriptor(null);
475 }
476 _validator = validator;
477 if (_validator != null) {
478 _validator.setDescriptor(this);
479 }
480 }
481
482
483
484
485
486 public FieldValidator getValidator() {
487 return _validator;
488 }
489
490
491
492
493
494
495 public void setXMLName(final String xmlName) {
496 _xmlName = xmlName;
497 }
498
499
500
501
502
503 public String getXMLName() {
504 return _xmlName;
505 }
506
507
508
509
510
511
512
513 public void setContainer(final boolean isContainer) {
514 _container = isContainer;
515 }
516
517
518
519
520
521 public boolean isContainer() {
522 return _container;
523 }
524
525
526
527
528
529
530
531
532
533 public void setIncremental(final boolean incremental) {
534 _incremental = incremental;
535 }
536
537
538
539
540
541 public boolean isIncremental() {
542 return _incremental;
543 }
544
545
546
547
548
549
550
551 public void setMapped(final boolean mapped) {
552 _mapped = mapped;
553 }
554
555
556
557
558
559 public boolean isMapped() {
560 return _mapped;
561 }
562
563
564
565
566
567
568
569
570
571
572 public void setNillable(final boolean nillable) {
573 _nillable = nillable;
574 }
575
576
577
578
579
580 public boolean isNillable() {
581 return _nillable;
582 }
583
584
585
586
587
588
589
590 public void setReference(final boolean isReference) {
591 _isReference = isReference;
592 }
593
594
595
596
597
598 public boolean isReference() {
599 return _isReference;
600 }
601
602
603
604
605
606
607
608 public void setQNamePrefix(final String qNamePrefix) {
609 _qNamePrefix = qNamePrefix;
610 }
611
612
613
614
615
616
617
618
619 public String getQNamePrefix() {
620 return _qNamePrefix;
621 }
622
623
624
625
626
627
628
629
630 public void setMatches(String matchExpr) {
631 _isWild = false;
632
633 if ((matchExpr == null) || (matchExpr.length() == 0)) {
634 return;
635 }
636
637 StringTokenizer st = new StringTokenizer(matchExpr);
638 List<String> names = new ArrayList<String>();
639 while (st.hasMoreTokens()) {
640 String token = st.nextToken();
641 if (WILD_CARD.equals(token)) {
642 _isWild = true;
643 break;
644 }
645 names.add(token);
646 }
647 _matches = new String[names.size()];
648 names.toArray(_matches);
649 }
650
651
652
653
654
655 public boolean matches(final String xmlName) {
656 if (xmlName != null) {
657 if (_isWild) {
658 return true;
659 } else if (_matches.length > 0) {
660 for (int i = 0; i < _matches.length; i++) {
661 if (xmlName.equals(_matches[i])) {
662 return true;
663 }
664 }
665 } else {
666 return xmlName.equals(_xmlName);
667 }
668 }
669 return false;
670 }
671
672
673
674
675
676 public boolean matches(final String xmlName, final String namespace) {
677
678 if (namespace == null) {
679 if ((_nsURI != null) && (_nsURI.length() > 0)) {
680 return false;
681 }
682 } else if (_nsURI == null) {
683 if ((namespace.length() > 0) && (!_isWild)) {
684 return false;
685 }
686 } else if (!_nsURI.equals(namespace)) {
687 return false;
688 }
689
690
691 return matches(xmlName);
692 }
693
694
695
696
697
698
699
700
701 public boolean equals(final Object obj) {
702 if (obj == this) {
703 return true;
704 }
705
706 if ((obj == null) || (!(obj instanceof XMLFieldDescriptor))) {
707 return false;
708 }
709
710 XMLFieldDescriptor descriptor = (XMLFieldDescriptor) obj;
711
712
713 if (!getFieldName().equals(descriptor.getFieldName())) {
714 return false;
715 }
716
717
718 if (!getFieldType().equals(descriptor.getFieldType())) {
719 return false;
720 }
721
722
723 FieldHandler tmpHandler = descriptor.getHandler();
724 if (getHandler() == null) {
725 return (tmpHandler == null);
726 } else if (tmpHandler == null) {
727 return false;
728 }
729
730
731
732
733
734
735 return (getHandler().getClass().isInstance(tmpHandler));
736 }
737
738
739
740
741
742 public int hashCode() {
743 int hash = 17;
744 hash = 17 * getFieldName().hashCode();
745 hash = hash * 17 * getFieldType().hashCode();
746 if (getHandler() != null) { hash = hash * 17 * getHandler().hashCode(); }
747 return hash;
748 }
749
750 public String toString() {
751 StringBuffer buffer = new StringBuffer(32);
752 buffer.append("XMLFieldDesciptor: ");
753 buffer.append(getFieldName());
754 buffer.append(" AS ");
755 buffer.append(_xmlName);
756 if (getNameSpaceURI() != null) {
757 buffer.append("{" + getNameSpaceURI() + "}");
758 }
759 return buffer.toString();
760 }
761
762
763
764
765
766
767
768
769 private static boolean isPrimitive(final Class<?> type) {
770 if (type == null) {
771 return false;
772 }
773 if (type.isPrimitive()) {
774 return true;
775 }
776 if (type == String.class) {
777 return true;
778 }
779 if ((type == Boolean.class) || (type == Character.class)) {
780 return true;
781 }
782
783
784 return (type.getSuperclass() == Number.class);
785 }
786
787
788
789
790
791
792
793
794 private static boolean isBuiltInType(final Class<?> type) {
795 if (type == null) {
796 return false;
797 }
798
799
800 return (CoreDescriptors.getDescriptor(type) != null);
801 }
802
803 private static boolean isMappedItem(final Class<?> fieldType) {
804 return (fieldType == MapItem.class);
805 }
806
807 private static boolean isAnyNode(final Class<?> type) {
808 return (type == Object.class);
809 }
810
811
812
813
814
815 public List<String> getSubstitutes() {
816 return _substitutes;
817 }
818
819
820
821
822
823 public void setSubstitutes(List<String> substitutes) {
824 _substitutes = substitutes;
825 }
826
827
828
829
830
831
832 public void setDerivedFromXSList(final boolean derivedFromXSList) {
833 _derivedFromXSList = derivedFromXSList;
834 }
835
836
837
838
839
840
841 public boolean isDerivedFromXSList() {
842 return _derivedFromXSList;
843 }
844
845 }