@Test(expected = IllegalArgumentException.class) public void namespaceContextWontReturnPrefixForNullURI() { NamespaceContext ctx = Convert.toNamespaceContext(new HashMap<String, String>()); ctx.getPrefix(null); }
public static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI) { final Map<String, String> copy = new LinkedHashMap<String, String>(prefix2URI); return new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix == null) { throw new IllegalArgumentException("prefix must not be null"); } if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { return XMLConstants.XML_NS_URI; } if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } String uri = copy.get(prefix); return uri != null ? uri : XMLConstants.NULL_NS_URI; } public String getPrefix(String uri) { Iterator i = getPrefixes(uri); return i.hasNext() ? (String) i.next() : null; } public Iterator getPrefixes(String uri) { if (uri == null) { throw new IllegalArgumentException("uri must not be null"); } Collection<String> c = new LinkedHashSet<String>(); boolean done = false; if (XMLConstants.XML_NS_URI.equals(uri)) { c.add(XMLConstants.XML_NS_PREFIX); done = true; } if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { c.add(XMLConstants.XMLNS_ATTRIBUTE); done = true; } if (!done) { for (Map.Entry<String, String> entry : copy.entrySet()) { if (uri.equals(entry.getValue())) { c.add(entry.getKey()); } } } return c.iterator(); } }; }
Convert { public static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI) { final Map<String, String> copy = new LinkedHashMap<String, String>(prefix2URI); return new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix == null) { throw new IllegalArgumentException("prefix must not be null"); } if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { return XMLConstants.XML_NS_URI; } if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } String uri = copy.get(prefix); return uri != null ? uri : XMLConstants.NULL_NS_URI; } public String getPrefix(String uri) { Iterator i = getPrefixes(uri); return i.hasNext() ? (String) i.next() : null; } public Iterator getPrefixes(String uri) { if (uri == null) { throw new IllegalArgumentException("uri must not be null"); } Collection<String> c = new LinkedHashSet<String>(); boolean done = false; if (XMLConstants.XML_NS_URI.equals(uri)) { c.add(XMLConstants.XML_NS_PREFIX); done = true; } if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { c.add(XMLConstants.XMLNS_ATTRIBUTE); done = true; } if (!done) { for (Map.Entry<String, String> entry : copy.entrySet()) { if (uri.equals(entry.getValue())) { c.add(entry.getKey()); } } } return c.iterator(); } }; } }
Convert { public static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI) { final Map<String, String> copy = new LinkedHashMap<String, String>(prefix2URI); return new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix == null) { throw new IllegalArgumentException("prefix must not be null"); } if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { return XMLConstants.XML_NS_URI; } if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } String uri = copy.get(prefix); return uri != null ? uri : XMLConstants.NULL_NS_URI; } public String getPrefix(String uri) { Iterator i = getPrefixes(uri); return i.hasNext() ? (String) i.next() : null; } public Iterator getPrefixes(String uri) { if (uri == null) { throw new IllegalArgumentException("uri must not be null"); } Collection<String> c = new LinkedHashSet<String>(); boolean done = false; if (XMLConstants.XML_NS_URI.equals(uri)) { c.add(XMLConstants.XML_NS_PREFIX); done = true; } if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { c.add(XMLConstants.XMLNS_ATTRIBUTE); done = true; } if (!done) { for (Map.Entry<String, String> entry : copy.entrySet()) { if (uri.equals(entry.getValue())) { c.add(entry.getKey()); } } } return c.iterator(); } }; } private  Convert(); }
Convert { public static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI) { final Map<String, String> copy = new LinkedHashMap<String, String>(prefix2URI); return new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix == null) { throw new IllegalArgumentException("prefix must not be null"); } if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { return XMLConstants.XML_NS_URI; } if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } String uri = copy.get(prefix); return uri != null ? uri : XMLConstants.NULL_NS_URI; } public String getPrefix(String uri) { Iterator i = getPrefixes(uri); return i.hasNext() ? (String) i.next() : null; } public Iterator getPrefixes(String uri) { if (uri == null) { throw new IllegalArgumentException("uri must not be null"); } Collection<String> c = new LinkedHashSet<String>(); boolean done = false; if (XMLConstants.XML_NS_URI.equals(uri)) { c.add(XMLConstants.XML_NS_PREFIX); done = true; } if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { c.add(XMLConstants.XMLNS_ATTRIBUTE); done = true; } if (!done) { for (Map.Entry<String, String> entry : copy.entrySet()) { if (uri.equals(entry.getValue())) { c.add(entry.getKey()); } } } return c.iterator(); } }; } private  Convert(); static InputSource toInputSource(Source s); static InputSource toInputSource(Source s, TransformerFactory fac); static Document toDocument(Source s); static Document toDocument(Source s,
                                      DocumentBuilderFactory factory); static Node toNode(Source s); static Node toNode(Source s,
                              DocumentBuilderFactory factory); static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI); }
Convert { public static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI) { final Map<String, String> copy = new LinkedHashMap<String, String>(prefix2URI); return new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix == null) { throw new IllegalArgumentException("prefix must not be null"); } if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { return XMLConstants.XML_NS_URI; } if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } String uri = copy.get(prefix); return uri != null ? uri : XMLConstants.NULL_NS_URI; } public String getPrefix(String uri) { Iterator i = getPrefixes(uri); return i.hasNext() ? (String) i.next() : null; } public Iterator getPrefixes(String uri) { if (uri == null) { throw new IllegalArgumentException("uri must not be null"); } Collection<String> c = new LinkedHashSet<String>(); boolean done = false; if (XMLConstants.XML_NS_URI.equals(uri)) { c.add(XMLConstants.XML_NS_PREFIX); done = true; } if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { c.add(XMLConstants.XMLNS_ATTRIBUTE); done = true; } if (!done) { for (Map.Entry<String, String> entry : copy.entrySet()) { if (uri.equals(entry.getValue())) { c.add(entry.getKey()); } } } return c.iterator(); } }; } private  Convert(); static InputSource toInputSource(Source s); static InputSource toInputSource(Source s, TransformerFactory fac); static Document toDocument(Source s); static Document toDocument(Source s,
                                      DocumentBuilderFactory factory); static Node toNode(Source s); static Node toNode(Source s,
                              DocumentBuilderFactory factory); static NamespaceContext toNamespaceContext(Map<String, String> prefix2URI);  }