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