001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.generator;
030:
031: import org.apache.bcel.generic.Type;
032: import org.jibx.binding.model.IClassItem;
033: import org.jibx.runtime.IUnmarshallingContext;
034:
035: /**
036: * Collection field customization information.
037: */
038: public class CollectionFieldCustom extends MemberFieldCustom {
039: // values specific to member level
040: private String m_itemType;
041: private String m_itemName;
042:
043: /**
044: * Constructor.
045: *
046: * @param parent
047: */
048: protected CollectionFieldCustom(NestingBase parent) {
049: super (parent);
050: }
051:
052: /**
053: * Constructor with name known.
054: *
055: * @param parent
056: * @param name
057: */
058: protected CollectionFieldCustom(NestingBase parent, String name) {
059: super (parent, name);
060: }
061:
062: /**
063: * Check if collection member.
064: *
065: * @return <code>true</code>
066: */
067: public boolean isCollection() {
068: return true;
069: }
070:
071: /**
072: * Get item type.
073: *
074: * @return item type (<code>null</code> if none)
075: */
076: public String getItemType() {
077: return m_itemType;
078: }
079:
080: /**
081: * Get item element name.
082: *
083: * @return item name (<code>null</code> if none)
084: */
085: public String getItemName() {
086: return m_itemName;
087: }
088:
089: /**
090: * Complete customization information based on field information.
091: *
092: * @param field (<code>null</code> if none available)
093: * @param req required member flag (<code>null</code> if unknown)
094: */
095: /*package*/void completeField(IClassItem field, Boolean req) {
096: super .completeField(field, req);
097: if (field != null) {
098:
099: // set information from field
100: String tname = getWorkingType();
101: if (m_itemType == null) {
102: if (tname.endsWith("[]")) {
103: m_itemType = tname.substring(0, tname.length() - 2);
104: } else {
105: String sig = field.getGenericsSignature();
106: if (sig != null) {
107: int start = sig.indexOf('<');
108: int end = sig.lastIndexOf('>');
109: if (start > 0 && end > 0 && start < end) {
110: String tsig = sig.substring(start + 1, end);
111: if (tsig.indexOf('<') < 0) {
112: m_itemType = Type.getType(tsig)
113: .toString();
114: }
115: }
116: }
117: }
118: }
119: if (m_itemName == null) {
120: m_itemName = deriveItemName(getXmlName(), m_itemType,
121: getParent().getNameStyle());
122: }
123: }
124: if (m_itemType == null) {
125: m_itemType = "java.lang.Object";
126: }
127: if (m_itemName == null) {
128: m_itemName = "item";
129: }
130: }
131:
132: private static CollectionFieldCustom factory(
133: IUnmarshallingContext ictx) {
134: return new CollectionFieldCustom(getContainingClass(ictx));
135: }
136: }
|