View Javadoc
1   /**
2    * Copyright (C) 2014-2018 Philip Helger (www.helger.com)
3    * philip[at]helger[dot]com
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *         http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * The default implementation of {@link ISVRLErrorLevelDeterminator}.
32   *
33   * @author Philip Helger
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     * Default constructor using {@link #UNKNOWN_ERROR_LEVEL_HANDLER}.
51     */
52    public DefaultSVRLErrorLevelDeterminator ()
53    {
54      this (UNKNOWN_ERROR_LEVEL_HANDLER);
55    }
56  
57    /**
58     * Constructor with a custom error level.
59     *
60     * @param aUnknwownErrorLevelHandler
61     *        Custom error level provider. May not be <code>null</code>.
62     * @since 5.0.2
63     */
64    public DefaultSVRLErrorLevelDeterminator (@Nonnull final IFunction <String, ? extends IErrorLevel> aUnknwownErrorLevelHandler)
65    {
66      m_aUnknwownErrorLevelHandler = ValueEnforcer.notNull (aUnknwownErrorLevelHandler, "UnknwownErrorLevelHandler");
67    }
68  
69    /**
70     * @return The handler for unknown error levels.
71     * @since 5.0.2
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 }