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.component.UIComponent;
25 import javax.faces.component.ValueHolder;
26 import javax.faces.context.FacesContext;
27 import javax.faces.convert.Converter;
28 import javax.faces.view.facelets.ComponentHandler;
29 import javax.faces.view.facelets.FaceletContext;
30 import javax.faces.view.facelets.TagAttribute;
31 import javax.faces.view.facelets.TagAttributeException;
32 import javax.faces.view.facelets.TagConfig;
33 import javax.faces.view.facelets.TagException;
34 import javax.faces.view.facelets.TagHandler;
35 import java.io.IOException;
36
37 public class ConverterHandler extends TagHandler {
38
39 private final TagAttribute converterId;
40
41 private final TagAttribute binding;
42
43 public ConverterHandler(final TagConfig config) {
44 super(config);
45 binding = getAttribute("binding");
46 converterId = getAttribute("type");
47 }
48
49 @Override
50 public void apply(final FaceletContext faceletContext, final UIComponent parent)
51 throws IOException, ELException {
52 if (parent instanceof ValueHolder) {
53 if (ComponentHandler.isNew(parent)) {
54 final ValueHolder valueHolder = (ValueHolder) parent;
55 Converter converter = null;
56 ValueExpression valueExpression = null;
57 if (binding != null) {
58 valueExpression = binding.getValueExpression(faceletContext, Converter.class);
59 converter = (Converter) valueExpression.getValue(faceletContext);
60 }
61 if (converter == null) {
62 try {
63 converter = FacesContext.getCurrentInstance().getApplication().createConverter(
64 (String) converterId.getValueExpression(faceletContext, String.class).getValue(faceletContext));
65 } catch (final Exception e) {
66 throw new TagAttributeException(tag, converterId, e.getCause());
67 }
68 if (valueExpression != null) {
69 valueExpression.setValue(faceletContext, converter);
70 }
71 }
72 if (converter != null) {
73 valueHolder.setConverter(converter);
74 }
75
76 }
77 } else {
78 throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
79 }
80 }
81 }