1 30 31 package oracle.toplink.libraries.asm.xml; 32 33 import oracle.toplink.libraries.asm.Attribute; 34 import oracle.toplink.libraries.asm.ClassVisitor; 35 import oracle.toplink.libraries.asm.CodeVisitor; 36 import oracle.toplink.libraries.asm.Constants; 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.helpers.AttributesImpl ; 40 41 42 53 public final class SAXClassAdapter implements ClassVisitor { 54 private ContentHandler h; 55 private boolean singleDocument; 56 57 65 public SAXClassAdapter( ContentHandler h, boolean singleDocument) { 66 this.h = h; 67 this.singleDocument = singleDocument; 68 if( !singleDocument) { 69 try { 70 h.startDocument(); 71 } catch( SAXException ex) { 72 throw new RuntimeException ( ex.getException()); 73 } 74 } 75 } 76 77 public final void visit( int version, int access, String name, String superName, String [] interfaces, String sourceFile) { 78 try { 79 StringBuffer sb = new StringBuffer (); 80 if(( access & Constants.ACC_PUBLIC)!=0) sb.append( "public "); 81 if(( access & Constants.ACC_PRIVATE)!=0) sb.append( "private "); 82 if(( access & Constants.ACC_PROTECTED)!=0) sb.append( "protected "); 83 if(( access & Constants.ACC_FINAL)!=0) sb.append( "final "); 84 if(( access & Constants.ACC_SUPER)!=0) sb.append( "super "); 85 if(( access & Constants.ACC_INTERFACE)!=0) sb.append( "interface "); 86 if(( access & Constants.ACC_ABSTRACT)!=0) sb.append( "abstract "); 87 if(( access & Constants.ACC_SYNTHETIC)!=0) sb.append( "synthetic "); 88 if(( access & Constants.ACC_ANNOTATION)!=0) sb.append( "annotation "); 89 if(( access & Constants.ACC_ENUM)!=0) sb.append( "enum "); 90 if(( access & Constants.ACC_DEPRECATED)!=0) sb.append( "deprecated "); 91 92 AttributesImpl attrs = new AttributesImpl (); 93 attrs.addAttribute( "", "access", "access", "", sb.toString()); 94 if( name!=null) attrs.addAttribute( "", "name", "name", "", name); 95 if( superName!=null) attrs.addAttribute( "", "parent", "parent", "", superName); 96 if( sourceFile!=null) attrs.addAttribute( "", "source", "source", "", sourceFile); 97 attrs.addAttribute( "", "major", "major", "", new Integer (version & 0xFFFF).toString()); 98 attrs.addAttribute( "", "minor", "minor", "", new Integer (version >>> 16).toString()); 99 h.startElement( "", "class", "class", attrs); 100 101 h.startElement( "", "interfaces", "interfaces", new AttributesImpl ()); 102 if( interfaces!=null && interfaces.length>0) { 103 for( int i = 0; i < interfaces.length; i++) { 104 AttributesImpl attrs2 = new AttributesImpl (); 105 attrs2.addAttribute( "", "name", "name", "", interfaces[ i]); 106 h.startElement( "", "interface", "interface", attrs2); 107 h.endElement( "", "interface", "interface"); 108 } 109 } 110 h.endElement( "", "interfaces", "interfaces"); 111 112 } catch( SAXException ex) { 113 throw new RuntimeException ( ex.getException()); 114 } 115 } 116 117 public final void visitField( int access, String name, String desc, Object value, Attribute attrs) { 118 StringBuffer sb = new StringBuffer (); 119 if(( access & Constants.ACC_PUBLIC)!=0) sb.append( "public "); 120 if(( access & Constants.ACC_PRIVATE)!=0) sb.append( "private "); 121 if(( access & Constants.ACC_PROTECTED)!=0) sb.append( "protected "); 122 if(( access & Constants.ACC_STATIC)!=0) sb.append( "static "); 123 if(( access & Constants.ACC_FINAL)!=0) sb.append( "final "); 124 if(( access & Constants.ACC_VOLATILE)!=0) sb.append( "volatile "); 125 if(( access & Constants.ACC_TRANSIENT)!=0) sb.append( "transient "); 126 if(( access & Constants.ACC_SYNTHETIC)!=0) sb.append( "synthetic "); 127 if(( access & Constants.ACC_ENUM)!=0) sb.append( "enum "); 128 if(( access & Constants.ACC_DEPRECATED)!=0) sb.append( "deprecated "); 129 130 AttributesImpl att = new AttributesImpl (); 131 att.addAttribute( "", "access", "access", "", sb.toString()); 132 att.addAttribute( "", "name", "name", "", name); 133 att.addAttribute( "", "desc", "desc", "", desc); 134 if( value!=null) { 135 att.addAttribute( "", "value", "value", "", encode( value.toString())); 136 } 137 try { 138 h.startElement( "", "field", "field", att); 139 h.endElement( "", "field", "field"); 140 } catch( SAXException ex) { 141 throw new RuntimeException ( ex.toString()); 142 } 143 } 144 145 public final CodeVisitor visitMethod( int access, String name, String desc, String [] exceptions, Attribute attrs) { 146 StringBuffer sb = new StringBuffer (); 147 if(( access & Constants.ACC_PUBLIC)!=0) sb.append( "public "); 148 if(( access & Constants.ACC_PRIVATE)!=0) sb.append( "private "); 149 if(( access & Constants.ACC_PROTECTED)!=0) sb.append( "protected "); 150 if(( access & Constants.ACC_STATIC)!=0) sb.append( "static "); 151 if(( access & Constants.ACC_FINAL)!=0) sb.append( "final "); 152 if(( access & Constants.ACC_SYNCHRONIZED)!=0) sb.append( "synchronized "); 153 if(( access & Constants.ACC_BRIDGE)!=0) sb.append( "bridge "); 154 if(( access & Constants.ACC_VARARGS)!=0) sb.append( "varargs "); 155 if(( access & Constants.ACC_NATIVE)!=0) sb.append( "native "); 156 if(( access & Constants.ACC_ABSTRACT)!=0) sb.append( "abstract "); 157 if(( access & Constants.ACC_STRICT)!=0) sb.append( "strict "); 158 if(( access & Constants.ACC_SYNTHETIC)!=0) sb.append( "synthetic "); 159 if(( access & Constants.ACC_DEPRECATED)!=0) sb.append( "deprecated "); 160 161 try { 162 AttributesImpl att = new AttributesImpl (); 163 att.addAttribute( "", "access", "access", "", sb.toString()); 164 att.addAttribute( "", "name", "name", "", name); 165 att.addAttribute( "", "desc", "desc", "", desc); 166 h.startElement( "", "method", "method", att); 167 168 h.startElement( "", "exceptions", "exceptions", new AttributesImpl ()); 169 if( exceptions!=null && exceptions.length>0) { 170 for( int i = 0; i < exceptions.length; i++) { 171 AttributesImpl att2 = new AttributesImpl (); 172 att2.addAttribute( "", "name", "name", "", exceptions[ i]); 173 h.startElement( "", "exception", "exception", att2); 174 h.endElement( "", "exception", "exception"); 175 } 176 } 177 h.endElement( "", "exceptions", "exceptions"); 178 if(( access & ( Constants.ACC_ABSTRACT | Constants.ACC_INTERFACE | Constants.ACC_NATIVE))>0) { 179 h.endElement( "", "method", "method"); 180 } else { 181 h.startElement( "", "code", "code", new AttributesImpl ()); 182 } 183 184 } catch( SAXException ex) { 185 throw new RuntimeException ( ex.toString()); 186 } 187 188 return new SAXCodeAdapter( h); 189 } 190 191 public final void visitInnerClass( String name, String outerName, String innerName, int access) { 192 StringBuffer sb = new StringBuffer (); 193 if(( access & Constants.ACC_PUBLIC)!=0) sb.append( "public "); 194 if(( access & Constants.ACC_PRIVATE)!=0) sb.append( "private "); 195 if(( access & Constants.ACC_PROTECTED)!=0) sb.append( "protected "); 196 if(( access & Constants.ACC_STATIC)!=0) sb.append( "static "); 197 if(( access & Constants.ACC_FINAL)!=0) sb.append( "final "); 198 if(( access & Constants.ACC_SUPER)!=0) sb.append( "super "); 199 if(( access & Constants.ACC_INTERFACE)!=0) sb.append( "interface "); 200 if(( access & Constants.ACC_ABSTRACT)!=0) sb.append( "abstract "); 201 if(( access & Constants.ACC_SYNTHETIC)!=0) sb.append( "synthetic "); 202 if(( access & Constants.ACC_ANNOTATION)!=0) sb.append( "annotation "); 203 if(( access & Constants.ACC_ENUM)!=0) sb.append( "enum "); 204 if(( access & Constants.ACC_DEPRECATED)!=0) sb.append( "deprecated "); 205 206 try { 207 AttributesImpl attrs = new AttributesImpl (); 208 attrs.addAttribute( "", "access", "access", "", sb.toString()); 209 if( name!=null) attrs.addAttribute( "", "name", "name", "", name); 210 if( outerName!=null) attrs.addAttribute( "", "outerName", "outerName", "", outerName); 211 if( innerName!=null) attrs.addAttribute( "", "innerName", "innerName", "", innerName); 212 h.startElement( "", "innerclass", "innerclass", attrs); 213 h.endElement( "", "innerclass", "innerclass"); 214 215 } catch( SAXException ex) { 216 throw new RuntimeException ( ex.toString()); 217 218 } 219 } 220 221 public final void visitAttribute( Attribute attr) { 222 } 224 225 public final void visitEnd() { 226 try { 227 h.endElement( "", "class", "class"); 228 if( !singleDocument) { 229 h.endDocument(); 230 } 231 } catch( SAXException ex) { 232 ex.getException().printStackTrace(); 233 ex.printStackTrace(); 234 throw new RuntimeException ( ex.toString()); 235 } 236 } 237 238 static final String encode( String s) { 239 StringBuffer sb = new StringBuffer (); 240 for( int i = 0; i<s.length(); i++) { 241 char c = s.charAt( i); 242 if( c=='\\') { 243 sb.append( "\\\\"); 244 } else if( c<0x20 || c>0x7f) { 245 sb.append( "\\u"); 246 if( c<0x10) { 247 sb.append( "000"); 248 } else if( c<0x100) { 249 sb.append( "00"); 250 } else if( c<0x1000) { 251 sb.append( "0"); 252 } 253 sb.append( Integer.toString( c, 16)); 254 } else { 255 sb.append( c); 256 } 257 } 258 return sb.toString(); 259 } 260 261 } 262 263 | Popular Tags |