Search in sources :

Example 6 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class XMLSecurityUtils method getExcC14NInclusiveNamespacePrefixes.

public static Set<String> getExcC14NInclusiveNamespacePrefixes(XMLSecStartElement xmlSecStartElement, boolean excludeVisible) {
    if (xmlSecStartElement == null) {
        return Collections.emptySet();
    }
    Set<String> prefixes = new TreeSet<>();
    XMLSecStartElement parentXMXmlSecStartElement = xmlSecStartElement.getParentXMLSecStartElement();
    if (parentXMXmlSecStartElement != null) {
        List<XMLSecNamespace> onElementDeclaredNamespaces = parentXMXmlSecStartElement.getOnElementDeclaredNamespaces();
        List<XMLSecNamespace> xmlSecNamespaces = new ArrayList<>();
        parentXMXmlSecStartElement.getNamespacesFromCurrentScope(xmlSecNamespaces);
        xmlSecNamespaces = xmlSecNamespaces.subList(0, xmlSecNamespaces.size() - onElementDeclaredNamespaces.size());
        // reverse iteration -> From current element namespaces to parent namespaces
        for (int i = xmlSecNamespaces.size() - 1; i >= 0; i--) {
            XMLSecNamespace xmlSecNamespace = xmlSecNamespaces.get(i);
            String prefix = xmlSecNamespace.getPrefix();
            if (prefix == null || prefix.isEmpty()) {
                prefixes.add("#default");
            } else {
                prefixes.add(xmlSecNamespace.getPrefix());
            }
        }
        if (excludeVisible) {
            for (int i = 0; i < onElementDeclaredNamespaces.size(); i++) {
                XMLSecNamespace xmlSecNamespace = onElementDeclaredNamespaces.get(i);
                String prefix = xmlSecNamespace.getPrefix();
                if (prefix == null || prefix.isEmpty()) {
                    prefixes.remove("#default");
                } else {
                    prefixes.remove(prefix);
                }
            }
            if (xmlSecStartElement.getName().getPrefix() == null || xmlSecStartElement.getName().getPrefix().isEmpty()) {
                prefixes.remove("#default");
            } else {
                prefixes.remove(xmlSecStartElement.getName().getPrefix());
            }
        }
    }
    return prefixes;
}
Also used : XMLSecStartElement(org.apache.xml.security.stax.ext.stax.XMLSecStartElement) XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace)

Example 7 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class XMLSecStartElementImpl method writeAsEncodedUnicode.

@Override
public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {
    try {
        writer.write('<');
        final String prefix = getName().getPrefix();
        if (prefix != null && !prefix.isEmpty()) {
            writer.write(prefix);
            writer.write(':');
        }
        writer.write(getName().getLocalPart());
        for (Namespace xmlSecNamespace : namespaces) {
            writer.write(" xmlns");
            final String nsPrefix = xmlSecNamespace.getPrefix();
            if (nsPrefix != null && !nsPrefix.isEmpty()) {
                writer.write(':');
                writer.write(nsPrefix);
            }
            writer.write("=\"");
            writer.write(xmlSecNamespace.getValue());
            writer.write('"');
        }
        for (Attribute xmlSecAttribute : attributes) {
            writer.write(' ');
            final String attrPrefix = xmlSecAttribute.getName().getPrefix();
            if (attrPrefix != null && !attrPrefix.isEmpty()) {
                writer.write(attrPrefix);
                writer.write(':');
            }
            writer.write(xmlSecAttribute.getName().getLocalPart());
            writer.write("=\"");
            writer.write(xmlSecAttribute.getValue());
            writer.write('"');
        }
        writer.write('>');
    } catch (IOException e) {
        throw new XMLStreamException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Attribute(javax.xml.stream.events.Attribute) XMLSecAttribute(org.apache.xml.security.stax.ext.stax.XMLSecAttribute) IOException(java.io.IOException) Namespace(javax.xml.stream.events.Namespace) XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace)

Example 8 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class XMLSecStartElementImpl method getNamespaceContext.

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

        @Override
        public String getNamespaceURI(String prefix) {
            for (int i = 0; i < namespaces.size(); i++) {
                Namespace comparableNamespace = namespaces.get(i);
                if (prefix.equals(comparableNamespace.getPrefix())) {
                    return comparableNamespace.getNamespaceURI();
                }
            }
            if (parentXMLSecStartELement != null) {
                return parentXMLSecStartELement.getNamespaceURI(prefix);
            }
            return null;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            for (int i = 0; i < namespaces.size(); i++) {
                Namespace comparableNamespace = namespaces.get(i);
                if (namespaceURI.equals(comparableNamespace.getNamespaceURI())) {
                    return comparableNamespace.getPrefix();
                }
            }
            if (parentXMLSecStartELement != null) {
                return parentXMLSecStartELement.getNamespaceContext().getPrefix(namespaceURI);
            }
            return null;
        }

        @SuppressWarnings("rawtypes")
        @Override
        public Iterator getPrefixes(String namespaceURI) {
            Set<String> prefixes = new HashSet<>();
            List<XMLSecNamespace> xmlSecNamespaces = new ArrayList<>();
            getNamespacesFromCurrentScope(xmlSecNamespaces);
            for (int i = 0; i < xmlSecNamespaces.size(); i++) {
                Namespace xmlSecNamespace = xmlSecNamespaces.get(i);
                if (namespaceURI.equals(xmlSecNamespace.getNamespaceURI())) {
                    prefixes.add(xmlSecNamespace.getPrefix());
                }
            }
            return prefixes.iterator();
        }
    };
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext) XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace) Namespace(javax.xml.stream.events.Namespace) XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace)

Aggregations

XMLSecNamespace (org.apache.xml.security.stax.ext.stax.XMLSecNamespace)8 XMLSecAttribute (org.apache.xml.security.stax.ext.stax.XMLSecAttribute)4 QName (javax.xml.namespace.QName)3 Namespace (javax.xml.stream.events.Namespace)2 XMLSecStartElement (org.apache.xml.security.stax.ext.stax.XMLSecStartElement)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 UnsyncByteArrayInputStream (org.apache.xml.security.utils.UnsyncByteArrayInputStream)1 Attr (org.w3c.dom.Attr)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1