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: import org.jibx.runtime.IUnmarshallingContext;
034: import org.jibx.runtime.JiBXException;
035:
036: /**
037: * Model component for <b>namespace</b> element of binding definition.
038: *
039: * @author Dennis M. Sosnoski
040: * @version 1.0
041: */
042:
043: public class NamespaceElement extends ElementBase {
044: /** Enumeration of allowed attribute names */
045: public static final StringArray s_allowedAttributes = new StringArray(
046: new String[] { "default", "prefix", "uri" });
047:
048: //
049: // Enumeration for namespace usage.
050:
051: public static final int NODEFAULT_USAGE = 0;
052: public static final int ELEMENTS_USAGE = 1;
053: public static final int ATTRIBUTES_USAGE = 2;
054: public static final int ALLDEFAULT_USAGE = 3;
055:
056: public static final EnumSet s_defaultEnum = new EnumSet(
057: NODEFAULT_USAGE, new String[] { "none", "elements",
058: "attributes", "all" });
059:
060: //
061: // Actual instance data
062:
063: /** Default type name. */
064: private String m_defaultName = s_defaultEnum
065: .getName(NODEFAULT_USAGE);
066:
067: /** Actual selected default. */
068: private int m_defaultIndex;
069:
070: /** Namespace URI. */
071: private String m_uri;
072:
073: /** Namespace prefix (may be <code>null</code>, but not ""). */
074: private String m_prefix;
075:
076: /**
077: * Constructor.
078: */
079: public NamespaceElement() {
080: super (NAMESPACE_ELEMENT);
081: }
082:
083: /**
084: * Get prefix.
085: *
086: * @return prefix text
087: */
088: public String getPrefix() {
089: return m_prefix;
090: }
091:
092: /**
093: * Set prefix.
094: *
095: * @param prefix text
096: */
097: public void setPrefix(String text) {
098: m_prefix = text;
099: }
100:
101: /**
102: * Get namespace URI.
103: *
104: * @return namespace URI (<code>null</code> if no-namespace namespace)
105: */
106: public String getUri() {
107: return m_uri;
108: }
109:
110: /**
111: * Set namespace URI.
112: *
113: * @param uri namespace URI (<code>null</code> if no-namespace namespace)
114: */
115: public void setUri(String uri) {
116: m_uri = uri;
117: }
118:
119: /**
120: * Set namespace default type name.
121: *
122: * @param name namespace default type
123: */
124: public void setDefaultName(String name) {
125: m_defaultName = name;
126: }
127:
128: /**
129: * Get namespace default type name.
130: *
131: * @return namespace default type name
132: */
133: public String getDefaultName() {
134: return m_defaultName;
135: }
136:
137: /**
138: * Check if default namespace for attributes. This method is only meaningful
139: * after a call to {@link #validate}.
140: *
141: * @return <code>true</code> if default namespace for attributes,
142: * <code>false</code> if not
143: */
144: public boolean isAttributeDefault() {
145: return m_defaultIndex == ATTRIBUTES_USAGE
146: || m_defaultIndex == ALLDEFAULT_USAGE;
147: }
148:
149: /**
150: * Check if default namespace for elements. This method is only meaningful
151: * after a call to {@link #validate}.
152: *
153: * @return <code>true</code> if default namespace for elements,
154: * <code>false</code> if not
155: */
156: public boolean isElementDefault() {
157: return m_defaultIndex == ELEMENTS_USAGE
158: || m_defaultIndex == ALLDEFAULT_USAGE;
159: }
160:
161: //
162: // Validation methods
163:
164: /**
165: * Make sure all attributes are defined.
166: *
167: * @param uctx unmarshalling context
168: * @exception JiBXException on unmarshalling error
169: */
170: private void preSet(IUnmarshallingContext uctx)
171: throws JiBXException {
172: validateAttributes(uctx, s_allowedAttributes);
173: }
174:
175: /**
176: * Prevalidate attributes of element in isolation.
177: *
178: * @param vctx validation context
179: */
180: public void prevalidate(ValidationContext vctx) {
181: m_defaultIndex = s_defaultEnum.getValue(m_defaultName);
182: if (m_defaultIndex < 0) {
183: vctx
184: .addError("Value \""
185: + m_defaultName
186: + "\" is not a valid choice for namespace default usage");
187: }
188: }
189: }
|