KickJava   Java API By Example, From Geeks To Geeks.

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


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.EnumSet;
33 import org.jibx.runtime.IUnmarshallingContext;
34 import org.jibx.runtime.JiBXException;
35
36 /**
37  * Model component for <b>value</b> element. This element defines a value that
38  * can be represented as a simple text string, which may be expressed as an
39  * attribute, element, or text component of the XML document.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44  
45 public class ValueElement extends ElementBase implements IComponent
46 {
47     /** Enumeration of allowed attribute names */
48     public static final StringArray s_allowedAttributes =
49         new StringArray(new String JavaDoc[] { "constant", "format", "ident", "style" },
50         new StringArray(new StringArray
51         (NameAttributes.s_allowedAttributes,
52         PropertyAttributes.s_allowedAttributes),
53         StringAttributes.s_allowedAttributes));
54     
55     //
56
// Value set information
57

58     public static final int CDATA_STYLE = 2;
59     public static final int TEXT_STYLE = 3;
60     
61     private static final EnumSet s_styleEnum =
62         new EnumSet(NestingAttributes.s_styleEnum, CDATA_STYLE,
63         new String JavaDoc[] { "cdata", "text" });
64     
65     public static final int NONE_IDENT = 0;
66     public static final int DEF_IDENT = 1;
67     public static final int REF_IDENT = 2;
68 /* public static final int AUTO_IDENT = 3; */
69     
70     /*package*/ static final EnumSet s_identEnum = new EnumSet(NONE_IDENT,
71         new String JavaDoc[] { "none", "def", "ref"/*, "auto"*/ });
72     
73     //
74
// Instance data
75

76     /** Supplied constant value. */
77     private String JavaDoc m_constantValue;
78     
79     /** Supplied style name. */
80     private String JavaDoc m_styleName;
81     
82     /** Actual selected style. */
83     private int m_styleIndex;
84     
85     /** Supplied identity name. */
86     private String JavaDoc m_identName;
87     
88     /** Actual selected identity. */
89     private int m_identIndex;
90     
91     /** Name attributes information for value. */
92     private NameAttributes m_nameAttrs;
93     
94     /** Property attributes information for value. */
95     private PropertyAttributes m_propertyAttrs;
96     
97     /** String attributes information for value. */
98     private StringAttributes m_stringAttrs;
99     
100     /**
101      * Constructor.
102      */

103     public ValueElement() {
104         super(VALUE_ELEMENT);
105         m_nameAttrs = new NameAttributes();
106         m_propertyAttrs = new PropertyAttributes();
107         m_stringAttrs = new StringAttributes();
108     }
109     
110     /**
111      * Get constant value.
112      *
113      * @return constant value, or <code>null</code> if not a constant
114      */

115     public String JavaDoc getConstantValue() {
116         return m_constantValue;
117     }
118     
119     /**
120      * Set constant value.
121      *
122      * @param value constant value, or <code>null</code> if not a constant
123      */

124     public void setConstantValue(String JavaDoc value) {
125         m_constantValue = value;
126     }
127     
128     /**
129      * Get style string value.
130      *
131      * @return style string value
132      */

133     public String JavaDoc getStyleName() {
134         return m_styleName;
135     }
136     
137     /**
138      * Get style value. This call is only meaningful after validation.
139      *
140      * @return style value
141      */

142     public int getStyle() {
143         return m_styleIndex;
144     }
145     
146     /**
147      * Set style name.
148      *
149      * @param name style name (<code>null</code> if to use inherited default)
150      */

151     public void setStyleName(String JavaDoc name) {
152         m_styleName = name;
153     }
154     
155     /**
156      * Get name for style that applies to this value. This call is only
157      * meaningful after validation.
158      *
159      * @return name for style
160      */

161     public String JavaDoc getEffectiveStyleName() {
162         return s_styleEnum.getName(m_styleIndex);
163     }
164     
165     /**
166      * Get identity string value.
167      *
168      * @return identity string value
169      */

