View Javadoc
1   /**
2    * Copyright (C) 2014-2016 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.EErrorLevel;
23  import com.helger.commons.error.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    @Nullable
48    public IPSErrorHandler getNestedErrorHandler ()
49    {
50      return m_aNestedErrorHandler;
51    }
52  
53    /**
54     * The abstract method that is called for both warnings and errors.
55     *
56     * @param aRes
57     *        The resource in which the error occurred.
58     * @param aErrorLevel
59     *        The error level. Never <code>null</code>.
60     * @param aSourceElement
61     *        The source schematron element, in which the error occurred. Maybe
62     *        <code>null</code> for XPath errors.
63     * @param sMessage
64     *        The error message. Never <code>null</code>.
65     * @param t
66     *        The optional exception that might have occurred. May be
67     *        <code>null</code>.
68     */
69    protected abstract void handle (@Nullable IReadableResource aRes,
70                                    @Nonnull IErrorLevel aErrorLevel,
71                                    @Nullable IPSElement aSourceElement,
72                                    @Nonnull String sMessage,
73                                    @Nullable Throwable t);
74  
75    public final void warn (@Nullable final IReadableResource aRes,
76                            @Nullable final IPSElement aSourceElement,
77                            @Nonnull final String sMessage)
78    {
79      handle (aRes, EErrorLevel.WARN, aSourceElement, sMessage, (Throwable) null);
80  
81      // Do we have a nested error handler?
82      final IPSErrorHandler aNestedErrorHandler = getNestedErrorHandler ();
83      if (aNestedErrorHandler != null)
84        aNestedErrorHandler.warn (aRes, aSourceElement, sMessage);
85    }
86  
87    public final void error (@Nullable final IPSElement aSourceElement, @Nonnull final String sMessage)
88    {
89      error ((IReadableResource) null, aSourceElement, sMessage, (Throwable) null);
90    }
91  
92    public final void error (@Nullable final IReadableResource aRes,
93                             @Nullable final IPSElement aSourceElement,
94                             @Nonnull final String sMessage,
95                             @Nullable final Throwable t)
96    {
97      handle (aRes, EErrorLevel.ERROR, aSourceElement, sMessage, t);
98  
99      // Do we have a nested error handler?
100     final IPSErrorHandler aNestedErrorHandler = getNestedErrorHandler ();
101     if (aNestedErrorHandler != null)
102       aNestedErrorHandler.error (aRes, aSourceElement, sMessage, t);
103   }
104 
105   @Override
106   public String toString ()
107   {
108     return new ToStringGenerator (this).appendIfNotNull ("nestedErrorHandler", m_aNestedErrorHandler).toString ();
109   }
110 }