blob: 1f93979d635c35df09dabe4be06677c40129d903 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.html5.renderkit.input.util;
import java.text.ParseException;
import java.util.Date;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.apache.myfaces.commons.util.DebugUtils;
import org.apache.myfaces.html5.component.input.HtmlInputDateTime;
/**
* Converter for use in hx:inputDateTime.
*
* @author Ali Ok
*
*/
public class Html5DateTimeConverter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent uiComponent, String value) throws ConverterException
{
if (uiComponent instanceof HtmlInputDateTime)
{
HtmlInputDateTime component = (HtmlInputDateTime) uiComponent;
String type = component.getType();
try
{
return Html5DateTimeFormatUtils.parseDateTime(value, type);
}
catch (ParseException e)
{
throw new ConverterException(new FacesMessage("Unable to parse input " + value + " for " + DebugUtils.getPathToComponent(uiComponent) + " with type " + type), e);
}
}
else
{
throw new FacesException("Component " + DebugUtils.getPathToComponent(uiComponent) + " is not a HtmlInputDateTime");
}
}
public String getAsString(FacesContext context, UIComponent uiComponent, Object objValue) throws ConverterException
{
if (objValue == null)
return null;
if (!(objValue instanceof Date))
throw new FacesException("Value is not a java.util.Date for component " + DebugUtils.getPathToComponent(uiComponent) + ".");
if (uiComponent instanceof HtmlInputDateTime)
{
Date value = (Date) objValue;
HtmlInputDateTime component = (HtmlInputDateTime) uiComponent;
String type = component.getType();
return Html5DateTimeFormatUtils.formatDateTime(value, type);
}
else
{
throw new FacesException("Component " + DebugUtils.getPathToComponent(uiComponent) + " is not a HtmlInputDateTime");
}
}
}