KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 import org.jibx.binding.util.StringArray;
34 import org.jibx.runtime.IUnmarshallingContext;
35 import org.jibx.runtime.JiBXException;
36
37 /**
38  * Model component for <b>structure</b> element of binding definition.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43  
44 public class StructureElement extends StructureElementBase
45 {
46     /** Enumeration of allowed attribute names */
47     public static final StringArray s_allowedAttributes =
48         new StringArray(new String JavaDoc[] { "map-as" },
49         StructureElementBase.s_allowedAttributes);
50     
51     /** Mapped type name to use for this object. */
52     private String JavaDoc m_mapAsName;
53     
54     /** Binding to use for this object. */
55     private TemplateElementBase m_mapAsMapping;
56     
57     /**
58      * Default constructor.
59      */

60     public StructureElement() {
61         super(STRUCTURE_ELEMENT);
62     }
63     
64     /**
65      * Get name of specified type.
66      *
67      * @return specified type name (or <code>null</code> if none)
68      */

69     public String JavaDoc getMapAsName() {
70         return m_mapAsName;
71     }
72     
73     /**
74      * Set name of specified type.
75      *
76      * @param name specified type name (or <code>null</code> if none)
77      */

78     public void setMapAsName(String JavaDoc name) {
79         m_mapAsName = name;
80     }
81     
82     /**
83      * Get specified type mapping. This call is only meaningful after
84      * validation.
85      *
86      * @return specified type mapping (or <code>null</code> if none)
87      */

88     public TemplateElementBase getMapAsMapping() {
89         return m_mapAsMapping;
90     }
91     
92     //
93
// Overrides of base class methods
94

95     /**
96      * Get effective type of class linked to binding element. This call is only
97      * meaningful after validation.
98      *
99      * @return information for class linked by binding
100      */

101     public IClass getEffectiveType() {
102         return getType();
103     }
104     
105     /**
106      * Get actual class linked to binding element. This call is only meaningful
107      * after validation.
108      *
109      * @return information for class linked by binding, or <code>null</code> if
110      * no actual object linked to this binding element
111      */

112     public IClass getActualType() {
113         if (hasType()) {
114             return getType();
115         } else {
116             return null;
117         }
118     }
119     
120     /* (non-Javadoc)
121      * @see org.jibx.binding.model.IComponent#hasName()
122      */

123     public boolean hasName() {
124         if (m_mapAsMapping instanceof MappingElement) {
125             if (((MappingElement)m_mapAsMapping).getName() != null) {
126                 return true;
127             }
128         }
129         if (children().size() == 0) {
130             // must be an implicit mapping reference
131
return true;
132         }
133         return super.hasName();
134     }
135     
136     /* (non-Javadoc)
137      * @see org.jibx.binding.model.IComponent#hasType()
138      */

139     public boolean hasType() {
140         return m_mapAsMapping != null || super.hasType();
141     }
142     
143     /* (non-Javadoc)
144      * @see org.jibx.binding.model.IComponent#getType()
145      */

146     public IClass getType() {
147         if (m_mapAsMapping == null) {
148             return super.getType();
149         } else {
150             return m_mapAsMapping.getHandledClass();
151         }
152     }
153         
154     //
155
// Validation methods
156

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

163     private void preSet(IUnmarshallingContext uctx) throws JiBXException {
164         validateAttributes(uctx, s_allowedAttributes);
165     }
166
167     /* (non-Javadoc)
168      * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext)
169      */

170     public void validate(ValidationContext vctx) {
171         
172         // check if there's a mapping if used without children
173
DefinitionContext dctx = vctx.getDefinitions();
174         if (children().size() == 0) {
175             if (m_mapAsName == null) {
176                 
177                 // see if this is using implicit marshaller/unmarshaller
178
if ((vctx.isInBinding() && getUnmarshallerName() == null) ||
179                     (vctx.isOutBinding() && getMarshallerName() == null)) {
180                     if (getUsing() == null) {
181                         if (!dctx.isCompatibleTemplateType(getType())) {
182                             vctx.addFatal
183                                 ("No compatible mapping defined for type " +
184                                 getType().getName());
185                         }
186                     }
187                 }
188             } else {
189                 m_mapAsMapping = dctx.getSpecificTemplate(m_mapAsName);
190             }
191         } else if (m_mapAsName != null) {
192             vctx.addError("map-as attribute cannot be used with children");
193         }
194         IClass type = getType();
195         if (type != null) {
196             
197             // check each child component for compatibile type
198
ArrayList JavaDoc children = children();
199             for (int i = 0; i < children.size(); i++) {
200                 ElementBase child = (ElementBase)children.get(i);
201                 if (child instanceof IComponent) {
202                     IComponent comp = (IComponent)child;
203                     if (comp.isImplicit() && comp.hasType()) {
204                         IClass ctype = comp.getType();
205                         if (!ctype.isAssignable(type)) {
206                             vctx.addFatal("Implicit references to structure " +
207                                 "object must have types compatible with " +
208                                 "structure type", child);
209                         }
210                     }
211                 }
212             }
213         }
214         super.validate(vctx);
215     }
216 }
Popular Tags