KickJava   Java API By Example, From Geeks To Geeks.

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


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 Java based, static implementation of the {@link AttributeBuilder}
32  * interface. This implementation produces standard Java code that sets
33  * component attributes.
34  */

35
36 public class StaticJavaAttributeBuilder 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     PrintWriter JavaDoc pw = (PrintWriter JavaDoc)((Map JavaDoc)context).get("printwriter");
58     if (((String JavaDoc)component).startsWith("P")) {
59       String JavaDoc v = value;
60       Class JavaDoc c = loader.loadClass(attributeController);
61       String JavaDoc attrName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
62       Method JavaDoc getter = c.getMethod("get" + attrName, new Class JavaDoc[0]);
63       if (getter.getReturnType() == String JavaDoc.class) {
64         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
65         buf.append("\"");
66         for (int i = 0; i < v.length(); ++i) {
67           char car = v.charAt(i);
68           if (car == '\n') {
69             buf.append("\\n");
70           } else if (car == '\\') {
71             buf.append("\\\\");
72           } else if (car == '"') {
73             buf.append("\\\"");
74           } else {
75             buf.append(car);
76           }
77         }
78         buf.append("\"");
79         v = buf.toString();
80       }
81       pw.print(component);
82       pw.print(".set");
83       pw.print(name);
84       pw.print('(');
85       pw.print(v);
86       pw.println(");");
87     } else {
88       // does nothing: composite component attributes not supported
89
}
90   }
91 }
Popular Tags