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.List;
20  import java.util.regex.Matcher;
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.ext.CommonsArrayList;
29  import com.helger.commons.collection.ext.ICommonsList;
30  import com.helger.commons.error.level.IErrorLevel;
31  import com.helger.commons.error.location.ErrorLocation;
32  import com.helger.commons.regex.RegExHelper;
33  import com.helger.commons.string.StringHelper;
34  import com.helger.commons.string.ToStringGenerator;
35  import com.helger.schematron.svrl.SVRLResourceError.SVRLErrorBuilder;
36  
37  /**
38   * A wrapper around FailedAssert and SuccessfulReport with easier error level
39   * handling.
40   *
41   * @author Philip Helger
42   */
43  public abstract class AbstractSVRLMessage
44  {
45    protected ICommonsList <DiagnosticReference> m_aDiagnosticReferences;
46    protected String m_sText;
47    protected String m_sLocation;
48    protected String m_sTest;
49    protected String m_sRole;
50    protected IErrorLevel m_aFlag;
51  
52    @Nonnull
53    protected static String getBeautifiedLocation (@Nonnull final String sLocation)
54    {
55      String sResult = sLocation;
56      // Handle namespaces:
57      // Search for "*:xx[namespace-uri()='yy']" where xx is the localname and yy
58      // is the namespace URI
59      final Matcher aMatcher = RegExHelper.getMatcher ("\\Q*:\\E([a-zA-Z0-9_]+)\\Q[namespace-uri()='\\E([^']+)\\Q']\\E",
60                                                       sResult);
61      while (aMatcher.find ())
62      {
63        final String sLocalName = aMatcher.group (1);
64        final String sNamespaceURI = aMatcher.group (2);
65  
66        // Check if there is a known beautifier for this pair of namespace and
67        // local name
68        final String sBeautified = SVRLLocationBeautifierRegistry.getBeautifiedLocation (sNamespaceURI, sLocalName);
69        if (sBeautified != null)
70          sResult = StringHelper.replaceAll (sResult, aMatcher.group (), sBeautified);
71      }
72      return sResult;
73    }
74  
75    public AbstractSVRLMessage ()
76    {}
77  
78    public AbstractSVRLMessage (@Nullable final List <DiagnosticReference> aDiagnosticReferences,
79                                @Nullable final String sText,
80                                @Nullable final String sLocation,
81                                @Nullable final String sTest,
82                                @Nullable final String sRole,
83                                @Nullable final IErrorLevel aFlag)
84    {
85      m_aDiagnosticReferences = new CommonsArrayList <> (aDiagnosticReferences);
86      m_sText = sText;
87      m_sLocation = sLocation;
88      m_sTest = sTest;
89      m_sRole = sRole;
90      m_aFlag = aFlag;
91    }
92  
93    @Nonnull
94    @ReturnsMutableCopy
95    public ICommonsList <DiagnosticReference> getDiagnisticReferences ()
96    {
97      return m_aDiagnosticReferences.getClone ();
98    }
99  
100   @Nullable
101   public String getText ()
102   {
103     return m_sText;
104   }
105 
106   @Nullable
107   public String getLocation ()
108   {
109     return m_sLocation;
110   }
111 
112   @Nullable
113   public String getTest ()
114   {
115     return m_sTest;
116   }
117 
118   @Nullable
119   public String getRole ()
120   {
121     return m_sRole;
122   }
123 
124   @Nonnull
125   public IErrorLevel getFlag ()
126   {
127     return m_aFlag;
128   }
129 
130   @Nonnull
131   public SVRLResourceError getAsResourceError (@Nullable final String sResourceName)
132   {
133     return new SVRLErrorBuilder (m_sTest).setErrorLevel (m_aFlag)
134                                          .setErrorFieldName (m_sLocation)
135                                          .setErrorLocation (new ErrorLocation (sResourceName))
136                                          .setErrorText (m_sText)
137                                          .build ();
138   }
139 
140   @Override
141   public String toString ()
142   {
143     return new ToStringGenerator (this).append ("diagnosticRefs", m_aDiagnosticReferences)
144                                        .append ("text", m_sText)
145                                        .append ("location", m_sLocation)
146                                        .append ("test", m_sTest)
147                                        .appendIfNotNull ("role", m_sRole)
148                                        .append ("flag", m_aFlag)
149                                        .getToString ();
150   }
151 }