KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.helpers.AttributesImpl JavaDoc;
40
41
42 /**
43  * A {@link oracle.toplink.libraries.asm.ClassVisitor ClassVisitor} that generates SAX 2.0
44  * events from the visited class. It can feed any kind of
45  * {@link org.xml.sax.ContentHandler ContentHandler},
46  * e.g. XML serializer, XSLT or XQuery engines.
47  *
48  * @see oracle.toplink.libraries.asm.xml.Processor
49  * @see oracle.toplink.libraries.asm.xml.ASMContentHandler
50  *
51  * @author Eugene Kuleshov
52  */

53 public final class SAXClassAdapter implements ClassVisitor {
54   private ContentHandler JavaDoc h;
55   private boolean singleDocument;
56
57   /**
58    * Constructs a new {@link SAXClassAdapter SAXClassAdapter} object.
59    *
60    * @param h content handler that will be used to send SAX 2.0 events.
61    * @param singleDocument if <tt>true</tt> adapter will not produce
62    * {@link ContentHandler#startDocument() startDocument()} and
63    * {@link ContentHandler#endDocument() endDocument()} events.
64    */

65   public SAXClassAdapter( ContentHandler JavaDoc h, boolean singleDocument) {
66     this.h = h;
67     this.singleDocument = singleDocument;
68     if( !singleDocument) {
69       try {
70         h.startDocument();
71       } catch( SAXException JavaDoc ex) {
72         throw new RuntimeException JavaDoc( ex.getException());
73       }
74     }
75   }
76
77   public final void visit( int version, int access, String JavaDoc name, String JavaDoc superName, String JavaDoc[] interfaces, String JavaDoc sourceFile) {
78     try {
79       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc(version & 0xFFFF).toString());
98       attrs.addAttribute( "", "minor", "minor", "", new Integer JavaDoc(version >>> 16).toString());
99       h.startElement( "", "class", "class", attrs);
100       
101       h.startElement( "", "interfaces", "interfaces", new AttributesImpl JavaDoc());
102       if( interfaces!=null && interfaces.length>0) {
103         for( int i = 0; i < interfaces.length; i++) {
104           AttributesImpl JavaDoc attrs2 = new AttributesImpl JavaDoc();
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 JavaDoc ex) {
113       throw new RuntimeException JavaDoc( ex.getException());
114     }
115   }
116
117   public final void visitField( int access, String JavaDoc name, String JavaDoc desc, Object JavaDoc value, Attribute attrs) {
118     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc att = new AttributesImpl JavaDoc();
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 JavaDoc ex) {
141       throw new RuntimeException JavaDoc( ex.toString());
142     }
143   }
144
145   public final CodeVisitor visitMethod( int access, String JavaDoc name, String JavaDoc desc, String JavaDoc[] exceptions, Attribute attrs) {
146     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc att = new AttributesImpl JavaDoc();
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 JavaDoc());
169       if( exceptions!=null && exceptions.length>0) {
170         for( int i = 0; i < exceptions.length; i++) {
171           AttributesImpl JavaDoc att2 = new AttributesImpl JavaDoc();
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 JavaDoc());
182       }
183       
184     } catch( SAXException JavaDoc ex) {
185       throw new RuntimeException JavaDoc( ex.toString());
186     }
187     
188     return new SAXCodeAdapter( h);
189   }
190
191   public final void visitInnerClass( String JavaDoc name, String JavaDoc outerName, String JavaDoc innerName, int access) {
192     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc ex) {
216       throw new RuntimeException JavaDoc( ex.toString());
217     
218     }
219   }
220
221   public final void visitAttribute( Attribute attr) {
222     // TODO Auto-generated SAXClassAdapter.visitAttribute
223
}
224
225   public final void visitEnd() {
226     try {
227       h.endElement( "", "class", "class");
228       if( !singleDocument) {
229         h.endDocument();
230       }
231     } catch( SAXException JavaDoc ex) {
232       ex.getException().printStackTrace();
233       ex.printStackTrace();
234       throw new RuntimeException JavaDoc( ex.toString());
235     }
236   }
237   
238   static final String JavaDoc encode( String JavaDoc s) {
239     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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