KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.bcel.Constants;
32 import org.jibx.binding.classes.BoundClass;
33 import org.jibx.binding.classes.ClassFile;
34 import org.jibx.binding.classes.ContextMethodBuilder;
35 import org.jibx.binding.classes.ExceptionMethodBuilder;
36 import org.jibx.runtime.JiBXException;
37
38 /**
39  * Base class for mapping definitions. This is used for both normal and custom
40  * mappings. It handles adding the appropriate marshalling and/or unmarshalling
41  * interfaces and methods to the classes.
42  */

43  
44 public abstract class MappingBase extends LinkableBase implements IMapping
45 {
46     //
47
// Constants and such related to code generation.
48

49     // definitions for IMarshallable interface defined on mapped classes
50
protected static final String JavaDoc IMARSHALLABLE_INTERFACE =
51         "org.jibx.runtime.IMarshallable";
52     protected static final String JavaDoc MARSHALLABLE_METHODNAME = "marshal";
53     protected static final String JavaDoc MARSHALLABLE_SIGNATURE =
54         "(Lorg/jibx/runtime/IMarshallingContext;)V";
55     protected static final String JavaDoc GETINDEX_METHODNAME = "JiBX_getIndex";
56     protected static final String JavaDoc GETINDEX_SIGNATURE = "()I";
57     protected static final String JavaDoc CHECKEXTENDS_METHODNAME = "isExtension";
58     protected static final String JavaDoc CHECKEXTENDS_SIGNATURE = "(I)Z";
59     
60     // definitions for IUnmarshallable interface defined on mapped classes
61
protected static final String JavaDoc IUNMARSHALLABLE_INTERFACE =
62         "org.jibx.runtime.IUnmarshallable";
63     protected static final String JavaDoc UNMARSHALLABLE_METHODNAME = "unmarshal";
64     protected static final String JavaDoc UNMARSHALLABLE_SIGNATURE =
65         "(Lorg/jibx/runtime/IUnmarshallingContext;)V";
66     
67     // interface implemented by unmarshaller class
68
protected static final String JavaDoc UNMARSHALLER_INTERFACE =
69         "org.jibx.runtime.IUnmarshaller";
70     protected static final String JavaDoc UNMARSHALLERUNMARSHAL_METHOD =
71         "org.jibx.runtime.IUnmarshaller.unmarshal";
72     protected static final String JavaDoc UNMARSHALLERUNMARSHAL_SIGNATURE =
73         "(Ljava/lang/Object;Lorg/jibx/runtime/IUnmarshallingContext;)" +
74         "Ljava/lang/Object;";
75     
76     // interface implemented by marshaller class
77
protected static final String JavaDoc MARSHALLER_INTERFACE =
78         "org.jibx.runtime.IMarshaller";
79     protected static final String JavaDoc ABSTRACTMARSHALLER_INTERFACE =
80         "org.jibx.runtime.IAbstractMarshaller";
81     protected static final String JavaDoc MARSHALLERMARSHAL_METHOD =
82         "org.jibx.runtime.IMarshaller.marshal";
83     protected static final String JavaDoc MARSHALLERMARSHAL_SIGNATURE =
84         "(Ljava/lang/Object;Lorg/jibx/runtime/IMarshallingContext;)V";
85     
86     // definitions for context methods used to find marshaller and unmarshaller
87
protected static final String JavaDoc GETMARSHALLER_METHOD =
88         "org.jibx.runtime.IMarshallingContext.getMarshaller";
89     protected static final String JavaDoc GETMARSHALLER_SIGNATURE =
90         "(ILjava/lang/String;)Lorg/jibx/runtime/IMarshaller;";
91     protected static final String JavaDoc GETUNMARSHALLER_METHOD =
92         "org.jibx.runtime.IUnmarshallingContext.getUnmarshaller";
93     protected static final String JavaDoc GETUNMARSHALLER_SIGNATURE =
94         "(I)Lorg/jibx/runtime/IUnmarshaller;";
95     
96     //
97
// Actual instance data.
98

99     /** Index number for this particular binding definition. */
100     private final int m_indexNumber;
101
102     /**
103      * Constructor. This version requires the component to be set later,
104      * using the {@link
105      * org.jibx.binding.def.PassThroughComponent#setWrappedComponent} method.
106      *
107      * @param contain containing binding definition structure
108      * @param type bound class name
109      */

110
111     public MappingBase(IContainer contain, String JavaDoc type) {
112         m_indexNumber = contain.getBindingRoot().getMappedClassIndex(type);
113     }
114
115     /**
116      * Constructor with wrapped component supplied.
117      *
118      * @param contain containing binding definition structure
119      * @param type bound class name
120      * @param wrap wrapped binding component
121      */

122
123     public MappingBase(IContainer contain, String JavaDoc type, IComponent wrap) {
124         this(contain, type);
125         setWrappedComponent(wrap);
126     }
127
128     /**
129      * Get the mapped class information. This must be implemented in each
130      * subclass to return the type of the bound class.
131      *
132      * @return information for mapped class
133      */

134     
135     public abstract BoundClass getBoundClass();
136
137     /**
138      * Generate marshallable interface methods for this mapping. This is not
139      * applicable to abstract mappings, since they cannot be marshalled as
140      * separate items.
141      *
142      * @throws JiBXException if error in generating code
143      */

144      
145     protected void addIMarshallableMethod() throws JiBXException {
146         
147         // set up for constructing actual marshal method
148
BoundClass clas = getBoundClass();
149         ClassFile cf = clas.getMungedFile();
150         ContextMethodBuilder mb = new ContextMethodBuilder
151             (MARSHALLABLE_METHODNAME, MARSHALLABLE_SIGNATURE, cf,
152             Constants.ACC_PUBLIC, 0, clas.getClassFile().getName(),
153             1, MARSHALLER_INTERFACE);
154         
155         // create call to marshalling context method with class index and
156
// actual class name
157
mb.loadContext();
158         mb.appendLoadConstant(getIndex());
159         mb.appendLoadConstant(cf.getName());
160         mb.appendCallInterface(GETMARSHALLER_METHOD, GETMARSHALLER_SIGNATURE);
161         
162         // call the returned marshaller with this object and the marshalling
163
// context as parameters
164
mb.loadObject();;
165         mb.loadContext();
166         mb.appendCallInterface(MARSHALLERMARSHAL_METHOD,
167             MARSHALLERMARSHAL_SIGNATURE);
168         mb.appendReturn();
169         
170         // add method to class
171
clas.getUniqueNamed(mb);
172         
173         // set up for constructing get index method
174
ExceptionMethodBuilder xb = new ExceptionMethodBuilder
175             (GETINDEX_METHODNAME, GETINDEX_SIGNATURE, cf, Constants.ACC_PUBLIC);
176         
177         // generate code to return the constant index number
178
xb.appendLoadConstant(getIndex());
179         xb.appendReturn("int");
180         
181         // add the method and interface to class
182
clas.getUniqueNamed(xb);
183         clas.getClassFile().addInterface(IMARSHALLABLE_INTERFACE);
184     }
185
186     /**
187      * Generate unmarshallable interface method for this mapping. This is not
188      * applicable to abstract mappings, since they cannot be unmarshalled as
189      * separate items.
190      *
191      * @throws JiBXException if error in generating code
192      */

193      
194     protected void addIUnmarshallableMethod() throws JiBXException {
195         
196         // set up for constructing new method
197
BoundClass clas = getBoundClass();
198         ClassFile cf = clas.getMungedFile();
199         ContextMethodBuilder mb = new ContextMethodBuilder
200             (UNMARSHALLABLE_METHODNAME, UNMARSHALLABLE_SIGNATURE, cf,
201             Constants.ACC_PUBLIC, 0, clas.getClassFile().getName(),
202             1, UNMARSHALLER_INTERFACE);
203         
204         // create call to unmarshalling context method with class index
205
mb.loadContext();
206         mb.appendLoadConstant(getIndex());
207         mb.appendCallInterface(GETUNMARSHALLER_METHOD,
208             GETUNMARSHALLER_SIGNATURE);
209         
210         // call the returned unmarshaller with this object and the unmarshalling
211
// context as parameters
212
mb.loadObject();;
213         mb.loadContext();
214         mb.appendCallInterface(UNMARSHALLERUNMARSHAL_METHOD,
215             UNMARSHALLERUNMARSHAL_SIGNATURE);
216         mb.appendReturn();
217         
218         // add the method and interface to class
219
clas.getUniqueNamed(mb);
220         clas.getClassFile().addInterface(IUNMARSHALLABLE_INTERFACE);
221     }
222     
223     //
224
// IMapping interface method definitions
225

226     public int getIndex() {
227         return m_indexNumber;
228     }
229 }
Popular Tags