170     public String JavaDoc getIdentName() {
171         return m_identName;
172     }
173     
174     /**
175      * Get identity value. This call is only meaningful after validation.
176      *
177      * @return identity value
178      */

179     public int getIdent() {
180         return m_identIndex;
181     }
182     
183     /**
184      * Set identity name.
185      *
186      * @param name identity name
187      */

188     public void setIdentName(String JavaDoc name) {
189         m_identName = name;
190     }
191     
192     //
193
// Name attribute delegate methods
194

195     /**
196      * Get name.
197      *
198      * @return name text
199      */

200     public String JavaDoc getName() {
201         return m_nameAttrs.getName();
202     }
203     
204     /**
205      * Set name.
206      *
207      * @param name text for name
208      */

209     public void setName(String JavaDoc name) {
210         m_nameAttrs.setName(name);
211     }
212
213     /**
214      * Get specified namespace URI.
215      *
216      * @return namespace URI (<code>null</code> if not set)
217      */

218     public String JavaDoc getUri() {
219         return m_nameAttrs.getUri();
220     }
221
222     /**
223      * Set namespace URI.
224      *
225      * @param uri namespace URI (<code>null</code> if not set)
226      */

227     public void setUri(String JavaDoc uri) {
228         m_nameAttrs.setUri(uri);
229     }
230
231     /**
232      * Get specified namespace prefix.
233      *
234      * @return namespace prefix (<code>null</code> if not set)
235      */

236     public String JavaDoc getPrefix() {
237         return m_nameAttrs.getPrefix();
238     }
239
240     /**
241      * Set namespace prefix.
242      *
243      * @param prefix namespace prefix (<code>null</code> if not set)
244      */

245     public void setPrefix(String JavaDoc prefix) {
246         m_nameAttrs.setPrefix(prefix);
247     }
248     
249     /**
250      * Get effective namespace information. This call is only meaningful after
251      * validation.
252      *
253      * @return effective namespace information
254      */

255     public NamespaceElement getNamespace() {
256         return m_nameAttrs.getNamespace();
257     }
258     
259     //
260
// Property attribute delegate methods
261

262     /**
263      * Get usage name.
264      *
265      * @return usage name
266      */

267     public String JavaDoc getUsageName() {
268         return m_propertyAttrs.getUsageName();
269     }
270     
271     /**
272      * Get usage value. This call is only meaningful after validation.
273      *
274      * @return usage value
275      */

276     public int getUsage() {
277         return m_propertyAttrs.getUsage();
278     }
279     
280     /**
281      * Set usage name.
282      *
283      * @param name usage name
284      */

285     public void setUsageName(String JavaDoc name) {
286         m_propertyAttrs.setUsageName(name);
287     }
288     
289     /**
290      * Get declared type name. This call is only meaningful after validation.
291      *
292      * @return type name (or <code>null</code> if none)
293      */

294     public String JavaDoc getDeclaredType() {
295         return m_propertyAttrs.getDeclaredType();
296     }
297     
298     /**
299      * Set declared type name.
300      *
301      * @param type name (or <code>null</code> if none)
302      */

303     public void setDeclaredType(String JavaDoc type) {
304         m_propertyAttrs.setDeclaredType(type);
305     }
306     
307     /**
308      * Get field name.
309      *
310      * @return field name (or <code>null</code> if none)
311      */

312     public String JavaDoc getFieldName() {
313         return m_propertyAttrs.getFieldName();
314     }
315     
316     /**
317      * Get field information. This call is only meaningful after validation.
318      *
319      * @return field information (or <code>null</code> if none)
320      */

321     public IClassItem getField() {
322         return m_propertyAttrs.getField();
323     }
324     
325     /**
326      * Set field name.
327      *
328      * @param field field name (or <code>null</code> if none)
329      */

