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.IUnmarshallingContext;
032:
033: /**
034: * Base class for all binding customizations that can contain other
035: * customizations. This includes inherited values shared with customization
036: * extensions (in particular, the WSDL extensions).
037: */
038: public abstract class SharedNestingBase extends CustomBase {
039: // values inherited through nesting
040: private String m_namespace;
041: private Integer m_namespaceStyle;
042: private Integer m_nameStyle;
043: private Integer m_require;
044:
045: // value set by subclasses
046: private String m_actualNamespace;
047:
048: /**
049: * Constructor.
050: *
051: * @param parent
052: */
053: public SharedNestingBase(SharedNestingBase parent) {
054: super (parent);
055: }
056:
057: //
058: // Access methods for values inherited through nesting
059:
060: /**
061: * Get namespace specified on this element. This method is only intended for
062: * use by subclasses - otherwise the {@link #getNamespace()} method should
063: * instead be used.
064: *
065: * @return namespace (<code>null</code> if none)
066: */
067: protected String getSpecifiedNamespace() {
068: return m_namespace;
069: }
070:
071: /**
072: * Get namespace style.
073: *
074: * @return namespace style
075: */
076: public int getNamespaceStyle() {
077: if (m_namespaceStyle == null) {
078: if (getParent() == null) {
079: return DERIVE_BY_PACKAGE;
080: } else {
081: return getParent().getNamespaceStyle();
082: }
083: } else {
084: return m_namespaceStyle.intValue();
085: }
086: }
087:
088: /**
089: * Set name style.
090: *
091: * @param style (<code>null</code> if none at this level)
092: */
093: public void setNamespaceStyle(Integer style) {
094: m_namespaceStyle = style;
095: }
096:
097: /**
098: * Get name style.
099: *
100: * @return name style
101: */
102: public int getNameStyle() {
103: if (m_nameStyle == null) {
104: if (getParent() == null) {
105: return HYPHENATED_NAMES;
106: } else {
107: return getParent().getNameStyle();
108: }
109: } else {
110: return m_nameStyle.intValue();
111: }
112: }
113:
114: /**
115: * Set name style.
116: *
117: * @param style (<code>null</code> if none at this level)
118: */
119: public void setNameStyle(Integer style) {
120: m_nameStyle = style;
121: }
122:
123: /**
124: * Get the namespace for schema definitions. This value must be set by
125: * subclasses using the {@link #setNamespace(String)} method.
126: *
127: * @return schema namespace
128: */
129: public final String getNamespace() {
130: return m_actualNamespace;
131: }
132:
133: /**
134: * Set the namespace to be used for schema definitions. This method is only
135: * intended for use by subclasses.
136: *
137: * @param ns
138: */
139: protected void setNamespace(String ns) {
140: m_actualNamespace = ns;
141: }
142:
143: /**
144: * Check if primitive value should be treated as required.
145: *
146: * @param type primitive type
147: * @return <code>true</code> if required value, <code>false</code> if not
148: */
149: public boolean isPrimitiveRequired(String type) {
150: if (m_require == null) {
151: if (getParent() == null) {
152: return true;
153: } else {
154: return getParent().isPrimitiveRequired(type);
155: }
156: } else {
157: return m_require.intValue() == REQUIRE_ALL
158: || m_require.intValue() == REQUIRE_PRIMITIVES;
159: }
160: }
161:
162: /**
163: * Check if object value should be treated as required.
164: *
165: * @param type object type
166: * @return <code>true</code> if required value, <code>false</code> if not
167: */
168: public boolean isObjectRequired(String type) {
169: if (m_require == null) {
170: if (getParent() == null) {
171: return false;
172: } else {
173: return getParent().isObjectRequired(type);
174: }
175: } else {
176: return m_require.intValue() == REQUIRE_ALL
177: || m_require.intValue() == REQUIRE_OBJECTS;
178: }
179: }
180:
181: /**
182: * Convert class or unprefixed field name to element or attribute name using
183: * current format.
184: *
185: * @param base class or simple field name to be converted
186: * @return element or attribute name
187: */
188: public String convertName(String base) {
189: return convertName(base, getNameStyle());
190: }
191:
192: /**
193: * Name style set text method. This is intended for use during unmarshalling.
194: * TODO: add validation
195: *
196: * @param text
197: * @param ictx
198: */
199: private void setNameStyleText(String text,
200: IUnmarshallingContext ictx) {
201: if (text == null) {
202: m_nameStyle = null;
203: } else {
204: m_nameStyle = new Integer(s_nameStyleEnum.getValue(text));
205: }
206: }
207:
208: /**
209: * Name style get text method. This is intended for use during marshalling.
210: *
211: * @return text
212: */
213: private String getNameStyleText() {
214: if (m_nameStyle == null) {
215: return null;
216: } else {
217: return s_nameStyleEnum.getName(m_nameStyle.intValue());
218: }
219: }
220:
221: /**
222: * Namespace style set text method. This is intended for use during
223: * unmarshalling.
224: * TODO: add validation
225: *
226: * @param text
227: * @param ictx
228: */
229: private void setNamespaceStyleText(String text,
230: IUnmarshallingContext ictx) {
231: if (text == null) {
232: m_namespaceStyle = null;
233: } else {
234: m_namespaceStyle = new Integer(s_namespaceStyleEnum
235: .getValue(text));
236: }
237: }
238:
239: /**
240: * Namespace style get text method. This is intended for use during
241: * marshalling.
242: *
243: * @return text
244: */
245: private String getNamespaceStyleText() {
246: if (m_namespaceStyle == null) {
247: return null;
248: } else {
249: return s_namespaceStyleEnum.getName(m_namespaceStyle
250: .intValue());
251: }
252: }
253:
254: /**
255: * Required set text method. This is intended for use during unmarshalling.
256: * TODO: add validation
257: *
258: * @param text
259: * @param ictx
260: */
261: private void setRequireText(String text, IUnmarshallingContext ictx) {
262: if (text == null) {
263: m_require = null;
264: } else {
265: m_require = new Integer(s_requireEnum.getValue(text));
266: }
267: }
268:
269: /**
270: * Required get text method. This is intended for use during marshalling.
271: *
272: * @return text
273: */
274: private String getRequireText() {
275: if (m_require == null) {
276: return null;
277: } else {
278: return s_requireEnum.getName(m_require.intValue());
279: }
280: }
281: }
|