1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.helger.schematron.svrl;
18
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.helger.commons.ValueEnforcer;
26 import com.helger.commons.error.level.EErrorLevel;
27 import com.helger.commons.error.level.IErrorLevel;
28 import com.helger.commons.functional.IFunction;
29
30
31
32
33
34
35 public class DefaultSVRLErrorLevelDeterminator implements ISVRLErrorLevelDeterminator
36 {
37 private static final Logger LOGGER = LoggerFactory.getLogger (DefaultSVRLErrorLevelDeterminator.class);
38 public static final IErrorLevel DEFAULT_ERROR_LEVEL = EErrorLevel.ERROR;
39 public static final IFunction <String, IErrorLevel> UNKNOWN_ERROR_LEVEL_HANDLER = sFlag -> {
40 if (sFlag != null)
41 LOGGER.warn ("Cannot convert the SVRL flag '" +
42 sFlag +
43 "' to an error level. Using default error level instead!");
44 return DEFAULT_ERROR_LEVEL;
45 };
46
47 private final IFunction <String, ? extends IErrorLevel> m_aUnknwownErrorLevelHandler;
48
49
50
51
52 public DefaultSVRLErrorLevelDeterminator ()
53 {
54 this (UNKNOWN_ERROR_LEVEL_HANDLER);
55 }
56
57
58
59
60
61
62
63
64 public DefaultSVRLErrorLevelDeterminator (@Nonnull final IFunction <String, ? extends IErrorLevel> aUnknwownErrorLevelHandler)
65 {
66 m_aUnknwownErrorLevelHandler = ValueEnforcer.notNull (aUnknwownErrorLevelHandler, "UnknwownErrorLevelHandler");
67 }
68
69
70
71
72
73 @Nonnull
74 public IFunction <String, ? extends IErrorLevel> getUnknwownErrorLevelHandler ()
75 {
76 return m_aUnknwownErrorLevelHandler;
77 }
78
79 @Nullable
80 public static IErrorLevel getDefaultErrorLevelFromString (@Nullable final String sFlag)
81 {
82 if (sFlag != null)
83 {
84 if (sFlag.equalsIgnoreCase ("information") ||
85 sFlag.equalsIgnoreCase ("info") ||
86 sFlag.equalsIgnoreCase ("notice") ||
87 sFlag.equalsIgnoreCase ("note"))
88 return EErrorLevel.INFO;
89
90 if (sFlag.equalsIgnoreCase ("warning") || sFlag.equalsIgnoreCase ("warn"))
91 return EErrorLevel.WARN;
92
93 if (sFlag.equalsIgnoreCase ("error") || sFlag.equalsIgnoreCase ("err"))
94 return EErrorLevel.ERROR;
95
96 if (sFlag.equalsIgnoreCase ("fatal") ||
97 sFlag.equalsIgnoreCase ("fatal_error") ||
98 sFlag.equalsIgnoreCase ("fatal-error") ||
99 sFlag.equalsIgnoreCase ("fatalerror"))
100 return EErrorLevel.FATAL_ERROR;
101 }
102
103 return null;
104 }
105
106 @Nonnull
107 public IErrorLevel getErrorLevelFromString (@Nullable final String sFlag)
108 {
109 final IErrorLevel ret = getDefaultErrorLevelFromString (sFlag);
110 if (ret != null)
111 return ret;
112
113 return m_aUnknwownErrorLevelHandler.apply (sFlag);
114 }
115 }