View Javadoc
1   /**
2    * Copyright (C) 2014-2018 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.io.Serializable;
20  import java.util.List;
21  
22  import javax.annotation.Nonnull;
23  import javax.annotation.Nullable;
24  
25  import org.oclc.purl.dsdl.svrl.DiagnosticReference;
26  
27  import com.helger.commons.annotation.ReturnsMutableCopy;
28  import com.helger.commons.collection.impl.CommonsArrayList;
29  import com.helger.commons.collection.impl.ICommonsList;
30  import com.helger.commons.error.level.IErrorLevel;
31  import com.helger.commons.location.SimpleLocation;
32  import com.helger.commons.string.StringHelper;
33  import com.helger.commons.string.ToStringGenerator;
34  import com.helger.schematron.svrl.SVRLResourceError.SVRLErrorBuilder;
35  
36  /**
37   * A wrapper around FailedAssert and SuccessfulReport with easier error level
38   * handling.
39   *
40   * @author Philip Helger
41   */
42  public abstract class AbstractSVRLMessage implements Serializable
43  {
44    protected ICommonsList <DiagnosticReference> m_aDiagnosticReferences;
45    protected String m_sText;
46    protected String m_sLocation;
47    protected String m_sTest;
48    protected String m_sRole;
49    protected IErrorLevel m_aFlag;
50  
51    public AbstractSVRLMessage (@Nullable final List <DiagnosticReference> aDiagnosticReferences,
52                                @Nullable final String sText,
53                                @Nullable final String sLocation,
54                                @Nullable final String sTest,
55                                @Nullable final String sRole,
56                                @Nullable final IErrorLevel aFlag)
57    {
58      m_aDiagnosticReferences = new CommonsArrayList <> (aDiagnosticReferences);
59      m_sText = StringHelper.trim (sText);
60      m_sLocation = sLocation;
61      m_sTest = sTest;
62      m_sRole = sRole;
63      m_aFlag = aFlag;
64    }
65  
66    @Nonnull
67    @ReturnsMutableCopy
68    public ICommonsList <DiagnosticReference> getDiagnisticReferences ()
69    {
70      return m_aDiagnosticReferences.getClone ();
71    }
72  
73    @Nullable
74    public String getText ()
75    {
76      return m_sText;
77    }
78  
79    @Nullable
80    public String getLocation ()
81    {
82      return m_sLocation;
83    }
84  
85    @Nullable
86    public String getTest ()
87    {
88      return m_sTest;
89    }
90  
91    @Nullable
92    public String getRole ()
93    {
94      return m_sRole;
95    }
96  
97    @Nonnull
98    public IErrorLevel getFlag ()
99    {
100     return m_aFlag;
101   }
102 
103   @Nonnull
104   public SVRLResourceError getAsResourceError (@Nullable final String sResourceName)
105   {
106     return new SVRLErrorBuilder (m_sTest).setErrorLevel (m_aFlag)
107                                          .setErrorFieldName (m_sLocation)
108                                          .setErrorLocation (new SimpleLocation (sResourceName))
109                                          .setErrorText (m_sText)
110                                          .build ();
111   }
112 
113   @Override
114   public String toString ()
115   {
116     return new ToStringGenerator (this).append ("diagnosticRefs", m_aDiagnosticReferences)
117                                        .append ("text", m_sText)
118                                        .append ("location", m_sLocation)
119                                        .append ("test", m_sTest)
120                                        .appendIfNotNull ("role", m_sRole)
121                                        .append ("flag", m_aFlag)
122                                        .getToString ();
123   }
124 }