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)
51 throws IOException, FacesException, FaceletException, ELException
52 {
53 if (parent.getParent() == null)
54 {
55 if (parent instanceof EditableValueHolder)
56 {
57 Converter converter = createConverter(beanName.getValue());
58
59 if (useWrapper == null || !"false".equals(useWrapper.getValue()) &&
60 !(converter instanceof SerializableConverter))
61 {
62
63
64
65
66 converter = new SerializableConverter(beanName.getValue(), converter);
67 }
68
69 ((EditableValueHolder) parent).setConverter(converter);
70 }
71 else
72 {
73 throw new FacesException("parent is not an EditableValueHolder");
74 }
75 }
76 }
77
78
79
80
81 protected static Converter createConverter(String beanName)
82 {
83 FacesContext facesContext = FacesContext.getCurrentInstance();
84 Application application = facesContext.getApplication();
85 Object converter = application.getVariableResolver().resolveVariable(facesContext, beanName);
86 return (Converter) converter;
87 }
88 }