KickJava   Java API By Example, From Geeks To Geeks.

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


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
34 /**
35  * Model component for <b>nesting</b> attribute group in binding definition.
36  *
37  * @author Dennis M. Sosnoski
38  * @version 1.0
39  */

40  
41 public class NestingAttributes extends AttributeBase
42 {
43     /** Enumeration of allowed attribute names */
44     public static final StringArray s_allowedAttributes =
45         new StringArray(new String JavaDoc[] { "value-style" });
46     
47     //
48
// Value set information
49

50     public static final int ATTRIBUTE_STYLE = 0;
51     public static final int ELEMENT_STYLE = 1;
52     
53     /*package*/ static final EnumSet s_styleEnum = new EnumSet(ATTRIBUTE_STYLE,
54         new String JavaDoc[] { "attribute", "element" });
55     
56     //
57
// Instance data
58

59     /** Supplied style name. */
60     private String JavaDoc m_styleName;
61     
62     /** Actual selected style. */
63     private int m_styleIndex;
64     
65     /**
66      * Get style string value.
67      *
68      * @return style string value (<code>null</code> if undefined at this level)
69      */

70     public String JavaDoc getStyleName() {
71         return m_styleName;
72     }
73     
74     /**
75      * Get style value. This method is only usable after a call to {@link
76      * #validate}.
77      *
78      * @return style value
79      */

80     public int getStyle() {
81         return m_styleIndex;
82     }
83     
84     /**
85      * Set style name.
86      *
87      * @param name style name (<code>null</code> to undefine style at this
88      * level)
89      */

90     public void setStyleName(String JavaDoc name) {
91         m_styleName = name;
92     }
93     
94     //
95
// Overrides of base class methods
96

97     /* (non-Javadoc)
98      * @see org.jibx.binding.model.AttributeBase#prevalidate(org.jibx.binding.model.ValidationContext)
99      */

100     public void prevalidate(ValidationContext vctx) {
101         if (m_styleName == null) {
102             NestingElementBase parent = vctx.getParentElement();
103             if (parent == null) {
104                 m_styleIndex = ELEMENT_STYLE;
105             } else {
106                 m_styleIndex = parent.getDefaultStyle();
107             }
108         } else {
109             int style = s_styleEnum.getValue(m_styleName);
110             if (style < 0) {
111                 vctx.addError("Value \"" + m_styleName +
112                     "\" is not a valid style");
113             } else {
114                 m_styleIndex = style;
115             }
116         }
117         super.prevalidate(vctx);
118     }
119 }
Popular Tags