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.List;
20  import java.util.regex.Matcher;
21  
22  import javax.annotation.Nonnull;
23  import javax.annotation.Nullable;
24  import javax.annotation.concurrent.Immutable;
25  
26  import org.oclc.purl.dsdl.svrl.DiagnosticReference;
27  
28  import com.helger.commons.annotations.ReturnsMutableCopy;
29  import com.helger.commons.collections.ContainerHelper;
30  import com.helger.commons.error.EErrorLevel;
31  import com.helger.commons.error.ResourceLocation;
32  import com.helger.commons.regex.RegExHelper;
33  import com.helger.commons.string.ToStringGenerator;
34  
35  /**
36   * A wrapper around FailedAssert and SuccessfulReport with easier error level
37   * handling.
38   *
39   * @author Philip Helger
40   */
41  @Immutable
42  public abstract class AbstractSVRLMessage
43  {
44    protected List <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 EErrorLevel m_eFlag;
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 m = RegExHelper.getMatcher ("\\Q*:\\E([a-zA-Z0-9_]+)\\Q[namespace-uri()='\\E([^']+)\\Q']\\E", sResult);
59      while (m.find ())
60      {
61        final String sLocalName = m.group (1);
62        final String sNamespaceURI = m.group (2);
63  
64        // Check if there is a known beautifier for this pair of namespace and
65        // local name
66        final String sBeautified = SVRLLocationBeautifierRegistry.getBeautifiedLocation (sNamespaceURI, sLocalName);
67        if (sBeautified != null)
68          sResult = sResult.replace (m.group (), sBeautified);
69      }
70      return sResult;
71    }
72  
73    public AbstractSVRLMessage ()
74    {}
75  
76    public AbstractSVRLMessage (@Nullable final List <DiagnosticReference> aDiagnosticReferences,
77                                @Nullable final String sText,
78                                @Nullable final String sLocation,
79                                @Nullable final String sTest,
80                                @Nullable final String sRole,
81                                @Nullable final EErrorLevel eFlag)
82    {
83      m_aDiagnosticReferences = ContainerHelper.newList (aDiagnosticReferences);
84      m_sText = sText;
85      m_sLocation = sLocation;
86      m_sTest = sTest;
87      m_sRole = sRole;
88      m_eFlag = eFlag;
89    }
90  
91    @Nonnull
92    @ReturnsMutableCopy
93    public List <DiagnosticReference> getDiagnisticReferences ()
94    {
95      return ContainerHelper.newList (m_aDiagnosticReferences);
96    }
97  
98    @Nullable
99    public String getText ()
100   {
101     return m_sText;
102   }
103 
104   @Nullable
105   public String getLocation ()
106   {
107     return m_sLocation;
108   }
109 
110   @Nullable
111   public String getTest ()
112   {
113     return m_sTest;
114   }
115 
116   @Nullable
117   public String getRole ()
118   {
119     return m_sRole;
120   }
121 
122   @Nonnull
123   public EErrorLevel getFlag ()
124   {
125     return m_eFlag;
126   }
127 
128   @Nonnull
129   public SVRLResourceError getAsResourceError (@Nullable final String sResourceName)
130   {
131     return new SVRLResourceError (new ResourceLocation (sResourceName, m_sLocation), m_eFlag, m_sText, m_sTest);
132   }
133 
134   @Override
135   public String toString ()
136   {
137     return new ToStringGenerator (this).append ("diagnosticRefs", m_aDiagnosticReferences)
138                                        .append ("text", m_sText)
139                                        .append ("location", m_sLocation)
140                                        .append ("test", m_sTest)
141                                        .appendIfNotNull ("role", m_sRole)
142                                        .append ("flag", m_eFlag)
143                                        .toString ();
144   }
145 }