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.resolve;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  
22  import javax.annotation.Nonnull;
23  import javax.annotation.Nullable;
24  
25  import com.helger.commons.annotation.Nonempty;
26  import com.helger.commons.io.resource.IReadableResource;
27  import com.helger.commons.string.ToStringGenerator;
28  import com.helger.commons.xml.ls.SimpleLSResourceResolver;
29  
30  /**
31   * The default implementation of {@link ISchematronIncludeResolver} using the
32   * {@link SimpleLSResourceResolver#doStandardResourceResolving(String, String)}
33   * method internally.
34   *
35   * @author Philip Helger
36   */
37  public class DefaultSchematronIncludeResolver implements ISchematronIncludeResolver
38  {
39    private final String m_sBaseHref;
40  
41    @Nullable
42    private static String _getBaseHref (@Nonnull final IReadableResource aResource)
43    {
44      final URL aURL = aResource.getAsURL ();
45      return aURL == null ? null : aURL.toExternalForm ();
46    }
47  
48    public DefaultSchematronIncludeResolver (@Nonnull final IReadableResource aResource)
49    {
50      this (_getBaseHref (aResource));
51    }
52  
53    public DefaultSchematronIncludeResolver (@Nullable final String sBaseHref)
54    {
55      m_sBaseHref = sBaseHref;
56    }
57  
58    @Nullable
59    public String getBaseHref ()
60    {
61      return m_sBaseHref;
62    }
63  
64    @Nonnull
65    public IReadableResource getResolvedSchematronResource (@Nonnull @Nonempty final String sHref) throws IOException
66    {
67      return SimpleLSResourceResolver.doStandardResourceResolving (sHref, getBaseHref ());
68    }
69  
70    @Override
71    public String toString ()
72    {
73      return new ToStringGenerator (this).append ("baseHref", m_sBaseHref).toString ();
74    }
75  }