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

   
    @Test
    public void testGetPrefixes() throws Exception {
       XMLStreamReader reader = staxHelper.createXMLStreamReader(XML2);
         reader.nextTag();
         NamespaceContext ctx = xpathHelper.getNamespaceContext("//sca:reference[IntentRefs('test:foo')]",
             reader.getNamespaceContext());
         Assert.assertNotNull(ctx.getNamespaceURI("sca"));
         Assert.assertNotNull(ctx.getNamespaceURI("test"));
   
    }
View Full Code Here


   
    @Test
    public void testGetPrefixes2() throws Exception {
      XMLStreamReader reader = staxHelper.createXMLStreamReader(XML2);
        reader.nextTag();
        NamespaceContext ctx = xpathHelper.getNamespaceContext("//sca:implementation.java[ IntentRefs( 'test:foo' ) ]",
            reader.getNamespaceContext());
        Assert.assertNotNull(ctx.getNamespaceURI("sca"));
        Assert.assertNotNull(ctx.getNamespaceURI("test"));
    }
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

   
    String attachTo = reader.getAttributeValue(null, ATTACH_TO);
    if ( attachTo != null ) {
       try {
               XPath path = xpathHelper.newXPath();
               NamespaceContext nsContext = xpathHelper.getNamespaceContext(attachTo, reader.getNamespaceContext());
               path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext));               
                                         
               attachTo = PolicyXPathFunction.normalize(attachTo,getSCAPrefix(nsContext));
               XPathExpression expression = xpathHelper.compile(path, nsContext, attachTo);
               attachment.setAttachTo(attachTo);
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

                    // 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

    private List rootPrefixes;

    public FragmentStreamReader(XMLStreamReader parent) {
        super(parent);
        rootPrefixes = new ArrayList();
        NamespaceContext ctx = getParent().getNamespaceContext();
        if (ctx instanceof ExtendedNamespaceContext) {
            Iterator it = ((ExtendedNamespaceContext) ctx).getPrefixes();
            while (it.hasNext()) {
                String prefix = (String) it.next();
                rootPrefixes.add(prefix);
View Full Code Here

            } else if ("hexBinary".equals(xsType.getName())) {
                dv = JExpr._new(outline.getCodeModel().ref(HexBinaryAdapter.class))
                    .invoke("unmarshal").arg(defaultValue);
            }
        } else if ("javax.xml.namespace.QName".equals(typeName)) {
            NamespaceContext nsc = new NamespaceContextAdapter(xmlDefaultValue);
            QName qn = DatatypeConverter.parseQName(xmlDefaultValue.value, nsc);
            dv = JExpr._new(outline.getCodeModel().ref(QName.class))
                .arg(qn.getNamespaceURI())
                .arg(qn.getLocalPart())
                .arg(qn.getPrefix());
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

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.