001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.schema;
020:
021: import javax.xml.namespace.QName;
022: import java.util.*;
023:
024: /**
025: * This class is used as a holder to pass on the meta information to the bean writer.
026: * This meta information is used by the writer to write the databinding conversion code.
027: * Note - Metainfholders are not meant to be reused!!!. They are per-class basis and are strictly
028: * not thread safe!!!!
029: */
030: public class BeanWriterMetaInfoHolder {
031:
032: protected boolean ordered = false;
033: protected boolean anonymous = false;
034: protected boolean choice = false;
035: protected boolean simple = false;
036:
037: protected boolean extension = false;
038: protected boolean restriction = false;
039: private String extensionClassName = "";
040: private String restrictionClassName = "";
041: private QName extensionBaseType = null;
042: private QName restrictionBaseType = null;
043: protected Map elementToSchemaQNameMap = new LinkedHashMap();
044: protected Map elementToJavaClassMap = new LinkedHashMap();
045: protected Map specialTypeFlagMap = new LinkedHashMap();
046: protected Map qNameMaxOccursCountMap = new LinkedHashMap();
047: protected Map qNameMinOccursCountMap = new LinkedHashMap();
048: protected Map qNameOrderMap = new LinkedHashMap();
049: protected QName ownQname = null;
050: protected String ownClassName = null;
051:
052: protected long lengthFacet = -1;
053: protected long maxLengthFacet = -1;
054: protected long minLengthFacet = -1;
055: protected ArrayList enumFacet = new ArrayList();
056: protected String patternFacet = null;
057: protected String maxExclusiveFacet = null;
058: protected String minExclusiveFacet = null;
059: protected String maxInclusiveFacet = null;
060: protected String minInclusiveFacet = null;
061:
062: protected Map memberTypes = new HashMap();
063: protected QName itemTypeQName;
064: protected String itemTypeClassName;
065: protected boolean isUnion;
066: protected boolean isList;
067:
068: protected boolean isParticleClass;
069: // keep whether this class has a partical class type variable
070: protected boolean hasParticleType;
071:
072: protected List nillableQNameList = new ArrayList();
073:
074: //the parent metainfo holder, useful in handling extensions and
075: //restrictions
076: protected BeanWriterMetaInfoHolder parent = null;
077:
078: public boolean isChoice() {
079: return choice;
080: }
081:
082: public void setChoice(boolean choice) {
083: this .choice = choice;
084: }
085:
086: public boolean isSimple() {
087: return simple;
088: }
089:
090: public void setSimple(boolean simple) {
091: this .simple = simple;
092: }
093:
094: public String getOwnClassName() {
095: return ownClassName;
096: }
097:
098: public void setOwnClassName(String ownClassName) {
099: this .ownClassName = ownClassName;
100: }
101:
102: public QName getOwnQname() {
103: return ownQname;
104: }
105:
106: public void setOwnQname(QName ownQname) {
107: this .ownQname = ownQname;
108: }
109:
110: /**
111: * Gets the parent
112: */
113: public BeanWriterMetaInfoHolder getParent() {
114: return parent;
115: }
116:
117: /**
118: * Gets the anonymous status.
119: *
120: * @return Returns boolean.
121: */
122: public boolean isAnonymous() {
123: return anonymous;
124: }
125:
126: /**
127: * Sets the anonymous flag.
128: *
129: * @param anonymous
130: */
131: public void setAnonymous(boolean anonymous) {
132: this .anonymous = anonymous;
133: }
134:
135: /**
136: * Sets the extensions base class name. Valid only when the isExtension
137: * returns true.
138: *
139: * @return Returns String.
140: */
141: public String getExtensionClassName() {
142: return extensionClassName;
143: }
144:
145: /**
146: * Sets the extensions base class name. Valid only when the isExtension
147: * returns true.
148: *
149: * @param extensionClassName
150: */
151: public void setExtensionClassName(String extensionClassName) {
152: this .extensionClassName = extensionClassName;
153: }
154:
155: /**
156: * Gets the extension status.
157: *
158: * @return Returns boolean.
159: */
160: public boolean isExtension() {
161: return extension;
162: }
163:
164: /**
165: * Sets the extension status.
166: *
167: * @param extension
168: */
169: public void setExtension(boolean extension) {
170: this .extension = extension;
171: }
172:
173: public String getRestrictionClassName() {
174: return restrictionClassName;
175: }
176:
177: /**
178: * Sets the restriction base class name. Valid only when the isRestriction
179: * returns true.
180: *
181: * @param restrictionClassName
182: */
183: public void setRestrictionClassName(String restrictionClassName) {
184: this .restrictionClassName = restrictionClassName;
185: }
186:
187: /**
188: * Gets the restriction status.
189: *
190: * @return Returns boolean.
191: */
192: public boolean isRestriction() {
193: return restriction;
194: }
195:
196: /**
197: * Sets the restriction status.
198: *
199: * @param restriction
200: */
201: public void setRestriction(boolean restriction) {
202: this .restriction = restriction;
203: }
204:
205: /**
206: * Sets the extension basetype.
207: *
208: * @param extensionBaseType
209: */
210: public void setExtensionBaseType(QName extensionBaseType) {
211: this .extensionBaseType = extensionBaseType;
212: }
213:
214: /**
215: * Checks if it is a extension base type.
216: *
217: * @param extensionBaseType
218: */
219: public boolean isExtensionBaseType(QName extensionBaseType) {
220: return (this .extensionBaseType == extensionBaseType);
221: }
222:
223: /**
224: * Sets the restriction basetype.
225: *
226: * @param restrictionBaseType
227: */
228: public void setRestrictionBaseType(QName restrictionBaseType) {
229: this .restrictionBaseType = restrictionBaseType;
230: }
231:
232: /**
233: * Checks if it is a restriction base type.
234: *
235: * @param restrictionBaseType
236: */
237: public boolean isRestrictionBaseType(QName restrictionBaseType) {
238: QName baseTypeQName = (QName) this .elementToSchemaQNameMap
239: .get(restrictionBaseType);
240: return (this .restrictionBaseType != null)
241: && (baseTypeQName != null)
242: && this .restrictionBaseType.equals(baseTypeQName);
243: }
244:
245: /**
246: * Gets the ordered status.
247: *
248: * @return Returns boolean.
249: */
250: public boolean isOrdered() {
251: return ordered;
252: }
253:
254: /**
255: * Sets the ordered flag.
256: *
257: * @param ordered
258: */
259: public void setOrdered(boolean ordered) {
260: this .ordered = ordered;
261: }
262:
263: /**
264: * Registers a mapping.
265: *
266: * @param qName
267: * @param schemaName
268: * @param javaClassName
269: */
270: public void registerMapping(QName qName, QName schemaName,
271: String javaClassName) {
272: registerMapping(qName, schemaName, javaClassName,
273: SchemaConstants.ELEMENT_TYPE);
274: }
275:
276: /**
277: * Registers a Qname as nillable
278: * The qName better be of an element
279: *
280: * @param qName
281: * @param schemaName
282: * @param javaClassName
283: */
284: public void registerNillableQName(QName eltQName) {
285: nillableQNameList.add(eltQName);
286: }
287:
288: /**
289: * Returns whether a QName is nillable or not
290: *
291: * @param eltQName
292: */
293: public boolean isNillable(QName eltQName) {
294: return nillableQNameList.contains(eltQName);
295: }
296:
297: /**
298: * Registers a mapping.
299: *
300: * @param qName
301: * @param schemaName
302: * @param javaClassName
303: * @param type
304: */
305: public void registerMapping(QName qName, QName schemaName,
306: String javaClassName, int type) {
307: this .elementToJavaClassMap.put(qName, javaClassName);
308: this .elementToSchemaQNameMap.put(qName, schemaName);
309: addtStatus(qName, type);
310:
311: }
312:
313: /**
314: * Gets the schema name for the given QName.
315: *
316: * @param eltQName
317: * @return Returns QName.
318: */
319: public QName getSchemaQNameForQName(QName eltQName) {
320: return (QName) this .elementToSchemaQNameMap.get(eltQName);
321: }
322:
323: /**
324: * Gets the class name for the QName.
325: *
326: * @param eltQName
327: * @return Returns String.
328: */
329: public String getClassNameForQName(QName eltQName) {
330: return (String) this .elementToJavaClassMap.get(eltQName);
331: }
332:
333: /**
334: * Gets whether a given QName is an attribute
335: *
336: * @param qName
337: * @return Returns boolean.
338: */
339: public boolean getAttributeStatusForQName(QName qName) {
340:
341: Integer state = (Integer) specialTypeFlagMap.get(qName);
342: return state != null
343: && getStatus(state.intValue(),
344: SchemaConstants.ATTRIBUTE_TYPE);
345: }
346:
347: /**
348: * checks the element corresponds to the qName type is xsd:anyType
349: *
350: * @param qName
351: * @return is element corresponds to qName has xsd:anyType
352: */
353:
354: public boolean getDefaultStatusForQName(QName qName) {
355: boolean isDefault = false;
356: QName schemaTypeQName = (QName) this .elementToSchemaQNameMap
357: .get(qName);
358: if (schemaTypeQName != null) {
359: isDefault = schemaTypeQName
360: .equals(SchemaConstants.XSD_ANYTYPE);
361: }
362: return isDefault;
363: }
364:
365: /**
366: * Gets whether a given QName represents a anyType
367: *
368: * @param qName
369: * @return Returns boolean.
370: */
371: public boolean getAnyStatusForQName(QName qName) {
372: Integer state = (Integer) specialTypeFlagMap.get(qName);
373: return state != null
374: && getStatus(state.intValue(), SchemaConstants.ANY_TYPE);
375: }
376:
377: /**
378: * Gets whether a given QName refers to an array.
379: *
380: * @param qName
381: * @return Returns boolean.
382: */
383: public boolean getArrayStatusForQName(QName qName) {
384: Integer state = (Integer) specialTypeFlagMap.get(qName);
385: return state != null
386: && getStatus(state.intValue(),
387: SchemaConstants.ARRAY_TYPE);
388: }
389:
390: /**
391: * Gets whether a given QName refers to binary.
392: *
393: * @param qName
394: * @return Returns boolean.
395: */
396: public boolean getBinaryStatusForQName(QName qName) {
397: Integer state = (Integer) specialTypeFlagMap.get(qName);
398: return state != null
399: && getStatus(state.intValue(),
400: SchemaConstants.BINARY_TYPE);
401: }
402:
403: /**
404: *
405: * @param qName
406: * @return is this a inner choice
407: */
408:
409: public boolean getInnerChoiceStatusForQName(QName qName) {
410: Integer state = (Integer) specialTypeFlagMap.get(qName);
411: return state != null
412: && getStatus(state.intValue(),
413: SchemaConstants.INNER_CHOICE_ELEMENT);
414: }
415:
416: /**
417: * Gets whether a given QName refers to Simple Type.
418: *
419: * @param qName
420: * @return Returns boolean.
421: */
422: public boolean getSimpleStatusForQName(QName qName) {
423: Integer state = (Integer) specialTypeFlagMap.get(qName);
424: return state != null
425: && getStatus(state.intValue(),
426: SchemaConstants.SIMPLE_TYPE_OR_CONTENT);
427: }
428:
429: /**
430: *
431: * @param qName
432: * @return whether the attribute is a partical class or not
433: */
434:
435: public boolean getParticleTypeStatusForQName(QName qName) {
436: Integer state = (Integer) specialTypeFlagMap.get(qName);
437: return state != null
438: && getStatus(state.intValue(),
439: SchemaConstants.PARTICLE_TYPE_ELEMENT);
440: }
441:
442: /**
443: * Gets whether a given QName has the any attribute status.
444: *
445: * @param qName
446: * @return Returns boolean.
447: */
448: public boolean getAnyAttributeStatusForQName(QName qName) {
449: return getArrayStatusForQName(qName)
450: && getAnyStatusForQName(qName);
451: }
452:
453: /**
454: * Gets whether a given QName has the optional attribute status.
455: *
456: * @param qName QName of attribute
457: * @return Returns <code>true</code> if attribute has optional status
458: */
459: public boolean getOptionalAttributeStatusForQName(QName qName) {
460: Integer state = (Integer) specialTypeFlagMap.get(qName);
461: return state != null
462: && getStatus(state.intValue(),
463: SchemaConstants.OPTIONAL_TYPE);
464: }
465:
466: /**
467: * Clears the whole set of tables.
468: */
469: public void clearTables() {
470: this .elementToJavaClassMap.clear();
471: this .elementToSchemaQNameMap.clear();
472: this .elementToSchemaQNameMap.clear();
473: this .elementToJavaClassMap.clear();
474: this .specialTypeFlagMap.clear();
475: this .qNameMaxOccursCountMap.clear();
476: this .qNameMinOccursCountMap.clear();
477: this .qNameOrderMap.clear();
478: }
479:
480: /**
481: * Adds the minOccurs associated with a QName.
482: *
483: * @param qName
484: * @param minOccurs
485: */
486: public void addMinOccurs(QName qName, long minOccurs) {
487: this .qNameMinOccursCountMap.put(qName, new Long(minOccurs));
488: }
489:
490: /**
491: * Registers a QName for the order.
492: *
493: * @param qName
494: * @param index
495: */
496: public void registerQNameIndex(QName qName, int index) {
497: this .qNameOrderMap.put(new Integer(index), qName);
498: }
499:
500: /**
501: * Adds the minOccurs associated with a QName.
502: *
503: * @param qName
504: * @return Returns long.
505: */
506: public long getMinOccurs(QName qName) {
507: Long l = (Long) this .qNameMinOccursCountMap.get(qName);
508: return l != null ? l.longValue() : 1; //default for min is 1
509: }
510:
511: /**
512: * Gets the maxOccurs associated with a QName.
513: *
514: * @param qName
515: * @return Returns long.
516: */
517: public long getMaxOccurs(QName qName) {
518: Long l = (Long) this .qNameMaxOccursCountMap.get(qName);
519: return l != null ? l.longValue() : 1; //default for max is 1
520: }
521:
522: /**
523: * Adds the maxOccurs associated with a QName.
524: *
525: * @param qName
526: * @param maxOccurs
527: */
528: public void addMaxOccurs(QName qName, long maxOccurs) {
529: this .qNameMaxOccursCountMap.put(qName, new Long(maxOccurs));
530: }
531:
532: /**
533: * @return Returns Iterator.
534: * @deprecated Use #getQNameArray
535: */
536: public Iterator getElementQNameIterator() {
537: return elementToJavaClassMap.keySet().iterator();
538: }
539:
540: /**
541: * Gets the QName array - may not be ordered.
542: *
543: * @return Returns QName[].
544: */
545: public QName[] getQNameArray() {
546: Set keySet = elementToJavaClassMap.keySet();
547: return (QName[]) keySet.toArray(new QName[keySet.size()]);
548: }
549:
550: /**
551: * Gets the ordered QName array - useful in sequences where the order needs to be preserved
552: * Note - #registerQNameIndex needs to be called if this is to work properly!
553: *
554: * @return Returns QName[].
555: */
556: public QName[] getOrderedQNameArray() {
557: //get the keys of the order map
558: Set set = qNameOrderMap.keySet();
559: int count = set.size();
560: Integer[] keys = (Integer[]) set.toArray(new Integer[count]);
561: Arrays.sort(keys);
562:
563: //Now refill the Ordered QName Array
564: List returnQNames = new ArrayList();
565: for (int i = 0; i < keys.length; i++) {
566: returnQNames.add(qNameOrderMap.get(keys[i]));
567: }
568:
569: //we've missed the attributes, so if there are attributes
570: //add them explicitly to the end of this list
571: QName[] allNames = getQNameArray();
572: for (int i = 0; i < allNames.length; i++) {
573: if (getAttributeStatusForQName(allNames[i])) {
574: returnQNames.add(allNames[i]);
575: }
576: }
577:
578: return (QName[]) returnQNames.toArray(new QName[returnQNames
579: .size()]);
580: }
581:
582: /**
583: * Finds the starting count for the addition of new items to the order
584: *
585: * @return the starting number for the sequence
586: */
587: public int getOrderStartPoint() {
588: return qNameOrderMap.size();
589: }
590:
591: /**
592: * Creates link to th
593: *
594: * @param metaInfo
595: */
596: public void setAsParent(BeanWriterMetaInfoHolder metaInfo) {
597: parent = metaInfo;
598: }
599:
600: /**
601: * Adds a another status to a particular Qname.
602: * A Qname can be associated with multiple status flags
603: * and they all will be preserved
604: *
605: * @param type
606: * @param mask
607: */
608:
609: public void addtStatus(QName type, int mask) {
610: Object obj = this .specialTypeFlagMap.get(type);
611: if (obj != null) {
612: int preValue = ((Integer) obj).intValue();
613: this .specialTypeFlagMap.put(type, new Integer(
614: (preValue | mask)));
615: } else {
616: this .specialTypeFlagMap.put(type, new Integer(mask));
617: }
618:
619: }
620:
621: private boolean getStatus(int storedStatus, int mask) {
622: //when the mask is anded with the status then we should get
623: //the mask it self!
624: return (mask == (mask & storedStatus));
625: }
626:
627: /**
628: * Sets the length facet.
629: *
630: * @param lengthFacet
631: */
632: public void setLengthFacet(long lengthFacet) {
633: this .lengthFacet = lengthFacet;
634: }
635:
636: /**
637: * Gets the length facet.
638: *
639: * @return Returns length facet.
640: */
641: public long getLengthFacet() {
642: return this .lengthFacet;
643: }
644:
645: /**
646: * Sets the maxExclusive.
647: *
648: * @param maxExclusiveFacet
649: */
650: public void setMaxExclusiveFacet(String maxExclusiveFacet) {
651: this .maxExclusiveFacet = maxExclusiveFacet;
652: }
653:
654: /**
655: * Gets the maxExclusive.
656: *
657: * @return Returns the maxExclusive.
658: */
659: public String getMaxExclusiveFacet() {
660: return this .maxExclusiveFacet;
661: }
662:
663: /**
664: * Sets the minExclusive.
665: *
666: * @param minExclusiveFacet
667: */
668: public void setMinExclusiveFacet(String minExclusiveFacet) {
669: this .minExclusiveFacet = minExclusiveFacet;
670: }
671:
672: /**
673: * Gets the minExclusive.
674: *
675: * @return Returns the minExclusive.
676: */
677: public String getMinExclusiveFacet() {
678: return this .minExclusiveFacet;
679: }
680:
681: /**
682: * Sets the maxInclusive.
683: *
684: * @param maxInclusiveFacet
685: */
686: public void setMaxInclusiveFacet(String maxInclusiveFacet) {
687: this .maxInclusiveFacet = maxInclusiveFacet;
688: }
689:
690: /**
691: * Gets the maxInclusive.
692: *
693: * @return Returns the maxInclusive.
694: */
695: public String getMaxInclusiveFacet() {
696: return this .maxInclusiveFacet;
697: }
698:
699: /**
700: * Sets the minInclusive.
701: *
702: * @param minInclusiveFacet
703: */
704: public void setMinInclusiveFacet(String minInclusiveFacet) {
705: this .minInclusiveFacet = minInclusiveFacet;
706: }
707:
708: /**
709: * Gets the minInclusive.
710: *
711: * @return Returns the minInclusive.
712: */
713: public String getMinInclusiveFacet() {
714: return this .minInclusiveFacet;
715: }
716:
717: /**
718: * Sets the maxLength.
719: *
720: * @param maxLengthFacet
721: */
722: public void setMaxLengthFacet(long maxLengthFacet) {
723: this .maxLengthFacet = maxLengthFacet;
724: }
725:
726: /**
727: * Gets the maxLength.
728: *
729: * @return Returns maxLength.
730: */
731: public long getMaxLengthFacet() {
732: return this .maxLengthFacet;
733: }
734:
735: /**
736: * Sets the minLength.
737: *
738: * @param minLengthFacet
739: */
740: public void setMinLengthFacet(long minLengthFacet) {
741: this .minLengthFacet = minLengthFacet;
742: }
743:
744: /**
745: * Gets the minLength.
746: *
747: * @return Returns minLength.
748: */
749: public long getMinLengthFacet() {
750: return this .minLengthFacet;
751: }
752:
753: /**
754: * Sets the enumeration.
755: *
756: * @param enumFacet
757: */
758: public void setEnumFacet(ArrayList enumFacet) {
759: this .enumFacet = enumFacet;
760: }
761:
762: /**
763: * Adds the enumeration.
764: *
765: * @param enumFacet
766: */
767: public void addEnumFacet(String enumFacet) {
768: this .enumFacet.add(enumFacet);
769: }
770:
771: /**
772: * Gets the enumeration.
773: *
774: * @return Returns enumeration.
775: */
776: public List getEnumFacet() {
777: return this .enumFacet;
778: }
779:
780: /**
781: * Sets the pattern.
782: *
783: * @param patternFacet
784: */
785: public void setPatternFacet(String patternFacet) {
786: this .patternFacet = patternFacet;
787: }
788:
789: /**
790: * Gets the pattern.
791: *
792: * @return Returns pattern.
793: */
794: public String getPatternFacet() {
795: return this .patternFacet;
796: }
797:
798: /**
799: *
800: * @return Returns is union
801: */
802:
803: public boolean isUnion() {
804: return isUnion;
805: }
806:
807: public void setUnion(boolean union) {
808: isUnion = union;
809: }
810:
811: /**
812: *
813: * @return Returns memeber type in a union
814: */
815:
816: public Map getMemberTypes() {
817: return memberTypes;
818: }
819:
820: public void setMemberTypes(Map memberTypes) {
821: this .memberTypes = memberTypes;
822: }
823:
824: public void addMemberType(QName qname, String className) {
825: this .memberTypes.put(qname, className);
826: }
827:
828: public boolean isList() {
829: return isList;
830: }
831:
832: public void setList(boolean list) {
833: isList = list;
834: }
835:
836: public QName getItemTypeQName() {
837: return itemTypeQName;
838: }
839:
840: public void setItemTypeQName(QName itemTypeQName) {
841: this .itemTypeQName = itemTypeQName;
842: }
843:
844: public String getItemTypeClassName() {
845: return itemTypeClassName;
846: }
847:
848: public void setItemTypeClassName(String itemTypeClassName) {
849: this .itemTypeClassName = itemTypeClassName;
850: }
851:
852: public boolean isParticleClass() {
853: return isParticleClass;
854: }
855:
856: public void setParticleClass(boolean particleClass) {
857: isParticleClass = particleClass;
858: }
859:
860: public boolean isHasParticleType() {
861: return hasParticleType;
862: }
863:
864: public void setHasParticleType(boolean hasParticleType) {
865: this.hasParticleType = hasParticleType;
866: }
867:
868: }
|