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:
033: /**
034: * Model component for <b>structure</b> attribute group in binding definition.
035: *
036: * @author Dennis M. Sosnoski
037: * @version 1.0
038: */
039: public class StructureAttributes extends AttributeBase {
040: /** Enumeration of allowed attribute names */
041: public static final StringArray s_allowedAttributes = new StringArray(
042: new String[] { "allow-repeats", "choice", "flexible",
043: "label", "ordered", "using" });
044:
045: //
046: // Instance data.
047:
048: /** Flexible element handling flag. */
049: private boolean m_isFlexible;
050:
051: /** Flag for ordered child content. */
052: private boolean m_isOrdered;
053:
054: /** Flag for choice child content. */
055: private boolean m_isChoice;
056:
057: /** Flag for repeated child elements to be ignored. */
058: private boolean m_isAllowRepeats;
059:
060: /** Name for labeled child content defined elsewhere. */
061: private String m_usingName;
062:
063: /** Name for labeled child content potentially referenced elsewhere. */
064: private String m_labelName;
065:
066: /**
067: * Constructor.
068: */
069: public StructureAttributes() {
070: m_isOrdered = true;
071: }
072:
073: /**
074: * Get flexible flag.
075: *
076: * @return flexible flag
077: */
078: public boolean isFlexible() {
079: return m_isFlexible;
080: }
081:
082: /**
083: * Set flexible flag.
084: *
085: * @param flexible
086: */
087: public void setFlexible(boolean flexible) {
088: m_isFlexible = flexible;
089: }
090:
091: /**
092: * Check if child components are ordered.
093: *
094: * @return <code>true</code> if ordered, <code>false</code> if not
095: */
096: public boolean isOrdered() {
097: return m_isOrdered;
098: }
099:
100: /**
101: * Set child components ordered flag.
102: *
103: * @param ordered <code>true</code> if ordered, <code>false</code> if not
104: */
105: public void setOrdered(boolean ordered) {
106: m_isOrdered = ordered;
107: }
108:
109: /**
110: * Check if child components are a choice.
111: *
112: * @return <code>true</code> if choice, <code>false</code> if not
113: */
114: public boolean isChoice() {
115: return m_isChoice;
116: }
117:
118: /**
119: * Set child components choice flag.
120: *
121: * @param ordered <code>true</code> if choice, <code>false</code> if not
122: */
123: public void setChoice(boolean choice) {
124: m_isChoice = choice;
125: }
126:
127: /**
128: * Check if repeated child elements are allowed.
129: *
130: * @return <code>true</code> if repeats allowed, <code>false</code> if not
131: */
132: public boolean isAllowRepeats() {
133: return m_isAllowRepeats;
134: }
135:
136: /**
137: * Set repeated child elements allowed flag.
138: *
139: * @param ignore <code>true</code> if repeated child elements to be allowed,
140: * <code>false</code> if not
141: */
142: public void setAllowRepeats(boolean ignore) {
143: m_isAllowRepeats = ignore;
144: }
145:
146: /**
147: * Get name for child component list definition.
148: *
149: * @return text of name defining child components (<code>null</code> if
150: * none)
151: */
152: public String getUsingName() {
153: return m_usingName;
154: }
155:
156: /**
157: * Set name for child component list definition.
158: *
159: * @param name text of name defining child components (<code>null</code> if
160: * none)
161: */
162: public void setUsingName(String name) {
163: m_usingName = name;
164: }
165:
166: /**
167: * Get label name for child component list.
168: *
169: * @return label name text (<code>null</code> if none)
170: */
171: public String getLabelName() {
172: return m_labelName;
173: }
174:
175: /**
176: * Set label name for child component list.
177: *
178: * @param name label text for name (<code>null</code> if none)
179: */
180: public void setLabelName(String name) {
181: m_labelName = name;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.jibx.binding.model.AttributeBase#prevalidate(org.jibx.binding.model.ValidationContext)
186: */
187: public void prevalidate(ValidationContext vctx) {
188: if (m_isOrdered) {
189: if (m_isChoice) {
190: vctx
191: .addError("choice='true' cannot be used with ordered children");
192: }
193: if (m_isFlexible) {
194: vctx
195: .addError("flexible='true' cannot be used with ordered children");
196: }
197: if (m_isAllowRepeats) {
198: vctx
199: .addError("allow-repeats='true' cannot be used with ordered children");
200: }
201: }
202: if (m_isChoice && m_isFlexible) {
203: vctx
204: .addError("choice='true' and flexible='true' cannot be used together");
205: }
206: if (m_isChoice && m_isAllowRepeats) {
207: vctx
208: .addError("choice='true' and allow-repeats='true' cannot be used together");
209: }
210: super .prevalidate(vctx);
211: }
212:
213: /* (non-Javadoc)
214: * @see org.jibx.binding.model.AttributeBase#validate(org.jibx.binding.model.ValidationContext)
215: */
216: public void validate(ValidationContext vctx) {
217: if (m_usingName != null) {
218: DefinitionContext dctx = vctx.getBindingRoot()
219: .getDefinitions();
220: if (dctx.getNamedStructure(m_usingName) == null) {
221: vctx.addError("Label \"" + m_usingName
222: + "\" is not defined");
223: } else {
224: vctx
225: .addWarning("The label/using approach is deprecated and "
226: + "will not be supported in the future - consider using an "
227: + "abstract mapping instead");
228: }
229: }
230: if (m_labelName != null) {
231: vctx
232: .addWarning("The label/using approach is deprecated and "
233: + "will not be supported in the future - consider using an "
234: + "abstract mapping instead");
235: }
236: super.validate(vctx);
237: }
238: }
|