View Javadoc
1   /**
2    * Copyright (C) 2014-2017 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.pure.errorhandler;
18  
19  import javax.annotation.Nonnull;
20  import javax.annotation.Nullable;
21  
22  import com.helger.commons.error.level.EErrorLevel;
23  import com.helger.commons.error.level.IErrorLevel;
24  import com.helger.commons.io.resource.IReadableResource;
25  import com.helger.commons.string.ToStringGenerator;
26  import com.helger.schematron.pure.model.IPSElement;
27  
28  /**
29   * Abstract implementation of {@link IPSErrorHandler}.
30   *
31   * @author Philip Helger
32   */
33  public abstract class AbstractPSErrorHandler implements IPSErrorHandler
34  {
35    private final IPSErrorHandler m_aNestedErrorHandler;
36  
37    protected AbstractPSErrorHandler ()
38    {
39      this (null);
40    }
41  
42    protected AbstractPSErrorHandler (@Nullable final IPSErrorHandler aNestedErrorHandler)
43    {
44      m_aNestedErrorHandler = aNestedErrorHandler;
45    }
46  
47    /**
48     * @return The nested error handler as passed in the constructor or
49     *         <code>null</code> if none was provided.
50     */
51    @Nullable
52    public IPSErrorHandler getNestedErrorHandler ()
53    {
54      return m_aNestedErrorHandler;
55    }
56  
57    /**
58     * The abstract method that is called for both warnings and errors.
59     *
60     * @param aRes
61     *        The resource in which the error occurred.
62     * @param aErrorLevel
63     *        The error level. Never <code>null</code>.
64     * @param aSourceElement
65     *        The source schematron element, in which the error occurred. Maybe
66     *        <code>null</code> for XPath errors.
67     * @param sMessage
68     *        The error message. Never <code>null</code>.
69     * @param t
70     *        The optional exception that might have occurred. May be
71     *        <code>null</code>.
72     */
73    protected abstract void handle (@Nullable IReadableResource aRes,
74                                    @Nonnull IErrorLevel aErrorLevel,
75                                    @Nullable IPSElement aSourceElement,
76                                    @Nonnull String sMessage,
77                                    @Nullable Throwable t);
78  
79    public final void warn (@Nullable final IReadableResource aRes,
80                            @Nullable final IPSElement aSourceElement,
81                            @Nonnull final String sMessage)
82    {
83      handle (aRes, EErrorLevel.WARN, aSourceElement, sMessage, (Throwable) null);
84  
85      // Do we have a nested error handler?
86      final IPSErrorHandler aNestedErrorHandler = getNestedErrorHandler ();
87      if (aNestedErrorHandler != null)
88        aNestedErrorHandler.warn (aRes, aSourceElement, sMessage);
89    }
90  
91    public final void error (@Nullable final IReadableResource aRes,
92                             @Nullable final IPSElement aSourceElement,
93                             @Nonnull final String sMessage,
94                             @Nullable final Throwable t)
95    {
96      handle (aRes, EErrorLevel.ERROR, aSourceElement, sMessage, t);
97  
98      // Do we have a nested error handler?
99      final IPSErrorHandler aNestedErrorHandler = getNestedErrorHandler ();
100     if (aNestedErrorHandler != null)
101       aNestedErrorHandler.error (aRes, aSourceElement, sMessage, t);
102   }
103 
104   @Override
105   public String toString ()
106   {
107     return new ToStringGenerator (this).appendIfNotNull ("nestedErrorHandler", m_aNestedErrorHandler).getToString ();
108   }
109 }