001: /*
002: Copyright (c) 2003-2004, 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.def;
030:
031: import org.jibx.runtime.JiBXException;
032:
033: /**
034: * Namespace definition from binding.
035: *
036: * @author Dennis M. Sosnoski
037: * @version 1.0
038: */
039:
040: public class NamespaceDefinition {
041: //
042: // Enumeration for namespace usage.
043:
044: /*package*/static final int NODEFAULT_USAGE = 0;
045: /*package*/static final int ELEMENTS_USAGE = 1;
046: /*package*/static final int ATTRIBUTES_USAGE = 2;
047: /*package*/static final int ALLDEFAULT_USAGE = 3;
048:
049: //
050: // Actual instance data
051:
052: /** Namespace URI. */
053: private String m_uri;
054:
055: /** Namespace prefix (may be <code>null</code>, but not ""). */
056: private String m_prefix;
057:
058: /** Index in namespace table for binding. */
059: private int m_index;
060:
061: /** Use by default for nested elements. */
062: private boolean m_elementDefault;
063:
064: /** Use by default for nested attributes. */
065: private boolean m_attributeDefault;
066:
067: /**
068: * Constructor.
069: *
070: * @param uri namespace URI
071: * @param prefix namespace prefix (may be <code>null</code> for default
072: * namespace, but not "")
073: * @param usage code for default usage of namespace
074: * @throws JiBXException if configuration error
075: */
076:
077: public NamespaceDefinition(String uri, String prefix, int usage)
078: throws JiBXException {
079: m_uri = uri;
080: m_prefix = prefix;
081: m_elementDefault = (usage == ALLDEFAULT_USAGE)
082: || (usage == ELEMENTS_USAGE);
083: m_attributeDefault = (usage == ALLDEFAULT_USAGE)
084: || (usage == ATTRIBUTES_USAGE);
085: if (usage != ELEMENTS_USAGE && prefix == null) {
086: throw new JiBXException(
087: "Prefix required for namespace unless element default");
088: }
089: if (m_attributeDefault && m_prefix == null) {
090: throw new JiBXException(
091: "Prefix required for attribute namespace");
092: }
093: }
094:
095: /**
096: * Check if default namespace for attributes.
097: *
098: * @return <code>true</code> if default namespace for attributes,
099: * <code>false</code> if not
100: */
101:
102: public boolean isAttributeDefault() {
103: return m_attributeDefault;
104: }
105:
106: /**
107: * Check if default namespace for elements.
108: *
109: * @return <code>true</code> if default namespace for elements,
110: * <code>false</code> if not
111: */
112:
113: public boolean isElementDefault() {
114: return m_elementDefault;
115: }
116:
117: /**
118: * Get prefix for namespace.
119: *
120: * @return namespace prefix (may be <code>null</code>, but not "")
121: */
122:
123: public String getPrefix() {
124: return m_prefix;
125: }
126:
127: /**
128: * Get namespace URI.
129: *
130: * @return namespace URI
131: */
132:
133: public String getUri() {
134: return m_uri;
135: }
136:
137: /**
138: * Set namespace index.
139: *
140: * @param index namespace index
141: */
142:
143: public void setIndex(int index) {
144: m_index = index;
145: }
146:
147: /**
148: * Get namespace index.
149: *
150: * @return namespace index
151: */
152:
153: public int getIndex() {
154: return m_index;
155: }
156:
157: /**
158: * Instance builder with supplied values. Used for canned definitions.
159: *
160: * @param uri namespace URI
161: * @param prefix namespace prefix
162: * @throws JiBXException if configuration error
163: */
164:
165: public static NamespaceDefinition buildNamespace(String uri,
166: String prefix) throws JiBXException {
167: return new NamespaceDefinition(uri, prefix, NODEFAULT_USAGE);
168: }
169:
170: // DEBUG
171: public void print(int depth) {
172: BindingDefinition.indent(depth);
173: System.out.print("namespace " + m_uri);
174: if (m_prefix != null) {
175: System.out.print(" (prefix " + m_prefix + ")");
176: }
177: System.out.println();
178: }
179: }
|