KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Map JavaDoc;
28
29 import org.objectweb.fractal.adl.ADLException;
30 import org.objectweb.fractal.adl.Definition;
31 import org.objectweb.fractal.adl.AbstractLoader;
32 import org.objectweb.fractal.adl.Node;
33 import org.objectweb.fractal.adl.components.Component;
34 import org.objectweb.fractal.adl.components.ComponentContainer;
35 import org.objectweb.fractal.adl.components.ComponentDefinition;
36
37 /**
38  * A {@link org.objectweb.fractal.adl.Loader} to check {@link Attributes} nodes
39  * in definitions. This loader checks that the Java attribute controller
40  * interfaces specified in these nodes exist, and that the attribute names and
41  * values are consistent with the methods of theses interfaces.
42  */

43
44 public class AttributeLoader extends AbstractLoader {
45   
46   // --------------------------------------------------------------------------
47
// Implementation of the Loader interface
48
// --------------------------------------------------------------------------
49

50   public Definition load (final String JavaDoc name, final Map JavaDoc context)
51     throws ADLException
52   {
53     Definition d = clientLoader.load(name, context);
54     boolean extend = false;
55     if (d instanceof ComponentDefinition) {
56       extend = ((ComponentDefinition)d).getExtends() != null;
57     }
58     checkNode(d, extend, context);
59     return d;
60   }
61
62   // --------------------------------------------------------------------------
63
// Checking methods
64
// --------------------------------------------------------------------------
65

66   private void checkNode (
67     final Object JavaDoc node,
68     final boolean extend,
69     final Map JavaDoc context) throws ADLException
70   {
71     if (node instanceof AttributesContainer) {
72       checkAttributesContainer((AttributesContainer)node, extend, context);
73     }
74     if (node instanceof ComponentContainer) {
75       Component[] comps = ((ComponentContainer)node).getComponents();
76       for (int i = 0; i < comps.length; i++) {
77         checkNode(comps[i], extend, context);
78       }
79     }
80   }
81
82   private void checkAttributesContainer (
83     final AttributesContainer container,
84     final boolean extend,
85     final Map JavaDoc context) throws ADLException
86   {
87     Attributes attrs = container.getAttributes();
88     if (attrs != null) {
89       String JavaDoc signature = attrs.getSignature();
90       if (signature == null) {
91         if (!extend) {
92           throw new ADLException("Signature missing", (Node)attrs);
93         } else {
94           return;
95         }
96       }
97       Class JavaDoc c;
98       try {
99         c = getClassLoader(context).loadClass(signature);
100       } catch (ClassNotFoundException JavaDoc e) {
101         throw new ADLException(
102           "Invalid signature '" + signature + "'", (Node)attrs, e);
103       }
104
105       Attribute[] attributes = attrs.getAttributes();
106       for (int i = 0; i < attributes.length; ++i) {
107         String JavaDoc attrName = attributes[i].getName();
108         String JavaDoc attrValue = attributes[i].getValue();
109         if (attrName == null) {
110           throw new ADLException("Attribute name missing", (Node)attributes[i]);
111         }
112         if (attrValue == null) {
113           throw new ADLException("Attribute value missing", (Node)attributes[i]);
114         }
115         String JavaDoc getterName =
116           "get" +
117           Character.toUpperCase(attrName.charAt(0)) +
118           attrName.substring(1);
119         Method JavaDoc getter;
120         try {
121           getter = c.getMethod(getterName, new Class JavaDoc[0]);
122         } catch (Exception JavaDoc e) {
123           throw new ADLException("No such attribute", (Node)attributes[i], e);
124         }
125         Class JavaDoc attrType = getter.getReturnType();
126         if (attrType.isPrimitive()) {
127           if (attrType.equals(Integer.TYPE)) {
128             try {
129               Integer.valueOf(attrValue);
130             } catch (NumberFormatException JavaDoc e) {
131               throw new ADLException(
132                 "Bad integer value: " + attrValue, (Node)attributes[i]);
133             }
134           } else if (attrType.equals(Long.TYPE)) {
135             try {
136               Long.valueOf(attrValue);
137             } catch (NumberFormatException JavaDoc e) {
138               throw new ADLException(
139                 "Bad long value: " + attrValue, (Node)attributes[i]);
140             }
141           } else if (attrType.equals(Float.TYPE)) {
142             try {
143               Float.valueOf(attrValue);
144             } catch (NumberFormatException JavaDoc e) {
145               throw new ADLException(
146                 "Bad float value: " + attrValue, (Node)attributes[i]);
147             }
148           } else if (attrType.equals(Double.TYPE)) {
149             try {
150               Double.valueOf(attrValue);
151             } catch (NumberFormatException JavaDoc e) {
152               throw new ADLException(
153                 "Bad double value: " + attrValue, (Node)attributes[i]);
154             }
155           } else if (attrType.equals(Byte.TYPE)) {
156             try {
157               Byte.valueOf(attrValue);
158             } catch (NumberFormatException JavaDoc e) {
159               throw new ADLException(
160                 "Bad byte value: " + attrValue, (Node)attributes[i]);
161             }
162           } else if (attrType.equals(Character.TYPE)) {
163             if (attrValue.length() != 1) {
164               throw new ADLException(
165                 "Bad char value: " + attrValue, (Node)attributes[i]);
166             }
167           } else if (attrType.equals(Short.TYPE)) {
168             try {
169               Short.valueOf(attrValue);
170             } catch (NumberFormatException JavaDoc e) {
171               throw new ADLException(
172                 "Bad short value: " + attrValue, (Node)attributes[i]);
173             }
174           } else if (attrType.equals(Boolean.TYPE)) {
175             if (!attrValue.equals("true") && !attrValue.equals("false")) {
176               throw new ADLException(
177                 "Bad boolean value: " + attrValue, (Node)attributes[i]);
178             }
179           } else {
180             throw new ADLException("Unexpected case", (Node)attributes[i]);
181           }
182         } else if (attrType != String JavaDoc.class) {
183           throw new ADLException(
184             "Unsupported attribute type: " + attrType, (Node)attributes[i]);
185         }
186       }
187     }
188   }
189 }
190
Popular Tags