KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * A Fractal based, static implementation of the {@link AttributeBuilder}
32  * interface. This implementation produces code that uses the Fractal API to
33  * set component attributes.
34  */

35
36 public class StaticFractalAttributeBuilder implements AttributeBuilder {
37
38   // --------------------------------------------------------------------------
39
// Implementation of the AttributeBuilder interface
40
// --------------------------------------------------------------------------
41

42   public void setAttribute (
43     final Object JavaDoc component,
44     final String JavaDoc attributeController,
45     final String JavaDoc name,
46     final String JavaDoc value,
47     final Object JavaDoc context) throws Exception JavaDoc
48   {
49     ClassLoader JavaDoc loader = null;
50     if (context instanceof Map JavaDoc) {
51       loader = (ClassLoader JavaDoc)((Map JavaDoc)context).get("classloader");
52     }
53     if (loader == null) {
54       loader = getClass().getClassLoader();
55     }
56
57     String JavaDoc v = value;
58     Class JavaDoc c = loader.loadClass(attributeController);
59     String JavaDoc attrName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
60     Method JavaDoc getter = c.getMethod("get" + attrName, new Class JavaDoc[0]);
61     if (getter.getReturnType() == String JavaDoc.class) {
62       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
63       buf.append("\"");
64       for (int i = 0; i < v.length(); ++i) {
65         char car = v.charAt(i);
66         if (car == '\n') {
67           buf.append("\\n");
68         } else if (car == '\\') {
69           buf.append("\\\\");
70         } else if (car == '"') {
71           buf.append("\\\"");
72         } else {
73           buf.append(car);
74         }
75       }
76       buf.append("\"");
77       v = buf.toString();
78     }
79     PrintWriter JavaDoc pw = (PrintWriter JavaDoc)((Map JavaDoc)context).get("printwriter");
80     pw.print("((");
81     pw.print(attributeController);
82     pw.print(")Fractal.getAttributeController(");
83     pw.print(component);
84     pw.print(")).set");
85     pw.print(attrName);
86     pw.print("(");
87     pw.print(v);
88     pw.println(");");
89   }
90 }
Popular Tags