001: /*
002: Copyright (c) 2004-2007, 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>name</b> attribute group in binding definition.
035: *
036: * @author Dennis M. Sosnoski
037: */
038: public class NameAttributes extends AttributeBase {
039: /** Enumeration of allowed attribute names */
040: public static final StringArray s_allowedAttributes = new StringArray(
041: new String[] { "name", "ns" });
042: // TODO: add "prefix" to set for 2.0
043:
044: /** Name text. */
045: private String m_name;
046:
047: /** Namespace URI. */
048: private String m_uri;
049:
050: /** Namespace prefix. */
051: private String m_prefix;
052:
053: /** Name represents an attribute flag. */
054: private boolean m_isAttribute;
055:
056: /** Namespace definition used by this name. */
057: private NamespaceElement m_namespace;
058:
059: /**
060: * Default constructor.
061: */
062: public NameAttributes() {
063: }
064:
065: /**
066: * Set flag for an attribute name. This information is necessary for
067: * resolving the namespace definition to be used with a name, but has to be
068: * determined by the element owning this attribute group. It must be set (if
069: * different from the default of <code>false</code>) prior to validation.
070: *
071: * @param isattr flag for name represents an attribute
072: */
073: public void setIsAttribute(boolean isattr) {
074: m_isAttribute = isattr;
075: }
076:
077: /**
078: * Get flag for an attribute name.
079: *
080: * @return <code>true</code> if an attribute, <code>false</code> if an
081: * element
082: */
083: public boolean isAttribute() {
084: return m_isAttribute;
085: }
086:
087: /**
088: * Get name.
089: *
090: * @return name text
091: */
092: public String getName() {
093: return m_name;
094: }
095:
096: /**
097: * Set name.
098: *
099: * @param name text for name
100: */
101: public void setName(String name) {
102: m_name = name;
103: }
104:
105: /**
106: * Get specified namespace URI.
107: *
108: * @return namespace URI (<code>null</code> if not set)
109: */
110: public String getUri() {
111: return m_uri;
112: }
113:
114: /**
115: * Set namespace URI.
116: *
117: * @param uri namespace URI (<code>null</code> if not set)
118: */
119: public void setUri(String uri) {
120: m_uri = uri;
121: }
122:
123: /**
124: * Get specified namespace prefix.
125: *
126: * @return namespace prefix (<code>null</code> if not set)
127: */
128: public String getPrefix() {
129: return m_prefix;
130: }
131:
132: /**
133: * Set namespace prefix.
134: *
135: * @param prefix namespace prefix (<code>null</code> if not set)
136: */
137: public void setPrefix(String prefix) {
138: m_prefix = prefix;
139: }
140:
141: /**
142: * Get effective namespace definition. This call can only be used after
143: * validation.
144: *
145: * @return definition for namespace used by this name
146: */
147: public NamespaceElement getNamespace() {
148: return m_namespace;
149: }
150:
151: //
152: // Overrides of base class methods
153:
154: /* (non-Javadoc)
155: * @see org.jibx.binding.model.AttributeBase#validate(org.jibx.binding.model.ValidationContext)
156: */
157: public void validate(ValidationContext vctx) {
158: if (m_name != null) {
159: DefinitionContext dctx = null;
160: ElementBase elem = vctx.peekElement();
161: if (elem instanceof ContainerElementBase) {
162: dctx = ((ContainerElementBase) elem).getDefinitions();
163: }
164: if (dctx == null) {
165: dctx = vctx.getDefinitions();
166: }
167: if (m_isAttribute) {
168: m_namespace = dctx.getAttributeNamespace(this );
169: } else {
170: m_namespace = dctx.getElementNamespace(this );
171: }
172: }
173: super .validate(vctx);
174: }
175:
176: /* (non-Javadoc)
177: * @see java.lang.Object#equals(java.lang.Object)
178: */
179: public boolean equals(Object obj) {
180: if (obj instanceof NameAttributes) {
181: NameAttributes comp = (NameAttributes) obj;
182: if (m_name.equals(comp.m_name)) {
183: return m_namespace.getUri().equals(
184: comp.m_namespace.getUri());
185: } else {
186: return false;
187: }
188: } else {
189: return false;
190: }
191: }
192:
193: /* (non-Javadoc)
194: * @see java.lang.Object#hashCode()
195: */
196: public int hashCode() {
197: if (m_uri == null) {
198: return m_name.hashCode();
199: } else {
200: return m_name.hashCode() + m_uri.hashCode();
201: }
202: }
203: }
|