KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > config_browser > ShowValidatorAction


1 package com.opensymphony.webwork.config_browser;
2
3 import com.opensymphony.xwork.util.OgnlUtil;
4 import com.opensymphony.xwork.validator.Validator;
5 import ognl.Ognl;
6 import ognl.OgnlException;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import java.beans.BeanInfo JavaDoc;
11 import java.beans.IntrospectionException JavaDoc;
12 import java.beans.Introspector JavaDoc;
13 import java.beans.PropertyDescriptor JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.TreeSet JavaDoc;
18
19 /**
20  * ShowValidatorAction
21  *
22  * @author Jason Carreira
23  * Date: Jun 1, 2004 9:01:02 PM
24  */

25 public class ShowValidatorAction extends ListValidatorsAction {
26     private static Log log = LogFactory.getLog(ShowValidatorAction.class);
27
28     Set JavaDoc properties = Collections.EMPTY_SET;
29     int selected = 0;
30
31     public int getSelected() {
32         return selected;
33     }
34
35     public void setSelected(int selected) {
36         this.selected = selected;
37     }
38
39     public Set JavaDoc getProperties() {
40         return properties;
41     }
42
43     public Validator getSelectedValidator() {
44         return (Validator) validators.get(selected);
45     }
46
47     public String JavaDoc execute() throws Exception JavaDoc {
48         loadValidators();
49         Validator validator = getSelectedValidator();
50         properties = new TreeSet JavaDoc();
51         try {
52             Map JavaDoc context = Ognl.createDefaultContext(validator);
53             BeanInfo JavaDoc beanInfoFrom = null;
54             try {
55                 beanInfoFrom = Introspector.getBeanInfo(validator.getClass(), Object JavaDoc.class);
56             } catch (IntrospectionException JavaDoc e) {
57                 log.error("An error occurred", e);
58                 addActionError("An error occurred while introspecting a validator of type " + validator.getClass().getName());
59                 return ERROR;
60             }
61
62             PropertyDescriptor JavaDoc[] pds = beanInfoFrom.getPropertyDescriptors();
63
64             for (int i = 0; i < pds.length; i++) {
65                 PropertyDescriptor JavaDoc pd = pds[i];
66                 String JavaDoc name = pd.getName();
67                 Object JavaDoc value = null;
68                 if (pd.getReadMethod() == null) {
69                     value = "No read method for property";
70                 } else {
71                     try {
72                         Object JavaDoc expr = OgnlUtil.compile(name);
73                         value = Ognl.getValue(expr, context, validator);
74                     } catch (OgnlException e) {
75                         addActionError("Caught OGNL exception while getting property value for '" + name + "' on validator of type " + validator.getClass().getName());
76                     }
77                 }
78                 properties.add(new PropertyInfo(name, pd.getPropertyType(), value));
79             }
80         } catch (Exception JavaDoc e) {
81             log.warn("Unable to retrieve properties.", e);
82             addActionError("Unable to retrieve properties: " + e.toString());
83         }
84
85         if (hasErrors())
86             return ERROR;
87         else
88             return SUCCESS;
89     }
90
91     public static class PropertyInfo implements Comparable JavaDoc {
92         private String JavaDoc name;
93         private Class JavaDoc type;
94         private Object JavaDoc value;
95
96         public PropertyInfo(String JavaDoc name, Class JavaDoc type, Object JavaDoc value) {
97             if (name == null) {
98                 throw new IllegalArgumentException JavaDoc("Name must not be null");
99             }
100             if (type == null) {
101                 throw new IllegalArgumentException JavaDoc("Type must not be null");
102             }
103             this.name = name;
104             this.type = type;
105             this.value = value;
106         }
107
108         public Class JavaDoc getType() {
109             return type;
110         }
111
112         public void setType(Class JavaDoc type) {
113             this.type = type;
114         }
115
116         public Object JavaDoc getValue() {
117             return value;
118         }
119
120         public void setValue(Object JavaDoc value) {
121             this.value = value;
122         }
123
124         public String JavaDoc getName() {
125             return name;
126         }
127
128         public void setName(String JavaDoc name) {
129             this.name = name;
130         }
131
132         public boolean equals(Object JavaDoc o) {
133             if (this == o) return true;
134             if (!(o instanceof PropertyInfo)) return false;
135
136             final PropertyInfo propertyInfo = (PropertyInfo) o;
137
138             if (!name.equals(propertyInfo.name)) return false;
139             if (!type.equals(propertyInfo.type)) return false;
140             if (value != null ? !value.equals(propertyInfo.value) : propertyInfo.value != null) return false;
141
142             return true;
143         }
144
145         public int hashCode() {
146             int result;
147             result = name.hashCode();
148             result = 29 * result + type.hashCode();
149             result = 29 * result + (value != null ? value.hashCode() : 0);
150             return result;
151         }
152
153         public int compareTo(Object JavaDoc o) {
154             PropertyInfo other = (PropertyInfo) o;
155             return this.name.compareTo(other.name);
156         }
157     }
158 }
159
Popular Tags