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

        XMLSecurityStreamWriter xmlSecurityStreamWriter = new XMLSecurityStreamWriter(outputProcessorChain);

        StringWriter stdStringWriter = new StringWriter();
        XMLStreamWriter stdXmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(stdStringWriter);

        NamespaceContext namespaceContext = new NamespaceContext() {
            @Override
            public String getNamespaceURI(String prefix) {
                if ("t3".equals(prefix)) {
                    return "test3ns";
                }
View Full Code Here


            }
        }
       
        @Override
        public NamespaceContext getNamespaceContext() {
            return new NamespaceContext() {

                public String getNamespaceURI(String prefix) {
                    for (Map.Entry<String, String> entry : namespaceMap.entrySet()) {
                        if (entry.getValue().equals(prefix)) {
                            return entry.getKey();
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

        //We'll use some reflection to grab the known namespaces from woodstox
        //or the xerces parser and fake extra namespace decls on the root elements.
        //slight performance penalty, but there already is a penalty if you are validating
        //anyway.
       
        NamespaceContext c = source.getNamespaceContext();
        final Map<String, String> nsMap = new TreeMap<String, String>();
        try {
            if (c instanceof W3CNamespaceContext) {                               
                Element element = ((W3CNamespaceContext)c).getElement();
                while (element != null) {
                    NamedNodeMap namedNodeMap = element.getAttributes();
                    for (int i = 0; i < namedNodeMap.getLength(); i++) {
                        Attr attr = (Attr)namedNodeMap.item(i);
                        if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) {
                            nsMap.put(attr.getLocalName(), attr.getValue());
                        }
                    }
                    element = (Element)element.getParentNode();
                }
            } else {
                try {
                    //Woodstox version
                    c = (NamespaceContext)c.getClass().getMethod("createNonTransientNsContext",
                                                                 Location.class)
                        .invoke(c, new Object[1]);
                } catch (Throwable t) {
                    //ignore
                }
                Field f = ReflectionUtil.getDeclaredField(c.getClass(), "mNamespaces");
                ReflectionUtil.setAccessible(f);
                String ns[] = (String[])f.get(c);
                for (int x = 0; x < ns.length; x += 2) {
                    if (ns[x] == null) {
                        nsMap.put("", ns[x + 1]);
                    } else {
                        nsMap.put(ns[x], ns[x + 1]);
                    }
                }
            }
        } catch (Throwable t) {
            //internal JDK/xerces version
            try {
                Field f =  ReflectionUtil.getDeclaredField(c.getClass(), "fNamespaceContext");
                ReflectionUtil.setAccessible(f);
                Object c2 = f.get(c);
                Enumeration<?> enm = (Enumeration<?>)c2.getClass().getMethod("getAllPrefixes").invoke(c2);
                while (enm.hasMoreElements()) {
                    String s = (String)enm.nextElement();
                    if (s == null) {
                        nsMap.put("", c.getNamespaceURI(null));
                    } else {
                        nsMap.put(s, c.getNamespaceURI(s));
                    }
                }
            } catch (Throwable t2) {
                //ignore
            }
View Full Code Here

    public static String getUniquePrefix(XMLStreamWriter writer, String namespaceURI)
        throws XMLStreamException {
        return getUniquePrefix(writer, namespaceURI, false);
    }
    public static String getUniquePrefix(XMLStreamWriter writer) {
        NamespaceContext nc = writer.getNamespaceContext();
        if (nc == null) {
            return DEF_PREFIXES[0];
        }
        for (String t : DEF_PREFIXES) {
            String uri = nc.getNamespaceURI(t);
            if (StringUtils.isEmpty(uri)) {
                return t;
            }
        }

        int n = 10;
        while (true) {
            String nsPrefix = "ns" + n;
            String uri = nc.getNamespaceURI(nsPrefix);
            if (StringUtils.isEmpty(uri)) {
                return nsPrefix;
            }
            n++;
        }
View Full Code Here

            super(reader);
        }
       
        @Override
        public NamespaceContext getNamespaceContext() {
            final NamespaceContext ctx = super.getNamespaceContext();
            return new NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    String ns = ctx.getNamespaceURI(prefix);
                    if (namespace.equals(ns)) {
                        ns = jaxbNamespace;
                    }                       
                    return ns;
                }

                public String getPrefix(String namespaceURI) {
                    if (jaxbNamespace.equals(namespaceURI)) {
                        return ctx.getPrefix(namespace);
                    }
                    return ctx.getPrefix(namespaceURI);
                }

                @SuppressWarnings("rawtypes")
                public Iterator getPrefixes(String namespaceURI) {
                    if (jaxbNamespace.equals(namespaceURI)) {
                        return ctx.getPrefixes(namespace);
                    }
                    return ctx.getPrefixes(namespaceURI);
                }
            };
        }
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

    public DataHandler getDataHandler(String blobcid) {
        return ((OMXMLStreamReader)getParent()).getDataHandler(blobcid);
    }

    public NamespaceContext getNamespaceContext() {
        NamespaceContext namespaceContext = super.getNamespaceContext();
        if (namespaceContextWrapper == null || namespaceContextWrapper.getParent() != namespaceContext) {
            namespaceContextWrapper = new NamespaceURIInterningNamespaceContextWrapper(namespaceContext);
        }
        return namespaceContextWrapper;
    }
View Full Code Here

        for (int i=0; i<reader.getNamespaceCount(); i++) {
            assertInterned(reader.getNamespaceURI(i));
        }
        reader.nextTag();
        assertInterned(reader.getNamespaceURI("p"));
        NamespaceContext nc = reader.getNamespaceContext();
        assertInterned(nc.getNamespaceURI("p"));
    }
View Full Code Here

            }
           
            // It is possible that the namespace is associated with multiple prefixes,
            // So try getting the namespace as a second step.
            if (writerPrefix != null) {
                NamespaceContext nsContext = writer.getNamespaceContext();
                if(nsContext != null) {
                    String writerNS = nsContext.getNamespaceURI(prefix);
                    return namespace.equals(writerNS);
                }
            }
            return false;
        } else {
            // UNQUALIFIED NAMESPACE
           
            // Neither XML 1.0 nor XML 1.1 allow to associate a prefix with an unqualified name (see also AXIOM-372).
            if (prefix.length() > 0) {
                throw new OMException("Invalid namespace declaration: Prefixed namespace bindings may not be empty.")
            }
           
            // Get the namespace associated with the prefix.
            // It is illegal to call getPrefix with null, but the specification is not
            // clear on what happens if called with "".  So the following code is
            // protected
            try {
                String writerPrefix = writer.getPrefix("");
                if (writerPrefix != null && writerPrefix.length() == 0) {
                    return true;
                }
            } catch (Throwable t) {
                if (log.isDebugEnabled()) {
                    log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
                }
            }
           
           
           
            // Fallback to using the namespace context
            NamespaceContext nsContext = writer.getNamespaceContext();
            if (nsContext != null) {
                String writerNS = nsContext.getNamespaceURI("");
                if (writerNS != null && writerNS.length() > 0) {
                    return false;
                }
            }
            return true;
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.