330     public void setFieldName(String JavaDoc field) {
331         m_propertyAttrs.setFieldName(field);
332     }
333     
334     /**
335      * Get test method name.
336      *
337      * @return test method name (or <code>null</code> if none)
338      */

339     public String JavaDoc getTestName() {
340         return m_propertyAttrs.getTestName();
341     }
342     
343     /**
344      * Get test method information. This call is only meaningful after
345      * validation.
346      *
347      * @return test method information (or <code>null</code> if none)
348      */

349     public IClassItem getTest() {
350         return m_propertyAttrs.getTest();
351     }
352     
353     /**
354      * Set test method name.
355      *
356      * @param test test method name (or <code>null</code> if none)
357      */

358     public void setTestName(String JavaDoc test) {
359         m_propertyAttrs.setTestName(test);
360     }
361     
362     /**
363      * Get get method name.
364      *
365      * @return get method name (or <code>null</code> if none)
366      */

367     public String JavaDoc getGetName() {
368         return m_propertyAttrs.getGetName();
369     }
370     
371     /**
372      * Get get method information. This call is only meaningful after
373      * validation.
374      *
375      * @return get method information (or <code>null</code> if none)
376      */

377     public IClassItem getGet() {
378         return m_propertyAttrs.getGet();
379     }
380     
381     /**
382      * Get type for value loaded to stack. This call is only meaningful after
383      * validation.
384      *
385      * @return get value type (or <code>null</code> if none)
386      */

387     public IClass getGetType() {
388         return m_propertyAttrs.getGetType();
389     }
390     
391     /**
392      * Set get method name.
393      *
394      * @param get get method name (or <code>null</code> if none)
395      */

396     public void setGetName(String JavaDoc get) {
397         m_propertyAttrs.setGetName(get);
398     }
399     
400     /**
401      * Get set method name.
402      *
403      * @return set method name (or <code>null</code> if none)
404      */

405     public String JavaDoc getSetName() {
406         return m_propertyAttrs.getSetName();
407     }
408     
409     /**
410      * Get set method information. This call is only meaningful after
411      * validation.
412      *
413      * @return set method information (or <code>null</code> if none)
414      */

415     public IClassItem getSet() {
416         return m_propertyAttrs.getSet();
417     }
418     
419     /**
420      * Get type for value stored from stack. This call is only meaningful after
421      * validation.
422      *
423      * @return set value type (or <code>null</code> if none)
424      */

425     public IClass getSetType() {
426         return m_propertyAttrs.getSetType();
427     }
428     
429     /**
430      * Set set method name.
431      *
432      * @param set set method name (or <code>null</code> if none)
433      */

434     public void setSetName(String JavaDoc set) {
435         m_propertyAttrs.setSetName(set);
436     }
437     
438     /**
439      * Check if this value implicitly uses the containing object. This call
440      * is only meaningful after prevalidation.
441      *
442      * @return <code>true</code> if using the containing object,
443      * <code>false</code> if own value
444      */

445     public boolean isImplicit() {
446         return m_propertyAttrs.isImplicit();
447     }
448     
449     //
450
// String attribute delegate methods
451

452     /**
453      * Get default value text.
454      *
455      * @return default value text
456      */

457     public String JavaDoc getDefaultText() {
458         return m_stringAttrs.getDefaultText();
459     }
460     
461     /**
462      * Get default value. This call is only meaningful after validation.
463      *
464      * @return default value object
465      */

466     public Object JavaDoc getDefault() {
467         return m_stringAttrs.getDefault();
468     }
469     
470     /**
471      * Set default value text.
472      *
473      * @param value default value text
474      */

475     public void setDefaultText(String JavaDoc value) {
476         m_stringAttrs.setDefaultText(value);
477     }
478     
479     /**
480      * Get base format name.
481      *
482      * @return referenced base format
483      */

484     public String JavaDoc getFormatName() {
485         return m_stringAttrs.getFormatName();
486     }
487     
488     /**
489      * Set base format name.
490      *
491      * @param name referenced base format
492      */

