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

   {
      final String ns = "http://jboss.org/test/simple/bindings";
      String local = "test1";
      QName qName = new QName(ns, local);

      NamespaceContext nsCtx = new NamespaceContext()
      {
         public String getNamespaceURI(String p)
         {
            return null;
         }
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

            binding.getAlgorithmSuite().getAlgorithmSuiteType().getDigest();
        final String expectedCanonAlgorithm  = binding.getAlgorithmSuite().getC14n().getValue();
           
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        final NamespaceContext nsContext = this.getNamespaceContext();
        xpath.setNamespaceContext(nsContext);
       
        // Signature Algorithm
        final XPathExpression sigAlgoExpr =
            xpath.compile("/s:Envelope/s:Header/wsse:Security/ds:Signature/ds:SignedInfo"
View Full Code Here

    // TODO: This method can be removed when runOutInterceptorAndValidateSamlTokenAttached
    // is cleaned up.
    protected void verifySignatureCoversAssertion(Document signedDoc, String assertionId) throws Exception {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        final NamespaceContext nsContext = this.getNamespaceContext();
        xpath.setNamespaceContext(nsContext);
       
        // Find the SecurityTokenReference for the assertion
        final XPathExpression strExpr = xpath.compile(
            "/s:Envelope/s:Header/wsse:Security/wsse:SecurityTokenReference/wsse:KeyIdentifier");
       
        final NodeList strKeyIdNodes =
            (NodeList) strExpr.evaluate(signedDoc, XPathConstants.NODESET);
       
        String strId = null;
        for (int i = 0; i < strKeyIdNodes.getLength(); i++) {
            Node keyIdNode = (Node) strKeyIdNodes.item(i);
            String strKey = keyIdNode.getTextContent();
            if (strKey.equals(assertionId)) {
                Node strNode = (Node) keyIdNode.getParentNode();
                strId = strNode.getAttributes().
                    getNamedItemNS(nsContext.getNamespaceURI("wsu"), "Id").getNodeValue();
                break;
            }
        }
        assertNotNull("SecurityTokenReference for " + assertionId + " not found in security header.", strId);
       
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);
                }

                public Iterator getPrefixes(String namespaceURI) {
                    if (jaxbNamespace.equals(namespaceURI)) {
                        return ctx.getPrefixes(namespace);
                    }
                    return ctx.getPrefixes(namespaceURI);
                }
            };
        }
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");
                f.setAccessible(true);
                String ns[] = (String[])f.get(c);
                for (int x = 0; x < ns.length; x += 2) {
                    nsMap.put(ns[x], ns[x + 1]);
                }
            }
        } catch (Throwable t) {
            //internal JDK/xerces version
            try {
                Field f =  ReflectionUtil.getDeclaredField(c.getClass(), "fNamespaceContext");
                f.setAccessible(true);
                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(s));
                    } else {
                        nsMap.put(s, c.getNamespaceURI(s));
                    }
                }
            } catch (Throwable t2) {
                //ignore
            }
View Full Code Here

  private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia,
      final EntityProviderReadProperties readProperties)
      throws EdmException, EntityProviderException, XMLStreamException {
    EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
    NamespaceContext nsctx = reader.getNamespaceContext();

    boolean skipTag = true;
    if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

      if (targetPathInfo != null) {
        final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
        final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

        if (customPrefix != null && customNamespaceURI != null) {
          String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
          String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

          if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) {
            skipTag = false;
            reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
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

    }
    if (nodeAttr == null) {
      importChilds(pTarget, pBindings, pURI);
    } else {
      final XPath xpath = XPathFactory.newInstance().newXPath();
      NamespaceContext ctx = new NamespaceContext(){
        public Iterator getPrefixes(String pURI) {
          if (pURI == null) {
            throw new IllegalArgumentException("The URI must not be null.");
          }
          if (XMLConstants.XML_NS_URI.equals(pURI)) {
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.