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.binding.classes.MethodBuilder;
032: import org.jibx.runtime.JiBXException;
033:
034: /**
035: * Named value definition from binding. This is a component of all items
036: * in the mapping corresponding to elements or attributes in the document.
037: *
038: * @author Dennis M. Sosnoski
039: * @version 1.0
040: */
041:
042: public class NameDefinition {
043: /** Element or attribute name. */
044: private final String m_name;
045:
046: /** Element or attribute namespace URI. */
047: private String m_namespace;
048:
049: /** Flag for attribute name. */
050: private final boolean m_isAttribute;
051:
052: /** Namespace index used for marshalling (derived from nesting). */
053: private int m_namespaceIndex;
054:
055: /**
056: * Constructor.
057: *
058: * @param attr flag for attribute name
059: */
060:
061: public NameDefinition(String name, String ns, boolean attr) {
062: m_name = name;
063: m_namespace = ns;
064: m_isAttribute = attr;
065: }
066:
067: /**
068: * Check if namespace URI is null.
069: *
070: * @return <code>true</code> if URI null, <code>false</code> if not
071: */
072:
073: public boolean isNullUri() {
074: return m_namespace == null;
075: }
076:
077: /**
078: * Generate code to push namespace URI.
079: *
080: * @param mb method builder
081: */
082:
083: public void genPushUri(MethodBuilder mb) {
084: if (m_namespace == null) {
085: mb.appendACONST_NULL();
086: } else {
087: mb.appendLoadConstant(m_namespace);
088: }
089: }
090:
091: /**
092: * Generate code to push name.
093: *
094: * @param mb method builder
095: */
096:
097: public void genPushName(MethodBuilder mb) {
098: mb.appendLoadConstant(m_name);
099: }
100:
101: /**
102: * Generate code to push namespace URI followed by name.
103: *
104: * @param mb method builder
105: */
106:
107: public void genPushUriPair(MethodBuilder mb) {
108: genPushUri(mb);
109: genPushName(mb);
110: }
111:
112: /**
113: * Generate code to push namespace index followed by name.
114: *
115: * @param mb method builder
116: */
117:
118: public void genPushIndexPair(MethodBuilder mb) {
119: mb.appendLoadConstant(m_namespaceIndex);
120: genPushName(mb);
121: }
122:
123: /**
124: * Finds the index for the namespace used with a name. If no explicit
125: * namespace has been set it uses the appropriate default. This is a
126: * separate operation from the unmarshalling in order to properly handle
127: * namespace definitions as children of the named binding component.
128: *
129: * @param defc definition context for namespaces
130: * @throws JiBXException if error in namespace handling
131: */
132:
133: public void fixNamespace(DefinitionContext defc)
134: throws JiBXException {
135: if (m_namespace == null) {
136: m_namespace = defc.getDefaultURI(m_isAttribute);
137: m_namespaceIndex = defc.getDefaultIndex(m_isAttribute);
138: } else {
139: try {
140: m_namespaceIndex = defc.getNamespaceIndex(m_namespace,
141: m_isAttribute);
142: } catch (JiBXException ex) {
143: throw new JiBXException(
144: "Undefined or unusable namespace \""
145: + m_namespace + '"');
146: }
147: }
148: }
149:
150: // DEBUG
151: public String toString() {
152: if (m_namespace == null) {
153: return m_name;
154: } else {
155: return "{" + m_namespace + "}:" + m_name;
156: }
157: }
158: }
|