1 30 package org.objectweb.asm.xml; 31 32 import org.objectweb.asm.AnnotationVisitor; 33 import org.objectweb.asm.Type; 34 import org.xml.sax.ContentHandler ; 35 import org.xml.sax.helpers.AttributesImpl ; 36 37 42 public class SAXAnnotationAdapter extends SAXAdapter implements 43 AnnotationVisitor 44 { 45 private final String elementName; 46 47 public SAXAnnotationAdapter( 48 ContentHandler h, 49 String elementName, 50 int visible, 51 String name, 52 String desc) 53 { 54 this(h, elementName, visible, desc, name, -1); 55 } 56 57 public SAXAnnotationAdapter( 58 ContentHandler h, 59 String elementName, 60 int visible, 61 int parameter, 62 String desc) 63 { 64 this(h, elementName, visible, desc, null, parameter); 65 } 66 67 private SAXAnnotationAdapter( 68 ContentHandler h, 69 String elementName, 70 int visible, 71 String desc, 72 String name, 73 int parameter) 74 { 75 super(h); 76 this.elementName = elementName; 77 78 AttributesImpl att = new AttributesImpl (); 79 if (name != null) 80 att.addAttribute("", "name", "name", "", name); 81 if (visible != 0) 82 att.addAttribute("", "visible", "visible", "", visible > 0 83 ? "true" 84 : "false"); 85 if (parameter != -1) 86 att.addAttribute("", 87 "parameter", 88 "parameter", 89 "", 90 Integer.toString(parameter)); 91 if (desc != null) 92 att.addAttribute("", "desc", "desc", "", desc); 93 94 addStart(elementName, att); 95 } 96 97 public void visit(String name, Object value) { 98 Class c = value.getClass(); 99 if (c.isArray()) { 100 AnnotationVisitor av = visitArray(name); 101 if (value instanceof byte[]) { 102 byte[] b = (byte[]) value; 103 for (int i = 0; i < b.length; i++) 104 av.visit(null, new Byte (b[i])); 105 106 } else if (value instanceof char[]) { 107 char[] b = (char[]) value; 108 for (int i = 0; i < b.length; i++) 109 av.visit(null, new Character (b[i])); 110 111 } else if (value instanceof boolean[]) { 112 boolean[] b = (boolean[]) value; 113 for (int i = 0; i < b.length; i++) 114 av.visit(null, Boolean.valueOf(b[i])); 115 116 } else if (value instanceof int[]) { 117 int[] b = (int[]) value; 118 for (int i = 0; i < b.length; i++) 119 av.visit(null, new Integer (b[i])); 120 121 } else if (value instanceof long[]) { 122 long[] b = (long[]) value; 123 for (int i = 0; i < b.length; i++) 124 av.visit(null, new Long (b[i])); 125 126 } else if (value instanceof float[]) { 127 float[] b = (float[]) value; 128 for (int i = 0; i < b.length; i++) 129 av.visit(null, new Float (b[i])); 130 131 } else if (value instanceof double[]) { 132 double[] b = (double[]) value; 133 for (int i = 0; i < b.length; i++) 134 av.visit(null, new Double (b[i])); 135 136 } 137 av.visitEnd(); 138 } else { 139 addValueElement("annotationValue", 140 name, 141 Type.getDescriptor(c), 142 value.toString()); 143 } 144 } 145 146 public void visitEnum(String name, String desc, String value) { 147 addValueElement("annotationValueEnum", name, desc, value); 148 } 149 150 public AnnotationVisitor visitAnnotation(String name, String desc) { 151 return new SAXAnnotationAdapter(getContentHandler(), 152 "annotationValueAnnotation", 153 0, 154 name, 155 desc); 156 } 157 158 public AnnotationVisitor visitArray(String name) { 159 return new SAXAnnotationAdapter(getContentHandler(), 160 "annotationValueArray", 161 0, 162 name, 163 null); 164 } 165 166 public void visitEnd() { 167 addEnd(elementName); 168 } 169 170 private void addValueElement( 171 String element, 172 String name, 173 String desc, 174 String value) 175 { 176 AttributesImpl att = new AttributesImpl (); 177 if (name != null) 178 att.addAttribute("", "name", "name", "", name); 179 if (desc != null) 180 att.addAttribute("", "desc", "desc", "", desc); 181 if (value != null) 182 att.addAttribute("", 183 "value", 184 "value", 185 "", 186 SAXClassAdapter.encode(value)); 187 188 addElement(element, att); 189 } 190 191 } 192 | Popular Tags |