KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.jibx.binding.util.StringArray;
35
36 /**
37  * Model component for elements that define how instances of a particular class
38  * are converted to or from XML. This includes both <b>mapping</b> and
39  * <b>template</b> elements.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44  
45 public abstract class TemplateElementBase extends ContainerElementBase
46 {
47     /** Enumeration of allowed attribute names */
48     public static final StringArray s_allowedAttributes =
49         new StringArray(new String JavaDoc[] { "class" },
50         ContainerElementBase.s_allowedAttributes);
51     
52     /** Name of handled class. */
53     private String JavaDoc m_className;
54     
55     /** Handled class information. */
56     private IClass m_handledClass;
57
58     /** List of child elements. */
59     protected ArrayList JavaDoc m_children;
60
61     /** Templates or mappings that can be used in place of this one (as
62      * substitution group using mapping, or xsi:type with template). */

63     // TODO: collect these during validation
64
private ArrayList JavaDoc m_extensionTypes;
65     
66     /**
67      * Constructor.
68      *
69      * @param type element type code
70      */

71     public TemplateElementBase(int type) {
72         super(type);
73         m_extensionTypes = new ArrayList JavaDoc();
74     }
75     
76     /**
77      * Set mapped class name.
78      *
79      * @param name mapped class name
80      */

81     public void setClassName(String JavaDoc name) {
82         m_className = name;
83     }
84     
85     /**
86      * Get mapped class name.
87      *
88      * @return
89      */

90     public String JavaDoc getClassName() {
91         return m_className;
92     }
93
94     /**
95      * Get handled class information. This call is only meaningful after
96      * prevalidation.
97      *
98      * @return mapped class information
99      */

100     public IClass getHandledClass() {
101         return m_handledClass;
102     }
103     
104     /**
105      * Add template or mapping which derives from this one.
106      *
107      * @param ext derived template or mapping information
108      */

109     protected void addExtensionType(TemplateElementBase ext) {
110         m_extensionTypes.add(ext);
111     }
112     
113     /**
114      * Get templates or mappings which derive from this one.
115      *
116      * @return list of derived templates or mappings
117      */

118     public ArrayList JavaDoc getExtensionTypes() {
119         return m_extensionTypes;
120     }
121
122     /**
123      * Check if default template for type. Needs to be implemented by subclasses
124      * for common handling.
125      *
126      * @return <code>true</code> if default for type, <code>false</code> if not
127      */

128     public abstract boolean isDefaultTemplate();
129
130     /**
131      * Add top-level child element.
132      * TODO: should be ElementBase argument, but JiBX doesn't allow yet
133      *
134      * @param child element to be added as child of this element
135      */

136     public void addTopChild(Object JavaDoc child) {
137         m_children.add(child);
138     }
139
140     /**
141      * Get list of top-level child elements.
142      *
143      * @return list of child elements, or <code>null</code> if none
144      */

145     public ArrayList JavaDoc topChildren() {
146         return m_children;
147     }
148
149     /**
150      * Get iterator for top-level child elements.
151      *
152      * @return iterator for child elements
153      */

154     public Iterator JavaDoc topChildIterator() {
155         return m_children.iterator();
156     }
157     
158     //
159
// Overrides of base class methods
160

161     /* (non-Javadoc)
162      * @see org.jibx.binding.model.ElementBase#isOptional()
163      */

164     public boolean isOptional() {
165         throw new IllegalStateException JavaDoc
166             ("Internal error: method should never be called");
167     }
168
169     /* (non-Javadoc)
170      * @see org.jibx.binding.model.IContextObj#getType()
171      */

172     public IClass getEffectiveType() {
173         return m_handledClass;
174     }
175
176     /* (non-Javadoc)
177      * @see org.jibx.binding.model.IContextObj#getType()
178      */

179     public IClass getActualType() {
180         return m_handledClass;
181     }
182     
183     //
184
// Validation methods
185

186     /* (non-Javadoc)
187      * @see org.jibx.binding.model.ElementBase#prevalidate(org.jibx.binding.model.ValidationContext)
188      */

189     public void prevalidate(ValidationContext vctx) {
190         if (m_className == null) {
191             vctx.addFatal("Class name is required");
192         } else {
193             m_handledClass = vctx.getClassInfo(m_className);
194             if (m_handledClass == null) {
195                 vctx.addFatal("Cannot find information for class " +
196                     m_className);
197             } else {
198                 super.prevalidate(vctx);
199             }
200         }
201     }
202 }
Popular Tags