View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.taglib;
24  
25  import java.lang.reflect.AccessibleObject;
26  import java.lang.reflect.Field;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import javax.servlet.jsp.tagext.TagData;
31  import javax.servlet.jsp.tagext.TagExtraInfo;
32  import javax.servlet.jsp.tagext.VariableInfo;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import com.gisgraphy.Constants;
38  
39  /**
40   * Implementation of <code>TagExtraInfo</code> for the <b>constants</b> tag,
41   * identifying the scripting object(s) to be made visible.
42   * 
43   * @author Matt Raible
44   */
45  public class ConstantsTei extends TagExtraInfo {
46      private final Log log = LogFactory.getLog(ConstantsTei.class);
47  
48      /**
49       * Return information about the scripting variables to be created.
50       * 
51       * @param data
52       *                the input data
53       * @return VariableInfo array of variable information
54       */
55      @Override
56      public VariableInfo[] getVariableInfo(TagData data) {
57  	// loop through and expose all attributes
58  	List<VariableInfo> vars = new ArrayList<VariableInfo>();
59  
60  	try {
61  	    String clazz = data.getAttributeString("className");
62  
63  	    if (clazz == null) {
64  		clazz = Constants.class.getName();
65  	    }
66  
67  	    Class<?> c = Class.forName(clazz);
68  
69  	    // if no var specified, get all
70  	    if (data.getAttributeString("var") == null) {
71  		Field[] fields = c.getDeclaredFields();
72  
73  		AccessibleObject.setAccessible(fields, true);
74  
75  		for (Field field : fields) {
76  		    String type = field.getType().getName();
77  		    vars.add(new VariableInfo(field.getName(), ((field
78  			    .getType().isArray()) ? type.substring(2, type
79  			    .length() - 1)
80  			    + "[]" : type), true, VariableInfo.AT_END));
81  		}
82  	    } else {
83  		String var = data.getAttributeString("var");
84  		String type = c.getField(var).getType().getName();
85  		vars.add(new VariableInfo(c.getField(var).getName(), ((c
86  			.getField(var).getType().isArray()) ? type.substring(2,
87  			type.length() - 1)
88  			+ "[]" : type), true, VariableInfo.AT_END));
89  	    }
90  	} catch (Exception cnf) {
91  	    log.error(cnf.getMessage());
92  	}
93  
94  	return vars.toArray(new VariableInfo[] {});
95      }
96  }