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