/** * Bind the given namespace as default namespace. * @param namespaceUri the namespace uri */ public void bindDefaultNamespaceUri(String namespaceUri) { bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri); }
/** * Bind the given namespace as default namespace. * @param namespaceUri the namespace uri */ public void bindDefaultNamespaceUri(String namespaceUri) { bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri); }
@Test(expected = IllegalArgumentException.class) public void bindNamespaceUri_withNullNamespaceUri() { context.bindNamespaceUri("prefix", null); }
@Test(expected = IllegalArgumentException.class) public void bindNamespaceUri_withNullPrefix() { context.bindNamespaceUri(null, namespaceUri); }
@Test public void getBoundPrefixes() { context.bindNamespaceUri("prefix1", namespaceUri); context.bindNamespaceUri("prefix2", namespaceUri); context.bindNamespaceUri("prefix3", additionalNamespaceUri); assertThat("Returns all bound prefixes", getItemSet(context.getBoundPrefixes()), is(makeSet("prefix1", "prefix2", "prefix3"))); }
@Test public void clear() { context.bindNamespaceUri("prefix1", namespaceUri); context.bindNamespaceUri("prefix2", namespaceUri); context.bindNamespaceUri("prefix3", additionalNamespaceUri); context.clear(); assertThat("All bound prefixes were removed", context.getBoundPrefixes().hasNext(), is(false)); assertThat("All bound namespace URIs were removed", context.getPrefixes(namespaceUri).hasNext(), is(false)); }
@Test(expected = UnsupportedOperationException.class) public void getPrefixes_IteratorIsNotModifiable() throws Exception { context.bindNamespaceUri(prefix, namespaceUri); Iterator<String> iterator = context.getPrefixes(namespaceUri); iterator.remove(); }
@Test public void getPrefix() { assertThat("Always returns \"xmlns\" for \"http://www.w3.org/2000/xmlns/\"", context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI), is(XMLConstants.XMLNS_ATTRIBUTE)); assertThat("Always returns \"xml\" for \"http://www.w3.org/XML/1998/namespace\"", context.getPrefix(XMLConstants.XML_NS_URI), is(XMLConstants.XML_NS_PREFIX)); assertThat("Returns null for an unbound namespace URI", context.getPrefix(unboundNamespaceUri), is(nullValue())); context.bindNamespaceUri("prefix1", namespaceUri); context.bindNamespaceUri("prefix2", namespaceUri); assertThat("Returns a prefix for a bound namespace URI", context.getPrefix(namespaceUri), anyOf(is("prefix1"), is("prefix2"))); }
@Test public void getNamespaceURI() { context.bindNamespaceUri(XMLConstants.XMLNS_ATTRIBUTE, additionalNamespaceUri); assertThat("Always returns \"http://www.w3.org/2000/xmlns/\" for \"xmlns\"", context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE), is(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)); context.bindNamespaceUri(XMLConstants.XML_NS_PREFIX, additionalNamespaceUri); assertThat("Always returns \"http://www.w3.org/XML/1998/namespace\" for \"xml\"", context.getNamespaceURI(XMLConstants.XML_NS_PREFIX), is(XMLConstants.XML_NS_URI)); assertThat("Returns \"\" for an unbound prefix", context.getNamespaceURI(unboundPrefix), is(XMLConstants.NULL_NS_URI)); context.bindNamespaceUri(prefix, namespaceUri); assertThat("Returns the bound namespace URI for a bound prefix", context.getNamespaceURI(prefix), is(namespaceUri)); assertThat("By default returns URI \"\" for the default namespace prefix", context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX), is(XMLConstants.NULL_NS_URI)); context.bindDefaultNamespaceUri(defaultNamespaceUri); assertThat("Returns the set URI for the default namespace prefix", context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX), is(defaultNamespaceUri)); }
@Test public void removeBinding() { context.removeBinding(unboundPrefix); context.bindNamespaceUri(prefix, namespaceUri); context.removeBinding(prefix); assertThat("Returns default namespace URI for removed prefix", context.getNamespaceURI(prefix), is(XMLConstants.NULL_NS_URI)); assertThat("#getPrefix returns null when all prefixes for a namespace URI were removed", context.getPrefix(namespaceUri), is(nullValue())); assertThat("#getPrefixes returns an empty iterator when all prefixes for a namespace URI were removed", context.getPrefixes(namespaceUri).hasNext(), is(false)); context.bindNamespaceUri("prefix1", additionalNamespaceUri); context.bindNamespaceUri("prefix2", additionalNamespaceUri); context.removeBinding("prefix1"); assertThat("Prefix was unbound", context.getNamespaceURI("prefix1"), is(XMLConstants.NULL_NS_URI)); assertThat("#getPrefix returns a bound prefix after removal of another prefix for the same namespace URI", context.getPrefix(additionalNamespaceUri), is("prefix2")); assertThat("Prefix was removed from namespace URI", getItemSet(context.getPrefixes(additionalNamespaceUri)), is(makeSet("prefix2"))); }
@Test public void getPrefixes() { assertThat("Returns only \"xmlns\" for \"http://www.w3.org/2000/xmlns/\"", getItemSet(context.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)), is(makeSet(XMLConstants.XMLNS_ATTRIBUTE))); assertThat("Returns only \"xml\" for \"http://www.w3.org/XML/1998/namespace\"", getItemSet(context.getPrefixes(XMLConstants.XML_NS_URI)), is(makeSet(XMLConstants.XML_NS_PREFIX))); assertThat("Returns empty iterator for unbound prefix", context.getPrefixes("unbound Namespace URI").hasNext(), is(false)); context.bindNamespaceUri("prefix1", namespaceUri); context.bindNamespaceUri("prefix2", namespaceUri); assertThat("Returns all prefixes (and only those) bound to the namespace URI", getItemSet(context.getPrefixes(namespaceUri)), is(makeSet("prefix1", "prefix2"))); }
@Test public void bindNamespaceUri() { context.bindNamespaceUri(prefix, namespaceUri); assertThat("The Namespace URI was bound to the prefix", context.getNamespaceURI(prefix), is(namespaceUri)); assertThat("The prefix was bound to the namespace URI", getItemSet(context.getPrefixes(namespaceUri)), hasItem(prefix)); }
/** * Binds the given namespace as default namespace. * * @param namespaceUri the namespace uri */ public void bindDefaultNamespaceUri(String namespaceUri) { bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri); }
/** * Binds the given prefix to the given namespaces. * * @see SimpleNamespaceContext#bindNamespaceUri(String,String) */ public final void startPrefixMapping(String prefix, String uri) { namespaceContext.bindNamespaceUri(prefix, uri); namespaceContextChanged = true; }
/** * Sets the bindings for this namespace context. The supplied map must consist of string key value pairs. * * @param bindings the bindings */ public void setBindings(Map<String, String> bindings) { for (Map.Entry<String, String> entry : bindings.entrySet()) { bindNamespaceUri(entry.getKey(), entry.getValue()); } }
/** * Bind the given namespace as default namespace. * @param namespaceUri the namespace uri */ public void bindDefaultNamespaceUri(String namespaceUri) { bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri); }
public XpathQueryRunner(byte[] originalSource) { this.originalSource = originalSource; this.xPath = XPathFactory.newInstance().newXPath(); SimpleNamespaceContext nc = new SimpleNamespaceContext(); nc.bindNamespaceUri("wsdl", "http://schemas.xmlsoap.org/wsdl/"); nc.bindNamespaceUri("xsd", "http://www.w3.org/2001/XMLSchema"); this.xPath.setNamespaceContext(nc); }
private static void bindUnboundedNamespaces(SimpleNamespaceContext nsContext, Map<String, String> namespaces) { for (Map.Entry<String, String> entry : namespaces.entrySet()) { //making sure that namespace is not already bound. Otherwise UnsupportedException happens if(nsContext.getPrefix(entry.getValue()) == null) { nsContext.bindNamespaceUri(entry.getKey(), entry.getValue()); } } }
private static void bindUnboundedNamespaces(SimpleNamespaceContext nsContext, Map<String, String> namespaces) { for (Map.Entry<String, String> entry : namespaces.entrySet()) { //making sure that namespace is not already bound. Otherwise UnsupportedException happens if(nsContext.getPrefix(entry.getValue()) == null) { nsContext.bindNamespaceUri(entry.getKey(), entry.getValue()); } } }
void initXPath(XPath xpath) { SimpleNamespaceContext ctx = new SimpleNamespaceContext(); ctx.bindNamespaceUri("kml", "http://www.opengis.net/kml/2.2"); xpath.setNamespaceContext(ctx); }