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;
35 import org.xml.sax.helpers.AttributesImpl;
36 
37 /**
38  * SAXAnnotationAdapter
39  *
40  * @author Eugene Kuleshov
41  */
42 public class SAXAnnotationAdapter extends SAXAdapter implements
43         AnnotationVisitor
44 {
45     private final String elementName;
46 
SAXAnnotationAdapter( ContentHandler h, String elementName, int visible, String name, String desc)47     public SAXAnnotationAdapter(
48         ContentHandler h,
49         String elementName,
50         int visible,
51         String name,
52         String desc)
53     {
54         this(h, elementName, visible, desc, name, -1);
55     }
56 
SAXAnnotationAdapter( ContentHandler h, String elementName, int visible, int parameter, String desc)57     public SAXAnnotationAdapter(
58         ContentHandler h,
59         String elementName,
60         int visible,
61         int parameter,
62         String desc)
63     {
64         this(h, elementName, visible, desc, null, parameter);
65     }
66 
SAXAnnotationAdapter( ContentHandler h, String elementName, int visible, String desc, String name, int parameter)67     private SAXAnnotationAdapter(
68         ContentHandler h,
69         String elementName,
70         int visible,
71         String desc,
72         String name,
73         int parameter)
74     {
75         super(h);
76         this.elementName = elementName;
77 
78         AttributesImpl att = new AttributesImpl();
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 
visit(String name, Object value)97     public void visit(String name, Object value) {
98         Class 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(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(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(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(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(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(b[i]));
135 
136             }
137             av.visitEnd();
138         } else {
139             addValueElement("annotationValue",
140                     name,
141                     Type.getDescriptor(c),
142                     value.toString());
143         }
144     }
145 
visitEnum(String name, String desc, String value)146     public void visitEnum(String name, String desc, String value) {
147         addValueElement("annotationValueEnum", name, desc, value);
148     }
149 
visitAnnotation(String name, String desc)150     public AnnotationVisitor visitAnnotation(String name, String desc) {
151         return new SAXAnnotationAdapter(getContentHandler(),
152                 "annotationValueAnnotation",
153                 0,
154                 name,
155                 desc);
156     }
157 
visitArray(String name)158     public AnnotationVisitor visitArray(String name) {
159         return new SAXAnnotationAdapter(getContentHandler(),
160                 "annotationValueArray",
161                 0,
162                 name,
163                 null);
164     }
165 
visitEnd()166     public void visitEnd() {
167         addEnd(elementName);
168     }
169 
addValueElement( String element, String name, String desc, String value)170     private void addValueElement(
171         String element,
172         String name,
173         String desc,
174         String value)
175     {
176         AttributesImpl att = new AttributesImpl();
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