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 javax.annotation.Nonnull;
20  import javax.annotation.Nullable;
21  import javax.annotation.concurrent.NotThreadSafe;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import com.helger.commons.annotation.PresentForCodeCoverage;
27  import com.helger.commons.collection.ext.ICommonsList;
28  import com.helger.commons.debug.GlobalDebug;
29  import com.helger.commons.lang.ServiceLoaderHelper;
30  
31  /**
32   * A central registry for all {@link ISVRLLocationBeautifierSPI} instances.
33   *
34   * @author Philip Helger
35   */
36  @NotThreadSafe
37  public final class SVRLLocationBeautifierRegistry
38  {
39    private static final Logger s_aLogger = LoggerFactory.getLogger (SVRLLocationBeautifierRegistry.class);
40    private static final ICommonsList <ISVRLLocationBeautifierSPI> s_aList = ServiceLoaderHelper.getAllSPIImplementations (ISVRLLocationBeautifierSPI.class);
41  
42    @PresentForCodeCoverage
43    private static final SVRLLocationBeautifierRegistry s_aInstance = new SVRLLocationBeautifierRegistry ();
44  
45    private SVRLLocationBeautifierRegistry ()
46    {}
47  
48    /**
49     * Get the beautified location for the given namespace and local name.
50     *
51     * @param sNamespaceURI
52     *        The namespace URI
53     * @param sLocalName
54     *        The element local name
55     * @return <code>null</code> if no beautification is available
56     */
57    @Nullable
58    public static String getBeautifiedLocation (@Nonnull final String sNamespaceURI, @Nonnull final String sLocalName)
59    {
60      for (final ISVRLLocationBeautifierSPI aBeautifier : s_aList)
61      {
62        final String sBeautified = aBeautifier.getReplacementText (sNamespaceURI, sLocalName);
63        if (sBeautified != null)
64          return sBeautified;
65      }
66      if (GlobalDebug.isDebugMode ())
67        s_aLogger.warn ("Unsupported elements for beautification: " + sNamespaceURI + " -- " + sLocalName);
68      return null;
69    }
70  }