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(TagConfig config) {
44      super(config);
45      binding = getAttribute("binding");
46      converterId = getAttribute("type");
47    }
48  
49    public void apply(FaceletContext faceletContext, UIComponent parent)
50        throws IOException, ELException {
51      if (parent instanceof ValueHolder) {
52        if (ComponentHandler.isNew(parent)) {
53          ValueHolder valueHolder = (ValueHolder) parent;
54          Converter converter = null;
55          ValueExpression valueExpression = null;
56          if (binding != null) {
57            valueExpression = binding.getValueExpression(faceletContext, Converter.class);
58            converter = (Converter) valueExpression.getValue(faceletContext);
59          }
60          if (converter == null) {
61            try {
62              converter = FacesContext.getCurrentInstance().getApplication().createConverter(
63                  (String) converterId.getValueExpression(faceletContext, String.class).getValue(faceletContext));
64            } catch (Exception e) {
65              throw new TagAttributeException(tag, converterId, e.getCause());
66            }
67            if (valueExpression != null) {
68              valueExpression.setValue(faceletContext, converter);
69            }
70          }
71          if (converter != null) {
72            valueHolder.setConverter(converter);
73          }
74          // TODO else LOG.warn?
75        }
76      } else {
77        throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
78      }
79    }
80  }