1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }