KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > def > NameDefinition


1 /*
2 Copyright (c) 2003-2004, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.def;
30
31 import org.jibx.binding.classes.MethodBuilder;
32 import org.jibx.runtime.JiBXException;
33
34 /**
35  * Named value definition from binding. This is a component of all items
36  * in the mapping corresponding to elements or attributes in the document.
37  *
38  * @author Dennis M. Sosnoski
39  * @version 1.0
40  */

41
42 public class NameDefinition
43 {
44     /** Element or attribute name. */
45     private final String JavaDoc m_name;
46
47     /** Element or attribute namespace URI. */
48     private String JavaDoc m_namespace;
49
50     /** Flag for attribute name. */
51     private final boolean m_isAttribute;
52
53     /** Namespace index used for marshalling (derived from nesting). */
54     private int m_namespaceIndex;
55
56     /**
57      * Constructor.
58      *
59      * @param attr flag for attribute name
60      */

61
62     public NameDefinition(String JavaDoc name, String JavaDoc ns, boolean attr) {
63         m_name = name;
64         m_namespace = ns;
65         m_isAttribute = attr;
66     }
67
68     /**
69      * Check if namespace URI is null.
70      *
71      * @return <code>true</code> if URI null, <code>false</code> if not
72      */

73
74     public boolean isNullUri() {
75         return m_namespace == null;
76     }
77
78     /**
79      * Generate code to push namespace URI.
80      *
81      * @param mb method builder
82      * @throws JiBXException if error in configuration
83      */

84
85     public void genPushUri(MethodBuilder mb) throws JiBXException {
86         if (m_namespace == null) {
87             mb.appendACONST_NULL();
88         } else {
89             mb.appendLoadConstant(m_namespace);
90         }
91     }
92
93     /**
94      * Generate code to push name.
95      *
96      * @param mb method builder
97      * @throws JiBXException if error in configuration
98      */

99
100     public void genPushName(MethodBuilder mb) throws JiBXException {
101         mb.appendLoadConstant(m_name);
102     }
103
104     /**
105      * Generate code to push namespace URI followed by name.
106      *
107      * @param mb method builder
108      * @throws JiBXException if error in configuration
109      */

110
111     public void genPushUriPair(MethodBuilder mb) throws JiBXException {
112         genPushUri(mb);
113         genPushName(mb);
114     }
115
116     /**
117      * Generate code to push namespace index followed by name.
118      *
119      * @param mb method builder
120      * @throws JiBXException if error in configuration
121      */

122
123     public void genPushIndexPair(MethodBuilder mb) throws JiBXException {
124         mb.appendLoadConstant(m_namespaceIndex);
125         genPushName(mb);
126     }
127
128     /**
129      * Finds the index for the namespace used with a name. If no explicit
130      * namespace has been set it uses the appropriate default. This is a
131      * separate operation from the unmarshalling in order to properly handle
132      * namespace definitions as children of the named binding component.
133      *
134      * @param defc definition context for namespaces
135      * @throws JiBXException if error in namespace handling
136      */

137
138     public void fixNamespace(DefinitionContext defc) throws JiBXException {
139         if (m_namespace == null) {
140             m_namespace = defc.getDefaultURI(m_isAttribute);
141             m_namespaceIndex = defc.getDefaultIndex(m_isAttribute);
142         } else {
143             try {
144                 m_namespaceIndex = defc.getNamespaceIndex
145                     (m_namespace, m_isAttribute);
146             } catch (JiBXException ex) {
147                 throw new JiBXException("Undefined or unusable namespace \"" +
148                     m_namespace + '"');
149             }
150         }
151     }
152     
153     // DEBUG
154
public String JavaDoc toString() {
155         if (m_namespace == null) {
156             return m_name;
157         } else {
158             return "{" + m_namespace + "}:" + m_name;
159         }
160     }
161 }
162
Popular Tags