KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > tool > CppMarshallingClassesGenerator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire.tool;
19
20 import org.codehaus.jam.JAnnotation;
21 import org.codehaus.jam.JAnnotationValue;
22 import org.codehaus.jam.JClass;
23 import org.codehaus.jam.JProperty;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  *
34  * @version $Revision: 381410 $
35  */

36 public class CppMarshallingClassesGenerator extends CppMarshallingHeadersGenerator {
37
38     protected String JavaDoc getFilePostFix() {
39         return ".cpp";
40     }
41
42     protected void generateUnmarshalBodyForProperty(PrintWriter JavaDoc out, JProperty property, JAnnotationValue size) {
43         out.print(" ");
44         String JavaDoc setter = property.getSetter().getSimpleName();
45         String JavaDoc type = property.getType().getSimpleName();
46
47         if (type.equals("boolean")) {
48             out.println("info." + setter + "( bs.readBoolean() );");
49         }
50         else if (type.equals("byte")) {
51             out.println("info." + setter + "( DataStreamMarshaller.readByte(dataIn) );");
52         }
53         else if (type.equals("char")) {
54             out.println("info." + setter + "( DataStreamMarshaller.readChar(dataIn) );");
55         }
56         else if (type.equals("short")) {
57             out.println("info." + setter + "( DataStreamMarshaller.readShort(dataIn) );");
58         }
59         else if (type.equals("int")) {
60             out.println("info." + setter + "( DataStreamMarshaller.readInt(dataIn) );");
61         }
62         else if (type.equals("long")) {
63             out.println("info." + setter + "( UnmarshalLong(wireFormat, dataIn, bs) );");
64         }
65         else if (type.equals("String")) {
66             out.println("info." + setter + "( readString(dataIn, bs) );");
67         }
68         else if (type.equals("byte[]") || type.equals("ByteSequence")) {
69             if (size != null) {
70                 out.println("info." + setter + "( readBytes(dataIn, " + size.asInt() + ") );");
71             }
72             else {
73                 out.println("info." + setter + "( readBytes(dataIn, bs.readBoolean()) );");
74             }
75         }
76         else if (isThrowable(property.getType())) {
77             out.println("info." + setter + "( unmarshalBrokerError(wireFormat, dataIn, bs) );");
78         }
79         else if (isCachedProperty(property)) {
80             out.println("info." + setter + "( (" + type + ") unmarshalCachedObject(wireFormat, dataIn, bs) );");
81         }
82         else {
83             out.println("info." + setter + "( (" + type + ") unmarshalNestedObject(wireFormat, dataIn, bs) );");
84         }
85     }
86
87     protected void generateUnmarshalBodyForArrayProperty(PrintWriter JavaDoc out, JProperty property, JAnnotationValue size) {
88         JClass propertyType = property.getType();
89         String JavaDoc arrayType = propertyType.getArrayComponentType().getSimpleName();
90         String JavaDoc setter = property.getGetter().getSimpleName();
91         out.println();
92         if (size != null) {
93             out.println(" {");
94             out.println(" " + arrayType + "[] value = new " + arrayType + "[" + size.asInt() + "];");
95             out.println(" " + "for( int i=0; i < " + size.asInt() + "; i++ ) {");
96             out.println(" value[i] = (" + arrayType + ") unmarshalNestedObject(wireFormat,dataIn, bs);");
97             out.println(" }");
98             out.println(" info." + setter + "( value );");
99             out.println(" }");
100         }
101         else {
102             out.println(" if (bs.readBoolean()) {");
103             out.println(" short size = DataStreamMarshaller.readShort(dataIn);");
104             out.println(" " + arrayType + "[] value = new " + arrayType + "[size];");
105             out.println(" for( int i=0; i < size; i++ ) {");
106             out.println(" value[i] = (" + arrayType + ") unmarshalNestedObject(wireFormat,dataIn, bs);");
107             out.println(" }");
108             out.println(" info." + setter + "( value );");
109             out.println(" }");
110             out.println(" else {");
111             out.println(" info." + setter + "( null );");
112             out.println(" }");
113         }
114     }
115
116     protected int generateMarshal1Body(PrintWriter JavaDoc out) {
117         List JavaDoc properties = getProperties();
118         int baseSize = 0;
119         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
120             JProperty property = (JProperty) iter.next();
121             JAnnotation annotation = property.getAnnotation("openwire:property");
122             JAnnotationValue size = annotation.getValue("size");
123             JClass propertyType = property.getType();
124             String JavaDoc type = propertyType.getSimpleName();
125             String JavaDoc getter = "info." + property.getGetter().getSimpleName() + "()";
126
127             out.print(indent);
128             if (type.equals("boolean")) {
129                 out.println("bs.writeBoolean(" + getter + ");");
130             }
131             else if (type.equals("byte")) {
132                 baseSize += 1;
133             }
134             else if (type.equals("char")) {
135                 baseSize += 1;
136             }
137             else if (type.equals("short")) {
138                 baseSize += 1;
139             }
140             else if (type.equals("int")) {
141                 baseSize += 1;
142             }
143             else if (type.equals("long")) {
144                 out.println("rc += marshal1Long(wireFormat, " + getter + ", bs);");
145             }
146             else if (type.equals("String")) {
147                 out.println("rc += writeString(" + getter + ", bs);");
148             }
149             else if (type.equals("byte[]") || type.equals("ByteSequence")) {
150                 if (size == null) {
151                     out.println("bs.writeBoolean(" + getter + "!=null);");
152                     out.println(" rc += " + getter + "==null ? 0 : " + getter + ".Length+4;");
153                 }
154                 else {
155                     baseSize += size.asInt();
156                 }
157             }
158             else if (propertyType.isArrayType()) {
159                 if (size != null) {
160                     out.println("rc += marshalObjectArrayConstSize(wireFormat, " + getter + ", bs, " + size.asInt() + ");");
161                 }
162                 else {
163                     out.println("rc += marshalObjectArray(wireFormat, " + getter + ", bs);");
164                 }
165             }
166             else if (isThrowable(propertyType)) {
167                 out.println("rc += marshalBrokerError(wireFormat, " + getter + ", bs);");
168             }
169             else {
170                 if (isCachedProperty(property)) {
171                     out.println("rc += marshal1CachedObject(wireFormat, " + getter + ", bs);");
172                 }
173                 else {
174                     out.println("rc += marshal1NestedObject(wireFormat, " + getter + ", bs);");
175                 }
176             }
177         }
178         return baseSize;
179     }
180
181     protected void generateMarshal2Body(PrintWriter JavaDoc out) {
182         List JavaDoc properties = getProperties();
183         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
184             JProperty property = (JProperty) iter.next();
185             JAnnotation annotation = property.getAnnotation("openwire:property");
186             JAnnotationValue size = annotation.getValue("size");
187             JClass propertyType = property.getType();
188             String JavaDoc type = propertyType.getSimpleName();
189             String JavaDoc getter = "info." + property.getGetter().getSimpleName() + "()";
190
191             out.print(indent);
192             if (type.equals("boolean")) {
193                 out.println("bs.readBoolean();");
194             }
195             else if (type.equals("byte")) {
196                 out.println("DataStreamMarshaller.writeByte(" + getter + ", dataOut);");
197             }
198             else if (type.equals("char")) {
199                 out.println("DataStreamMarshaller.writeChar(" + getter + ", dataOut);");
200             }
201             else if (type.equals("short")) {
202                 out.println("DataStreamMarshaller.writeShort(" + getter + ", dataOut);");
203             }
204             else if (type.equals("int")) {
205                 out.println("DataStreamMarshaller.writeInt(" + getter + ", dataOut);");
206             }
207             else if (type.equals("long")) {
208                 out.println("marshal2Long(wireFormat, " + getter + ", dataOut, bs);");
209             }
210             else if (type.equals("String")) {
211                 out.println("writeString(" + getter + ", dataOut, bs);");
212             }
213             else if (type.equals("byte[]") || type.equals("ByteSequence")) {
214                 if (size != null) {
215                     out.println("dataOut.write(" + getter + ", 0, " + size.asInt() + ");");
216                 }
217                 else {
218                     out.println("if(bs.readBoolean()) {");
219                     out.println(" DataStreamMarshaller.writeInt(" + getter + ".Length, dataOut);");
220                     out.println(" dataOut.write(" + getter + ");");
221                     out.println(" }");
222                 }
223             }
224             else if (propertyType.isArrayType()) {
225                 if (size != null) {
226                     out.println("marshalObjectArrayConstSize(wireFormat, " + getter + ", dataOut, bs, " + size.asInt() + ");");
227                 }
228                 else {
229                     out.println("marshalObjectArray(wireFormat, " + getter + ", dataOut, bs);");
230                 }
231             }
232             else if (isThrowable(propertyType)) {
233                 out.println("marshalBrokerError(wireFormat, " + getter + ", dataOut, bs);");
234             }
235             else {
236                 if (isCachedProperty(property)) {
237                     out.println("marshal2CachedObject(wireFormat, " + getter + ", dataOut, bs);");
238                 }
239                 else {
240                     out.println("marshal2NestedObject(wireFormat, " + getter + ", dataOut, bs);");
241                 }
242             }
243         }
244     }
245     
246     
247     protected void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc {
248         generateLicence(out);
249         
250 out.println("#include \"marshal/"+className+".hpp\"");
251 out.println("");
252 out.println("using namespace apache::activemq::client::marshal;");
253 out.println("");
254 out.println("/*");
255 out.println(" * Marshalling code for Open Wire Format for "+jclass.getSimpleName()+"");
256 out.println(" *");
257 out.println(" * NOTE!: This file is autogenerated - do not modify!");
258 out.println(" * if you need to make a change, please see the Groovy scripts in the");
259 out.println(" * activemq-core module");
260 out.println(" */");
261 out.println("");
262 out.println(""+className+"::"+className+"()");
263 out.println("{");
264 out.println(" // no-op");
265 out.println("}");
266 out.println("");
267 out.println(""+className+"::~"+className+"()");
268 out.println("{");
269 out.println(" // no-op");
270 out.println("}");
271 out.println("");
272
273         if( !isAbstractClass() ) {
274 out.println("");
275 out.println("");
276 out.println("IDataStructure* "+className+"::createObject() ");
277 out.println("{");
278 out.println(" return new "+jclass.getSimpleName()+"();");
279 out.println("}");
280 out.println("");
281 out.println("char "+className+"::getDataStructureType() ");
282 out.println("{");
283 out.println(" return "+jclass.getSimpleName()+".ID_"+jclass.getSimpleName()+";");
284 out.println("}");
285         }
286
287 out.println("");
288 out.println(" /* ");
289 out.println(" * Un-marshal an object instance from the data input stream");
290 out.println(" */ ");
291 out.println("void "+className+"::unmarshal(ProtocolFormat& wireFormat, Object o, BinaryReader& dataIn, BooleanStream& bs) ");
292 out.println("{");
293 out.println(" base.unmarshal(wireFormat, o, dataIn, bs);");
294  
295         List JavaDoc properties = getProperties();
296         boolean marshallerAware = isMarshallerAware();
297         if( !properties.isEmpty() || marshallerAware ) {
298 out.println("");
299 out.println(" "+jclass.getSimpleName()+"& info = ("+jclass.getSimpleName()+"&) o;");
300         }
301
302         if( marshallerAware ) {
303 out.println("");
304 out.println(" info.beforeUnmarshall(wireFormat);");
305 out.println(" ");
306         }
307
308         generateTightUnmarshalBody(out);
309
310         if( marshallerAware ) {
311 out.println("");
312 out.println(" info.afterUnmarshall(wireFormat);");
313         }
314
315 out.println("");
316 out.println("}");
317 out.println("");
318 out.println("");
319 out.println("/*");
320 out.println(" * Write the booleans that this object uses to a BooleanStream");
321 out.println(" */");
322 out.println("int "+className+"::marshal1(ProtocolFormat& wireFormat, Object& o, BooleanStream& bs) {");
323 out.println(" "+jclass.getSimpleName()+"& info = ("+jclass.getSimpleName()+"&) o;");
324
325
326         if( marshallerAware ) {
327 out.println("");
328 out.println(" info.beforeMarshall(wireFormat);");
329         }
330
331 out.println("");
332 out.println(" int rc = base.marshal1(wireFormat, info, bs);");
333
334         int baseSize = generateMarshal1Body(out);
335     
336 out.println("");
337 out.println(" return rc + "+baseSize+";");
338 out.println("}");
339 out.println("");
340 out.println("/* ");
341 out.println(" * Write a object instance to data output stream");
342 out.println(" */");
343 out.println("void "+className+"::marshal2(ProtocolFormat& wireFormat, Object& o, BinaryWriter& dataOut, BooleanStream& bs) {");
344 out.println(" base.marshal2(wireFormat, o, dataOut, bs);");
345
346         if( !properties.isEmpty() || marshallerAware ) {
347 out.println("");
348 out.println(" "+jclass.getSimpleName()+"& info = ("+jclass.getSimpleName()+"&) o;");
349         }
350
351         generateMarshal2Body(out);
352
353         if( marshallerAware ) {
354 out.println("");
355 out.println(" info.afterMarshall(wireFormat);");
356         }
357
358 out.println("");
359 out.println("}");
360     }
361     
362     public void generateFactory(PrintWriter JavaDoc out) {
363         generateLicence(out);
364 out.println("");
365 out.println("// Marshalling code for Open Wire Format");
366 out.println("//");
367 out.println("//");
368 out.println("// NOTE!: This file is autogenerated - do not modify!");
369 out.println("// if you need to make a change, please see the Groovy scripts in the");
370 out.println("// activemq-openwire module");
371 out.println("//");
372 out.println("");
373 out.println("#include \"marshal/"+className+".hpp\"");
374 out.println("");
375
376         List JavaDoc list = new ArrayList JavaDoc(getConcreteClasses());
377         Collections.sort(list, new Comparator JavaDoc(){
378             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
379                 JClass c1 = (JClass) o1;
380                 JClass c2 = (JClass) o2;
381                 return c1.getSimpleName().compareTo(c2.getSimpleName());
382             }});
383         
384         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
385             JClass jclass = (JClass) iter.next();
386 out.println("#include \"marshal/"+jclass.getSimpleName()+"Marshaller.hpp\"");
387         }
388
389 out.println("");
390 out.println("");
391 out.println("using namespace apache::activemq::client::marshal;");
392 out.println("");
393 out.println("");
394 out.println("void MarshallerFactory::configure(ProtocolFormat& format) ");
395 out.println("{");
396
397         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
398             JClass jclass = (JClass) iter.next();
399 out.println(" format.addMarshaller(new "+jclass.getSimpleName()+"Marshaller());");
400         }
401
402 out.println("");
403 out.println("}");
404
405     }
406 }
407
Popular Tags