KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedHashMap JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.codehaus.jam.JAnnotation;
31 import org.codehaus.jam.JAnnotationValue;
32 import org.codehaus.jam.JClass;
33 import org.codehaus.jam.JProperty;
34
35 /**
36  *
37  * @version $Revision: 383749 $
38  */

39 public class CHeadersGenerator extends SingleSourceGenerator {
40
41     protected String JavaDoc targetDir = "./src/lib/openwire";
42
43     public Object JavaDoc run() {
44         filePostFix = ".h";
45         if (destFile == null) {
46             destFile = new File JavaDoc(targetDir + "/ow_commands_v" + getOpenwireVersion() + ".h");
47         }
48         return super.run();
49     }
50
51     public String JavaDoc getTargetDir() {
52         return targetDir;
53     }
54
55     public void setTargetDir(String JavaDoc targetDir) {
56         this.targetDir = targetDir;
57     }
58
59     protected void generateLicence(PrintWriter JavaDoc out) {
60 out.println("/**");
61 out.println(" *");
62 out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
63 out.println(" * contributor license agreements. See the NOTICE file distributed with");
64 out.println(" * this work for additional information regarding copyright ownership.");
65 out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
66 out.println(" * (the \"License\"); you may not use this file except in compliance with");
67 out.println(" * the License. You may obtain a copy of the License at");
68 out.println(" *");
69 out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
70 out.println(" *");
71 out.println(" * Unless required by applicable law or agreed to in writing, software");
72 out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
73 out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
74 out.println(" * See the License for the specific language governing permissions and");
75 out.println(" * limitations under the License.");
76 out.println(" */");
77     }
78
79     String JavaDoc changeCase(String JavaDoc value) {
80         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
81         char[] cs = value.toCharArray();
82         for (int i = 0; i < cs.length; i++) {
83             char c = cs[i];
84             if (Character.isUpperCase((char) c)) {
85                 b.append('_');
86                 b.append(Character.toLowerCase((char) c));
87             } else {
88                 b.append(c);
89             }
90         }
91         return b.toString();
92     }
93
94     String JavaDoc toPropertyCase(String JavaDoc value) {
95         return value.substring(0, 1).toLowerCase() + value.substring(1);
96     }
97
98     /**
99      * Sort the class list so that base classes come up first.
100      */

101     protected List JavaDoc sort(List JavaDoc source) {
102         LinkedHashMap JavaDoc rc = new LinkedHashMap JavaDoc();
103         ArrayList JavaDoc classes = new ArrayList JavaDoc(source);
104         Collections.sort(classes, new Comparator JavaDoc(){
105             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
106                 JClass c1 = (JClass) o1;
107                 JClass c2 = (JClass) o2;
108                 return c1.getSimpleName().compareTo(c2.getSimpleName());
109             }});
110         
111         // lets make a map of all the class names
112
HashMap JavaDoc classNames = new HashMap JavaDoc();
113         for (Iterator JavaDoc iter = classes.iterator(); iter.hasNext();) {
114             JClass c = (JClass) iter.next();
115             classNames.put(c, c);
116         }
117
118         // Add all classes that have no parent first
119
for (Iterator JavaDoc iter = classes.iterator(); iter.hasNext();) {
120             JClass c = (JClass) iter.next();
121             if (!classNames.containsKey(c.getSuperclass()))
122                 rc.put(c, c);
123         }
124
125         // now lets add the rest
126
for (Iterator JavaDoc iter = classes.iterator(); iter.hasNext();) {
127             JClass c = (JClass) iter.next();
128             if (!rc.containsKey(c))
129                 rc.put(c,c);
130         }
131
132         return new ArrayList JavaDoc(rc.keySet());
133     }
134
135     void generateFields(PrintWriter JavaDoc out, JClass jclass) {
136
137         if (jclass.getSuperclass() == null || jclass.getSuperclass().getSimpleName().equals("Object")) {
138             out.println("");
139             out.println(" ow_byte structType;");
140         } else {
141             generateFields(out, jclass.getSuperclass());
142         }
143
144         ArrayList JavaDoc properties = new ArrayList JavaDoc();
145         jclass.getDeclaredProperties();
146         for (int i = 0; i < jclass.getDeclaredProperties().length; i++) {
147             JProperty p = jclass.getDeclaredProperties()[i];
148             if (isValidProperty(p)) {
149                 properties.add(p);
150             }
151         }
152         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
153             JProperty property = (JProperty) iter.next();
154             JAnnotation annotation = property.getGetter().getAnnotation("openwire:property");
155             JAnnotationValue size = annotation.getValue("size");
156             String JavaDoc name = toPropertyCase(property.getSimpleName());
157             boolean cached = isCachedProperty(property);
158
159             String JavaDoc type = property.getType().getQualifiedName();
160             if (type.equals("boolean")) {
161                 out.println(" ow_"+type+" "+name+";");
162             } else if (type.equals("byte")) {
163                 out.println(" ow_"+type+" "+name+";");
164             } else if (type.equals("char")) {
165                 out.println(" ow_"+type+" "+name+";");
166             } else if (type.equals("short")) {
167                 out.println(" ow_"+type+" "+name+";");
168             } else if (type.equals("int")) {
169                 out.println(" ow_"+type+" "+name+";");
170             } else if (type.equals("long")) {
171                 out.println(" ow_"+type+" "+name+";");
172             } else if (type.equals("byte[]")) {
173                 out.println(" ow_byte_array *"+name+";");
174             } else if (type.equals("org.apache.activeio.packet.ByteSequence")) {
175                 out.println(" ow_byte_array *"+name+";");
176             } else if (type.equals("org.apache.activeio.packet.ByteSequence")) {
177                 out.println(" ow_byte_array *"+name+";");
178             } else if (type.equals("java.lang.String")) {
179                 out.println(" ow_string *"+name+";");
180             } else {
181                 if (property.getType().isArrayType()) {
182                     out.println(" ow_DataStructure_array *"+name+";");
183                 } else if (isThrowable(property.getType())) {
184                     out.println(" ow_throwable *"+name+";");
185                 } else {
186                     out.println(" struct ow_" + property.getType().getSimpleName() + " *"+name+";");
187                 }
188             }
189         }
190     }
191     
192     
193     protected void generateSetup(PrintWriter JavaDoc out) {
194         generateLicence(out);
195 out.println("");
196 out.println("/*****************************************************************************************");
197 out.println(" * ");
198 out.println(" * NOTE!: This file is auto generated - do not modify!");
199 out.println(" * if you need to make a change, please see the modify the groovy scripts in the");
200 out.println(" * under src/gram/script and then use maven openwire:generate to regenerate ");
201 out.println(" * this file.");
202 out.println(" * ");
203 out.println(" *****************************************************************************************/");
204 out.println(" ");
205 out.println("#ifndef OW_COMMANDS_V"+openwireVersion+"_H");
206 out.println("#define OW_COMMANDS_V"+openwireVersion+"_H");
207 out.println("");
208 out.println("#include \"ow.h\"");
209 out.println("");
210 out.println("#ifdef __cplusplus");
211 out.println("extern \"C\" {");
212 out.println("#endif /* __cplusplus */");
213 out.println(" ");
214 out.println("#define OW_WIREFORMAT_VERSION "+openwireVersion+"");
215
216 out.println("#define OW_WIREFORMAT_STACK_TRACE_MASK 0x00000001;");
217 out.println("#define OW_WIREFORMAT_TCP_NO_DELAY_MASK 0x00000002;");
218 out.println("#define OW_WIREFORMAT_CACHE_MASK 0x00000004;");
219 out.println("#define OW_WIREFORMAT_COMPRESSION_MASK 0x00000008;");
220
221         for (Iterator JavaDoc iterator = sortedClasses.iterator(); iterator.hasNext();) {
222             JClass jclass = (JClass) iterator.next();
223             String JavaDoc name = jclass.getSimpleName();
224             String JavaDoc type = ("ow_"+name).toUpperCase()+"_TYPE";
225             if( !isAbstract(jclass) ) {
226                 out.println("#define "+type+" "+getOpenWireOpCode(jclass));
227             }
228          }
229
230 out.println(" ");
231 out.println("apr_status_t ow_bitmarshall(ow_bit_buffer *buffer, ow_DataStructure *object);");
232 out.println("apr_status_t ow_marshall(ow_byte_buffer *buffer, ow_DataStructure *object);");
233     }
234     
235     protected void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc {
236
237         String JavaDoc structName = jclass.getSimpleName();
238         String JavaDoc type = "OW_" + structName.toUpperCase() + "_TYPE";
239
240 out.println("");
241 out.println("typedef struct ow_"+structName+" {");
242
243         // This recusivly generates the field definitions of the class and it's supper classes.
244
generateFields(out, jclass);
245
246 out.println("");
247 out.println("} ow_"+structName+";");
248 out.println("ow_"+structName+" *ow_"+structName+"_create(apr_pool_t *pool);");
249 out.println("ow_boolean ow_is_a_"+structName+"(ow_DataStructure *object);");
250
251     }
252     
253     protected void generateTearDown(PrintWriter JavaDoc out) {
254 out.println("");
255 out.println("#ifdef __cplusplus");
256 out.println("}");
257 out.println("#endif");
258 out.println("");
259 out.println("#endif /* ! OW_COMMANDS_V"+openwireVersion+"_H */");
260     }
261 }
262
Popular Tags