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.jibx.runtime.EnumSet;
032: import org.jibx.runtime.IUnmarshallingContext;
033: import org.jibx.util.Types;
034:
035: /**
036: * Base class for all standard binding customizations that can contain other
037: * customizations.
038: */
039: public abstract class NestingBase extends SharedNestingBase {
040: private static final String[] EMPTY_STRING_ARRAY = {};
041:
042: // value style value set information
043: public static final int ATTRIBUTE_VALUE_STYLE = 0;
044: public static final int ELEMENT_VALUE_STYLE = 1;
045: public static final int TEXT_VALUE_STYLE = 2;
046:
047: public static final EnumSet s_valueStyleEnum = new EnumSet(
048: ATTRIBUTE_VALUE_STYLE, new String[] { "attribute",
049: "element", "text" });
050:
051: // values inherited through nesting
052: private Integer m_valueStyle;
053: private Boolean m_propertyAccess;
054: private String[] m_stripPrefixes;
055: private String[] m_stripSuffixes;
056: private Boolean m_mapAbstract;
057:
058: /**
059: * Constructor.
060: *
061: * @param parent
062: */
063: public NestingBase(SharedNestingBase parent) {
064: super (parent);
065: }
066:
067: //
068: // Access methods for values inherited through nesting
069:
070: /**
071: * Check abstract mapping flag.
072: *
073: * @return abstract mapping flag
074: */
075: public boolean isMapAbstract() {
076: if (m_mapAbstract == null) {
077: if (getParent() == null) {
078: return false;
079: } else {
080: return ((NestingBase) getParent()).isMapAbstract();
081: }
082: } else {
083: return m_mapAbstract.booleanValue();
084: }
085: }
086:
087: /**
088: * Set abstract mapping flag.
089: *
090: * @param abs
091: */
092: public void setMapAbstract(Boolean abs) {
093: m_mapAbstract = abs;
094: }
095:
096: /**
097: * Check property access mode flag.
098: *
099: * @return <code>true</code> if bean-style get/set methods to be used,
100: * <code>false</code> if fields to be used directly
101: */
102: public boolean isPropertyAccess() {
103: if (m_propertyAccess == null) {
104: if (getParent() == null) {
105: return false;
106: } else {
107: return ((NestingBase) getParent()).isPropertyAccess();
108: }
109: } else {
110: return m_propertyAccess.booleanValue();
111: }
112: }
113:
114: /**
115: * Get prefixes to be stripped from field names.
116: *
117: * @return strip prefixes (<code>null</code> if none)
118: */
119: public String[] getStripPrefixes() {
120: if (m_stripPrefixes == null) {
121: if (getParent() == null) {
122: return EMPTY_STRING_ARRAY;
123: } else {
124: return ((NestingBase) getParent()).getStripPrefixes();
125: }
126: } else {
127: return m_stripPrefixes;
128: }
129: }
130:
131: /**
132: * Get suffixes to be stripped from field names.
133: *
134: * @return strip suffix (<code>null</code> if none)
135: */
136: public String[] getStripSuffixes() {
137: if (m_stripSuffixes == null) {
138: if (getParent() == null) {
139: return EMPTY_STRING_ARRAY;
140: } else {
141: return ((NestingBase) getParent()).getStripSuffixes();
142: }
143: } else {
144: return m_stripSuffixes;
145: }
146: }
147:
148: /**
149: * Get value style code.
150: *
151: * @param type value type name
152: * @return value from {@link NestingBase#s_valueStyleEnum} enumeration
153: */
154: public int getValueStyle(String type) {
155: if (m_valueStyle == null) {
156: if (getParent() == null) {
157: if (!"java.lang.String".equals(type)
158: && Types.isSimpleValue(type)) {
159: return ATTRIBUTE_VALUE_STYLE;
160: } else {
161: return ELEMENT_VALUE_STYLE;
162: }
163: } else {
164: return ((NestingBase) getParent()).getValueStyle(type);
165: }
166: } else {
167: return m_valueStyle.intValue();
168: }
169: }
170:
171: /**
172: * Set value style.
173: *
174: * @param style (<code>null</code> if none at this level)
175: */
176: public void setValueStyle(Integer style) {
177: m_valueStyle = style;
178: }
179:
180: /**
181: * Value style set text method. This is intended for use during unmarshalling.
182: * TODO: add validation
183: *
184: * @param text
185: * @param ictx
186: */
187: private void setValueStyleText(String text,
188: IUnmarshallingContext ictx) {
189: if (text == null) {
190: m_valueStyle = null;
191: } else {
192: m_valueStyle = new Integer(s_valueStyleEnum.getValue(text));
193: }
194: }
195:
196: /**
197: * Value style get text method. This is intended for use during marshalling.
198: *
199: * @return text
200: */
201: private String getValueStyleText() {
202: if (m_valueStyle == null) {
203: return null;
204: } else {
205: return s_valueStyleEnum.getName(m_valueStyle.intValue());
206: }
207: }
208: }
|