View Javadoc
1   /**
2    * Copyright (C) 2014-2018 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.saxon;
18  
19  import java.util.Iterator;
20  
21  import javax.annotation.Nonnull;
22  import javax.annotation.Nullable;
23  import javax.xml.namespace.NamespaceContext;
24  
25  import com.helger.commons.ValueEnforcer;
26  import com.helger.commons.annotation.DevelopersNote;
27  import com.helger.commons.collection.impl.CommonsArrayList;
28  import com.helger.commons.collection.impl.ICommonsList;
29  import com.helger.commons.string.StringHelper;
30  import com.helger.xml.namespace.MapBasedNamespaceContext;
31  
32  import net.sf.saxon.om.NamespaceResolver;
33  
34  public final class SaxonNamespaceContext implements NamespaceContext, NamespaceResolver
35  {
36    private final MapBasedNamespaceContext m_aCtx;
37  
38    public SaxonNamespaceContext (@Nonnull final MapBasedNamespaceContext aCtx)
39    {
40      m_aCtx = ValueEnforcer.notNull (aCtx, "Ctx");
41    }
42  
43    @Nullable
44    public String getURIForPrefix (@Nullable final String sPrefix, final boolean bUseDefault)
45    {
46      if (bUseDefault && StringHelper.hasNoText (sPrefix))
47        return m_aCtx.getDefaultNamespaceURI ();
48      return m_aCtx.getCustomNamespaceURI (sPrefix);
49    }
50  
51    @Nonnull
52    public Iterator <String> iteratePrefixes ()
53    {
54      final ICommonsList <String> aList = new CommonsArrayList <> (m_aCtx.getPrefixToNamespaceURIMap ().keySet ());
55      aList.add ("");
56      return aList.iterator ();
57    }
58  
59    @Nonnull
60    public String getNamespaceURI (@Nonnull final String sPrefix)
61    {
62      return m_aCtx.getNamespaceURI (sPrefix);
63    }
64  
65    @Nullable
66    public String getPrefix (@Nonnull final String sNamespaceURI)
67    {
68      return m_aCtx.getPrefix (sNamespaceURI);
69    }
70  
71    @Nonnull
72    @DevelopersNote ("Java 8: Iterator; Java 10: Iterator<String>")
73    public Iterator getPrefixes (@Nonnull final String sNamespaceURI)
74    {
75      return m_aCtx.getPrefixes (sNamespaceURI);
76    }
77  }