KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
32 import org.jibx.runtime.JiBXException;
33
34 /**
35  * Property reference with binding defined by component. This handles loading
36  * and storing the property value, calling the wrapped component methods for
37  * everything else.
38  *
39  * @author Dennis M. Sosnoski
40  * @version 1.0
41  */

42
43 public class ComponentProperty extends PassThroughComponent
44 {
45     /** Property definition. */
46     private final PropertyDefinition m_property;
47
48     /** Skip marshalling code generation flag. */
49     private final boolean m_skipMarshal;
50
51     /**
52      * Constructor.
53      *
54      * @param prop actual property definition
55      * @param impl component that defines marshalling and unmarshalling
56      * @param skip flag for marshalling code generation to be skipped
57      */

58
59     public ComponentProperty(PropertyDefinition prop, IComponent impl,
60         boolean skip) {
61         super(impl);
62         m_property = prop;
63         m_skipMarshal = skip;
64     }
65     
66     //
67
// IComponent interface method definitions (overrides of defaults)
68

69     public boolean isOptional() {
70         return m_property.isOptional();
71     }
72
73     public void genAttributeUnmarshal(ContextMethodBuilder mb)
74         throws JiBXException {
75         
76         // start by generating code to load owning object so can finish by
77
// storing to property
78
if (!m_property.isImplicit() && !m_property.isThis()) {
79             mb.loadObject();
80         }
81         BranchWrapper ifpres = null;
82         BranchWrapper tosave = null;
83         if (m_property.isOptional()) {
84             
85             // generate code to check presence for the case of an optional item,
86
// with branch if so; if not present, set a null value with branch
87
// to be targeted at property store.
88
m_component.genAttrPresentTest(mb);
89             ifpres = mb.appendIFNE(this);
90             mb.appendACONST_NULL();
91             tosave = mb.appendUnconditionalBranch(this);
92         }
93             
94         // generate unmarshalling code for not optional, or optional and
95
// present; get existing instance or create a new one and handle
96
// attribute unmarshalling
97
mb.targetNext(ifpres);
98         if (m_property.isImplicit()) {
99             m_component.genNewInstance(mb);
100         } else if (!m_property.isThis()) {
101             
102             // load current value, cast, copy, and test for non-null
103
mb.loadObject();
104             m_property.genLoad(mb);
105             mb.appendCreateCast(m_property.getGetValueType(),
106                 m_component.getType());
107             mb.appendDUP();
108             BranchWrapper haveinst = mb.appendIFNONNULL(this);
109             
110             // current value null, pop copy and create a new instance
111
mb.appendPOP();
112             m_component.genNewInstance(mb);
113             mb.targetNext(haveinst);
114         }
115         mb.appendDUP();
116         m_component.genAttributeUnmarshal(mb);
117         
118         // convert the type if necessary, then store result to property
119
mb.appendCreateCast(m_component.getType(),
120             m_property.getSetValueType());
121         mb.targetNext(tosave);
122         if (!m_property.isImplicit() && !m_property.isThis()) {
123             m_property.genStore(mb);
124         }
125     }
126
127     public void genAttributeMarshal(ContextMethodBuilder mb)
128         throws JiBXException {
129         if (m_skipMarshal) {
130             
131             // just generate pass-through marshal code generation
132
m_component.genAttributeMarshal(mb);
133             
134         } else {
135         
136             // start by generating code to load the actual object reference
137
if (!m_property.isImplicit()) {
138                 mb.loadObject();
139                 m_property.genLoad(mb);
140             }
141             BranchWrapper ifpres = null;
142             BranchWrapper toend = null;
143             if (m_property.isOptional()) {
144             
145                 // generate code to check nonnull for the case of an optional item,
146
// with branch if so; if not present, just pop the copy with branch
147
// to be targeted past end.
148
mb.appendDUP();
149                 ifpres = mb.appendIFNONNULL(this);
150                 mb.appendPOP();
151                 toend = mb.appendUnconditionalBranch(this);
152             }
153         
154             // generate code for actual marshalling if not optional, or optional and
155
// nonnull; then finish by setting target for optional with
156
// null value case
157
mb.targetNext(ifpres);
158             m_component.genAttributeMarshal(mb);
159             mb.targetNext(toend);
160         }
161     }
162
163     public void genContentUnmarshal(ContextMethodBuilder mb)
164         throws JiBXException {
165         
166         // check for both attribute and content components
167
if (m_component.hasAttribute()) {
168             
169             // start with code to load reference from attribute unmarshalling
170
if (!m_property.isImplicit()) {
171                 mb.loadObject();
172                 m_property.genLoad(mb);
173             } else {
174                 mb.appendDUP();
175             }
176             BranchWrapper toend = null;
177             if (m_property.isOptional()) {
178             
179                 // generate code to check value defined for the case of an
180
// optional item, with branch if so; if not present, just pop
181
// the copy with branch to be targeted past end.
182
toend = mb.appendIFNULL(this);
183                 if (!m_property.isImplicit()) {
184                     mb.loadObject();
185                     m_property.genLoad(mb);
186                 } else {
187                     mb.appendDUP();
188                 }
189             }
190             
191             // follow up in case where present with unmarshalling content
192
m_component.genContentUnmarshal(mb);
193             mb.targetNext(toend);
194             
195         } else {
196         
197             // start by generating code to load owning object so can finish by
198
// storing to property
199
if (!m_property.isImplicit() && !m_property.isThis()) {
200                 mb.loadObject();
201             }
202             BranchWrapper ifpres = null;
203             BranchWrapper tosave = null;
204             if (m_property.isOptional()) {
205             
206                 // generate code to check presence for the case of an optional
207
// item, with branch if so; if not present, set a null value
208
// with branch to be targeted at property store.
209
m_component.genContentPresentTest(mb);
210                 ifpres = mb.appendIFNE(this);
211                 mb.appendACONST_NULL();
212                 tosave = mb.appendUnconditionalBranch(this);
213             }
214             
215             // generate unmarshalling code for not optional, or optional and
216
// present; get existing instance or create a new one and handle
217
// content unmarshalling
218
mb.targetNext(ifpres);
219             if (m_property.isImplicit()) {
220                 m_component.genNewInstance(mb);
221             } else if (!m_property.isThis()) {
222                 
223                 // load current value, cast, copy, and test for non-null
224
mb.loadObject();
225                 m_property.genLoad(mb);
226                 mb.appendCreateCast(m_property.getGetValueType(),
227                     m_component.getType());
228                 mb.appendDUP();
229                 BranchWrapper haveinst = mb.appendIFNONNULL(this);
230                 
231                 // current value null, pop copy and create a new instance
232
mb.appendPOP();
233                 m_component.genNewInstance(mb);
234                 mb.targetNext(haveinst);
235             }
236             mb.appendDUP();
237             m_component.genContentUnmarshal(mb);
238             
239             // convert the type if necessary, then store result to property
240
mb.appendCreateCast(m_component.getType(),
241                 m_property.getSetValueType());
242             mb.targetNext(tosave);
243             if (!m_property.isImplicit() && !m_property.isThis()) {
244                 m_property.genStore(mb);
245             }
246         }
247     }
248
249     public void genContentMarshal(ContextMethodBuilder mb)
250         throws JiBXException {
251         if (m_skipMarshal) {
252         
253             // just generate pass-through marshal code generation
254
m_component.genContentMarshal(mb);
255         
256         } else {
257             
258             // start by generating code to load the actual object reference
259
if (!m_property.isImplicit()) {
260                 mb.loadObject();
261                 m_property.genLoad(mb);
262             }
263             BranchWrapper ifpres = null;
264             BranchWrapper tonext = null;
265             if (m_property.isOptional()) {
266                 
267                 // generate code to check nonull for the case of an optional item,
268
// with branch if so; if not present, just pop the copy with branch
269
// to be targeted past end.
270
mb.appendDUP();
271                 ifpres = mb.appendIFNONNULL(this);
272                 mb.appendPOP();
273                 tonext = mb.appendUnconditionalBranch(this);
274             }
275             
276             // generate code for actual marshalling if not optional, or optional and
277
// nonnull; then finish by setting target for optional with null value
278
// case
279
mb.targetNext(ifpres);
280             m_component.genContentMarshal(mb);
281             mb.targetNext(tonext);
282             
283         }
284     }
285     
286     // DEBUG
287
public void print(int depth) {
288         BindingDefinition.indent(depth);
289         System.out.print("component " + m_property.toString());
290         if (m_skipMarshal) {
291             System.out.print(" (pass-through marshal)");
292         }
293         System.out.println();
294         m_component.print(depth+1);
295     }
296 }
Popular Tags