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.svrl;
18  
19  import java.util.Locale;
20  
21  import javax.annotation.Nonnull;
22  
23  import com.helger.commons.ValueEnforcer;
24  import com.helger.commons.error.IErrorLevel;
25  import com.helger.commons.error.IResourceLocation;
26  import com.helger.commons.error.ResourceError;
27  import com.helger.commons.hashcode.HashCodeGenerator;
28  import com.helger.commons.string.StringHelper;
29  import com.helger.commons.string.ToStringGenerator;
30  
31  /**
32   * Special SVRL resource error that contains the Schematron "test" as
33   * well.
34   *
35   * @author Philip Helger
36   */
37  public class SVRLResourceError extends ResourceError
38  {
39    private final String m_sTest;
40  
41    /**
42     * Constructor.
43     *
44     * @param aLocation
45     *        Location where the error occurred. May not be <code>null</code>.
46     * @param aErrorLevel
47     *        The error level. May not be <code>null</code>.
48     * @param sErrorText
49     *        The error text. May not be <code>null</code>.
50     * @param sTest
51     *        The SVRL test that triggered this error. May not be
52     *        <code>null</code>.
53     */
54    public SVRLResourceError (@Nonnull final IResourceLocation aLocation,
55                              @Nonnull final IErrorLevel aErrorLevel,
56                              @Nonnull final String sErrorText,
57                              @Nonnull final String sTest)
58    {
59      super (aLocation, aErrorLevel, sErrorText, null);
60      ValueEnforcer.notNull (sTest, "Test");
61      m_sTest = sTest;
62    }
63  
64    /**
65     * @return The SVRL test that triggered this error.
66     */
67    @Nonnull
68    public String getTest ()
69    {
70      return m_sTest;
71    }
72  
73    @Override
74    public String getAsString (@Nonnull final Locale aContentLocale)
75    {
76      String ret = "[" + getErrorLevel ().getID () + "]";
77      final String sLocation = getLocation ().getAsString ();
78      if (StringHelper.hasText (sLocation))
79        ret += ' ' + sLocation;
80      ret += "; Test=" + m_sTest;
81      ret += "; Message=" + getDisplayText (aContentLocale);
82      return ret;
83    }
84  
85    @Override
86    public boolean equals (final Object o)
87    {
88      if (o == this)
89        return true;
90      if (!super.equals (o))
91        return false;
92      final SVRLResourceError rhs = (SVRLResourceError) o;
93      return m_sTest.equals (rhs.m_sTest);
94    }
95  
96    @Override
97    public int hashCode ()
98    {
99      return HashCodeGenerator.getDerived (super.hashCode ()).append (m_sTest).getHashCode ();
100   }
101 
102   @Override
103   public String toString ()
104   {
105     return ToStringGenerator.getDerived (super.toString ()).append ("test", m_sTest).toString ();
106   }
107 }