KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > model > FormatElement


1 /*
2 Copyright (c) 2004-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.model;
30
31 import org.jibx.binding.util.StringArray;
32 import org.jibx.runtime.IUnmarshallingContext;
33 import org.jibx.runtime.JiBXException;
34
35 /**
36  * Model component for <b>format</b> element. This element defines conversion to
37  * and from simple unstructured text representations.
38  *
39  * @author Dennis M. Sosnoski
40  * @version 1.0
41  */

42  
43 public class FormatElement extends ElementBase
44 {
45     /** Enumeration of allowed attribute names */
46     public static final StringArray s_allowedAttributes =
47         new StringArray(new String JavaDoc[] { "label", "type" },
48         StringAttributes.s_allowedAttributes);
49     
50     /** Format label. */
51     private String JavaDoc m_label;
52     
53     /** Default format for type flag. */
54     private boolean m_isDefault;
55     
56     /** Name of value type. */
57     private String JavaDoc m_typeName;
58     
59     /** Value type information. */
60     private IClass m_type;
61     
62     /** String attributes information for value. */
63     private StringAttributes m_stringAttrs;
64     
65     /**
66      * Constructor.
67      */

68     public FormatElement() {
69         super(FORMAT_ELEMENT);
70         m_stringAttrs = new StringAttributes();
71     }
72     
73     /**
74      * Get format label.
75      *
76      * @return format label (<code>null</code> if none)
77      */

78     public String JavaDoc getLabel() {
79         return m_label;
80     }
81     
82     /**
83      * Set format label.
84      *
85      * @param label format label (<code>null</code> if none)
86      */

87     public void setLabel(String JavaDoc label) {
88         m_label = label;
89     }
90     
91     /**
92      * Check if default format for type.
93      *
94      * @return <code>true</code> if default for type, <code>false</code> if not
95      */

96     public boolean isDefaultFormat() {
97         return m_isDefault;
98     }
99     
100     /**
101      * Set default format for type.
102      *
103      * @param dflt <code>true</code> if default for type, <code>false</code> if
104      * not
105      */

106     public void setDefaultFormat(boolean dflt) {
107         m_isDefault = dflt;
108     }
109     
110     /**
111      * Get value type. This method is only usable after a
112      * call to {@link #validate}.
113      *
114      * @return default value object
115      */

116     public IClass getType() {
117         return m_type;
118     }
119     
120     /**
121      * Get value type name.
122      *
123      * @return value type name
124      */

125     public String JavaDoc getTypeName() {
126         return m_typeName;
127     }
128     
129     /**
130      * Set value type name.
131      *
132      * @param value type name
133      */

134     public void setTypeName(String JavaDoc value) {
135         m_typeName = value;
136     }
137     
138     //
139
// String attribute delegate methods
140

141     /**
142      * Get default value text.
143      *
144      * @return default value text
145      */

146     public String JavaDoc getDefaultText() {
147         return m_stringAttrs.getDefaultText();
148     }
149     
150     /**
151      * Get default value. This call is only meaningful after validation.
152      *
153      * @return default value object
154      */

155     public Object JavaDoc getDefault() {
156         return m_stringAttrs.getDefault();
157     }
158     
159     /**
160      * Set default value text.
161      *
162      * @param value default value text
163      */

164     public void setDefaultText(String JavaDoc value) {
165         m_stringAttrs.setDefaultText(value);
166     }
167     
168     /**
169      * Get serializer name.
170      *
171      * @return fully qualified class and method name for serializer (or
172      * <code>null</code> if none)
173      */

174     public String JavaDoc getSerializerName() {
175         return m_stringAttrs.getSerializerName();
176     }
177     
178     /**
179      * Get serializer method information. This call is only meaningful after
180      * validation.
181      *
182      * @return serializer information (or <code>null</code> if none)
183      */

184     public IClassItem getSerializer() {
185         return m_stringAttrs.getSerializer();
186     }
187     
188     /**
189      * Set serializer method name.
190      *
191      * @param fully qualified class and method name for serializer
192      */

193     public void setSerializerName(String JavaDoc name) {
194         m_stringAttrs.setSerializerName(name);
195     }
196     
197     /**
198      * Get deserializer name.
199      *
200      * @return fully qualified class and method name for deserializer (or
201      * <code>null</code> if none)
202      */

203     public String JavaDoc getDeserializerName() {
204         return m_stringAttrs.getDeserializerName();
205     }
206     
207     /**
208      * Get deserializer method information. This call is only meaningful after
209      * validation.
210      *
211      * @return deserializer information (or <code>null</code> if none)
212      */

213     public IClassItem getDeserializer() {
214         return m_stringAttrs.getDeserializer();
215     }
216     
217     /**
218      * Set deserializer method name.
219      *
220      * @param fully qualified class and method name for deserializer
221      */

222     public void setDeserializerName(String JavaDoc name) {
223         m_stringAttrs.setDeserializerName(name);
224     }
225     
226     /**
227      * Get base format information. This method is only usable after a
228      * call to {@link #validate}.
229      *
230      * @return base format element (or <code>null</code> if none)
231      */

232     public FormatElement getBaseFormat() {
233         return m_stringAttrs.getBaseFormat();
234     }
235     
236     //
237
// Validation methods
238

239     /**
240      * Make sure all attributes are defined.
241      *
242      * @param uctx unmarshalling context
243      * @exception JiBXException on unmarshalling error
244      */

245     private void preSet(IUnmarshallingContext uctx) throws JiBXException {
246         validateAttributes(uctx, s_allowedAttributes);
247     }
248     
249     /**
250      * Prevalidate attributes of element in isolation. Note that this adds the
251      * format information to the context, which is necessary because the string
252      * attributes for values need to have access to the format information for
253      * their own prevalidation. This is the only type of registration which is
254      * done during the prevalidation pass.
255      *
256      * @param vctx validation context
257      */

258     public void prevalidate(ValidationContext vctx) {
259         
260         // prevalidate this format
261
if (m_typeName != null) {
262             m_type = vctx.getClassInfo(m_typeName);
263             if (m_type != null) {
264                 m_stringAttrs.setType(m_type);
265                 m_stringAttrs.prevalidate(vctx);
266             } else {
267                 vctx.addFatal("Unable to find type " + m_typeName);
268             }
269         } else {
270             vctx.addFatal("Missing required type name");
271         }
272         
273         // now try adding to context (except when run during setup) - kludgy
274
if (vctx.getParentElement() != null) {
275             vctx.getFormatDefinitions().addFormat(this, vctx);
276         }
277         super.prevalidate(vctx);
278     }
279     
280     /* (non-Javadoc)
281      * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext)
282      */

283     public void validate(ValidationContext vctx) {
284         m_stringAttrs.validate(vctx);
285         super.validate(vctx);
286     }
287 }
Popular Tags