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.ValueEnforcer;
23  import com.helger.commons.annotation.ReturnsMutableCopy;
24  import com.helger.commons.error.SingleError;
25  import com.helger.commons.error.SingleError.SingleErrorBuilder;
26  import com.helger.commons.error.level.IErrorLevel;
27  import com.helger.commons.error.list.ErrorList;
28  import com.helger.commons.error.list.IErrorList;
29  import com.helger.commons.error.location.ErrorLocation;
30  import com.helger.commons.io.resource.IReadableResource;
31  import com.helger.commons.lang.ClassHelper;
32  import com.helger.commons.state.EChange;
33  import com.helger.commons.string.ToStringGenerator;
34  import com.helger.schematron.pure.model.IPSElement;
35  import com.helger.schematron.pure.model.IPSHasID;
36  
37  /**
38   * Abstract collecting {@link IPSErrorHandler} that collects all error messages
39   * in an error list.
40   *
41   * @author Philip Helger
42   * @since 4.2.1
43   */
44  public abstract class AbstractCollectingPSErrorHandler extends AbstractPSErrorHandler
45  {
46    private final ErrorList m_aErrorList;
47  
48    public AbstractCollectingPSErrorHandler (@Nonnull final ErrorList aErrorList,
49                                             @Nullable final IPSErrorHandler aNestedErrorHandler)
50    {
51      super (aNestedErrorHandler);
52      m_aErrorList = ValueEnforcer.notNull (aErrorList, "ErrorList");
53    }
54  
55    @Override
56    protected void handle (@Nullable final IReadableResource aRes,
57                           @Nonnull final IErrorLevel aErrorLevel,
58                           @Nullable final IPSElement aSourceElement,
59                           @Nonnull final String sMessage,
60                           @Nullable final Throwable t)
61    {
62      final SingleErrorBuilder aBuilder = SingleError.builder ()
63                                                     .setErrorLevel (aErrorLevel)
64                                                     .setErrorLocation (aRes == null ? null
65                                                                                     : new ErrorLocation (aRes.getResourceID ()))
66                                                     .setErrorText (sMessage)
67                                                     .setLinkedException (t);
68  
69      if (aSourceElement != null)
70      {
71        String sField = ClassHelper.getClassLocalName (aSourceElement);
72        if (aSourceElement instanceof IPSHasID && ((IPSHasID) aSourceElement).hasID ())
73          sField += " [ID=" + ((IPSHasID) aSourceElement).getID () + "]";
74        aBuilder.setErrorFieldName (sField);
75      }
76      m_aErrorList.add (aBuilder.build ());
77    }
78  
79    @Nonnull
80    @ReturnsMutableCopy
81    public IErrorList getErrorList ()
82    {
83      return m_aErrorList.getClone ();
84    }
85  
86    @Nonnull
87    @ReturnsMutableCopy
88    public IErrorList getAllFailures ()
89    {
90      return m_aErrorList.getAllFailures ();
91    }
92  
93    @Nonnull
94    @ReturnsMutableCopy
95    public IErrorList getAllErrors ()
96    {
97      return m_aErrorList.getAllErrors ();
98    }
99  
100   /**
101    * Clear all currently stored errors. This might be helpful, if the same error
102    * handler is used several times.
103    *
104    * @return {@link EChange#CHANGED} if at least one item was cleared.
105    */
106   @Nonnull
107   public EChange clearResourceErrors ()
108   {
109     return m_aErrorList.clear ();
110   }
111 
112   /**
113    * @return <code>true</code> if no error is contained, <code>false</code> if
114    *         at least one error is contained.
115    */
116   public boolean isEmpty ()
117   {
118     return m_aErrorList.isEmpty ();
119   }
120 
121   @Override
122   public String toString ()
123   {
124     return ToStringGenerator.getDerived (super.toString ()).appendIfNotNull ("ErrorList", m_aErrorList).getToString ();
125   }
126 }