KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > AttributeStrategy


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.config;
31
32 import com.caucho.el.ELParser;
33 import com.caucho.el.Expr;
34 import com.caucho.util.L10N;
35 import com.caucho.xml.QName;
36
37 import org.w3c.dom.Node JavaDoc;
38
39 import javax.el.ELContext;
40 import javax.el.ELException;
41
42 public abstract class AttributeStrategy {
43   static final L10N L = new L10N(AttributeStrategy.class);
44
45   protected AttributeStrategy()
46   {
47   }
48  
49   /**
50    * Configures the parent object with the given node.
51    *
52    * @param builder the calling node builder (context)
53    * @param bean the bean to be configured
54    * @param name the name of the property
55    * @param node the configuration node for the value
56    */

57   public void configure(NodeBuilder builder,
58                         Object JavaDoc bean,
59                         QName name,
60                         Node JavaDoc node)
61     throws Exception JavaDoc
62   {
63   }
64
65   /**
66    * Sets the named attribute of the bean.
67    *
68    * @param bean the bean to be set
69    * @param name the attribute namee
70    * @param value the attribute value
71    * @throws Exception
72    */

73   public void setAttribute(Object JavaDoc bean, QName name, Object JavaDoc value)
74     throws Exception JavaDoc
75   {
76   }
77
78   /**
79    * Evaluate as a string.
80    */

81   public static String JavaDoc evalString(String JavaDoc exprString)
82     throws ELException
83   {
84     if (exprString.indexOf("${") >= 0) {
85       ELContext env = Config.getEnvironment();
86       
87       ELParser parser = new ELParser(env, exprString);
88       parser.setCheckEscape(true);
89       Expr expr = parser.parse();
90
91       return expr.evalString(env);
92     }
93     else
94       return exprString;
95   }
96
97   /**
98    * Evaluate as a boolean.
99    */

100   public static boolean evalBoolean(String JavaDoc exprString)
101     throws ELException
102   {
103     if (exprString.indexOf("${") >= 0) {
104       ELContext env = Config.getEnvironment();
105       
106       ELParser parser = new ELParser(env, exprString);
107       parser.setCheckEscape(true);
108       Expr expr = parser.parse();
109
110       return expr.evalBoolean(env);
111     }
112     else
113       return Expr.toBoolean(exprString, null);
114   }
115
116   /**
117    * Evaluate as a long.
118    */

119   public static long evalLong(String JavaDoc exprString)
120     throws ELException
121   {
122     if (exprString.indexOf("${") >= 0) {
123       ELContext env = Config.getEnvironment();
124       
125       ELParser parser = new ELParser(env, exprString);
126       parser.setCheckEscape(true);
127       Expr expr = parser.parse();
128
129       return expr.evalLong(env);
130     }
131     else
132       return Expr.toLong(exprString, null);
133   }
134
135   /**
136    * Evaluate as a double.
137    */

138   public static double evalDouble(String JavaDoc exprString)
139     throws ELException
140   {
141     if (exprString.indexOf("${") >= 0) {
142       ELContext env = Config.getEnvironment();
143       
144       ELParser parser = new ELParser(env, exprString);
145       parser.setCheckEscape(true);
146       Expr expr = parser.parse();
147
148       return expr.evalDouble(env);
149     }
150     else
151       return Expr.toDouble(exprString, null);
152   }
153
154   /**
155    * Evaluate as an object.
156    */

157   public static Object JavaDoc evalObject(String JavaDoc exprString)
158     throws ELException
159   {
160     if (exprString.indexOf("${") >= 0) {
161       ELContext env = Config.getEnvironment();
162       
163       ELParser parser = new ELParser(env, exprString);
164       parser.setCheckEscape(true);
165       Expr expr = parser.parse();
166
167       if (expr != null)
168     return expr.evalObject(env);
169       else
170     return exprString;
171     }
172     else
173       return exprString;
174   }
175 }
176
Popular Tags