KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > xml > SAXAnnotationAdapter


1 /***
2  * ASM XML Adapter
3  * Copyright (c) 2004, Eugene Kuleshov
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 package org.objectweb.asm.xml;
31
32 import org.objectweb.asm.AnnotationVisitor;
33 import org.objectweb.asm.Type;
34 import org.xml.sax.ContentHandler JavaDoc;
35 import org.xml.sax.helpers.AttributesImpl JavaDoc;
36
37 /**
38  * SAXAnnotationAdapter
39  *
40  * @author Eugene Kuleshov
41  */

42 public class SAXAnnotationAdapter extends SAXAdapter implements
43         AnnotationVisitor
44 {
45     private final String JavaDoc elementName;
46
47     public SAXAnnotationAdapter(
48         ContentHandler JavaDoc h,
49         String JavaDoc elementName,
50         int visible,
51         String JavaDoc name,
52         String JavaDoc desc)
53     {
54         this(h, elementName, visible, desc, name, -1);
55     }
56
57     public SAXAnnotationAdapter(
58         ContentHandler JavaDoc h,
59         String JavaDoc elementName,
60         int visible,
61         int parameter,
62         String JavaDoc desc)
63     {
64         this(h, elementName, visible, desc, null, parameter);
65     }
66
67     private SAXAnnotationAdapter(
68         ContentHandler JavaDoc h,
69         String JavaDoc elementName,
70         int visible,
71         String JavaDoc desc,
72         String JavaDoc name,
73         int parameter)
74     {
75         super(h);
76         this.elementName = elementName;
77
78         AttributesImpl JavaDoc att = new AttributesImpl JavaDoc();
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 JavaDoc name, Object JavaDoc value) {
98         Class JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc name, String JavaDoc desc, String JavaDoc value) {
147         addValueElement("annotationValueEnum", name, desc, value);
148     }
149
150     public AnnotationVisitor visitAnnotation(String JavaDoc name, String JavaDoc desc) {
151         return new SAXAnnotationAdapter(getContentHandler(),
152                 "annotationValueAnnotation",
153                 0,
154                 name,
155                 desc);
156     }
157
158     public AnnotationVisitor visitArray(String JavaDoc 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 JavaDoc element,
172         String JavaDoc name,
173         String JavaDoc desc,
174         String JavaDoc value)
175     {
176         AttributesImpl JavaDoc att = new AttributesImpl JavaDoc();
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