KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > model > NamespaceElement


1 /*
2 Copyright (c) 2004-2005, 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.model;
30
31 import org.jibx.binding.util.StringArray;
32 import org.jibx.runtime.EnumSet;
33 import org.jibx.runtime.IUnmarshallingContext;
34 import org.jibx.runtime.JiBXException;
35
36 /**
37  * Model component for <b>namespace</b> element of binding definition.
38  *
39  * @author Dennis M. Sosnoski
40  * @version 1.0
41  */

42  
43 public class NamespaceElement extends ElementBase
44 {
45     /** Enumeration of allowed attribute names */
46     public static final StringArray s_allowedAttributes =
47         new StringArray(new String JavaDoc[] { "default", "prefix", "uri" });
48     
49     //
50
// Enumeration for namespace usage.
51

52     public static final int NODEFAULT_USAGE = 0;
53     public static final int ELEMENTS_USAGE = 1;
54     public static final int ATTRIBUTES_USAGE = 2;
55     public static final int ALLDEFAULT_USAGE = 3;
56     
57     public static final EnumSet s_defaultEnum =
58         new EnumSet(NODEFAULT_USAGE,
59         new String JavaDoc[] { "none", "elements", "attributes", "all" });
60
61     //
62
// Actual instance data
63

64     /** Default type name. */
65     private String JavaDoc m_defaultName = s_defaultEnum.getName(NODEFAULT_USAGE);
66     
67     /** Actual selected default. */
68     private int m_defaultIndex;
69     
70     /** Namespace URI. */
71     private String JavaDoc m_uri;
72
73     /** Namespace prefix (may be <code>null</code>, but not ""). */
74     private String JavaDoc m_prefix;
75     
76     /**
77      * Constructor.
78      */

79     public NamespaceElement() {
80         super(NAMESPACE_ELEMENT);
81     }
82     
83     /**
84      * Get prefix.
85      *
86      * @return prefix text
87      */

88     public String JavaDoc getPrefix() {
89         return m_prefix;
90     }
91     
92     /**
93      * Set prefix.
94      *
95      * @param prefix text
96      */

97     public void setPrefix(String JavaDoc text) {
98         m_prefix = text;
99     }
100     
101     /**
102      * Get namespace URI.
103      *
104      * @return namespace URI (<code>null</code> if no-namespace namespace)
105      */

106     public String JavaDoc getUri() {
107         return m_uri;
108     }
109     
110     /**
111      * Set namespace URI.
112      *
113      * @param uri namespace URI (<code>null</code> if no-namespace namespace)
114      */

115     public void setUri(String JavaDoc uri) {
116         m_uri = uri;
117     }
118     
119     /**
120      * Set namespace default type name.
121      *
122      * @param name namespace default type
123      */

124     public void setDefaultName(String JavaDoc name) {
125         m_defaultName = name;
126     }
127
128     /**
129      * Check if default namespace for attributes. This method is only meaningful
130      * after a call to {@link #validate}.
131      *
132      * @return <code>true</code> if default namespace for attributes,
133      * <code>false</code> if not
134      */

135     public boolean isAttributeDefault() {
136         return m_defaultIndex == ATTRIBUTES_USAGE ||
137             m_defaultIndex == ALLDEFAULT_USAGE;
138     }
139
140     /**
141      * Check if default namespace for elements. This method is only meaningful
142      * after a call to {@link #validate}.
143      *
144      * @return <code>true</code> if default namespace for elements,
145      * <code>false</code> if not
146      */

147     public boolean isElementDefault() {
148         return m_defaultIndex == ELEMENTS_USAGE ||
149             m_defaultIndex == ALLDEFAULT_USAGE;
150     }
151     
152     //
153
// Validation methods
154

155     /**
156      * Make sure all attributes are defined.
157      *
158      * @param uctx unmarshalling context
159      * @exception JiBXException on unmarshalling error
160      */

161     private void preSet(IUnmarshallingContext uctx) throws JiBXException {
162         validateAttributes(uctx, s_allowedAttributes);
163     }
164     
165     /**
166      * Prevalidate attributes of element in isolation.
167      *
168      * @param vctx validation context
169      */

170     public void prevalidate(ValidationContext vctx) {
171         m_defaultIndex = s_defaultEnum.getValue(m_defaultName);
172         if (m_defaultIndex < 0) {
173             vctx.addError("Value \"" + m_defaultName +
174                 "\" is not a valid choice for namespace default usage");
175         }
176     }
177 }
Popular Tags