1 30 31 package oracle.toplink.libraries.asm.xml; 32 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import oracle.toplink.libraries.asm.Attribute; 37 import oracle.toplink.libraries.asm.CodeVisitor; 38 import oracle.toplink.libraries.asm.Constants; 39 import oracle.toplink.libraries.asm.Label; 40 import oracle.toplink.libraries.asm.Type; 41 import oracle.toplink.libraries.asm.util.PrintCodeVisitor; 42 import org.xml.sax.Attributes ; 43 import org.xml.sax.ContentHandler ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.helpers.AttributesImpl ; 46 47 56 public final class SAXCodeAdapter implements CodeVisitor { 57 private ContentHandler h; 58 private Map labelNames; 59 60 65 public SAXCodeAdapter( ContentHandler h) { 66 this.h = h; 67 labelNames = new HashMap (); 68 } 69 70 public final void visitInsn( int opcode) { 71 addElement( PrintCodeVisitor.OPCODES[ opcode], new AttributesImpl ()); 72 } 73 74 public final void visitIntInsn( int opcode, int operand) { 75 AttributesImpl attrs = new AttributesImpl (); 76 attrs.addAttribute( "", "value", "value", "", Integer.toString( operand)); 77 addElement( PrintCodeVisitor.OPCODES[ opcode], attrs); 78 } 79 80 public final void visitVarInsn( int opcode, int var) { 81 AttributesImpl attrs = new AttributesImpl (); 82 attrs.addAttribute( "", "var", "var", "", Integer.toString( var)); 83 addElement( PrintCodeVisitor.OPCODES[ opcode], attrs); 84 } 85 86 public final void visitTypeInsn( int opcode, String desc) { 87 AttributesImpl attrs = new AttributesImpl (); 88 attrs.addAttribute( "", "desc", "desc", "", desc); 89 addElement( PrintCodeVisitor.OPCODES[ opcode], attrs); 90 } 91 92 public final void visitFieldInsn( int opcode, String owner, String name, String desc) { 93 AttributesImpl attrs = new AttributesImpl (); 94 attrs.addAttribute( "", "owner", "owner", "", owner); 95 attrs.addAttribute( "", "name", "name", "", name); 96 attrs.addAttribute( "", "desc", "desc", "", desc); 97 addElement( PrintCodeVisitor.OPCODES[ opcode], attrs); 98 } 99 100 public final void visitMethodInsn( int opcode, String owner, String name, String desc) { 101 AttributesImpl attrs = new AttributesImpl (); 102 attrs.addAttribute( "", "owner", "owner", "", owner); 103 attrs.addAttribute( "", "name", "name", "", name); 104 attrs.addAttribute( "", "desc", "desc", "", desc); 105 addElement( PrintCodeVisitor.OPCODES[ opcode], attrs); 106 } 107 108 public final void visitJumpInsn( int opcode, Label label) { 109 AttributesImpl attrs = new AttributesImpl (); 110 attrs.addAttribute( "", "label", "label", "", getLabel( label)); 111 addElement( PrintCodeVisitor.OPCODES[ opcode], attrs); 112 } 113 114 public final void visitLabel( Label label) { 115 AttributesImpl attrs = new AttributesImpl (); 116 attrs.addAttribute( "", "name", "name", "", getLabel( label)); 117 addElement( "Label", attrs); 118 } 119 120 public final void visitLdcInsn( Object cst) { 121 AttributesImpl attrs = new AttributesImpl (); 122 attrs.addAttribute( "", "cst", "cst", "", SAXClassAdapter.encode( cst.toString())); 123 attrs.addAttribute( "", "desc", "desc", "", Type.getDescriptor( cst.getClass())); 124 addElement( PrintCodeVisitor.OPCODES[ Constants.LDC], attrs); 125 } 126 127 public final void visitIincInsn( int var, int increment) { 128 AttributesImpl attrs = new AttributesImpl (); 129 attrs.addAttribute( "", "var", "var", "", Integer.toString( var)); 130 attrs.addAttribute( "", "inc", "inc", "", Integer.toString( increment)); 131 addElement( PrintCodeVisitor.OPCODES[ Constants.IINC], attrs); 132 } 133 134 public final void visitTableSwitchInsn( int min, int max, Label dflt, Label[] labels) { 135 AttributesImpl attrs = new AttributesImpl (); 136 attrs.addAttribute( "", "min", "min", "", Integer.toString( min)); 137 attrs.addAttribute( "", "max", "max", "", Integer.toString( max)); 138 attrs.addAttribute( "", "dflt", "dflt", "", getLabel( dflt)); 139 String o = PrintCodeVisitor.OPCODES[ Constants.TABLESWITCH]; 140 addStart( o, attrs); 141 for( int i = 0; i < labels.length; i++) { 142 AttributesImpl att2 = new AttributesImpl (); 143 att2.addAttribute( "", "name", "name", "", getLabel( labels[ i])); 144 addElement( "label", att2); 145 } 146 addEnd( o); 147 } 148 149 public final void visitLookupSwitchInsn( Label dflt, int[] keys, Label[] labels) { 150 AttributesImpl att = new AttributesImpl (); 151 att.addAttribute( "", "dflt", "dflt", "", getLabel( dflt)); 152 String o = PrintCodeVisitor.OPCODES[ Constants.LOOKUPSWITCH]; 153 addStart( o, att); 154 for( int i = 0; i < labels.length; i++) { 155 AttributesImpl att2 = new AttributesImpl (); 156 att2.addAttribute( "", "name", "name", "", getLabel( labels[ i])); 157 att2.addAttribute( "", "key", "key", "", Integer.toString( keys[ i])); 158 addElement( "label", att2); 159 } 160 addEnd( o); 161 } 162 163 public final void visitMultiANewArrayInsn( String desc, int dims) { 164 AttributesImpl attrs = new AttributesImpl (); 165 attrs.addAttribute( "", "desc", "desc", "", desc); 166 attrs.addAttribute( "", "dims", "dims", "", Integer.toString( dims)); 167 addElement( PrintCodeVisitor.OPCODES[ Constants.MULTIANEWARRAY], attrs); 168 } 169 170 public final void visitTryCatchBlock( Label start, Label end, Label handler, String type) { 171 AttributesImpl attrs = new AttributesImpl (); 172 attrs.addAttribute( "", "start", "start", "", getLabel( start)); 173 attrs.addAttribute( "", "end", "end", "", getLabel( end)); 174 attrs.addAttribute( "", "handler", "handler", "", getLabel( handler)); 175 if( type!=null) attrs.addAttribute( "", "type", "type", "", type); 176 addElement( "TryCatch", attrs); 177 } 178 179 public final void visitMaxs( int maxStack, int maxLocals) { 180 AttributesImpl attrs = new AttributesImpl (); 181 attrs.addAttribute( "", "maxStack", "maxStack", "", Integer.toString( maxStack)); 182 attrs.addAttribute( "", "maxLocals", "maxLocals", "", Integer.toString( maxLocals)); 183 addElement( "Max", attrs); 184 185 addEnd( "code"); 186 addEnd( "method"); 187 } 189 190 public final void visitLocalVariable( String name, String desc, Label start, Label end, int index) { 191 AttributesImpl attrs = new AttributesImpl (); 192 attrs.addAttribute( "", "name", "name", "", name); 193 attrs.addAttribute( "", "desc", "desc", "", desc); 194 attrs.addAttribute( "", "start", "start", "", getLabel( start)); 195 attrs.addAttribute( "", "end", "end", "", getLabel( end)); 196 attrs.addAttribute( "", "var", "var", "", Integer.toString( index)); 197 addElement( "LocalVar", attrs); 198 } 199 200 public final void visitLineNumber( int line, Label start) { 201 AttributesImpl attrs = new AttributesImpl (); 202 attrs.addAttribute( "", "line", "line", "", Integer.toString( line)); 203 attrs.addAttribute( "", "start", "start", "", getLabel( start)); 204 addElement( "LineNumber", attrs); 205 } 206 207 public final void visitAttribute( Attribute attr) { 208 } 210 211 private final String getLabel( Label label) { 212 String name = (String ) labelNames.get( label); 213 if( name==null) { 214 name = Integer.toString( labelNames.size()); 215 labelNames.put( label, name); 216 } 217 return name; 218 } 219 220 private final void addElement( String name, Attributes attrs) { 221 addStart( name, attrs); 222 addEnd( name); 223 } 224 225 private final void addStart( String name, Attributes attrs) { 226 try { 227 h.startElement( "", name, name, attrs); 228 } catch( SAXException ex) { 229 throw new RuntimeException ( ex.toString()); 230 } 231 } 232 233 private final void addEnd( String name) { 234 try { 235 h.endElement( "", name, name); 236 } catch( SAXException ex) { 237 throw new RuntimeException ( ex.toString()); 238 } 239 } 240 241 } 242 243 | Popular Tags |