493     public void setFormatName(String JavaDoc name) {
494         m_stringAttrs.setFormatName(name);
495     }
496     
497     /**
498      * Get serializer name.
499      *
500      * @return fully qualified class and method name for serializer (or
501      * <code>null</code> if none)
502      */

503     public String JavaDoc getSerializerName() {
504         return m_stringAttrs.getSerializerName();
505     }
506     
507     /**
508      * Get serializer method information. This call is only meaningful after
509      * validation.
510      *
511      * @return serializer information (or <code>null</code> if none)
512      */

513     public IClassItem getSerializer() {
514         return m_stringAttrs.getSerializer();
515     }
516     
517     /**
518      * Set serializer method name.
519      *
520      * @param fully qualified class and method name for serializer
521      */

522     public void setSerializerName(String JavaDoc name) {
523         m_stringAttrs.setSerializerName(name);
524     }
525     
526     /**
527      * Get deserializer name.
528      *
529      * @return fully qualified class and method name for deserializer (or
530      * <code>null</code> if none)
531      */

532     public String JavaDoc getDeserializerName() {
533         return m_stringAttrs.getDeserializerName();
534     }
535     
536     /**
537      * Get deserializer method information. This call is only meaningful after
538      * validation.
539      *
540      * @return deserializer information (or <code>null</code> if none)
541      */

542     public IClassItem getDeserializer() {
543         return m_stringAttrs.getDeserializer();
544     }
545     
546     /**
547      * Set deserializer method name.
548      *
549      * @param fully qualified class and method name for deserializer
550      */

551     public void setDeserializerName(String JavaDoc name) {
552         m_stringAttrs.setDeserializerName(name);
553     }
554     
555     //
556
// Validation methods
557

558     /**
559      * Make sure all attributes are defined.
560      *
561      * @param uctx unmarshalling context
562      * @exception JiBXException on unmarshalling error
563      */

564     private void preSet(IUnmarshallingContext uctx) throws JiBXException {
565         validateAttributes(uctx, s_allowedAttributes);
566     }
567     
568     /* (non-Javadoc)
569      * @see org.jibx.binding.model.ElementBase#prevalidate(org.jibx.binding.model.ValidationContext)
570      */

