KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.codehaus.jam.JClass;
26 import org.codehaus.jam.JProperty;
27
28 /**
29  *
30  * @version $Revision: 409828 $
31  */

32 public class CppClassesGenerator extends MultiSourceGenerator {
33
34     protected String JavaDoc targetDir="./src/main/cpp";
35
36     public Object JavaDoc run() {
37         filePostFix = getFilePostFix();
38         if (destDir == null) {
39             destDir = new File JavaDoc(targetDir+"/activemq/command");
40         }
41         return super.run();
42     }
43
44     protected String JavaDoc getFilePostFix() {
45         return ".cpp";
46     }
47
48     /**
49      * Converts the Java type to a C++ type name
50      */

51     public String JavaDoc toCppType(JClass type) {
52         String JavaDoc name = type.getSimpleName();
53         if (name.equals("String")) {
54             return "p<string>";
55         }
56         else if (type.isArrayType()) {
57         if( name.equals("byte[]") )
58                 name = "char[]" ;
59         else if( name.equals("DataStructure[]") )
60                 name = "IDataStructure[]" ;
61
62             return "array<" + name.substring(0, name.length()-2) + ">";
63         }
64         else if (name.equals("Throwable") || name.equals("Exception")) {
65             return "p<BrokerError>";
66         }
67         else if (name.equals("ByteSequence")) {
68             return "array<char>";
69         }
70         else if (name.equals("boolean")) {
71             return "bool";
72         }
73         else if (name.equals("long")) {
74             return "long long";
75         }
76         else if (name.equals("byte")) {
77             return "char";
78         }
79         else if( name.equals("Command") || name.equals("DataStructure") ) {
80             return "p<I" + name + ">" ;
81         }
82         else if( !type.isPrimitiveType() ) {
83             return "p<" + name + ">" ;
84         }
85         else {
86             return name ;
87         }
88     }
89
90     /**
91      * Converts the Java type to a C++ default value
92      */

93     public String JavaDoc toCppDefaultValue(JClass type) {
94         String JavaDoc name = type.getSimpleName();
95
96         if ( name.equals("boolean") ) {
97             return "false";
98         }
99         else if (!type.isPrimitiveType()) {
100             return "NULL";
101         }
102         else {
103             return "0";
104         }
105     }
106
107     /**
108      * Converts the Java type to the name of the C++ marshal method
109      * to be used
110      */

111     public String JavaDoc toMarshalMethodName(JClass type) {
112         String JavaDoc name = type.getSimpleName();
113         if (name.equals("String")) {
114             return "marshalString";
115         }
116         else if (type.isArrayType()) {
117             if ( type.getArrayComponentType().isPrimitiveType() && name.equals("byte[]") )
118                 return "marshalByteArray" ;
119             else
120                 return "marshalObjectArray" ;
121         }
122         else if ( name.equals("ByteSequence") ) {
123             return "marshalByteArray";
124         }
125         else if (name.equals("short") ) {
126             return "marshalShort";
127         }
128         else if (name.equals("int") ) {
129             return "marshalInt";
130         }
131         else if (name.equals("long") ) {
132             return "marshalLong";
133         }
134         else if (name.equals("byte")) {
135             return "marshalByte";
136         }
137         else if (name.equals("double")) {
138             return "marshalDouble";
139         }
140         else if (name.equals("float")) {
141             return "marshalFloat";
142         }
143         else if (name.equals("boolean")) {
144             return "marshalBoolean";
145         }
146         else if( !type.isPrimitiveType() ) {
147             return "marshalObject" ;
148         }
149         else {
150             return name ;
151         }
152     }
153
154     /**
155      * Converts the Java type to the name of the C++ unmarshal method
156      * to be used
157      */

158     public String JavaDoc toUnmarshalMethodName(JClass type) {
159         String JavaDoc name = type.getSimpleName();
160         if (name.equals("String")) {
161             return "unmarshalString";
162         }
163         else if (type.isArrayType()) {
164             if ( type.getArrayComponentType().isPrimitiveType() && name.equals("byte[]") )
165                 return "unmarshalByteArray" ;
166             else
167                 return "unmarshalObjectArray" ;
168         }
169         else if ( name.equals("ByteSequence") ) {
170             return "unmarshalByteArray";
171         }
172         else if (name.equals("short") ) {
173             return "unmarshalShort";
174         }
175         else if (name.equals("int") ) {
176             return "unmarshalInt";
177         }
178         else if (name.equals("long") ) {
179             return "unmarshalLong";
180         }
181         else if (name.equals("byte")) {
182             return "unmarshalByte";
183         }
184         else if (name.equals("double")) {
185             return "unmarshalDouble";
186         }
187         else if (name.equals("float")) {
188             return "unmarshalFloat";
189         }
190         else if (name.equals("boolean")) {
191             return "unmarshalBoolean";
192         }
193         else if( !type.isPrimitiveType() ) {
194             return "unmarshalObject" ;
195         }
196         else {
197             return name ;
198         }
199     }
200
201     /**
202      * Converts the Java type to a C++ pointer cast
203      */

204     public String JavaDoc toUnmarshalCast(JClass type) {
205         String JavaDoc name = toCppType(type) ;
206
207         if( name.startsWith("p<") )
208             return "p_cast<" + name.substring(2) ;
209         else if( name.startsWith("array<") &&
210                  (type.isArrayType() && !type.getArrayComponentType().isPrimitiveType()) &&
211                  !type.getSimpleName().equals("ByteSequence") )
212             return "array_cast<" + name.substring(6) ;
213         else
214             return "" ;
215     }
216     
217     
218     protected void generateLicence(PrintWriter JavaDoc out) {
219 out.println("/*");
220 out.println(" *");
221 out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
222 out.println(" * contributor license agreements. See the NOTICE file distributed with");
223 out.println(" * this work for additional information regarding copyright ownership.");
224 out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
225 out.println(" * (the \"License\"); you may not use this file except in compliance with");
226 out.println(" * the License. You may obtain a copy of the License at");
227 out.println(" *");
228 out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
229 out.println(" *");
230 out.println(" * Unless required by applicable law or agreed to in writing, software");
231 out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
232 out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
233 out.println(" * See the License for the specific language governing permissions and");
234 out.println(" * limitations under the License.");
235 out.println(" */");
236     }
237
238     protected void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc {
239         generateLicence(out);
240 out.println("#include \"activemq/command/"+className+".hpp\"");
241 out.println("");
242 out.println("using namespace apache::activemq::command;");
243 out.println("");
244 out.println("/*");
245 out.println(" *");
246 out.println(" * Command and marshalling code for OpenWire format for "+className+"");
247 out.println(" *");
248 out.println(" *");
249 out.println(" * NOTE!: This file is autogenerated - do not modify!");
250 out.println(" * if you need to make a change, please see the Groovy scripts in the");
251 out.println(" * activemq-core module");
252 out.println(" *");
253 out.println(" */");
254 out.println(""+className+"::"+className+"()");
255 out.println("{");
256
257         List JavaDoc properties = getProperties();
258         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
259             JProperty property = (JProperty) iter.next();
260             String JavaDoc value = toCppDefaultValue(property.getType());
261             String JavaDoc propertyName = property.getSimpleName();
262             String JavaDoc parameterName = decapitalize(propertyName);
263 out.println(" this->"+parameterName+" = "+value+" ;");
264         }
265 out.println("}");
266 out.println("");
267 out.println(""+className+"::~"+className+"()");
268 out.println("{");
269 out.println("}");
270 out.println("");
271 out.println("unsigned char "+className+"::getDataStructureType()");
272 out.println("{");
273 out.println(" return "+className+"::TYPE ; ");
274 out.println("}");
275         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
276             JProperty property = (JProperty) iter.next();
277             String JavaDoc type = toCppType(property.getType());
278             String JavaDoc propertyName = property.getSimpleName();
279             String JavaDoc parameterName = decapitalize(propertyName);
280 out.println("");
281 out.println(" ");
282 out.println(""+type+" "+className+"::get"+propertyName+"()");
283 out.println("{");
284 out.println(" return "+parameterName+" ;");
285 out.println("}");
286 out.println("");
287 out.println("void "+className+"::set"+propertyName+"("+type+" "+parameterName+")");
288 out.println("{");
289 out.println(" this->"+parameterName+" = "+parameterName+" ;");
290 out.println("}");
291         }
292 out.println("");
293 out.println("int "+className+"::marshal(p<IMarshaller> marshaller, int mode, p<IOutputStream> ostream) throw (IOException)");
294 out.println("{");
295 out.println(" int size = 0 ;");
296 out.println("");
297 out.println(" size += "+baseClass+"::marshal(marshaller, mode, ostream) ; ");
298
299         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
300             JProperty property = (JProperty) iter.next();
301             String JavaDoc marshalMethod = toMarshalMethodName(property.getType());
302             String JavaDoc propertyName = decapitalize(property.getSimpleName());
303 out.println(" size += marshaller->"+marshalMethod+"("+propertyName+", mode, ostream) ; ");
304         }
305 out.println(" return size ;");
306 out.println("}");
307 out.println("");
308 out.println("void "+className+"::unmarshal(p<IMarshaller> marshaller, int mode, p<IInputStream> istream) throw (IOException)");
309 out.println("{");
310 out.println(" "+baseClass+"::unmarshal(marshaller, mode, istream) ; ");
311         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
312             JProperty property = (JProperty) iter.next();
313             String JavaDoc cast = toUnmarshalCast(property.getType());
314             String JavaDoc unmarshalMethod = toUnmarshalMethodName(property.getType());
315             String JavaDoc propertyName = decapitalize(property.getSimpleName());
316 out.println(" "+propertyName+" = "+cast+"(marshaller->"+unmarshalMethod+"(mode, istream)) ; ");
317         }
318 out.println("}");
319     }
320
321     public String JavaDoc getTargetDir() {
322         return targetDir;
323     }
324
325     public void setTargetDir(String JavaDoc targetDir) {
326         this.targetDir = targetDir;
327     }
328     
329 }
330
Popular Tags