KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.JiBXException;
32
33 /**
34  * Namespace definition from binding.
35  *
36  * @author Dennis M. Sosnoski
37  * @version 1.0
38  */

39
40 public class NamespaceDefinition
41 {
42     //
43
// Enumeration for namespace usage.
44

45     /*package*/ static final int NODEFAULT_USAGE = 0;
46     /*package*/ static final int ELEMENTS_USAGE = 1;
47     /*package*/ static final int ATTRIBUTES_USAGE = 2;
48     /*package*/ static final int ALLDEFAULT_USAGE = 3;
49
50     //
51
// Actual instance data
52

53     /** Namespace URI. */
54     private String JavaDoc m_uri;
55
56     /** Namespace prefix (may be <code>null</code>, but not ""). */
57     private String JavaDoc m_prefix;
58     
59     /** Index in namespace table for binding. */
60     private int m_index;
61
62     /** Use by default for nested elements. */
63     private boolean m_elementDefault;
64
65     /** Use by default for nested attributes. */
66     private boolean m_attributeDefault;
67
68     /**
69      * Constructor.
70      *
71      * @param uri namespace URI
72      * @param prefix namespace prefix (may be <code>null</code> for default
73      * namespace, but not "")
74      * @param usage code for default usage of namespace
75      * @throws JiBXException if configuration error
76      */

77
78     public NamespaceDefinition(String JavaDoc uri, String JavaDoc prefix, int usage)
79         throws JiBXException {
80         m_uri = uri;
81         m_prefix = prefix;
82         m_elementDefault =
83              (usage == ALLDEFAULT_USAGE) || (usage == ELEMENTS_USAGE);
84         m_attributeDefault =
85              (usage == ALLDEFAULT_USAGE) || (usage == ATTRIBUTES_USAGE);
86         if (usage != ELEMENTS_USAGE && prefix == null) {
87             throw new JiBXException
88                 ("Prefix required for namespace unless element default");
89         }
90         if (m_attributeDefault && m_prefix == null) {
91             throw new JiBXException("Prefix required for attribute namespace");
92         }
93     }
94
95     /**
96      * Check if default namespace for attributes.
97      *
98      * @return <code>true</code> if default namespace for attributes,
99      * <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 JavaDoc getPrefix() {
124         return m_prefix;
125     }
126
127     /**
128      * Get namespace URI.
129      *
130      * @return namespace URI
131      */

132
133     public String JavaDoc 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 JavaDoc uri,
166         String JavaDoc 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 }
180
Popular Tags