Search in sources :

Example 6 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Canonicalizer20010315 method handleAttributesSubtree.

/**
     * Returns the Attr[]s to be output for the given element.
     * <br>
     * The code of this method is a copy of {@link #handleAttributes(Element,
     * NameSpaceSymbTable)},
     * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
     * So if the element in question isRoot of c14n, it's parent is not in the
     * node set, as well as all other ancestors.
     *
     * @param element
     * @param ns
     * @return the Attr[]s to be output
     * @throws CanonicalizationException
     */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns) throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();
    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();
        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();
            if (!XMLNS_URI.equals(NUri)) {
                //It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                //The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);
                if (n != null) {
                    //Render the ns definition
                    result.add((Attr) n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                    }
                }
            }
        }
    }
    if (firstCall) {
        //It is the first node of the subtree
        //Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        //output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }
    return result.iterator();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 7 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Canonicalizer20010315 method handleAttributes.

/**
     * Returns the Attr[]s to be output for the given element.
     * <br>
     * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a DOM which has
     * been prepared using {@link com.sun.org.apache.xml.internal.security.utils.XMLUtils#circumventBug2650(
     * org.w3c.dom.Document)}.
     *
     * @param element
     * @param ns
     * @return the Attr[]s to be output
     * @throws CanonicalizationException
     */
@Override
protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) throws CanonicalizationException {
    // result will contain the attrs which have to be output
    xmlattrStack.push(ns.getLevel());
    boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
    final SortedSet<Attr> result = this.result;
    result.clear();
    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();
        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();
            if (!XMLNS_URI.equals(NUri)) {
                //A non namespace definition node.
                if (XML_LANG_URI.equals(NUri)) {
                    xmlattrStack.addXmlnsAttr(attribute);
                } else if (isRealVisible) {
                    //The node is visible add the attribute to the list of output attributes.
                    result.add(attribute);
                }
            } else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) {
                //add the prefix binding to the ns symb table.
                if (isVisible(attribute)) {
                    if (isRealVisible || !ns.removeMappingIfRender(NName)) {
                        //The xpath select this node output it if needed.
                        Node n = ns.addMappingAndRender(NName, NValue, attribute);
                        if (n != null) {
                            result.add((Attr) n);
                            if (C14nHelper.namespaceIsRelative(attribute)) {
                                Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                                throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                            }
                        }
                    }
                } else {
                    if (isRealVisible && !XMLNS.equals(NName)) {
                        ns.removeMapping(NName);
                    } else {
                        ns.addMapping(NName, NValue, attribute);
                    }
                }
            }
        }
    }
    if (isRealVisible) {
        //The element is visible, handle the xmlns definition
        Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
        Node n = null;
        if (xmlns == null) {
            //No xmlns def just get the already defined.
            n = ns.getMapping(XMLNS);
        } else if (!isVisible(xmlns)) {
            //There is a definition but the xmlns is not selected by the xpath.
            //then xmlns=""
            n = ns.addMappingAndRender(XMLNS, "", getNullNode(xmlns.getOwnerDocument()));
        }
        //output the xmlns def if needed.
        if (n != null) {
            result.add((Attr) n);
        }
        //Float all xml:* attributes of the unselected parent elements to this one.
        xmlattrStack.getXmlnsAttr(result);
        ns.getUnrenderedNodes(result);
    }
    return result.iterator();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 8 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Canonicalizer20010315Excl method handleAttributesSubtree.

@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns) throws CanonicalizationException {
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();
    // The prefix visibly utilized (in the attribute or in the name) in
    // the element
    SortedSet<String> visiblyUtilized = new TreeSet<String>();
    if (inclusiveNSSet != null && !inclusiveNSSet.isEmpty()) {
        visiblyUtilized.addAll(inclusiveNSSet);
    }
    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();
        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NName = attribute.getLocalName();
            String NNodeValue = attribute.getNodeValue();
            if (!XMLNS_URI.equals(attribute.getNamespaceURI())) {
                // Not a namespace definition.
                // The Element is output element, add the prefix (if used) to
                // visiblyUtilized
                String prefix = attribute.getPrefix();
                if (prefix != null && !(prefix.equals(XML) || prefix.equals(XMLNS))) {
                    visiblyUtilized.add(prefix);
                }
                // Add to the result.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NNodeValue)) && ns.addMapping(NName, NNodeValue, attribute) && C14nHelper.namespaceIsRelative(NNodeValue)) {
                // The default mapping for xml must not be output.
                // New definition check if it is relative.
                Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
            }
        }
    }
    String prefix = null;
    if (element.getNamespaceURI() != null && !(element.getPrefix() == null || element.getPrefix().length() == 0)) {
        prefix = element.getPrefix();
    } else {
        prefix = XMLNS;
    }
    visiblyUtilized.add(prefix);
    for (String s : visiblyUtilized) {
        Attr key = ns.getMapping(s);
        if (key != null) {
            result.add(key);
        }
    }
    return result.iterator();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeSet(java.util.TreeSet) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Attr(org.w3c.dom.Attr)

