1 30 package org.objectweb.asm.xml; 31 32 import org.objectweb.asm.AnnotationVisitor; 33 import org.objectweb.asm.Attribute; 34 import org.objectweb.asm.ClassVisitor; 35 import org.objectweb.asm.FieldVisitor; 36 import org.objectweb.asm.MethodVisitor; 37 import org.objectweb.asm.Opcodes; 38 import org.xml.sax.ContentHandler ; 39 import org.xml.sax.helpers.AttributesImpl ; 40 41 52 public final class SAXClassAdapter extends SAXAdapter implements ClassVisitor { 53 private boolean singleDocument; 54 55 63 public SAXClassAdapter(ContentHandler h, boolean singleDocument) { 64 super(h); 65 this.singleDocument = singleDocument; 66 if (!singleDocument) { 67 addDocumentStart(); 68 } 69 } 70 71 public void visitSource(String source, String debug) { 72 if (source == null && debug == null) { 73 return; 74 } 75 76 AttributesImpl att = new AttributesImpl (); 77 if (source != null) 78 att.addAttribute("", "file", "file", "", encode(source)); 79 if (debug != null) 80 att.addAttribute("", "debug", "debug", "", encode(debug)); 81 82 addElement("source", att); 83 } 84 85 public void visitOuterClass(String owner, String name, String desc) { 86 AttributesImpl att = new AttributesImpl (); 87 att.addAttribute("", "owner", "owner", "", owner); 88 if (name != null) 89 att.addAttribute("", "name", "name", "", name); 90 if (desc != null) 91 att.addAttribute("", "desc", "desc", "", desc); 92 93 addElement("outerclass", att); 94 } 95 96 public final void visitAttribute(Attribute attr) { 97 } 99 100 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 101 return new SAXAnnotationAdapter(getContentHandler(), 102 "annotation", 103 visible ? 1 : -1, 104 null, 105 desc); 106 } 107 108 public void visit( 109 int version, 110 int access, 111 String name, 112 String signature, 113 String superName, 114 String [] interfaces) 115 { 116 StringBuffer sb = new StringBuffer (); 117 if ((access & Opcodes.ACC_PUBLIC) != 0) 118 sb.append("public "); 119 if ((access & Opcodes.ACC_PRIVATE) != 0) 120 sb.append("private "); 121 if ((access & Opcodes.ACC_PROTECTED) != 0) 122 sb.append("protected "); 123 if ((access & Opcodes.ACC_FINAL) != 0) 124 sb.append("final "); 125 if ((access & Opcodes.ACC_SUPER) != 0) 126 sb.append("super "); 127 if ((access & Opcodes.ACC_INTERFACE) != 0) 128 sb.append("interface "); 129 if ((access & Opcodes.ACC_ABSTRACT) != 0) 130 sb.append("abstract "); 131 if ((access & Opcodes.ACC_SYNTHETIC) != 0) 132 sb.append("synthetic "); 133 if ((access & Opcodes.ACC_ANNOTATION) != 0) 134 sb.append("annotation "); 135 if ((access & Opcodes.ACC_ENUM) != 0) 136 sb.append("enum "); 137 if ((access & Opcodes.ACC_DEPRECATED) != 0) 138 sb.append("deprecated "); 139 140 AttributesImpl att = new AttributesImpl (); 141 att.addAttribute("", "access", "access", "", sb.toString()); 142 if (name != null) 143 att.addAttribute("", "name", "name", "", name); 144 if (signature != null) 145 att.addAttribute("", 146 "signature", 147 "signature", 148 "", 149 encode(signature)); 150 if (superName != null) 151 att.addAttribute("", "parent", "parent", "", superName); 152 att.addAttribute("", 153 "major", 154 "major", 155 "", 156 Integer.toString(version & 0xFFFF)); 157 att.addAttribute("", 158 "minor", 159 "minor", 160 "", 161 Integer.toString(version >>> 16)); 162 addStart("class", att); 163 164 addStart("interfaces", new AttributesImpl ()); 165 if (interfaces != null && interfaces.length > 0) { 166 for (int i = 0; i < interfaces.length; i++) { 167 AttributesImpl att2 = new AttributesImpl (); 168 att2.addAttribute("", "name", "name", "", interfaces[i]); 169 addElement("interface", att2); 170 } 171 } 172 addEnd("interfaces"); 173 } 174 175 public FieldVisitor visitField( 176 int access, 177 String name, 178 String desc, 179 String signature, 180 Object value) 181 { 182 StringBuffer sb = new StringBuffer (); 183 if ((access & Opcodes.ACC_PUBLIC) != 0) 184 sb.append("public "); 185 if ((access & Opcodes.ACC_PRIVATE) != 0) 186 sb.append("private "); 187 if ((access & Opcodes.ACC_PROTECTED) != 0) 188 sb.append("protected "); 189 if ((access & Opcodes.ACC_STATIC) != 0) 190 sb.append("static "); 191 if ((access & Opcodes.ACC_FINAL) != 0) 192 sb.append("final "); 193 if ((access & Opcodes.ACC_VOLATILE) != 0) 194 sb.append("volatile "); 195 if ((access & Opcodes.ACC_TRANSIENT) != 0) 196 sb.append("transient "); 197 if ((access & Opcodes.ACC_SYNTHETIC) != 0) 198 sb.append("synthetic "); 199 if ((access & Opcodes.ACC_ENUM) != 0) 200 sb.append("enum "); 201 if ((access & Opcodes.ACC_DEPRECATED) != 0) 202 sb.append("deprecated "); 203 204 AttributesImpl att = new AttributesImpl (); 205 att.addAttribute("", "access", "access", "", sb.toString()); 206 att.addAttribute("", "name", "name", "", name); 207 att.addAttribute("", "desc", "desc", "", desc); 208 if (signature != null) 209 att.addAttribute("", 210 "signature", 211 "signature", 212 "", 213 encode(signature)); 214 if (value != null) { 215 att.addAttribute("", "value", "value", "", encode(value.toString())); 216 } 217 218 return new SAXFieldAdapter(getContentHandler(), att); 219 } 220 221 public MethodVisitor visitMethod( 222 int access, 223 String name, 224 String desc, 225 String signature, 226 String [] exceptions) 227 { 228 StringBuffer sb = new StringBuffer (); 229 if ((access & Opcodes.ACC_PUBLIC) != 0) 230 sb.append("public "); 231 if ((access & Opcodes.ACC_PRIVATE) != 0) 232 sb.append("private "); 233 if ((access & Opcodes.ACC_PROTECTED) != 0) 234 sb.append("protected "); 235 if ((access & Opcodes.ACC_STATIC) != 0) 236 sb.append("static "); 237 if ((access & Opcodes.ACC_FINAL) != 0) 238 sb.append("final "); 239 if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) 240 sb.append("synchronized "); 241 if ((access & Opcodes.ACC_BRIDGE) != 0) 242 sb.append("bridge "); 243 if ((access & Opcodes.ACC_VARARGS) != 0) 244 sb.append("varargs "); 245 if ((access & Opcodes.ACC_NATIVE) != 0) 246 sb.append("native "); 247 if ((access & Opcodes.ACC_ABSTRACT) != 0) 248 sb.append("abstract "); 249 if ((access & Opcodes.ACC_STRICT) != 0) 250 sb.append("strict "); 251 if ((access & Opcodes.ACC_SYNTHETIC) != 0) 252 sb.append("synthetic "); 253 if ((access & Opcodes.ACC_DEPRECATED) != 0) 254 sb.append("deprecated "); 255 256 AttributesImpl att = new AttributesImpl (); 257 att.addAttribute("", "access", "access", "", sb.toString()); 258 att.addAttribute("", "name", "name", "", name); 259 att.addAttribute("", "desc", "desc", "", desc); 260 if (signature != null) { 261 att.addAttribute("", "signature", "signature", "", signature); 262 } 263 addStart("method", att); 264 265 addStart("exceptions", new AttributesImpl ()); 266 if (exceptions != null && exceptions.length > 0) { 267 for (int i = 0; i < exceptions.length; i++) { 268 AttributesImpl att2 = new AttributesImpl (); 269 att2.addAttribute("", "name", "name", "", exceptions[i]); 270 addElement("exception", att2); 271 } 272 } 273 addEnd("exceptions"); 274 275 return new SAXCodeAdapter(getContentHandler(), access); 276 } 277 278 public final void visitInnerClass( 279 String name, 280 String outerName, 281 String innerName, 282 int access) 283 { 284 StringBuffer sb = new StringBuffer (); 285 if ((access & Opcodes.ACC_PUBLIC) != 0) 286 sb.append("public "); 287 if ((access & Opcodes.ACC_PRIVATE) != 0) 288 sb.append("private "); 289 if ((access & Opcodes.ACC_PROTECTED) != 0) 290 sb.append("protected "); 291 if ((access & Opcodes.ACC_STATIC) != 0) 292 sb.append("static "); 293 if ((access & Opcodes.ACC_FINAL) != 0) 294 sb.append("final "); 295 if ((access & Opcodes.ACC_SUPER) != 0) 296 sb.append("super "); 297 if ((access & Opcodes.ACC_INTERFACE) != 0) 298 sb.append("interface "); 299 if ((access & Opcodes.ACC_ABSTRACT) != 0) 300 sb.append("abstract "); 301 if ((access & Opcodes.ACC_SYNTHETIC) != 0) 302 sb.append("synthetic "); 303 if ((access & Opcodes.ACC_ANNOTATION) != 0) 304 sb.append("annotation "); 305 if ((access & Opcodes.ACC_ENUM) != 0) 306 sb.append("enum "); 307 if ((access & Opcodes.ACC_DEPRECATED) != 0) 308 sb.append("deprecated "); 309 310 AttributesImpl att = new AttributesImpl (); 311 att.addAttribute("", "access", "access", "", sb.toString()); 312 if (name != null) 313 att.addAttribute("", "name", "name", "", name); 314 if (outerName != null) 315 att.addAttribute("", "outerName", "outerName", "", outerName); 316 if (innerName != null) 317 att.addAttribute("", "innerName", "innerName", "", innerName); 318 addElement("innerclass", att); 319 } 320 321 public final void visitEnd() { 322 addEnd("class"); 323 if (!singleDocument) { 324 addDocumentEnd(); 325 } 326 } 327 328 static final String encode(String s) { 329 StringBuffer sb = new StringBuffer (); 330 for (int i = 0; i < s.length(); i++) { 331 char c = s.charAt(i); 332 if (c == '\\') { 333 sb.append("\\\\"); 334 } else if (c < 0x20 || c > 0x7f) { 335 sb.append("\\u"); 336 if (c < 0x10) { 337 sb.append("000"); 338 } else if (c < 0x100) { 339 sb.append("00"); 340 } else if (c < 0x1000) { 341 sb.append("0"); 342 } 343 sb.append(Integer.toString(c, 16)); 344 } else { 345 sb.append(c); 346 } 347 } 348 return sb.toString(); 349 } 350 351 } 352 | Popular Tags |