lucian
3 years ago
7 changed files with 243 additions and 16 deletions
@ -0,0 +1,26 @@
|
||||
package com.fr.design.widget.ui.designer.date; |
||||
|
||||
/** |
||||
* 日期控件格式检测接口 |
||||
* |
||||
* @author Lucian.Chen |
||||
* @version 11.0 |
||||
* Created by Lucian.Chen on 2022/4/8 |
||||
*/ |
||||
public interface DateFormatCheck { |
||||
|
||||
/** |
||||
* 校验日期格式 |
||||
* @param date 日期 |
||||
* @param format 格式 |
||||
* @return 是否通过 |
||||
*/ |
||||
boolean accept(String date, String format); |
||||
|
||||
/** |
||||
* 校验结果 |
||||
* @param sample 提示 |
||||
* @return 校验结果 |
||||
*/ |
||||
DateFormatCheckResult result(String sample); |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.fr.design.widget.ui.designer.date; |
||||
|
||||
import com.fr.design.widget.ui.designer.date.check.DateFormatCustomCheck; |
||||
import com.fr.design.widget.ui.designer.date.check.DateFormatFrontCheck; |
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author Lucian.Chen |
||||
* @version 11.0 |
||||
* Created by Lucian.Chen on 2022/4/8 |
||||
*/ |
||||
public class DateFormatCheckManager { |
||||
|
||||
private static final List<DateFormatCheck> CHECKS = new ArrayList<>(); |
||||
|
||||
static { |
||||
// 顺序执行
|
||||
CHECKS.add(DateFormatFrontCheck.KEY); |
||||
CHECKS.add(DateFormatCustomCheck.KEY); |
||||
} |
||||
|
||||
@NotNull |
||||
public static DateFormatCheckResult check(String date, String format) { |
||||
for (DateFormatCheck check : CHECKS) { |
||||
if (!check.accept(date, format)) { |
||||
return check.result(date); |
||||
} |
||||
} |
||||
return DateFormatCheckResult.create(date); |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
package com.fr.design.widget.ui.designer.date; |
||||
|
||||
import java.awt.Color; |
||||
|
||||
/** |
||||
* @author Lucian.Chen |
||||
* @version 11.0 |
||||
* Created by Lucian.Chen on 2022/4/8 |
||||
*/ |
||||
public class DateFormatCheckResult { |
||||
|
||||
private String sample; |
||||
private Color color; |
||||
|
||||
public static DateFormatCheckResult create(String sample) { |
||||
return create(sample, Color.BLACK); |
||||
} |
||||
|
||||
public static DateFormatCheckResult create(String sample, Color color) { |
||||
return new DateFormatCheckResult(sample, color); |
||||
} |
||||
|
||||
DateFormatCheckResult(String sample, Color color) { |
||||
this.sample = sample; |
||||
this.color = color; |
||||
} |
||||
|
||||
public String getSample() { |
||||
return sample; |
||||
} |
||||
|
||||
public void setSample(String sample) { |
||||
this.sample = sample; |
||||
} |
||||
|
||||
public Color getColor() { |
||||
return color; |
||||
} |
||||
|
||||
public void setColor(Color color) { |
||||
this.color = color; |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.fr.design.widget.ui.designer.date.check; |
||||
|
||||
import com.fr.data.core.FormatField; |
||||
import com.fr.design.i18n.Toolkit; |
||||
import com.fr.design.widget.ui.designer.date.DateFormatCheck; |
||||
import com.fr.design.widget.ui.designer.date.DateFormatCheckResult; |
||||
import com.fr.stable.ArrayUtils; |
||||
|
||||
import java.awt.Color; |
||||
|
||||
/** |
||||
* 自定义格式校验 |
||||
* @author Lucian.Chen |
||||
* @version 11.0 |
||||
* Created by Lucian.Chen on 2022/4/8 |
||||
*/ |
||||
public class DateFormatCustomCheck implements DateFormatCheck { |
||||
|
||||
public static final DateFormatCustomCheck KEY = new DateFormatCustomCheck(); |
||||
|
||||
/** |
||||
* 不是自定义格式 |
||||
*/ |
||||
@Override |
||||
public boolean accept(String date, String format) { |
||||
return ArrayUtils.contains(FormatField.getInstance().getDateFormatArray(), format); |
||||
} |
||||
|
||||
@Override |
||||
public DateFormatCheckResult result(String sample) { |
||||
sample += " " + Toolkit.i18nText("Fine-Design_Basic_DateFormat_Custom_Warning"); |
||||
return DateFormatCheckResult.create(sample, Color.red); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,95 @@
|
||||
package com.fr.design.widget.ui.designer.date.check; |
||||
|
||||
import com.fr.design.i18n.Toolkit; |
||||
import com.fr.design.widget.ui.designer.date.DateFormatCheck; |
||||
import com.fr.design.widget.ui.designer.date.DateFormatCheckResult; |
||||
import com.fr.stable.AssistUtils; |
||||
import com.fr.stable.StringUtils; |
||||
|
||||
import java.awt.Color; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* 前端的解析方案检测 |
||||
* FR.str2Date |
||||
* @author Lucian.Chen |
||||
* @version 11.0 |
||||
* Created by Lucian.Chen on 2022/4/7 |
||||
*/ |
||||
public class DateFormatFrontCheck implements DateFormatCheck { |
||||
|
||||
private static final Pattern YEAR_PATTERN_1 = Pattern.compile("y{4,}"); //yyyy
|
||||
private static final Pattern YEAR_PATTERN_2 = Pattern.compile("y{2,}"); //yy
|
||||
private static final Pattern MONTH_PATTERN_1 = Pattern.compile("M{4,}"); //MMMM
|
||||
private static final Pattern MONTH_PATTERN_2 = Pattern.compile("M{3}"); //MMM
|
||||
private static final Pattern MONTH_PATTERN_3 = Pattern.compile("M{2}"); //MM
|
||||
private static final Pattern MONTH_PATTERN_4 = Pattern.compile("M"); //M
|
||||
private static final Pattern MONTH_PATTERN_5 = Pattern.compile("a"); |
||||
private static final Pattern DAY_PATTERN_1 = Pattern.compile("d{2,}"); //dd
|
||||
private static final Pattern DAY_PATTERN_2 = Pattern.compile("d"); //d
|
||||
private static final Pattern HOUR_PATTERN_1 = Pattern.compile("h+"); //hh、h
|
||||
private static final Pattern HOUR_PATTERN_2 = Pattern.compile("H+"); //HH、H
|
||||
private static final Pattern MINUTE_PATTERN = Pattern.compile("m{2,}"); //mm
|
||||
private static final Pattern SECOND_PATTERN = Pattern.compile("s{2,}"); //ss
|
||||
private static final Pattern FMT_PATTERN = Pattern.compile("%."); |
||||
|
||||
public static final DateFormatFrontCheck KEY = new DateFormatFrontCheck(); |
||||
|
||||
/** |
||||
* 前端支持的格式 |
||||
*/ |
||||
@Override |
||||
public boolean accept(String date, String format) { |
||||
if (StringUtils.isBlank(date) || StringUtils.isBlank(format)) { |
||||
return false; |
||||
} |
||||
|
||||
String fmt = parseFmt(format); |
||||
int fmtCount = 0; |
||||
Matcher matcher = FMT_PATTERN.matcher(fmt); |
||||
while (matcher.find()) { |
||||
fmtCount ++; |
||||
} |
||||
String[] dates = date.split("\\W+"); |
||||
|
||||
return dates.length == fmtCount || isSpecialFmt(fmt); |
||||
} |
||||
|
||||
@Override |
||||
public DateFormatCheckResult result(String sample) { |
||||
return DateFormatCheckResult.create(Toolkit.i18nText("Fine-Design_Basic_DateFormat_Not_Support"), Color.red); |
||||
} |
||||
|
||||
private boolean isSpecialFmt(String fmt) { |
||||
String lowerFmt = fmt.toLowerCase(); |
||||
return AssistUtils.equals(lowerFmt, "%y%x") || AssistUtils.equals(lowerFmt, "%y%x%d"); |
||||
} |
||||
|
||||
private String parseFmt(String fmt) { |
||||
//年
|
||||
fmt = YEAR_PATTERN_1.matcher(fmt).replaceAll("%Y"); |
||||
fmt = YEAR_PATTERN_2.matcher(fmt).replaceAll("%y"); |
||||
//月
|
||||
fmt = MONTH_PATTERN_1.matcher(fmt).replaceAll("%b"); |
||||
fmt = MONTH_PATTERN_2.matcher(fmt).replaceAll("%B"); |
||||
fmt = MONTH_PATTERN_3.matcher(fmt).replaceAll("%X"); |
||||
fmt = MONTH_PATTERN_4.matcher(fmt).replaceAll("%x"); |
||||
fmt = MONTH_PATTERN_5.matcher(fmt).replaceAll("%p"); |
||||
//天
|
||||
Matcher dayMatcher = DAY_PATTERN_1.matcher(fmt); |
||||
if (dayMatcher.find()) { |
||||
fmt = dayMatcher.replaceAll("%d"); |
||||
} else { |
||||
fmt = DAY_PATTERN_2.matcher(fmt).replaceAll("%e"); |
||||
} |
||||
//时
|
||||
fmt = HOUR_PATTERN_1.matcher(fmt).replaceAll("%I"); |
||||
fmt = HOUR_PATTERN_2.matcher(fmt).replaceAll("%H"); |
||||
//分
|
||||
fmt = MINUTE_PATTERN.matcher(fmt).replaceAll("%M"); |
||||
//秒
|
||||
fmt = SECOND_PATTERN.matcher(fmt).replaceAll("%S"); |
||||
return fmt; |
||||
} |
||||
} |
Loading…
Reference in new issue