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 CollectionPropertyCustom extends MemberPropertyCustom {
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 CollectionPropertyCustom(NestingBase parent) {
049: super (parent);
050: }
051:
052: /**
053: * Constructor with name known.
054: *
055: * @param parent
056: * @param name
057: */
058: protected CollectionPropertyCustom(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 access method information.
091: *
092: * @param gmeth read access method (<code>null</code> if none)
093: * @param smeth write access method (<code>null</code> if none)
094: * @param type value type
095: * @param req required member flag (<code>null</code> if unknown)
096: */
097: /*package*/void completeProperty(IClassItem gmeth,
098: IClassItem smeth, String type, Boolean req) {
099: super .completeProperty(gmeth, smeth, type, req);
100: if (type != null) {
101:
102: // set information from field
103: String tname = getWorkingType();
104: if (m_itemType == null) {
105: if (tname.endsWith("[]")) {
106: m_itemType = tname.substring(0, tname.length() - 2);
107: } else {
108: String sig;
109: if (gmeth == null) {
110: sig = smeth.getGenericsSignature();
111: } else {
112: sig = gmeth.getGenericsSignature();
113: }
114: int start = sig.indexOf('<');
115: int end = sig.lastIndexOf('>');
116: if (start > 0 && end > 0 && start < end) {
117: String tsig = sig.substring(start + 1, end);
118: if (tsig.indexOf('<') < 0) {
119: m_itemType = Type.getType(tsig).toString();
120: }
121: }
122: }
123: }
124: if (m_itemName == null) {
125: m_itemName = deriveItemName(getXmlName(), m_itemType,
126: getParent().getNameStyle());
127: }
128: }
129: if (m_itemType == null) {
130: m_itemType = "java.lang.Object";
131: }
132: if (m_itemName == null) {
133: m_itemName = "item";
134: }
135: }
136:
137: private static CollectionPropertyCustom factory(
138: IUnmarshallingContext ictx) {
139: return new CollectionPropertyCustom(getContainingClass(ictx));
140: }
141: }
|