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  package org.apache.myfaces.trinidad.webapp;
20  
21  import java.lang.reflect.Method;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.ValueHolder;
25  import javax.faces.convert.Converter;
26  import javax.faces.webapp.UIComponentClassicTagBase;
27  import javax.faces.webapp.UIComponentELTag;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.tagext.Tag;
30  
31  import org.apache.myfaces.trinidad.convert.ColorConverter;
32  import org.apache.myfaces.trinidad.convert.DateTimeConverter;
33  import org.apache.myfaces.trinidad.convert.NumberConverter;
34  
35  /**
36   * This is the Trinidad version of the JSF <code>ConverterELTag</code> class.
37   * The main difference is that this class is <b>NOT</b> inheriting from
38   * the standard <code>TagSupport</code> and therefore does not
39   * implement <code>Serializable</code> interface.
40   *
41   * @author Apache MyFaces team
42   */
43  public abstract class TrinidadConverterELTag extends TrinidadTagSupport
44  {
45      @Override
46      public int doStartTag() throws JspException
47      {
48          UIComponentClassicTagBase componentTag = UIComponentELTag.getParentUIComponentClassicTagBase(pageContext);
49          if (componentTag == null)
50          {
51              throw new JspException("no parent UIComponentTag found");
52          }
53          if (!componentTag.getCreated())
54          {
55              return Tag.SKIP_BODY;
56          }
57  
58          UIComponent component = componentTag.getComponentInstance();
59          if (component == null)
60          {
61              throw new JspException("parent UIComponentTag has no UIComponent");
62          }
63          if (!(component instanceof ValueHolder))
64          {
65              throw new JspException("UIComponent is no ValueHolder");
66          }
67  
68          Converter converter = createConverter();
69  
70          // Check whether component supports multiple converters with method addConverter
71          // if it does, add it by calling addConverter, otherwise call setConverter
72          Class cls = component.getClass();
73          boolean multipleConvertersSupported = true;
74          Method methodAddConverter = null;
75          try
76          {
77            Class[] types = new Class[] {Converter.class};
78            methodAddConverter = cls.getMethod("addConverter", types);
79            types = new Class[] {};
80          }
81          catch (NoSuchMethodException e)
82          {
83            multipleConvertersSupported = false;
84          }
85          
86          if (multipleConvertersSupported)
87          {
88            try
89            {
90              methodAddConverter.invoke(component, new Object[] {converter});
91            }
92            catch (Exception e)
93            {
94              // let it go
95              ;
96            }
97          }
98          else            
99            ((ValueHolder)component).setConverter(converter);
100 
101         return Tag.SKIP_BODY;
102     }
103 
104     protected abstract Converter createConverter() throws JspException;
105 }