Example 9 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Canonicalizer11 method handleAttributesSubtree.

/**
     * Returns the Attr[]s to be output for the given element.
     * <br>
     * The code of this method is a copy of {@link #handleAttributes(Element,
     * NameSpaceSymbTable)},
     * whereas it takes into account that subtree-c14n is -- well --
     * subtree-based.
     * So if the element in question isRoot of c14n, it's parent is not in the
     * node set, as well as all other ancestors.
     *
     * @param element
     * @param ns
     * @return the Attr[]s to be output
     * @throws CanonicalizationException
     */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns) throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();
    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();
        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();
            if (!XMLNS_URI.equals(NUri)) {
                // It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                // The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);
                if (n != null) {
                    // Render the ns definition
                    result.add((Attr) n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                    }
                }
            }
        }
    }
    if (firstCall) {
        // It is the first node of the subtree
        // Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        // output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }
    return result.iterator();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 10 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class CanonicalizerBase method canonicalizeSubTree.

/**
     * Method canonicalizeSubTree, this function is a recursive one.
     *
     * @param currentNode
     * @param ns
     * @param endnode
     * @throws CanonicalizationException
     * @throws IOException
     */
protected final void canonicalizeSubTree(Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel) throws CanonicalizationException, IOException {
    if (isVisibleInt(currentNode) == -1) {
        return;
    }
    Node sibling = null;
    Node parentNode = null;
    final OutputStream writer = this.writer;
    final Node excludeNode = this.excludeNode;
    final boolean includeComments = this.includeComments;
    Map<String, byte[]> cache = new HashMap<String, byte[]>();
    do {
        switch(currentNode.getNodeType()) {
            case Node.ENTITY_NODE:
            case Node.NOTATION_NODE:
            case Node.ATTRIBUTE_NODE:
                // illegal node type during traversal
                throw new CanonicalizationException("empty");
            case Node.DOCUMENT_FRAGMENT_NODE:
            case Node.DOCUMENT_NODE:
                ns.outputNodePush();
                sibling = currentNode.getFirstChild();
                break;
            case Node.COMMENT_NODE:
                if (includeComments) {
                    outputCommentToWriter((Comment) currentNode, writer, documentLevel);
                }
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                outputPItoWriter((ProcessingInstruction) currentNode, writer, documentLevel);
                break;
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                outputTextToWriter(currentNode.getNodeValue(), writer);
                break;
            case Node.ELEMENT_NODE:
                documentLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
                if (currentNode == excludeNode) {
                    break;
                }
                Element currentElement = (Element) currentNode;
                //Add a level to the nssymbtable. So latter can be pop-back.
                ns.outputNodePush();
                writer.write('<');
                String name = currentElement.getTagName();
                UtfHelpper.writeByte(name, writer, cache);
                Iterator<Attr> attrs = this.handleAttributesSubtree(currentElement, ns);
                if (attrs != null) {
                    //we output all Attrs which are available
                    while (attrs.hasNext()) {
                        Attr attr = attrs.next();
                        outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
                    }
                }
                writer.write('>');
                sibling = currentNode.getFirstChild();
                if (sibling == null) {
                    writer.write(END_TAG.clone());
                    UtfHelpper.writeStringToUtf8(name, writer);
                    writer.write('>');
                    //We finished with this level, pop to the previous definitions.
                    ns.outputNodePop();
                    if (parentNode != null) {
                        sibling = currentNode.getNextSibling();
                    }
                } else {
                    parentNode = currentElement;
                }
                break;
            case Node.DOCUMENT_TYPE_NODE:
            default:
                break;
        }
        while (sibling == null && parentNode != null) {
            writer.write(END_TAG.clone());
            UtfHelpper.writeByte(((Element) parentNode).getTagName(), writer, cache);
            writer.write('>');
            //We finished with this level, pop to the previous definitions.
            ns.outputNodePop();
            if (parentNode == endnode) {
                return;
            }
            sibling = parentNode.getNextSibling();
            parentNode = parentNode.getParentNode();
            if (parentNode == null || Node.ELEMENT_NODE != parentNode.getNodeType()) {
                documentLevel = NODE_AFTER_DOCUMENT_ELEMENT;
                parentNode = null;
            }
        }
        if (sibling == null) {
            return;
        }
        currentNode = sibling;
        sibling = currentNode.getNextSibling();
    } while (true);
}
Also used : HashMap(java.util.HashMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UnsyncByteArrayOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Aggregations

CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)17 Attr (org.w3c.dom.Attr)9 Node (org.w3c.dom.Node)7 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)6 IOException (java.io.IOException)6 NamedNodeMap (org.w3c.dom.NamedNodeMap)6 Element (org.w3c.dom.Element)5 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)4 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)4 UnsyncByteArrayOutputStream (com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)3 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)3 OutputStream (java.io.OutputStream)3 Transforms (com.sun.org.apache.xml.internal.security.transforms.Transforms)2 InclusiveNamespaces (com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 SignatureAlgorithm (com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm)1 Canonicalizer20010315ExclOmitComments (com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclOmitComments)1