Package javax.xml.namespace

Examples of javax.xml.namespace.NamespaceContext

All get*(*) methods operate in the current scope for Namespace URI and prefix resolution.

Note that a Namespace URI can be bound to multiple prefixes in the current scope. This can occur when multiple XMLConstants.XMLNS_ATTRIBUTE ("xmlns") Namespace declarations occur in the same Start-Tag and refer to the same Namespace URI. e.g.

 <element xmlns:prefix1="http://Namespace-name-URI" xmlns:prefix2="http://Namespace-name-URI"> 

This can also occur when the same Namespace URI is used in multiple XMLConstants.XMLNS_ATTRIBUTE ("xmlns") Namespace declarations in the logical parent element hierarchy. e.g.
 <parent xmlns:prefix1="http://Namespace-name-URI"> <child xmlns:prefix2="http://Namespace-name-URI"> ... </child> </parent> 

A prefix can only be bound to a single Namespace URI in the current scope.

@author JAXP Java Community Process @author JAXP Reference Implementation @version 1.0.proposed @see javax.xml.XMLConstants declarations of common XML values @see Namespaces in XML @see Namespaces in XML Errata @see XML Schema Part2: Datatypes

        assertEquals(1, sr.getNamespaceCount());
        assertEquals("ns", sr.getNamespacePrefix(0));
        assertEquals("http://foo", sr.getNamespaceURI(0));

        NamespaceContext nsCtxt = sr.getNamespaceContext();
        assertNotNull(nsCtxt);
        /* 28-Apr-2006, TSa: Alas, namespace access is only fully
         *   implemented in DOM Level 3 (JDK 1.5+)... thus, can't check:
         */
        /*
 
View Full Code Here


            +"</root>";
        XMLStreamReader2 sr = getNsReader(XML, true);

        assertEquals(START_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        NamespaceContext curr = sr.getNamespaceContext();
        assertNotNull(curr);
        checkValidityOfNs1(curr);
        NamespaceContext nc1 = sr.getNonTransientNamespaceContext();
        assertNotNull(nc1);
        checkValidityOfNs1(nc1);

        assertEquals(START_ELEMENT, sr.next());
        assertEquals("branch", sr.getLocalName());
View Full Code Here

        // Ok, got the start element... is it ok?
        assertTrue(evt.isStartElement());
        StartElement se = evt.asStartElement();
        testEventWritability(se);

        NamespaceContext nsCtxt = se.getNamespaceContext();

        assertNotNull("StartElement.getNamespaceContext() should never return null", nsCtxt);
        // First, ones we shouldn't get:
        assertNull(nsCtxt.getPrefix("a"));
        assertNull(nsCtxt.getPrefix("http://foobar"));
        assertNull(nsCtxt.getNamespaceURI("b"));
        assertNull(nsCtxt.getNamespaceURI("http://my"));

        {
            Iterator it = nsCtxt.getPrefixes("http://foobar");
            // Specs don't specify if we should get null, or empty iterator
            assertTrue((it == null) || !it.hasNext());
            it = nsCtxt.getPrefixes("a");
            assertTrue((it == null) || !it.hasNext());
        }
        // Then ones we should:

        assertEquals("a", nsCtxt.getPrefix("ns:attrs"));
        assertEquals("", nsCtxt.getPrefix("http://my"));
        assertEquals("http://my", nsCtxt.getNamespaceURI(""));
        assertEquals("ns:attrs", nsCtxt.getNamespaceURI("a"));

        // Plus, let's check the other namespace access:
        Iterator it = se.getNamespaces();
        assertEquals(2, countElements(it));
View Full Code Here

        StartElement se = evt.asStartElement();
        testEventWritability(se);

        // Let's first check that it has 1 declaration:
        assertEquals(0, countElements(se.getNamespaces()));
        NamespaceContext nsCtxt = se.getNamespaceContext();
        assertNotNull("StartElement.getNamespaceContext() should never return null", nsCtxt);
        assertNull(nsCtxt.getPrefix("a"));
        assertNull(nsCtxt.getNamespaceURI("b"));

        // then first leaf:
        evt = er.nextEvent();
        assertTrue(evt.isStartElement());
        se = evt.asStartElement();
        assertEquals("leaf", se.getName().getLocalPart());
        assertEquals(1, countElements(se.getNamespaces()));
        assertEquals("x", se.getName().getNamespaceURI());
        nsCtxt = se.getNamespaceContext();
        assertEquals("x", nsCtxt.getNamespaceURI(""));
        assertEquals("", nsCtxt.getPrefix("x"));
        testEventWritability(se);

        evt = er.nextEvent();
        assertTrue(evt.isEndElement());
        testEventWritability(evt);

        // Ok, branch:
        evt = er.nextEvent();
        assertTrue(evt.isStartElement());
        se = evt.asStartElement();
        assertEquals("branch", se.getName().getLocalPart());
        assertEquals(2, countElements(se.getNamespaces()));
        nsCtxt = se.getNamespaceContext();
        assertEquals("a", nsCtxt.getPrefix("b"));
        assertEquals("b", nsCtxt.getNamespaceURI("a"));
        assertEquals("x", nsCtxt.getPrefix("url"));
        assertEquals("url", nsCtxt.getNamespaceURI("x"));
        testEventWritability(se);

        // second leaf
        evt = er.nextEvent();
        assertTrue(evt.isStartElement());
        se = evt.asStartElement();
        nsCtxt = se.getNamespaceContext();
        assertEquals("leaf", se.getName().getLocalPart());
        // only one declared in this particular element
        assertEquals(1, countElements(se.getNamespaces()));
        // but should still show the other bound ns from parent
        nsCtxt = se.getNamespaceContext();
        assertEquals("a", nsCtxt.getPrefix("c"));
        assertEquals("c", nsCtxt.getNamespaceURI("a"));
        assertEquals("x", nsCtxt.getPrefix("url"));
        assertEquals("url", nsCtxt.getNamespaceURI("x"));
        // ok, but how about masking:
        assertNull(nsCtxt.getPrefix("b"));

        // Ok, fine... others we don't care about:
        assertTrue(er.nextEvent().isEndElement());
    }
View Full Code Here

        evt = er.nextEvent(); // branch
        assertTokenType(START_ELEMENT, evt.getEventType());
        se = evt.asStartElement();
        assertEquals("branch", se.getName().getLocalPart());
        assertEquals(1, countElements(se.getNamespaces()));
        NamespaceContext nsCtxt = se.getNamespaceContext();
        assertNotNull("StartElement.getNamespaceContext() should never return null", nsCtxt);
        assertEquals("url", nsCtxt.getNamespaceURI("a"));
        assertEquals("a", nsCtxt.getPrefix("url"));

        evt = er.nextEvent(); // leaf
        assertTokenType(START_ELEMENT, evt.getEventType());
        se = evt.asStartElement();
        assertEquals("leaf", se.getName().getLocalPart());
        assertEquals(0, countElements(se.getNamespaces()));
        nsCtxt = se.getNamespaceContext();
        assertEquals("url", nsCtxt.getNamespaceURI("a"));
        assertEquals("a", nsCtxt.getPrefix("url"));

        assertTrue(er.nextEvent().isEndElement()); // /leaf
        assertTrue(er.nextEvent().isEndElement()); // /branch
        assertTrue(er.nextEvent().isEndElement()); // /root
    }
View Full Code Here

                    return be;
                }
                /* Ok, not woodstox impl, will be bit more work (plus less
                 * efficient, and may miss some info)... but can be done.
                 */
                NamespaceContext nsCtxt = null;
                if (r instanceof XMLStreamReader2) {
                    nsCtxt = ((XMLStreamReader2) r).getNonTransientNamespaceContext();
                }
                Map<QName,Attribute> attrs;
                {
View Full Code Here

    }

    protected StartElement createStartElement(XMLStreamReader r, Location loc)
        throws XMLStreamException
    {
        NamespaceContext nsCtxt = null;
        /* Note: there's no way to get non-transient namespace context via
         * Stax 1.0 interface -- the context you can access from reader
         * remains stable only during current event.
         */
        if (r instanceof XMLStreamReader2) {
View Full Code Here

        String prefix;
        // Ok as is? In repairing mode need to ensure it's properly bound
        if (mNsRepairing) {
            String uri = name.getNamespaceURI();
            // First: let's see if a valid binding already exists:
            NamespaceContext ctxt = getNamespaceContext();
            prefix = (ctxt == null) ? null : ctxt.getPrefix(uri);
            if (prefix == null) {
                // nope: need to (try to) bind
                String origPrefix = name.getPrefix();
                if (origPrefix == null || origPrefix.length() == 0) {
                    prefix = "";
View Full Code Here

         */
        String xsiTypeQname = root.getAttributeValue(SOAPConstants.XSI_NS, "type");
        if (xsiTypeQname != null) {
            Matcher m = QNAME_PATTERN.matcher(xsiTypeQname);
            if (m.matches()) {
                NamespaceContext nc = root.getNamespaceContext();
                this.xsiType = new QName(nc.getNamespaceURI(m.group(1)), m.group(2), m.group(1));
            } else {
                this.xsiType = new QName(this.namespace, xsiTypeQname, "");
            }
        }
    }
View Full Code Here

        if (xmlnsSchemeIndex >= 0) {
            xmlnsSchemeIndex += xmlnsSchemeString.length();
            int xmlnsSchemeEndIndex = this.findBalancedEndIndex(xpointer, xmlnsSchemeIndex, '(', ')');
            String namespaceScheme = xpointer.substring(xmlnsSchemeIndex, xmlnsSchemeEndIndex);
            final String[] namespaceSplit = namespaceScheme.split("=");
            xPath.setNamespaceContext(new NamespaceContext() {
                @Override
                public String getNamespaceURI(String prefix) {
                    if (prefix.equals(namespaceSplit[0])) {
                        return namespaceSplit[1];
                    }
View Full Code Here

TOP

Related Classes of javax.xml.namespace.NamespaceContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.