View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          // TODO else LOG.warn?
76        }
77      } else {
78        throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
79      }
80    }
81  }