1 18 package org.apache.activemq.openwire.tool; 19 20 import java.io.File ; 21 import java.io.PrintWriter ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.LinkedHashMap ; 28 import java.util.List ; 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 39 public class CHeadersGenerator extends SingleSourceGenerator { 40 41 protected String targetDir = "./src/lib/openwire"; 42 43 public Object run() { 44 filePostFix = ".h"; 45 if (destFile == null) { 46 destFile = new File (targetDir + "/ow_commands_v" + getOpenwireVersion() + ".h"); 47 } 48 return super.run(); 49 } 50 51 public String getTargetDir() { 52 return targetDir; 53 } 54 55 public void setTargetDir(String targetDir) { 56 this.targetDir = targetDir; 57 } 58 59 protected void generateLicence(PrintWriter 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 changeCase(String value) { 80 StringBuffer b = new StringBuffer (); 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 toPropertyCase(String value) { 95 return value.substring(0, 1).toLowerCase() + value.substring(1); 96 } 97 98 101 protected List sort(List source) { 102 LinkedHashMap rc = new LinkedHashMap (); 103 ArrayList classes = new ArrayList (source); 104 Collections.sort(classes, new Comparator (){ 105 public int compare(Object o1, Object o2) { 106 JClass c1 = (JClass) o1; 107 JClass c2 = (JClass) o2; 108 return c1.getSimpleName().compareTo(c2.getSimpleName()); 109 }}); 110 111 HashMap classNames = new HashMap (); 113 for (Iterator iter = classes.iterator(); iter.hasNext();) { 114 JClass c = (JClass) iter.next(); 115 classNames.put(c, c); 116 } 117 118 for (Iterator 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 for (Iterator 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 (rc.keySet()); 133 } 134 135 void generateFields(PrintWriter 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 properties = new ArrayList (); 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 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 name = toPropertyCase(property.getSimpleName()); 157 boolean cached = isCachedProperty(property); 158 159 String 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 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 iterator = sortedClasses.iterator(); iterator.hasNext();) { 222 JClass jclass = (JClass) iterator.next(); 223 String name = jclass.getSimpleName(); 224 String 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 out) throws Exception { 236 237 String structName = jclass.getSimpleName(); 238 String type = "OW_" + structName.toUpperCase() + "_TYPE"; 239 240 out.println(""); 241 out.println("typedef struct ow_"+structName+" {"); 242 243 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 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 |