KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > attributes > JavaAttributeBuilder


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.attributes;
25
26 import java.lang.reflect.Method JavaDoc;
27
28 /**
29  * A Java based implementation of the {@link AttributeBuilder} interface.
30  */

31
32 public class JavaAttributeBuilder implements AttributeBuilder {
33
34   // --------------------------------------------------------------------------
35
// Implementation of the AttributeBuilder interface
36
// --------------------------------------------------------------------------
37

38   public void setAttribute (
39     final Object JavaDoc component,
40     final String JavaDoc attributeController,
41     final String JavaDoc name,
42     final String JavaDoc value,
43     final Object JavaDoc context) throws Exception JavaDoc
44   {
45     Class JavaDoc c = component.getClass();
46
47     String JavaDoc attrName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
48     
49     // sets the attribute's value
50
String JavaDoc getterName = "get" + attrName;
51     String JavaDoc setterName = "set" + attrName;
52     Method JavaDoc getter = c.getMethod(getterName, new Class JavaDoc[0]);
53     Method JavaDoc setter = c.getMethod(setterName, new Class JavaDoc[] {
54         getter.getReturnType()
55     });
56     Class JavaDoc attrType = getter.getReturnType();
57     Object JavaDoc attrValue;
58     if (attrType.equals(String JavaDoc.class)) {
59       attrValue = value;
60     } else if (attrType.isPrimitive()) {
61       if (attrType.equals(Integer.TYPE)) {
62         attrValue = Integer.valueOf(value);
63       } else if (attrType.equals(Long.TYPE)) {
64         attrValue = Long.valueOf(value);
65       } else if (attrType.equals(Float.TYPE)) {
66         attrValue = Float.valueOf(value);
67       } else if (attrType.equals(Double.TYPE)) {
68         attrValue = Double.valueOf(value);
69       } else if (attrType.equals(Byte.TYPE)) {
70         attrValue = Byte.valueOf(value);
71       } else if (attrType.equals(Character.TYPE)) {
72         if (value.length() != 1) {
73           throw new Exception JavaDoc("Bad char value: " + value);
74         }
75         attrValue = new Character JavaDoc(value.charAt(0));
76       } else if (attrType.equals(Short.TYPE)) {
77         attrValue = Short.valueOf(value);
78       } else if (attrType.equals(Boolean.TYPE)) {
79         if (!value.equals("true") &&
80             !value.equals("false"))
81         {
82           throw new Exception JavaDoc("Bad boolean value: " + value);
83         }
84         attrValue = new Boolean JavaDoc(value.equals("true"));
85       } else {
86         throw new Exception JavaDoc("Unexpected case");
87       }
88     } else {
89       throw new Exception JavaDoc("Unsupported attribute type: " + attrType);
90     }
91     setter.invoke(component, new Object JavaDoc[]{attrValue});
92   }
93 }
94
Popular Tags