You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2021/05/06 09:26:29 UTC
[myfaces-tobago] branch master updated: feat: progress on date input
This is an automated email from the ASF dual-hosted git repository.
lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git
The following commit(s) were added to refs/heads/master by this push:
new 91baaed feat: progress on date input
91baaed is described below
commit 91baaedb9012d6bf3a1b33505c395c9107025484
Author: Udo Schnurpfeil <ud...@irian.eu>
AuthorDate: Thu May 6 10:56:22 2021 +0200
feat: progress on date input
clean up
issue: TOBAGO-2071
---
.../internal/renderkit/renderer/DateRenderer.java | 19 +-
.../tobago/internal/util/DateFormatUtils.java | 246 ---------------------
.../internal/util/DateFormatUtilsUnitTest.java | 97 --------
3 files changed, 3 insertions(+), 359 deletions(-)
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java
index 362b756..b0a4c97 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java
@@ -23,7 +23,6 @@ import org.apache.myfaces.tobago.component.Attributes;
import org.apache.myfaces.tobago.internal.component.AbstractUIDate;
import org.apache.myfaces.tobago.internal.context.DateTimeI18n;
import org.apache.myfaces.tobago.internal.util.AccessKeyLogger;
-import org.apache.myfaces.tobago.internal.util.DateFormatUtils;
import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
import org.apache.myfaces.tobago.internal.util.JsonUtils;
import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
@@ -99,18 +98,6 @@ public class DateRenderer<T extends AbstractUIDate> extends MessageLayoutRendere
super.writeAdditionalAttributes(facesContext, writer, date);
// writer.writeAttribute(HtmlAttributes.PATTERN,
-// new DateFormatUtils.DateTimeJavaScriptPattern(input.getPattern()).getDatePattern(), true);
- final HtmlInputTypes type = date.getType();
- if (type == HtmlInputTypes.TEXT) {
- // todo
- final DateFormatUtils.DateTimeJavaScriptPattern patterns
- = new DateFormatUtils.DateTimeJavaScriptPattern(date.getPattern());
- writer.writeAttribute(CustomAttributes.DATE_PATTERN, patterns.getDatePattern(), true);
- writer.writeAttribute(CustomAttributes.TIME_PATTERN, patterns.getTimePattern(), true);
- }
- // todo: also render pattern, either yyyy-mm-dd or in case of type=text the combination of upper
-// final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-// writer.writeAttribute(CustomAttributes.TODAY, sdf.format(new Date()), true); XXX seem no longer needed
final DateTimeI18n dateTimeI18n = DateTimeI18n.valueOf(facesContext.getViewRoot().getLocale());
writer.writeAttribute(CustomAttributes.I18N, JsonUtils.encode(dateTimeI18n), true);
writer.writeAttribute(CustomAttributes.TODAY_BUTTON, date.isTodayButton());
@@ -358,10 +345,10 @@ public class DateRenderer<T extends AbstractUIDate> extends MessageLayoutRendere
} else if (TYPE_WEEK.equals(typeFromConverter)) {
type = HtmlInputTypes.WEEK;
} else { // unknown type
- type = HtmlInputTypes.TEXT;
+ type = HtmlInputTypes.DATE;
}
- } else {
- type = HtmlInputTypes.TEXT;
+ } else { // no converter
+ type = HtmlInputTypes.DATE;
}
}
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/DateFormatUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/DateFormatUtils.java
deleted file mode 100644
index 9f9185d..0000000
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/DateFormatUtils.java
+++ /dev/null
@@ -1,246 +0,0 @@
-/*
- * 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.tobago.internal.util;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.faces.convert.ConverterException;
-import javax.faces.convert.DateTimeConverter;
-import java.lang.invoke.MethodHandles;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Locale;
-
-/**
- * This code is taken from myfaces core.
- * TODO: Should be sharable (e.g. myfaces-commons).
- */
-public final class DateFormatUtils {
-
- private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- private static final String TYPE_DATE = "date";
- private static final String TYPE_TIME = "time";
- private static final String TYPE_BOTH = "both";
- private static final String STYLE_DEFAULT = "default";
- private static final String STYLE_MEDIUM = "medium";
- private static final String STYLE_SHORT = "short";
- private static final String STYLE_LONG = "long";
- private static final String STYLE_FULL = "full";
-
- private DateFormatUtils() {
- }
-
- /**
- * Find a pattern for the converter.
- * Returns the pattern inside the converter, if any.
- * Otherwise compute the pattern.
- *
- * @return the patter or null, if DateFormat.getDateInstance() returns no SimpleDateFormat.
- */
- public static String findPattern(final DateTimeConverter converter) {
- String pattern = converter.getPattern();
-
- if (pattern == null) {
- final DateFormat dateFormat = getDateFormat(
- converter.getType(), converter.getDateStyle(),
- converter.getTimeStyle(), converter.getLocale());
- if (dateFormat instanceof SimpleDateFormat) {
- final SimpleDateFormat format = (SimpleDateFormat) dateFormat;
- pattern = format.toPattern();
- }
- }
-
- return pattern;
- }
-
- private static DateFormat getDateFormat(
- final String type, final String dateStyle, final String timeStyle, final Locale locale) {
- final DateFormat format;
- if (type.equals(TYPE_DATE)) {
- format = DateFormat.getDateInstance(calcStyle(dateStyle), locale);
- } else if (type.equals(TYPE_TIME)) {
- format = DateFormat.getTimeInstance(calcStyle(timeStyle), locale);
- } else if (type.equals(TYPE_BOTH)) {
- format = DateFormat.getDateTimeInstance(calcStyle(dateStyle),
- calcStyle(timeStyle),
- locale);
- } else {
- throw new ConverterException("invalid type '" + type + "'");
- }
-
- // format cannot be lenient (JSR-127)
- format.setLenient(false);
- return format;
- }
-
- private static int calcStyle(final String name) {
- if (name.equals(STYLE_DEFAULT)) {
- return DateFormat.DEFAULT;
- }
- if (name.equals(STYLE_MEDIUM)) {
- return DateFormat.MEDIUM;
- }
- if (name.equals(STYLE_SHORT)) {
- return DateFormat.SHORT;
- }
- if (name.equals(STYLE_LONG)) {
- return DateFormat.LONG;
- }
- if (name.equals(STYLE_FULL)) {
- return DateFormat.FULL;
- }
-
- throw new ConverterException("invalid style '" + name + "'");
- }
-
- /**
- * Get the pattern from the "Java world",
- * see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html
- * and convert it to 'vanillajs-datepicker', see https://mymth.github.io/vanillajs-datepicker/#/date-string+format
- * Attention: Not every pattern char is supported.
- */
- public static class DateTimeJavaScriptPattern {
-
- private StringBuilder buffer = new StringBuilder();
- private StringBuilder datePattern;
- private StringBuilder timePattern;
- private String separator;
-
- private boolean dateMode = false;
- private boolean timeMode = false;
-
- private int lastDateIndex = -1;
- private int lastTimeIndex = -1;
-
- public DateTimeJavaScriptPattern(final String javaPattern) {
- String pattern;
- if (javaPattern == null) {
- LOG.warn("Empty pattern not supported!");
- // XXX an missing pattern might be supporable without Datepicker, but with simple <tc:in>
- pattern = "";
- } else if (javaPattern.length() > 100) {
- LOG.warn("Pattern too long (max = 100): '{}'!", javaPattern);
- pattern = "";
- } else if (javaPattern.indexOf('\'') > -1) {
- LOG.warn("Pattern escape char ' is not supported: '{}'!", javaPattern);
- pattern = "";
- } else if (StringUtils.containsAny(javaPattern, "GWFKzX")) {
- LOG.warn("Pattern chars 'G', 'W', 'F', 'K', 'z' and 'X' are not supported: '{}'!", javaPattern);
- pattern = "";
- } else {
- pattern = javaPattern;
- }
-
- for (int i = 0; i < pattern.length(); i++) {
- final char c = pattern.charAt(i);
- checkSpit(i, c);
- append(c);
- }
- }
-
- private void checkSpit(final int index, final char c) {
-
- boolean isDate = isDate(c);
- boolean isTime = isTime(c);
-
- if (isDate){
- lastDateIndex = index;
- }
- if (isTime){
- lastTimeIndex = index;
- }
-
- if (!dateMode && !timeMode && isDate) {
- dateMode = true;
- datePattern = new StringBuilder(buffer);
- buffer.setLength(0);
- } else if (!dateMode && !timeMode && isTime) {
- timeMode = true;
- timePattern = new StringBuilder(buffer);
- buffer.setLength(0);
- } else if (!dateMode && !timeMode) {
- LOG.warn("Prefix without date/time char currently not supported!");
- } else if (dateMode && isTime) {
- timeMode = true;
- dateMode = false;
- timePattern = new StringBuilder();
- separator = datePattern.substring(lastDateIndex + 1);
- datePattern.setLength(lastDateIndex + 1);
- if (timePattern != null) {
- LOG.warn("Pattern mixed!");
- }
- } else if (timeMode && isDate) {
- timeMode = false;
- dateMode = true;
- datePattern = new StringBuilder();
- separator = timePattern.substring(lastTimeIndex + 1);
- timePattern.setLength(lastTimeIndex + 1);
- if (datePattern != null) {
- LOG.warn("Pattern mixed!");
- }
- } else {
- // nothing to switch
- }
- }
-
- private void append(final char c) {
- final char converted = convert(c);
-
- if (dateMode) {
- datePattern.append(converted);
- } else if (timeMode) {
- timePattern.append(converted);
- } else {
- buffer.append(converted);
- }
- }
-
- public String getDatePattern() {
- return datePattern != null ? datePattern.toString() : null;
- }
-
- public String getTimePattern() {
- return timePattern != null ? timePattern.toString() : null;
- }
-
- public String getSeparator() {
- return separator;
- }
-
- public static boolean isDate(final char c) {
- return c == 'y' || c == 'Y' || c == 'M' || c == 'L' || c == 'd';
- }
-
- private static boolean isTime(final char c) {
- return c == 'H' || c == 'm' || c == 's';
- }
-
- private char convert(char c) {
-
- if (c == 'M') {
- return 'm';
- } else {
- return c;
- }
- }
- }
-}
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/DateFormatUtilsUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/DateFormatUtilsUnitTest.java
deleted file mode 100644
index 81e6b6b..0000000
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/DateFormatUtilsUnitTest.java
+++ /dev/null
@@ -1,97 +0,0 @@
-package org.apache.myfaces.tobago.internal.util;
-
-/*
- * 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.
- */
-
-import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-public class DateFormatUtilsUnitTest extends AbstractTobagoTestBase {
-
- @Test
- public void date() {
- final String p1 = "yyyy-MM-dd";
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertEquals("yyyy-mm-dd", js1.getDatePattern());
- Assertions.assertNull(js1.getTimePattern());
- Assertions.assertNull(js1.getSeparator());
- }
-
- @Test
- public void time() {
- final String p1 = "HH:mm"; // hour 0-23 : minute 0-59
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertNull(js1.getDatePattern());
- Assertions.assertEquals("HH:mm", js1.getTimePattern());
- Assertions.assertNull(js1.getSeparator());
- }
-
- @Test
- public void bothDateTime() {
- final String p1 = "yyyy-MM-dd HH:mm";
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertEquals("yyyy-mm-dd", js1.getDatePattern());
- Assertions.assertEquals("HH:mm", js1.getTimePattern());
- Assertions.assertEquals(" ", js1.getSeparator());
- }
-
- @Test
- public void bothTimeDate() {
- final String p1 = "HH:mm yyyy-MM-dd";
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertEquals("yyyy-mm-dd", js1.getDatePattern());
- Assertions.assertEquals("HH:mm", js1.getTimePattern());
- Assertions.assertEquals(" ", js1.getSeparator());
- }
-
- @Test
- public void both1() {
- final String p1 = "HH----yyyy";
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertEquals("yyyy", js1.getDatePattern());
- Assertions.assertEquals("HH", js1.getTimePattern());
- Assertions.assertEquals("----", js1.getSeparator());
- }
-
- @Test
- public void both2() {
- final String p1 = "::yy--ss::";
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertEquals("::yy", js1.getDatePattern());
- Assertions.assertEquals("ss::", js1.getTimePattern());
- Assertions.assertEquals("--", js1.getSeparator());
- }
-
- @Test
- public void both3() {
- final String p1 = "::MMmm::";
- final DateFormatUtils.DateTimeJavaScriptPattern js1 = new DateFormatUtils.DateTimeJavaScriptPattern(p1);
-
- Assertions.assertEquals("::mm", js1.getDatePattern());
- Assertions.assertEquals("mm::", js1.getTimePattern());
- Assertions.assertEquals("", js1.getSeparator());
- }
-}