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.annotation.ReturnsMutableCopy;
23  import com.helger.commons.collection.ext.ICommonsList;
24  import com.helger.commons.error.IErrorLevel;
25  import com.helger.commons.error.IResourceError;
26  import com.helger.commons.error.IResourceErrorGroup;
27  import com.helger.commons.error.ResourceError;
28  import com.helger.commons.error.ResourceErrorGroup;
29  import com.helger.commons.error.ResourceLocation;
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   * An implementation if {@link IPSErrorHandler} that collects all error
39   * messages.
40   *
41   * @author Philip Helger
42   */
43  public class CollectingPSErrorHandler extends AbstractPSErrorHandler
44  {
45    private final ResourceErrorGroup m_aErrors = new ResourceErrorGroup ();
46  
47    public CollectingPSErrorHandler ()
48    {
49      super ();
50    }
51  
52    public CollectingPSErrorHandler (@Nullable final IPSErrorHandler aNestedErrorHandler)
53    {
54      super (aNestedErrorHandler);
55    }
56  
57    @Override
58    protected void handle (@Nullable final IReadableResource aRes,
59                           @Nonnull final IErrorLevel aErrorLevel,
60                           @Nullable final IPSElement aSourceElement,
61                           @Nonnull final String sMessage,
62                           @Nullable final Throwable t)
63    {
64      String sField = "";
65      if (aSourceElement != null)
66      {
67        sField += ClassHelper.getClassLocalName (aSourceElement);
68        if (aSourceElement instanceof IPSHasID && ((IPSHasID) aSourceElement).hasID ())
69          sField += " [ID=" + ((IPSHasID) aSourceElement).getID () + "]";
70      }
71      final ResourceLocation aLocation = new ResourceLocation (aRes == null ? null : aRes.getResourceID (), sField);
72      m_aErrors.addResourceError (new ResourceError (aLocation, aErrorLevel, sMessage, t));
73    }
74  
75    @Nonnull
76    @ReturnsMutableCopy
77    public IResourceErrorGroup getResourceErrors ()
78    {
79      return m_aErrors.getClone ();
80    }
81  
82    @Nonnull
83    public ICommonsList <IResourceError> getAllResourceErrors ()
84    {
85      return m_aErrors.getAllResourceErrors ();
86    }
87  
88    @Nonnull
89    @ReturnsMutableCopy
90    public IResourceErrorGroup getAllFailures ()
91    {
92      return m_aErrors.getAllFailures ();
93    }
94  
95    @Nonnull
96    @ReturnsMutableCopy
97    public IResourceErrorGroup getAllErrors ()
98    {
99      return m_aErrors.getAllErrors ();
100   }
101 
102   /**
103    * Clear all currently stored errors. This might be helpful, if the same error
104    * handler is used several times.
105    *
106    * @return {@link EChange#CHANGED} if at least one item was cleared.
107    */
108   @Nonnull
109   public EChange clearResourceErrors ()
110   {
111     return m_aErrors.clear ();
112   }
113 
114   /**
115    * @return <code>true</code> if no error is contained, <code>false</code> if
116    *         at least one error is contained.
117    */
118   public boolean isEmpty ()
119   {
120     return m_aErrors.isEmpty ();
121   }
122 
123   @Override
124   public String toString ()
125   {
126     return ToStringGenerator.getDerived (super.toString ()).appendIfNotNull ("errors", m_aErrors).toString ();
127   }
128 }