1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.orchestra.conversation.jsf.components.facelets;
21
22 import com.sun.facelets.FaceletContext;
23 import com.sun.facelets.FaceletException;
24 import com.sun.facelets.tag.TagAttribute;
25 import com.sun.facelets.tag.TagConfig;
26 import com.sun.facelets.tag.TagHandler;
27 import org.apache.myfaces.orchestra.lib.jsf.SerializableConverter;
28
29 import javax.el.ELException;
30 import javax.faces.FacesException;
31 import javax.faces.application.Application;
32 import javax.faces.component.EditableValueHolder;
33 import javax.faces.component.UIComponent;
34 import javax.faces.context.FacesContext;
35 import javax.faces.convert.Converter;
36 import java.io.IOException;
37
38 public class ConverterTagHandler extends TagHandler
39 {
40 private final TagAttribute beanName;
41 private final TagAttribute useWrapper;
42
43 public ConverterTagHandler(TagConfig config)
44 {
45 super(config);
46 beanName = getRequiredAttribute("beanName");
47 useWrapper = getAttribute("useWrapper");
48 }
49
50 public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException, FacesException, FaceletException, ELException
51 {
52 if (parent.getParent() == null)
53 {
54 if (parent instanceof EditableValueHolder)
55 {
56 Converter converter = createConverter(beanName.getValue());
57
58 if (useWrapper == null || !"false".equals(useWrapper.getValue()) &&
59 !(converter instanceof SerializableConverter))
60 {
61
62
63
64
65 converter = new SerializableConverter(beanName.getValue(), converter);
66 }
67
68 ((EditableValueHolder) parent).setConverter(converter);
69 }
70 else
71 {
72 throw new FacesException("parent is not an EditableValueHolder");
73 }
74 }
75 }
76
77
78
79
80 protected static Converter createConverter(String beanName)
81 {
82 FacesContext facesContext = FacesContext.getCurrentInstance();
83 Application application = facesContext.getApplication();
84 Object converter = application.getVariableResolver().resolveVariable(facesContext, beanName);
85 return (Converter) converter;
86 }
87 }