git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / validation / vo / TimeValidation.java @ 779bac69
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
|
7 |
package fr.ias.sitools.validation.vo; |
8 |
|
9 |
import fr.cnes.sitools.extensions.common.NumberArrayValidation; |
10 |
import fr.cnes.sitools.extensions.common.Validation; |
11 |
import java.text.ParseException; |
12 |
import java.text.SimpleDateFormat; |
13 |
import java.util.HashMap; |
14 |
import java.util.Map; |
15 |
import java.util.logging.Level; |
16 |
import java.util.logging.Logger; |
17 |
|
18 |
/**
|
19 |
*
|
20 |
* @author marc
|
21 |
*/
|
22 |
public class TimeValidation extends NumberArrayValidation{ |
23 |
|
24 |
/**
|
25 |
* Logger.
|
26 |
*/
|
27 |
private static final Logger LOG = Logger.getLogger(TimeValidation.class.getName()); |
28 |
|
29 |
/**
|
30 |
* private time to check
|
31 |
*/
|
32 |
private String timeRange; |
33 |
/**
|
34 |
* private band to check
|
35 |
*/
|
36 |
private double[] bandRange; |
37 |
/**
|
38 |
* Char time split
|
39 |
*/
|
40 |
String timeYearCharSplit = "-"; |
41 |
/**
|
42 |
* Char Hour split
|
43 |
*/
|
44 |
String timeHourCharSplit = ":"; |
45 |
/**
|
46 |
* Char Date split
|
47 |
*/
|
48 |
String timeDateCharSplit = "T"; |
49 |
/**
|
50 |
*
|
51 |
* First element of the range.
|
52 |
*/
|
53 |
private static final int MIN = 0; |
54 |
/**
|
55 |
* Last element of the range.
|
56 |
*/
|
57 |
private static final int MAX = 1; |
58 |
|
59 |
public TimeValidation(final Validation validation,final String keyword,final String timeToCheck) { |
60 |
super(validation, keyword, ",", 2); |
61 |
this.timeRange = timeToCheck;
|
62 |
} |
63 |
|
64 |
|
65 |
|
66 |
@Override
|
67 |
protected Map<String, String> localValidation() { |
68 |
final Map<String, String> error = new HashMap<String, String>(); |
69 |
if(getTimeRange().split(",").length == 2){ |
70 |
String timeMin = getTimeRange().split(",")[MIN]; |
71 |
String timeMax = getTimeRange().split(",")[MAX]; |
72 |
if(timeMin.contains(timeDateCharSplit)){
|
73 |
if(timeMin.split(timeDateCharSplit).length > 2 ){ |
74 |
error.put("Time", "Time value must be in ISO8601 format, eg yyyy-mm-ddThh:mm:ss"); |
75 |
} |
76 |
if(!isValidDate(timeMin)){
|
77 |
error.put("Time", timeMin+" is not a valide date ! Time value must be in ISO8601 format, eg yyyy-mm-ddThh:mm:ss"); |
78 |
} |
79 |
} |
80 |
if(timeMax.contains(timeDateCharSplit)){
|
81 |
if(timeMax.split(timeDateCharSplit).length > 2 ){ |
82 |
error.put("Time", "Time value must be in ISO8601 format, eg yyyy-mm-ddThh:mm:ss"); |
83 |
} |
84 |
if(!isValidDate(timeMax)){
|
85 |
error.put("Time", timeMax+" is not a valide date ! Time value must be in ISO8601 format, eg yyyy-mm-ddThh:mm:ss"); |
86 |
} |
87 |
} |
88 |
}else{
|
89 |
String time = getTimeRange();
|
90 |
if(!isValidDate(time)){
|
91 |
error.put("Time", time+" is not a valide date ! Time value must be in ISO8601 format, eg yyyy-mm-ddThh:mm:ss"); |
92 |
} |
93 |
} |
94 |
return error;
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* @return the timeRange
|
99 |
*/
|
100 |
public String getTimeRange() { |
101 |
return timeRange;
|
102 |
} |
103 |
|
104 |
public static boolean isValidDate(String text) { |
105 |
if (text == null || !text.matches("\\d{4}-[01]\\d-[0-3]\\dT[0-9]{2}:[0-9]{2}:[0-9]{2}")){ |
106 |
return false; |
107 |
} |
108 |
|
109 |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
110 |
df.setLenient(false);
|
111 |
try {
|
112 |
df.parse(text); |
113 |
return true; |
114 |
} catch (ParseException ex) { |
115 |
return false; |
116 |
} |
117 |
} |
118 |
} |