001: /*
002: Copyright (c) 2004-2005, 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.model;
030:
031: import org.jibx.binding.util.StringArray;
032: import org.jibx.runtime.IMarshallingContext;
033: import org.jibx.runtime.IUnmarshallingContext;
034: import org.jibx.runtime.JiBXException;
035: import org.jibx.runtime.QName;
036:
037: /**
038: * Model component for <b>format</b> element. This element defines conversion to
039: * and from simple unstructured text representations.
040: *
041: * @author Dennis M. Sosnoski
042: * @version 1.0
043: */
044:
045: public class FormatElement extends ElementBase {
046: /** Enumeration of allowed attribute names */
047: public static final StringArray s_allowedAttributes = new StringArray(
048: new String[] { "label", "type" },
049: StringAttributes.s_allowedAttributes);
050:
051: /** Format label. */
052: private String m_label;
053:
054: /** Format qualified name. */
055: private QName m_qname;
056:
057: /** Default format for type flag. */
058: private boolean m_isDefault;
059:
060: /** Name of value type. */
061: private String m_typeName;
062:
063: /** Value type information. */
064: private IClass m_type;
065:
066: /** String attributes information for value. */
067: private StringAttributes m_stringAttrs;
068:
069: /**
070: * Constructor.
071: */
072: public FormatElement() {
073: super (FORMAT_ELEMENT);
074: m_stringAttrs = new StringAttributes();
075: }
076:
077: /**
078: * Get format label.
079: *
080: * @return format label (<code>null</code> if none)
081: */
082: public String getLabel() {
083: return m_label;
084: }
085:
086: /**
087: * Set format label. This method changes the qualified name to match the
088: * label.
089: *
090: * @param label format label (<code>null</code> if none)
091: */
092: public void setLabel(String label) {
093: m_label = label;
094: m_qname = (label == null) ? null : new QName(label);
095: }
096:
097: /**
098: * Get format qualified name.
099: *
100: * @return format qualified name (<code>null</code> if none)
101: */
102: public QName getQName() {
103: return m_qname;
104: }
105:
106: /**
107: * Set format qualified name. This method changes the label value to match
108: * the qualified name.
109: *
110: * @return format qualified name (<code>null</code> if none)
111: */
112: public void setQName(QName qname) {
113: m_qname = qname;
114: m_label = (qname == null) ? null : qname.toString();
115: }
116:
117: /**
118: * Check if default format for type.
119: *
120: * @return <code>true</code> if default for type, <code>false</code> if not
121: */
122: public boolean isDefaultFormat() {
123: return m_isDefault;
124: }
125:
126: /**
127: * Set default format for type.
128: *
129: * @param dflt <code>true</code> if default for type, <code>false</code> if
130: * not
131: */
132: public void setDefaultFormat(boolean dflt) {
133: m_isDefault = dflt;
134: }
135:
136: /**
137: * Get value type. This method is only usable after a
138: * call to {@link #validate}.
139: *
140: * @return default value object
141: */
142: public IClass getType() {
143: return m_type;
144: }
145:
146: /**
147: * Get value type name.
148: *
149: * @return value type name
150: */
151: public String getTypeName() {
152: return m_typeName;
153: }
154:
155: /**
156: * Set value type name.
157: *
158: * @param value type name
159: */
160: public void setTypeName(String value) {
161: m_typeName = value;
162: }
163:
164: //
165: // String attribute delegate methods
166:
167: /**
168: * Get default value text.
169: *
170: * @return default value text
171: */
172: public String getDefaultText() {
173: return m_stringAttrs.getDefaultText();
174: }
175:
176: /**
177: * Get default value. This call is only meaningful after validation.
178: *
179: * @return default value object
180: */
181: public Object getDefault() {
182: return m_stringAttrs.getDefault();
183: }
184:
185: /**
186: * Set default value text.
187: *
188: * @param value default value text
189: */
190: public void setDefaultText(String value) {
191: m_stringAttrs.setDefaultText(value);
192: }
193:
194: /**
195: * Get serializer name.
196: *
197: * @return fully qualified class and method name for serializer (or
198: * <code>null</code> if none)
199: */
200: public String getSerializerName() {
201: return m_stringAttrs.getSerializerName();
202: }
203:
204: /**
205: * Get serializer method information. This call is only meaningful after
206: * validation.
207: *
208: * @return serializer information (or <code>null</code> if none)
209: */
210: public IClassItem getSerializer() {
211: return m_stringAttrs.getSerializer();
212: }
213:
214: /**
215: * Set serializer method name.
216: *
217: * @param fully qualified class and method name for serializer
218: */
219: public void setSerializerName(String name) {
220: m_stringAttrs.setSerializerName(name);
221: }
222:
223: /**
224: * Get deserializer name.
225: *
226: * @return fully qualified class and method name for deserializer (or
227: * <code>null</code> if none)
228: */
229: public String getDeserializerName() {
230: return m_stringAttrs.getDeserializerName();
231: }
232:
233: /**
234: * Get deserializer method information. This call is only meaningful after
235: * validation.
236: *
237: * @return deserializer information (or <code>null</code> if none)
238: */
239: public IClassItem getDeserializer() {
240: return m_stringAttrs.getDeserializer();
241: }
242:
243: /**
244: * Set deserializer method name.
245: *
246: * @param fully qualified class and method name for deserializer
247: */
248: public void setDeserializerName(String name) {
249: m_stringAttrs.setDeserializerName(name);
250: }
251:
252: /**
253: * Get base format information. This method is only usable after a
254: * call to {@link #validate}.
255: *
256: * @return base format element (or <code>null</code> if none)
257: */
258: public FormatElement getBaseFormat() {
259: return m_stringAttrs.getBaseFormat();
260: }
261:
262: //
263: // Validation methods
264:
265: /**
266: * JiBX access method to set format label as qualified name.
267: *
268: * @param label format label text (<code>null</code> if none)
269: * @param ictx unmarshalling context
270: * @throws JiBXException on deserialization error
271: */
272: private void setQualifiedLabel(String label,
273: IUnmarshallingContext ictx) throws JiBXException {
274: setQName(QName.deserialize(label, ictx));
275: }
276:
277: /**
278: * JiBX access method to get format label as qualified name.
279: *
280: * @param ictx marshalling context
281: * @return format label text (<code>null</code> if none)
282: * @throws JiBXException on deserialization error
283: */
284: private String getQualifiedLabel(IMarshallingContext ictx)
285: throws JiBXException {
286: return QName.serialize(getQName(), ictx);
287: }
288:
289: /**
290: * Make sure all attributes are defined.
291: *
292: * @param uctx unmarshalling context
293: * @exception JiBXException on unmarshalling error
294: */
295: private void preSet(IUnmarshallingContext uctx)
296: throws JiBXException {
297: validateAttributes(uctx, s_allowedAttributes);
298: }
299:
300: /**
301: * Set default flag based on whether name supplied or not.
302: * TODO: use explicit flag for 2.0
303: */
304: private void postSet() {
305: m_isDefault = m_label == null;
306: }
307:
308: /**
309: * Prevalidate attributes of element in isolation. Note that this adds the
310: * format information to the context, which is necessary because the string
311: * attributes for values need to have access to the format information for
312: * their own prevalidation. This is the only type of registration which is
313: * done during the prevalidation pass.
314: *
315: * @param vctx validation context
316: */
317: public void prevalidate(ValidationContext vctx) {
318:
319: // prevalidate this format
320: if (m_typeName != null) {
321: m_type = vctx.getClassInfo(m_typeName);
322: if (m_type != null) {
323: m_stringAttrs.setType(m_type);
324: m_stringAttrs.prevalidate(vctx);
325: } else {
326: vctx.addFatal("Unable to find type " + m_typeName);
327: }
328: } else {
329: vctx.addFatal("Missing required type name");
330: }
331:
332: // now try adding to context (except when run during setup) - kludgy
333: if (vctx.getParentElement() != null) {
334: vctx.getFormatDefinitions().addFormat(this , vctx);
335: }
336: super .prevalidate(vctx);
337: }
338:
339: /* (non-Javadoc)
340: * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext)
341: */
342: public void validate(ValidationContext vctx) {
343: m_stringAttrs.validate(vctx);
344: super.validate(vctx);
345: }
346: }
|