KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Linkage to generic object with defined marshaller and/or unmarshaller. This
36  * provides methods used to generate code for marshalling and unmarshalling
37  * objects of types unknown at binding time, so long as they have mappings
38  * defined.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class DirectGeneric implements IComponent
45 {
46     //
47
// Constants and such related to code generation.
48

49     private static final String JavaDoc ISEND_METHOD =
50         "org.jibx.runtime.impl.UnmarshallingContext.isEnd";
51     private static final String JavaDoc ISEND_SIGNATURE = "()Z";
52     private static final String JavaDoc UNMARSHALELEMENT_METHOD =
53         "org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement";
54     private static final String JavaDoc UNMARSHALELEMENT_SIGNATURE =
55         "()Ljava/lang/Object;";
56     private static final String JavaDoc MARSHALLABLE_INTERFACE =
57         "org.jibx.runtime.IMarshallable";
58     private static final String JavaDoc MARSHALLABLE_METHOD =
59         "org.jibx.runtime.IMarshallable.marshal";
60     private static final String JavaDoc MARSHALLABLE_SIGNATURE =
61         "(Lorg/jibx/runtime/IMarshallingContext;)V";
62
63     //
64
// Actual instance data.
65

66     /** Containing binding definition structure. */
67     private final IContainer m_parent;
68     
69     /** Type handled by this binding. */
70     private final String JavaDoc m_type;
71
72     /** Optional property definition. */
73     private final PropertyDefinition m_property;
74
75     /**
76      * Constructor without implicit property.
77      *
78      * @param parent containing binding definition structure
79      * @param type fully qualified class name of object type handled by this
80      * binding (<code>null</code> if unspecified)
81      */

82
83     public DirectGeneric(IContainer parent, String JavaDoc type) {
84         m_parent = parent;
85         m_type = (type == null) ? "java.lang.Object" : type;
86         m_property = null;
87     }
88
89     /**
90      * Constructor with defined property.
91      *
92      * @param parent containing binding definition structure
93      * @param type fully qualified class name of object type handled by this
94      * binding (<code>null</code> if unspecified)
95      * @param prop associated property information
96      */

97
98     public DirectGeneric(IContainer parent, String JavaDoc type,
99         PropertyDefinition prop) {
100         m_parent = parent;
101         m_type = (type == null) ? "java.lang.Object" : type;
102         m_property = prop;
103     }
104
105     /**
106      * Generate presence test code for this mapping. The generated code just
107      * checks that a start tag is next in the document, rather than an end tag.
108      *
109      * @param mb method builder
110      */

111
112     public void genTestPresent(ContextMethodBuilder mb) {
113         
114         // append code to call unmarshalling context method to check for end tag
115
// as next in document, then invert the result
116
mb.loadContext();
117         mb.appendCallVirtual(ISEND_METHOD, ISEND_SIGNATURE);
118         mb.appendLoadConstant(1);
119         mb.appendIXOR();
120     }
121
122     /**
123      * Generate unmarshalling code for this mapping. The generated code just
124      * calls the generic unmarshal element method, leaving the unmarshalled
125      * object on the stack (after casting it, if necessary, to the appropriate
126      * type).
127      * TODO: Instead call unmarshalling method with class passed directly, for
128      * better error reporting.
129      *
130      * @param mb method builder
131      */

132
133     public void genUnmarshal(ContextMethodBuilder mb) throws JiBXException {
134         
135         // check for optional property
136
BranchWrapper tosave = null;
137         if (m_property != null && m_property.isOptional()) {
138             
139             // generate code to check presence for the case of an optional
140
// item, with branch if so; if not present, set a null value
141
// with branch to be targeted at property store.
142
genTestPresent(mb);
143             BranchWrapper ifpres = mb.appendIFNE(this);
144             mb.appendACONST_NULL();
145             tosave = mb.appendUnconditionalBranch(this);
146             mb.targetNext(ifpres);
147         }
148         
149         // append code to call unmarshalling context method for generic element
150
mb.loadContext();
151         mb.appendCallVirtual(UNMARSHALELEMENT_METHOD,
152             UNMARSHALELEMENT_SIGNATURE);
153         mb.appendCreateCast(m_type);
154         mb.targetNext(tosave);
155         if (m_property != null && !m_property.isImplicit()) {
156             m_property.genStore(mb);
157         }
158     }
159
160     /**
161      * Generate marshalling code for this mapping. The generated code loads the
162      * object reference and casts it to the generic marshal interface, then
163      * calls the marshal method of that interface.
164      *
165      * @param mb method builder
166      */

167
168     public void genMarshal(ContextMethodBuilder mb) throws JiBXException {
169         
170         // append code to cast object and call generic marshal method
171
BranchWrapper toend = null;
172         if (m_property != null && !m_property.isImplicit()) {
173             mb.loadObject();
174             m_property.genLoad(mb);
175             if (m_property.isOptional()) {
176             
177                 // generate code to check nonnull for the case of an optional item,
178
// with branch if so; if not present, just pop the copy with branch
179
// to be targeted past end.
180
mb.appendDUP();
181                 BranchWrapper ifpres = mb.appendIFNONNULL(this);
182                 mb.appendPOP();
183                 toend = mb.appendUnconditionalBranch(this);
184                 mb.targetNext(ifpres);
185             }
186         }
187         mb.appendCreateCast(MARSHALLABLE_INTERFACE);
188         mb.loadContext();
189         mb.appendCallInterface(MARSHALLABLE_METHOD, MARSHALLABLE_SIGNATURE);
190         mb.targetNext(toend);
191     }
192     
193     //
194
// IComponent interface method definitions
195

196     public boolean isOptional() {
197         return false;
198     }
199
200     public boolean hasAttribute() {
201         return false;
202     }
203
204     public void genAttrPresentTest(ContextMethodBuilder mb) {
205         throw new IllegalStateException JavaDoc
206             ("Internal error - no attributes defined");
207     }
208
209     public void genAttributeUnmarshal(ContextMethodBuilder mb) {
210         throw new IllegalStateException JavaDoc
211             ("Internal error - no attributes defined");
212     }
213
214     public void genAttributeMarshal(ContextMethodBuilder mb) {
215         throw new IllegalStateException JavaDoc
216             ("Internal error - no attributes defined");
217     }
218
219     public boolean hasContent() {
220         return true;
221     }
222
223     public void genContentPresentTest(ContextMethodBuilder mb)
224         throws JiBXException {
225         genTestPresent(mb);
226     }
227
228     public void genContentUnmarshal(ContextMethodBuilder mb)
229         throws JiBXException {
230         genUnmarshal(mb);
231     }
232
233     public void genContentMarshal(ContextMethodBuilder mb)
234         throws JiBXException {
235         genMarshal(mb);
236     }
237     
238     public void genNewInstance(ContextMethodBuilder mb) {
239         throw new IllegalStateException JavaDoc
240             ("Internal error - no instance creation");
241     }
242
243     public String JavaDoc getType() {
244         return MARSHALLABLE_INTERFACE;
245     }
246
247     public boolean hasId() {
248         return false;
249     }
250
251     public void genLoadId(ContextMethodBuilder mb) {
252         throw new IllegalStateException JavaDoc("Internal error - no ID allowed");
253     }
254
255     public boolean checkContentSequence(boolean text) {
256         return false;
257     }
258
259     public void setLinkages() {}
260     
261     // DEBUG
262
public void print(int depth) {
263         BindingDefinition.indent(depth);
264         System.out.println("direct generic reference" );
265     }
266 }
Popular Tags