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

                    // Use the prefix already bound to the given uri
                    return prefix + ":" + qname.getLocalPart();
                } else {
                   
                    // Find an available prefix and bind it to the given uri
                    NamespaceContext nsc = writer.getNamespaceContext();
                    for (int i=1; ; i++) {
                        prefix = "ns" + i;
                        if (nsc.getNamespaceURI(prefix) == null) {
                            break;
                        }
                    }
                    writer.setPrefix(prefix, uri);
                    writer.writeNamespace(prefix, uri);
View Full Code Here


                if (prefix != null) {
                    return;
                } else {
                   
                    // Find an available prefix and bind it to the given uri
                    NamespaceContext nsc = writer.getNamespaceContext();
                    for (int i=1; ; i++) {
                        prefix = "ns" + i;
                        if (nsc.getNamespaceURI(prefix) == null) {
                            break;
                        }
                    }
                    writer.setPrefix(prefix, uri);
                }
View Full Code Here

        if (prefix != null) {
            return null;
        } else {

            // Find an available prefix and bind it to the given URI
            NamespaceContext nsc = writer.getNamespaceContext();
            for (int i=1; ; i++) {
                prefix = "ns" + i;
                if (nsc.getNamespaceURI(prefix) == null) {
                    break;
                }
            }
            writer.setPrefix(prefix, uri);
            return prefix;
View Full Code Here

      Element inlineSimpleType = XDOMUtil.getFirstChildElementNS(
          restrictionEl, XmlSchema.SCHEMA_NS, "simpleType");

      if (restrictionEl.hasAttribute("base")) {
        NamespaceContext ctx = NodeNamespaceContext.getNamespaceContext(restrictionEl);
        restriction.baseTypeName = getRefQName(restrictionEl
            .getAttribute("base"), ctx);
      } else if (inlineSimpleType != null) {

        restriction.baseType = handleSimpleType(schema,
View Full Code Here

    }

    public static String getUniquePrefix(XMLStreamWriter writer) {
        int n = 1;
       
        NamespaceContext nc = writer.getNamespaceContext();
        while (true) {
            String nsPrefix = "ns" + n;

            if (nc == null || nc.getNamespaceURI(nsPrefix) == null) {
                return nsPrefix;
            }

            n++;
        }
View Full Code Here

    protected void runTest() throws Throwable {
        InputStream in = TestGetNamespaceContext.class.getResourceAsStream("namespacecontext.xml");
        OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocumentElement();
        OMElement inner = root.getFirstElement().getFirstElement();
        NamespaceContext context = inner.getNamespaceContext(detached);
        assertEquals("urn:test2", context.getNamespaceURI("p"));
        assertEquals("urn:test3", context.getNamespaceURI("q"));
        assertEquals("urn:test3", context.getNamespaceURI("r"));
        assertEquals("urn:test4", context.getNamespaceURI(""));
        assertEquals("", context.getNamespaceURI("unbound"));
       
        assertNull(context.getPrefix("urn:test1"));
        assertEquals("p", context.getPrefix("urn:test2"));
        String prefix = context.getPrefix("urn:test3");
        assertTrue(prefix.equals("q") || prefix.equals("r"));
        assertEquals("", context.getPrefix("urn:test4"));
        assertNull(context.getPrefix("unbound"));
       
        Iterator it = context.getPrefixes("urn:test1");
        assertFalse(it.hasNext());
       
        it = context.getPrefixes("urn:test2");
        assertTrue(it.hasNext());
        assertEquals("p", it.next());
        assertFalse(it.hasNext());

        it = context.getPrefixes("urn:test3");
        Set prefixes = new HashSet();
        while (it.hasNext()) {
            prefixes.add(it.next());
        }
        assertEquals(2, prefixes.size());
View Full Code Here

        XMLStreamReader stream = cache ? element.getXMLStreamReader()
                : element.getXMLStreamReaderWithoutCaching();
        stream.next();
        assertEquals(XMLStreamReader.START_ELEMENT, stream.next());
        assertEquals("b", stream.getLocalName());
        NamespaceContext context = stream.getNamespaceContext();
        assertEquals("urn:ns1", context.getNamespaceURI(""));
        assertEquals("urn:ns2", context.getNamespaceURI("ns2"));
        assertEquals("urn:ns3", context.getNamespaceURI("ns3"));
        assertEquals("ns2", context.getPrefix("urn:ns2"));
        element.close(false);
    }
View Full Code Here

    public QName getName() {
        return state == STATE_PASS_THROUGH ? parent.getName() : XOPConstants.INCLUDE_QNAME;
    }

    public NamespaceContext getNamespaceContext() {
        NamespaceContext ctx = parent.getNamespaceContext();
        if (state != STATE_PASS_THROUGH) {
            ctx = new NamespaceContextWrapper(ctx);
        }
        return ctx;
    }
View Full Code Here

                        }
                    }
                    depth++;
                }
                if (depth > 0) {
                    NamespaceContext nc = reader.getNamespaceContext();
                    for (Iterator it = prefixes.iterator(); it.hasNext(); ) {
                        String prefix = (String)it.next();
                        String expectedUri = refNc.getNamespaceURI(prefix);
                        String actualUri = nc.getNamespaceURI(prefix);
                        assertEquals("Namespace URI for prefix '" + prefix + "'", expectedUri, actualUri);
                    }
                    for (Iterator it = namespaceURIs.iterator(); it.hasNext(); ) {
                        String namespaceURI = (String)it.next();
                        assertEquals(
                                "Prefix for namespace URI '" + namespaceURI + "'",
                                refNc.getPrefix(namespaceURI),
                                nc.getPrefix(namespaceURI));
                        assertEquals(
                                "Prefixes for namespace URI '" + namespaceURI + "'",
                                toPrefixSet(refNc.getPrefixes(namespaceURI)),
                                toPrefixSet(nc.getPrefixes(namespaceURI)));
                    }
                }
                if (eventType == XMLStreamReader.END_ELEMENT) {
                    refNc.endScope();
                    depth--;
View Full Code Here

    {
        String xml =
            "<root>\n" +
                "<ns:a xmlns:ns=\"http://a.com\"><b foo=\"value\">text1<c/>text2<c/>text3<c>text</c>text4</b></ns:a>\n" +
                "</root>";
        NamespaceContext ns = new NamespaceContext() {
            public String getNamespaceURI(String prefix)
            {
                if ("ns".equals(prefix))
                    return "http://a.com";
                else
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.