StaticNamespaceContext implements NamespaceContext { @Override public Iterator<String> getPrefixes(String namespaceURI) { Set<String> prefixes = new HashSet<String>(); for (Entry<String, String> entry : this.nsMapping.entrySet()) { if (entry.getValue().equals(namespaceURI)) { prefixes.add(entry.getKey()); } } return prefixes.iterator(); } StaticNamespaceContext(); void addMapping(String prefix, String namespace); void removeMapping(String prefix); @Override String getNamespaceURI(String prefix); @Override String getPrefix(String namespaceURI); @Override Iterator<String> getPrefixes(String namespaceURI);  }
@Test public void testGetPrefixes() { StaticNamespaceContext ctx = new StaticNamespaceContext(); ctx.addMapping("ns1", "urn:ns1"); ctx.addMapping("ns1-1", "urn:ns1"); ctx.addMapping("ns2", "urn:ns2"); ctx.addMapping("ns3", "urn:ns3"); Assert.assertTrue(ctx.getPrefixes("urn:ns2").hasNext()); Assert.assertTrue(ctx.getPrefixes("urn:ns3").hasNext()); Assert.assertFalse(ctx.getPrefixes("urn:ns4").hasNext()); Iterator<String> iter1 = ctx.getPrefixes("urn:ns1"); Assert.assertTrue(iter1.hasNext()); Assert.assertTrue(iter1.next().startsWith("ns1")); Assert.assertTrue(iter1.hasNext()); Assert.assertTrue(iter1.next().startsWith("ns1")); }