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 org.apache.myfaces.tobago.convert.DateTimeConverter;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.el.ELException;
27  import javax.el.ValueExpression;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.ValueHolder;
30  import javax.faces.context.FacesContext;
31  import javax.faces.convert.Converter;
32  import javax.faces.view.facelets.ComponentHandler;
33  import javax.faces.view.facelets.ConverterConfig;
34  import javax.faces.view.facelets.ConverterHandler;
35  import javax.faces.view.facelets.FaceletContext;
36  import javax.faces.view.facelets.TagAttribute;
37  import javax.faces.view.facelets.TagException;
38  import java.lang.invoke.MethodHandles;
39  import java.util.Locale;
40  import java.util.TimeZone;
41  
42  public class ConvertDateTimeHandler extends ConverterHandler {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45  
46    private final TagAttribute binding;
47    private final TagAttribute locale;
48    private final TagAttribute pattern;
49    private final TagAttribute timeZone;
50    private final TagAttribute type;
51  
52    public ConvertDateTimeHandler(final ConverterConfig config) {
53      super(config);
54      binding = getAttribute("binding");
55      locale = getAttribute("locale");
56      pattern = getAttribute("pattern");
57      timeZone = getAttribute("timeZone");
58      type = getAttribute("type");
59    }
60  
61    @Override
62    public void apply(final FaceletContext faceletContext, final UIComponent parent) throws ELException {
63      if (parent instanceof ValueHolder) {
64        if (ComponentHandler.isNew(parent)) {
65          final ValueHolder valueHolder = (ValueHolder) parent;
66          Converter converter = null;
67          ValueExpression valueExpression = null;
68          if (binding != null) {
69            valueExpression = binding.getValueExpression(faceletContext, Converter.class);
70            converter = (Converter) valueExpression.getValue(faceletContext);
71          }
72          if (converter == null) {
73            converter = FacesContext.getCurrentInstance().getApplication()
74                .createConverter(DateTimeConverter.CONVERTER_ID);
75            DateTimeConverter dtConverter = (DateTimeConverter) converter;
76  
77            if (locale != null) {
78              final Object localeObject = locale.getObject(faceletContext);
79              if (localeObject instanceof Locale) {
80                dtConverter.setLocale((Locale) localeObject);
81              } else {
82                LOG.warn("Could not set local.");
83              }
84            }
85            if (pattern != null) {
86              final Object patternObject = pattern.getObject(faceletContext);
87              if (patternObject instanceof String) {
88                dtConverter.setPattern((String) patternObject);
89              } else {
90                LOG.warn("Could not set pattern.");
91              }
92            }
93            if (timeZone != null) {
94              final Object timeZoneObject = timeZone.getObject(faceletContext);
95              if (timeZoneObject instanceof TimeZone) {
96                dtConverter.setTimeZone((TimeZone) timeZoneObject);
97              } else {
98                LOG.warn("Could not set time zone.");
99              }
100           }
101           if (type != null) {
102             final Object typeObject = type.getObject(faceletContext);
103             if (typeObject instanceof String) {
104               dtConverter.setType((String) typeObject);
105             } else {
106               LOG.warn("Could not set type.");
107             }
108           }
109 
110           if (valueExpression != null) {
111             valueExpression.setValue(faceletContext, converter);
112           }
113         }
114         if (converter != null) {
115           valueHolder.setConverter(converter);
116         }
117         // TODO else LOG.warn?
118       }
119     } else {
120       throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
121     }
122   }
123 }