1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.facelets;
21
22 import javax.el.ELException;
23 import javax.el.ValueExpression;
24 import javax.faces.FacesException;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.ValueHolder;
27 import javax.faces.context.FacesContext;
28 import javax.faces.convert.Converter;
29 import javax.faces.view.facelets.ComponentHandler;
30 import javax.faces.view.facelets.FaceletContext;
31 import javax.faces.view.facelets.TagAttribute;
32 import javax.faces.view.facelets.TagAttributeException;
33 import javax.faces.view.facelets.TagConfig;
34 import javax.faces.view.facelets.TagException;
35 import javax.faces.view.facelets.TagHandler;
36 import java.io.IOException;
37
38
39
40
41 public class ConverterHandler extends TagHandler {
42
43 private final TagAttribute converterId;
44
45 private final TagAttribute binding;
46
47 public ConverterHandler(TagConfig config) {
48 super(config);
49 binding = getAttribute("binding");
50 converterId = getAttribute("type");
51 }
52
53 public void apply(FaceletContext faceletContext, UIComponent parent)
54 throws IOException, FacesException, ELException {
55 if (parent instanceof ValueHolder) {
56 if (ComponentHandler.isNew(parent)) {
57 ValueHolder valueHolder = (ValueHolder) parent;
58 Converter converter = null;
59 ValueExpression valueExpression = null;
60 if (binding != null) {
61 valueExpression = binding.getValueExpression(faceletContext, Converter.class);
62 converter = (Converter) valueExpression.getValue(faceletContext);
63 }
64 if (converter == null) {
65 try {
66 converter = FacesContext.getCurrentInstance().getApplication().createConverter(
67 (String) converterId.getValueExpression(faceletContext, String.class).getValue(faceletContext));
68 } catch (Exception e) {
69 throw new TagAttributeException(tag, converterId, e.getCause());
70 }
71 if (valueExpression != null) {
72 valueExpression.setValue(faceletContext, converter);
73 }
74 }
75 if (converter != null) {
76 valueHolder.setConverter(converter);
77 }
78
79 }
80 } else {
81 throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
82 }
83 }
84 }