KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 Copyright (c) 2003-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.def;
30
31 import org.jibx.binding.classes.ClassCache;
32 import org.jibx.binding.classes.ClassFile;
33 import org.jibx.binding.classes.ContextMethodBuilder;
34 import org.jibx.runtime.JiBXException;
35
36 /**
37  * Reference to a structure definition. This is used as a placeholder when
38  * building the component structure of a binding definition. It's necessary
39  * because the referenced structure may not have been parsed yet. During the
40  * linkage phase that follows parsing this looks up the appropriate structure
41  * definition and sets up the corresponding component structure. Thereafter it
42  * operates as a simple pass-through wrapper for the top child component.
43  *
44  * @author Dennis M. Sosnoski
45  * @version 1.0
46  */

47
48 public class StructureReference extends PassThroughComponent
49 {
50     /** Containing binding component. */
51     private final IContainer m_container;
52     
53     /** Containing binding definition structure. */
54     private final IContextObj m_contextObject;
55     
56     /** Property definition (may be <code>null</code>). */
57     private final PropertyDefinition m_property;
58     
59     /** Identifier for referenced structure definition. */
60     private final String JavaDoc m_label;
61     
62     /** Flag for marshalling code generation to be skipped by component. */
63     private boolean m_skipMarshal;
64     
65     /** Object load needed for marshalling flag (used with object binding). */
66     private boolean m_needLoad;
67
68     /**
69      * Constructor.
70      *
71      * @param contain containing binding component
72      * @param label reference structure identifier
73      * @param prop property definition (may be <code>null</code>)
74      * @param hasname element name used with reference flag
75      * @param cobj context object
76      */

77
78     public StructureReference(IContainer contain, String JavaDoc label,
79         PropertyDefinition prop, boolean hasname, IContextObj cobj) {
80         super();
81         m_container = contain;
82         m_contextObject = cobj;
83         m_property = prop;
84         m_skipMarshal = hasname && prop != null && prop.isOptional();
85         m_label = label;
86     }
87     
88     //
89
// IComponent interface method definitions (overrides of defaults)
90

91     public void genAttributeMarshal(ContextMethodBuilder mb)
92         throws JiBXException {
93         if (m_needLoad) {
94             mb.loadObject();
95         }
96         m_component.genAttributeMarshal(mb);
97     }
98
99     public void genContentMarshal(ContextMethodBuilder mb)
100         throws JiBXException {
101         if (m_needLoad) {
102             mb.loadObject();
103         }
104         m_component.genContentMarshal(mb);
105     }
106
107     public void setLinkages() throws JiBXException {
108         
109         // find the structure being used
110
DefinitionContext defc = m_container.getDefinitionContext();
111         IComponent impl = defc.getNamedStructure(m_label);
112         
113         // verify compatible use of structure
114
String JavaDoc type = (m_property == null) ?
115             m_contextObject.getBoundClass().getClassName() :
116             m_property.getTypeName();
117         ClassFile cf = ClassCache.getClassFile(type);
118         String JavaDoc itype = impl.getType();
119         if (!cf.isSuperclass(itype)) {
120             throw new JiBXException("Reference to structure " + m_label +
121                 " has object of type " + type + " rather than required " +
122                 itype);
123         }
124         
125         // generate component matching mapping type
126
IComponent wrap;
127         if (impl instanceof DirectObject) {
128             wrap = new DirectProperty(m_property, (DirectObject)impl);
129         } else if (m_property == null || m_property.isImplicit()) {
130             wrap = impl;
131             m_needLoad = impl instanceof ObjectBinding;
132         } else {
133             wrap = new ComponentProperty(m_property, impl, m_skipMarshal);
134         }
135         
136         // set the wrapped component used for all other calls
137
setWrappedComponent(wrap);
138         m_component.setLinkages();
139     }
140     
141     // DEBUG
142
public void print(int depth) {
143         BindingDefinition.indent(depth);
144         System.out.print("structure reference to " + m_label);
145         if (m_property != null) {
146             System.out.println(" using " + m_property.toString());
147         }
148         System.out.println();
149     }
150 }
151
Popular Tags