KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > xml > SAXCodeAdapter


1 /***
2  * ASM XML Adapter
3  * Copyright (c) 2000,2002,2003 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package oracle.toplink.libraries.asm.xml;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
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 JavaDoc;
43 import org.xml.sax.ContentHandler JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.helpers.AttributesImpl JavaDoc;
46
47 /**
48  * A {@link oracle.toplink.libraries.asm.CodeVisitor CodeVisitor} that generates SAX 2.0
49  * events from the visited code.
50  *
51  * @see oracle.toplink.libraries.asm.xml.SAXClassAdapter
52  * @see oracle.toplink.libraries.asm.xml.Processor
53  *
54  * @author Eugene Kuleshov
55  */

56 public final class SAXCodeAdapter implements CodeVisitor {
57   private ContentHandler JavaDoc h;
58   private Map JavaDoc labelNames;
59
60   /**
61    * Constructs a new {@link SAXCodeAdapter SAXCodeAdapter} object.
62    *
63    * @param h content handler that will be used to send SAX 2.0 events.
64    */

65   public SAXCodeAdapter( ContentHandler JavaDoc h) {
66     this.h = h;
67     labelNames = new HashMap JavaDoc();
68   }
69
70   public final void visitInsn( int opcode) {
71     addElement( PrintCodeVisitor.OPCODES[ opcode], new AttributesImpl JavaDoc());
72   }
73
74   public final void visitIntInsn( int opcode, int operand) {
75     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
82     attrs.addAttribute( "", "var", "var", "", Integer.toString( var));
83     addElement( PrintCodeVisitor.OPCODES[ opcode], attrs);
84   }
85
86   public final void visitTypeInsn( int opcode, String JavaDoc desc) {
87     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
88     attrs.addAttribute( "", "desc", "desc", "", desc);
89     addElement( PrintCodeVisitor.OPCODES[ opcode], attrs);
90   }
91
92   public final void visitFieldInsn( int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
93     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
101     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
110     attrs.addAttribute( "", "label", "label", "", getLabel( label));
111     addElement( PrintCodeVisitor.OPCODES[ opcode], attrs);
112   }
113
114   public final void visitLabel( Label label) {
115     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
116     attrs.addAttribute( "", "name", "name", "", getLabel( label));
117     addElement( "Label", attrs);
118   }
119
120   public final void visitLdcInsn( Object JavaDoc cst) {
121     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc o = PrintCodeVisitor.OPCODES[ Constants.TABLESWITCH];
140     addStart( o, attrs);
141     for( int i = 0; i < labels.length; i++) {
142       AttributesImpl JavaDoc att2 = new AttributesImpl JavaDoc();
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 JavaDoc att = new AttributesImpl JavaDoc();
151     att.addAttribute( "", "dflt", "dflt", "", getLabel( dflt));
152     String JavaDoc o = PrintCodeVisitor.OPCODES[ Constants.LOOKUPSWITCH];
153     addStart( o, att);
154     for( int i = 0; i < labels.length; i++) {
155       AttributesImpl JavaDoc att2 = new AttributesImpl JavaDoc();
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 JavaDoc desc, int dims) {
164     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc type) {
171     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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     // TODO ensure it it is ok to close method element here
188
}
189
190   public final void visitLocalVariable( String JavaDoc name, String JavaDoc desc, Label start, Label end, int index) {
191     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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     // TODO Auto-generated SAXCodeAdapter.visitAttribute
209
}
210
211   private final String JavaDoc getLabel( Label label) {
212     String JavaDoc name = (String JavaDoc) 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 JavaDoc name, Attributes JavaDoc attrs) {
221     addStart( name, attrs);
222     addEnd( name);
223   }
224
225   private final void addStart( String JavaDoc name, Attributes JavaDoc attrs) {
226     try {
227       h.startElement( "", name, name, attrs);
228     } catch( SAXException JavaDoc ex) {
229       throw new RuntimeException JavaDoc( ex.toString());
230     }
231   }
232
233   private final void addEnd( String JavaDoc name) {
234     try {
235       h.endElement( "", name, name);
236     } catch( SAXException JavaDoc ex) {
237       throw new RuntimeException JavaDoc( ex.toString());
238     }
239   }
240
241 }
242
243
Popular Tags