571     public void prevalidate(ValidationContext vctx) {
572         
573         // set the style value
574
if (m_styleName != null) {
575             m_styleIndex = s_styleEnum.getValue(m_styleName);
576             if (m_styleIndex < 0) {
577                 vctx.addError("Value \"" + m_styleName +
578                     "\" is not a valid choice for style");
579             }
580         } else {
581             m_styleIndex = vctx.getParentElement().getDefaultStyle();
582         }
583         
584         // validate ID classification
585
m_identIndex = s_identEnum.getValue(m_identName);
586         if (m_identIndex < 0) {
587             vctx.addError("Value \"" + m_identName +
588                 " is not a valid choice for ident");
589         }
590         
591         // validate basic attribute groups
592
m_nameAttrs.setIsAttribute
593             (m_styleIndex == NestingAttributes.ATTRIBUTE_STYLE);
594         m_nameAttrs.prevalidate(vctx);
595         m_propertyAttrs.prevalidate(vctx);
596         if (m_styleIndex == CDATA_STYLE || m_styleIndex == TEXT_STYLE) {
597             if (m_nameAttrs.getName() != null) {
598                 vctx.addFatal("Values with \"text\" or \"cdata\" style " +
599                     "cannot have names");
600             }
601         } else {
602             if (m_nameAttrs.getName() == null) {
603                 vctx.addFatal
604                     ("Missing required name for element or attribute value");
605             }
606         }
607         
608         // make sure value is not constant
609
if (m_constantValue == null) {
610             
611             // process ID classification
612
if (m_identIndex == DEF_IDENT/* || m_identIndex == AUTO_IDENT*/) {
613                 vctx.getContextObject().setIdChild(this, vctx);
614             }
615             
616             // check string attributes only if valid to this point
617
if (!vctx.isSkipped(this)) {
618                 
619                 // handle string attributes based on identity type
620
if (m_identIndex == REF_IDENT) {
621                     if (m_propertyAttrs.isImplicit()) {
622                         vctx.addFatal
623                             ("No property value - ID reference can only " +
624                             "be used with a property of the appropriate type");
625                     }
626                     if (m_stringAttrs.getDeserializerName() != null ||
627                         m_stringAttrs.getSerializerName() != null ||
628                         m_stringAttrs.getFormatName() != null ||
629                         m_stringAttrs.getDefaultText() != null) {
630                         vctx.addWarning("String attributes serializer, " +
631                             "deserializer, format, and default are " +
632                             "prohibited with ID references");
633                     }
634                 } else {
635                     m_stringAttrs.setType(m_propertyAttrs.getType());
636                     m_stringAttrs.prevalidate(vctx);
637                 }
638                 super.prevalidate(vctx);
639             }
640             
641         } else {
642             
643             // check prohibited attributes with constant value
644
if (m_identIndex != NONE_IDENT) {
645                 vctx.addFatal("ident value must be \"none\" for constant");
646             } else if (m_propertyAttrs.hasProperty() ||
647                 m_propertyAttrs.getDeclaredType() != null) {
648                 vctx.addFatal
649                     ("Property attributes cannot be used with constant");
650             } else if (m_stringAttrs.getDefaultText() != null ||
651                 m_stringAttrs.getDeserializerName() != null ||
652                 m_stringAttrs.getSerializerName() != null ||
653                 m_stringAttrs.getFormatName() != null) {
654                 vctx.addFatal("String attributes cannot be used with constant");
655             }
656         }
657     }
658     
659     /* (non-Javadoc)
660      * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext)
661      */

662     public void validate(ValidationContext vctx) {
663         
664         // validate basic attributes
665
m_nameAttrs.validate(vctx);
666         m_propertyAttrs.validate(vctx);
667         if (!vctx.isSkipped(this)) {
668             
669             // check identity references for compatible objects
670
if (m_identIndex == REF_IDENT) {
671                 String JavaDoc type = m_propertyAttrs.getType().getName();
672                 if (!vctx.getBindingRoot().isIdClass(type)) {
673                     vctx.addError("No ID definitions for compatible type");
674                 }
675             }
676             super.validate(vctx);
677         }
678     }
679     
680     //
681
// IComponent implementation methods
682

683     /* (non-Javadoc)
684      * @see org.jibx.binding.model.IComponent#hasAttribute()
685      */

686     public boolean hasAttribute() {
687         return m_styleIndex == NestingAttributes.ATTRIBUTE_STYLE;
688     }
689
690     /* (non-Javadoc)
691      * @see org.jibx.binding.model.IComponent#hasContent()
692      */

693     public boolean hasContent() {
694         return m_styleIndex != NestingAttributes.ATTRIBUTE_STYLE;
695     }
696
697     /* (non-Javadoc)
698      * @see org.jibx.binding.model.IComponent#isOptional()
699      */

700     public boolean isOptional() {
701         return m_propertyAttrs.getUsage() == PropertyAttributes.OPTIONAL_USAGE;
702     }
703
704     /* (non-Javadoc)
705      * @see org.jibx.binding.model.IComponent#hasName()
706      */

707     public boolean hasName() {
708         return m_nameAttrs.getName() != null;
709     }
710
711     /* (non-Javadoc)
712      * @see org.jibx.binding.model.IComponent#hasType()
713      */

714     public boolean hasType() {
715         return m_propertyAttrs.getType() != null;
716     }
717     
718     /* (non-Javadoc)
719      * @see org.jibx.binding.model.IComponent#getType()
720      */

721     public IClass getType() {
722         return m_propertyAttrs.getType();
723     }
724 }
Popular Tags