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.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     TBD: Why we don't use a converter directly via the tobago.taglib.xml?
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          // TODO else LOG.warn?
79        }
80      } else {
81        throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
82      }
83    }
84  }