View Javadoc
1   /**
2    * Copyright (C) 2014-2016 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.IErrorLevel;
31  import com.helger.commons.error.ResourceLocation;
32  import com.helger.commons.regex.RegExHelper;
33  import com.helger.commons.string.StringHelper;
34  import com.helger.commons.string.ToStringGenerator;
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
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    @Nonnull
52    protected static String getBeautifiedLocation (@Nonnull final String sLocation)
53    {
54      String sResult = sLocation;
55      // Handle namespaces:
56      // Search for "*:xx[namespace-uri()='yy']" where xx is the localname and yy
57      // is the namespace URI
58      final Matcher aMatcher = RegExHelper.getMatcher ("\\Q*:\\E([a-zA-Z0-9_]+)\\Q[namespace-uri()='\\E([^']+)\\Q']\\E",
59                                                sResult);
60      while (aMatcher.find ())
61      {
62        final String sLocalName = aMatcher.group (1);
63        final String sNamespaceURI = aMatcher.group (2);
64  
65        // Check if there is a known beautifier for this pair of namespace and
66        // local name
67        final String sBeautified = SVRLLocationBeautifierRegistry.getBeautifiedLocation (sNamespaceURI, sLocalName);
68        if (sBeautified != null)
69          sResult = StringHelper.replaceAll (sResult, aMatcher.group (), sBeautified);
70      }
71      return sResult;
72    }
73  
74    public AbstractSVRLMessage ()
75    {}
76  
77    public AbstractSVRLMessage (@Nullable final List <DiagnosticReference> aDiagnosticReferences,
78                                @Nullable final String sText,
79                                @Nullable final String sLocation,
80                                @Nullable final String sTest,
81                                @Nullable final String sRole,
82                                @Nullable final IErrorLevel aFlag)
83    {
84      m_aDiagnosticReferences = new CommonsArrayList <> (aDiagnosticReferences);
85      m_sText = sText;
86      m_sLocation = sLocation;
87      m_sTest = sTest;
88      m_sRole = sRole;
89      m_aFlag = aFlag;
90    }
91  
92    @Nonnull
93    @ReturnsMutableCopy
94    public ICommonsList <DiagnosticReference> getDiagnisticReferences ()
95    {
96      return m_aDiagnosticReferences.getClone ();
97    }
98  
99    @Nullable
100   public String getText ()
101   {
102     return m_sText;
103   }
104 
105   @Nullable
106   public String getLocation ()
107   {
108     return m_sLocation;
109   }
110 
111   @Nullable
112   public String getTest ()
113   {
114     return m_sTest;
115   }
116 
117   @Nullable
118   public String getRole ()
119   {
120     return m_sRole;
121   }
122 
123   @Nonnull
124   public IErrorLevel getFlag ()
125   {
126     return m_aFlag;
127   }
128 
129   @Nonnull
130   public SVRLResourceError getAsResourceError (@Nullable final String sResourceName)
131   {
132     return new SVRLResourceError (new ResourceLocation (sResourceName, m_sLocation), m_aFlag, m_sText, m_sTest);
133   }
134 
135   @Override
136   public String toString ()
137   {
138     return new ToStringGenerator (this).append ("diagnosticRefs", m_aDiagnosticReferences)
139                                        .append ("text", m_sText)
140                                        .append ("location", m_sLocation)
141                                        .append ("test", m_sTest)
142                                        .appendIfNotNull ("role", m_sRole)
143                                        .append ("flag", m_aFlag)
144                                        .toString ();
145   }
146 }