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.EnumSet;
033:
034: /**
035: * Model component for <b>nesting</b> attribute group in binding definition.
036: *
037: * @author Dennis M. Sosnoski
038: */
039: public class NestingAttributes extends AttributeBase {
040: /** Enumeration of allowed attribute names */
041: public static final StringArray s_allowedAttributes = new StringArray(
042: new String[] { "value-style" });
043:
044: //
045: // Value set information
046:
047: public static final int ATTRIBUTE_STYLE = 0;
048: public static final int ELEMENT_STYLE = 1;
049:
050: /*package*/static final EnumSet s_styleEnum = new EnumSet(
051: ATTRIBUTE_STYLE, new String[] { "attribute", "element" });
052:
053: //
054: // Instance data
055:
056: /** Supplied style name. */
057: private String m_styleName;
058:
059: /** Actual selected style. */
060: private int m_styleIndex;
061:
062: /**
063: * Get style string value.
064: *
065: * @return style string value (<code>null</code> if undefined at this level)
066: */
067: public String getStyleName() {
068: return m_styleName;
069: }
070:
071: /**
072: * Get style value. This method is only usable after a call to {@link
073: * #validate}.
074: *
075: * @return style value
076: */
077: public int getStyle() {
078: return m_styleIndex;
079: }
080:
081: /**
082: * Set style name.
083: *
084: * @param name style name (<code>null</code> to undefine style at this
085: * level)
086: */
087: public void setStyleName(String name) {
088: m_styleName = name;
089: }
090:
091: //
092: // Overrides of base class methods
093:
094: /* (non-Javadoc)
095: * @see org.jibx.binding.model.AttributeBase#prevalidate(org.jibx.binding.model.ValidationContext)
096: */
097: public void prevalidate(ValidationContext vctx) {
098: if (m_styleName == null) {
099: NestingElementBase parent = vctx.getParentElement();
100: if (parent == null) {
101: m_styleIndex = ELEMENT_STYLE;
102: } else {
103: m_styleIndex = parent.getDefaultStyle();
104: }
105: } else {
106: int style = s_styleEnum.getValue(m_styleName);
107: if (style < 0) {
108: vctx.addError("Value \"" + m_styleName
109: + "\" is not a valid style");
110: } else {
111: m_styleIndex = style;
112: }
113: }
114: super.prevalidate(vctx);
115: }
116: }
|