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.svrl;
18  
19  import java.util.Locale;
20  
21  import javax.annotation.Nonnull;
22  import javax.annotation.Nullable;
23  
24  import com.helger.commons.ValueEnforcer;
25  import com.helger.commons.error.SingleError;
26  import com.helger.commons.error.level.IErrorLevel;
27  import com.helger.commons.error.location.IErrorLocation;
28  import com.helger.commons.error.text.IHasErrorText;
29  import com.helger.commons.hashcode.HashCodeGenerator;
30  import com.helger.commons.string.ToStringGenerator;
31  
32  /**
33   * Special SVRL resource error that contains the Schematron "test" as
34   * well.
35   *
36   * @author Philip Helger
37   */
38  public class SVRLResourceError extends SingleError
39  {
40    private final String m_sTest;
41  
42    /**
43     * Constructor.
44     *
45     * @param aErrorLevel
46     *        The error level. May not be <code>null</code>.
47     * @param sErrorID
48     *        Error ID. May be <code>null</code>.
49     * @param sErrorFieldName
50     *        Error field name. May be <code>null</code>.
51     * @param aErrorLocation
52     *        Location where the error occurred. May be <code>null</code>.
53     * @param aErrorText
54     *        The error text. May be <code>null</code>.
55     * @param aLinkedException
56     *        An exception that caused the error. May be <code>null</code>.
57     * @param sTest
58     *        The SVRL test that triggered this error. May not be
59     *        <code>null</code>.
60     */
61    public SVRLResourceError (@Nonnull final IErrorLevel aErrorLevel,
62                              @Nullable final String sErrorID,
63                              @Nullable final String sErrorFieldName,
64                              @Nullable final IErrorLocation aErrorLocation,
65                              @Nullable final IHasErrorText aErrorText,
66                              @Nullable final Throwable aLinkedException,
67                              @Nonnull final String sTest)
68    {
69      super (aErrorLevel, sErrorID, sErrorFieldName, aErrorLocation, aErrorText, aLinkedException);
70      m_sTest = ValueEnforcer.notNull (sTest, "Test");
71    }
72  
73    /**
74     * @return The SVRL test that triggered this error.
75     */
76    @Nonnull
77    public String getTest ()
78    {
79      return m_sTest;
80    }
81  
82    @Override
83    public String getAsString (@Nonnull final Locale aContentLocale)
84    {
85      String ret = super.getAsString (aContentLocale);
86      ret += " Test=" + m_sTest;
87      return ret;
88    }
89  
90    @Override
91    public boolean equals (final Object o)
92    {
93      if (o == this)
94        return true;
95      if (!super.equals (o))
96        return false;
97      final SVRLResourceError rhs = (SVRLResourceError) o;
98      return m_sTest.equals (rhs.m_sTest);
99    }
100 
101   @Override
102   public int hashCode ()
103   {
104     return HashCodeGenerator.getDerived (super.hashCode ()).append (m_sTest).getHashCode ();
105   }
106 
107   @Override
108   public String toString ()
109   {
110     return ToStringGenerator.getDerived (super.toString ()).append ("test", m_sTest).getToString ();
111   }
112 
113   public static class SVRLErrorBuilder extends AbstractBuilder <SVRLResourceError, SVRLErrorBuilder>
114   {
115     private String m_sTest;
116 
117     public SVRLErrorBuilder (@Nonnull final String sTest)
118     {
119       setTest (sTest);
120     }
121 
122     @Nonnull
123     public SVRLErrorBuilder setTest (@Nonnull final String sTest)
124     {
125       m_sTest = ValueEnforcer.notNull (sTest, "Test");
126       return this;
127     }
128 
129     @Override
130     public SVRLResourceError build ()
131     {
132       return new SVRLResourceError (m_aErrorLevel,
133                                     m_sErrorID,
134                                     m_sErrorFieldName,
135                                     m_aErrorLocation,
136                                     m_aErrorText,
137                                     m_aLinkedException,
138                                     m_sTest);
139     }
140   }
141 }