KickJava   Java API By Example, From Geeks To Geeks.

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


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.IUnmarshallingContext;
33 import org.jibx.runtime.JiBXException;
34 import org.jibx.runtime.impl.UnmarshallingContext;
35
36 /**
37  * Base class for all element structures in binding definition model. This just
38  * provides the linkages for the binding definition tree structure and related
39  * validation hooks.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44  
45 public abstract class ElementBase
46 {
47     //
48
// Element type definitions.
49

50     public static final int BINDING_ELEMENT = 0;
51     public static final int COLLECTION_ELEMENT = 1;
52     public static final int FORMAT_ELEMENT = 2;
53     public static final int MAPPING_ELEMENT = 3;
54     public static final int NAMESPACE_ELEMENT = 4;
55     public static final int STRUCTURE_ELEMENT = 5;
56     public static final int TEMPLATE_ELEMENT = 6;
57     public static final int VALUE_ELEMENT = 7;
58     // special elements eliminated during splitting
59
public static final int INCLUDE_ELEMENT = 8;
60     public static final int SPLIT_ELEMENT = 9;
61     public static final int INPUT_ELEMENT = 10;
62     public static final int OUTPUT_ELEMENT = 11;
63     
64     public static final String JavaDoc[] ELEMENT_NAMES =
65     {
66         "binding", "collection", "format", "mapping", "namespace", "structure",
67         "template", "value", "include", "split", "input", "output"
68     };
69     
70     //
71
// Instance data.
72

73     /** Element type. */
74     private final int m_type;
75     
76     /** Comment associated with element. */
77     private String JavaDoc m_comment;
78     
79     /**
80      * Constructor.
81      *
82      * @param type element type code
83      */

84     protected ElementBase(int type) {
85         m_type = type;
86     }
87     
88     /**
89      * Get element type.
90      *
91      * @return type code for this element
92      */

93     public final int type() {
94         return m_type;
95     }
96     
97     /**
98      * Get element name.
99      *
100      * @return type code for this element
101      */

102     public final String JavaDoc name() {
103         return ELEMENT_NAMES[m_type];
104     }
105     
106     /**
107      * Get element comment.
108      *
109      * @return comment for this element
110      */

111     public final String JavaDoc getComment() {
112         return m_comment;
113     }
114     
115     /**
116      * Set element comment.
117      *
118      * @param text comment for this element
119      */

120     public final void setComment(String JavaDoc text) {
121         m_comment = text;
122     }
123     
124     /**
125      * Validate attributes of element. This is designed to be called during
126      * unmarshalling as part of the pre-set method processing when a subclass
127      * instance is being created.
128      *
129      * @param vctx validation context
130      * @param attrs attributes array
131      * @exception JiBXException on unmarshalling error
132      */

133     protected void validateAttributes(IUnmarshallingContext ictx,
134         StringArray attrs) throws JiBXException {
135         
136         // setup for attribute access
137
int count = ictx.getStackDepth();
138         BindingElement.UnmarshalWrapper wrapper =
139             (BindingElement.UnmarshalWrapper)ictx.getStackObject(count-1);
140         ValidationContext vctx = wrapper.getValidation();
141         UnmarshallingContext uctx = (UnmarshallingContext)ictx;
142         try {
143             
144             // loop through all attributes of current element
145
for (int i = 0; i < uctx.getAttributeCount(); i++) {
146                 
147                 // check if nonamespace attribute is in the allowed set
148
String JavaDoc name = uctx.getAttributeName(i);
149                 if (uctx.getAttributeNamespace(i).length() == 0) {
150                     if (attrs.indexOf(name) < 0) {
151                         vctx.addWarning("Undefined attribute " + name, this);
152                     }
153                 }
154             }
155         } catch (JiBXException e) {
156         }
157     }
158     
159     /**
160      * Prevalidate element information. The prevalidation step is used to
161      * check isolated aspects of an element, such as the settings for enumerated
162      * values on the element and attributes. This empty base class
163      * implementation should be overridden by each subclass that requires
164      * prevalidation handling.
165      *
166      * @param vctx validation context
167      */

168     public void prevalidate(ValidationContext vctx) {}
169     
170     /**
171      * Validate element information. The validation step is used for checking
172      * the interactions between elements, such as name references to other
173      * elements. The {@link #prevalidate} method will always be called for every
174      * element in the binding definition before this method is called for any
175      * element. This empty base class implementation should be overridden by
176      * each subclass that requires validation handling.
177      *
178      * @param vctx validation context
179      */

180     public void validate(ValidationContext vctx) {}
181 }
Popular Tags