1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.webapp; |
17 | |
|
18 | |
import javax.faces.component.UIComponent; |
19 | |
import javax.faces.component.ValueHolder; |
20 | |
import javax.faces.context.FacesContext; |
21 | |
import javax.faces.convert.Converter; |
22 | |
import javax.faces.convert.ConverterException; |
23 | |
import javax.faces.internal.WebAppUtil; |
24 | |
import javax.servlet.jsp.JspException; |
25 | |
import javax.servlet.jsp.tagext.TagSupport; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class ConverterTag extends TagSupport { |
31 | |
|
32 | |
private static final long serialVersionUID = 1L; |
33 | |
|
34 | 30 | private String converterId = null; |
35 | |
|
36 | 30 | public ConverterTag() { |
37 | 30 | } |
38 | |
|
39 | |
public void setConverterId(String converterId) { |
40 | 4 | this.converterId = converterId; |
41 | 4 | } |
42 | |
|
43 | |
public int doStartTag() throws JspException { |
44 | 0 | UIComponentTag tag = UIComponentTag |
45 | |
.getParentUIComponentTag(pageContext); |
46 | 0 | if (tag == null) { |
47 | 0 | throw new JspException("No nested in UIComponentTag"); |
48 | |
} |
49 | |
|
50 | 0 | if (!tag.getCreated()) { |
51 | 0 | return (SKIP_BODY); |
52 | |
} |
53 | |
|
54 | 0 | Converter converter = createConverter(); |
55 | |
|
56 | 0 | UIComponent component = tag.getComponentInstance(); |
57 | 0 | if (component == null || !(component instanceof ValueHolder)) { |
58 | 0 | throw new JspException("Component is null or not value holder."); |
59 | |
} |
60 | 0 | ValueHolder valueHolder = (ValueHolder) component; |
61 | |
|
62 | 0 | valueHolder.setConverter(converter); |
63 | |
|
64 | 0 | Object localValue = valueHolder.getLocalValue(); |
65 | 0 | if (localValue instanceof String) { |
66 | |
try { |
67 | 0 | String str = (String) localValue; |
68 | 0 | FacesContext context = WebAppUtil.getFacesContext(); |
69 | 0 | localValue = converter.getAsObject(context, component, str); |
70 | 0 | valueHolder.setValue(localValue); |
71 | 0 | } catch (ConverterException e) { |
72 | 0 | JspException ex = new JspException(e); |
73 | 0 | throw ex; |
74 | 0 | } |
75 | |
} |
76 | 0 | return SKIP_BODY; |
77 | |
} |
78 | |
|
79 | |
public void release() { |
80 | 2 | converterId = null; |
81 | 2 | } |
82 | |
|
83 | |
protected Converter createConverter() throws JspException { |
84 | |
try { |
85 | 4 | String id = this.converterId; |
86 | 4 | if (UIComponentTag.isValueReference(id)) { |
87 | 0 | id = (String) WebAppUtil.getValueFromCreatedValueBinding(id); |
88 | |
} |
89 | 4 | return WebAppUtil.createConverter(id); |
90 | 0 | } catch (Exception e) { |
91 | 0 | throw new JspException(e); |
92 | |
} |
93 | |
} |
94 | |
} |