001: /***
002: * ASM XML Adapter
003: * Copyright (c) 2004, Eugene Kuleshov
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.ejb3unit.asm.xml;
030:
031: import org.ejb3unit.asm.AnnotationVisitor;
032: import org.ejb3unit.asm.Type;
033: import org.xml.sax.ContentHandler;
034: import org.xml.sax.helpers.AttributesImpl;
035:
036: /**
037: * SAXAnnotationAdapter
038: *
039: * @author Eugene Kuleshov
040: */
041: public class SAXAnnotationAdapter extends SAXAdapter implements
042: AnnotationVisitor {
043: private final String elementName;
044:
045: public SAXAnnotationAdapter(final ContentHandler h,
046: final String elementName, final int visible,
047: final String name, final String desc) {
048: this (h, elementName, visible, desc, name, -1);
049: }
050:
051: public SAXAnnotationAdapter(final ContentHandler h,
052: final String elementName, final int visible,
053: final int parameter, final String desc) {
054: this (h, elementName, visible, desc, null, parameter);
055: }
056:
057: private SAXAnnotationAdapter(final ContentHandler h,
058: final String elementName, final int visible,
059: final String desc, final String name, final int parameter) {
060: super (h);
061: this .elementName = elementName;
062:
063: AttributesImpl att = new AttributesImpl();
064: if (name != null) {
065: att.addAttribute("", "name", "name", "", name);
066: }
067: if (visible != 0) {
068: att.addAttribute("", "visible", "visible", "",
069: visible > 0 ? "true" : "false");
070: }
071: if (parameter != -1) {
072: att.addAttribute("", "parameter", "parameter", "", Integer
073: .toString(parameter));
074: }
075: if (desc != null) {
076: att.addAttribute("", "desc", "desc", "", desc);
077: }
078:
079: addStart(elementName, att);
080: }
081:
082: public void visit(final String name, final Object value) {
083: Class c = value.getClass();
084: if (c.isArray()) {
085: AnnotationVisitor av = visitArray(name);
086: if (value instanceof byte[]) {
087: byte[] b = (byte[]) value;
088: for (int i = 0; i < b.length; i++) {
089: av.visit(null, new Byte(b[i]));
090: }
091:
092: } else if (value instanceof char[]) {
093: char[] b = (char[]) value;
094: for (int i = 0; i < b.length; i++) {
095: av.visit(null, new Character(b[i]));
096: }
097:
098: } else if (value instanceof short[]) {
099: short[] b = (short[]) value;
100: for (int i = 0; i < b.length; i++) {
101: av.visit(null, new Short(b[i]));
102: }
103:
104: } else if (value instanceof boolean[]) {
105: boolean[] b = (boolean[]) value;
106: for (int i = 0; i < b.length; i++) {
107: av.visit(null, Boolean.valueOf(b[i]));
108: }
109:
110: } else if (value instanceof int[]) {
111: int[] b = (int[]) value;
112: for (int i = 0; i < b.length; i++) {
113: av.visit(null, new Integer(b[i]));
114: }
115:
116: } else if (value instanceof long[]) {
117: long[] b = (long[]) value;
118: for (int i = 0; i < b.length; i++) {
119: av.visit(null, new Long(b[i]));
120: }
121:
122: } else if (value instanceof float[]) {
123: float[] b = (float[]) value;
124: for (int i = 0; i < b.length; i++) {
125: av.visit(null, new Float(b[i]));
126: }
127:
128: } else if (value instanceof double[]) {
129: double[] b = (double[]) value;
130: for (int i = 0; i < b.length; i++) {
131: av.visit(null, new Double(b[i]));
132: }
133:
134: }
135: av.visitEnd();
136: } else {
137: addValueElement("annotationValue", name, Type
138: .getDescriptor(c), value.toString());
139: }
140: }
141:
142: public void visitEnum(final String name, final String desc,
143: final String value) {
144: addValueElement("annotationValueEnum", name, desc, value);
145: }
146:
147: public AnnotationVisitor visitAnnotation(final String name,
148: final String desc) {
149: return new SAXAnnotationAdapter(getContentHandler(),
150: "annotationValueAnnotation", 0, name, desc);
151: }
152:
153: public AnnotationVisitor visitArray(final String name) {
154: return new SAXAnnotationAdapter(getContentHandler(),
155: "annotationValueArray", 0, name, null);
156: }
157:
158: public void visitEnd() {
159: addEnd(elementName);
160: }
161:
162: private void addValueElement(final String element,
163: final String name, final String desc, final String value) {
164: AttributesImpl att = new AttributesImpl();
165: if (name != null) {
166: att.addAttribute("", "name", "name", "", name);
167: }
168: if (desc != null) {
169: att.addAttribute("", "desc", "desc", "", desc);
170: }
171: if (value != null) {
172: att.addAttribute("", "value", "value", "", SAXClassAdapter
173: .encode(value));
174: }
175:
176: addElement(element, att);
177: }
178:
179: }
|