Search in sources :

Example 81 with Attr

use of org.w3c.dom.Attr in project robovm by robovm.

the class DomTest method testUserDataHandlerNotifiedOfShallowImports.

/**
     * A shallow import requires importing the attributes but not the child
     * nodes.
     */
public void testUserDataHandlerNotifiedOfShallowImports() {
    RecordingHandler handler = new RecordingHandler();
    name.setUserData("a", "apple", handler);
    name.setUserData("b", "banana", handler);
    standard.setUserData("c", "cat", handler);
    waffles.setUserData("d", "dog", handler);
    Document newDocument = builder.newDocument();
    Element importedName = (Element) newDocument.importNode(name, false);
    Attr importedStandard = importedName.getAttributeNode("a:standard");
    Set<String> expected = new HashSet<String>();
    expected.add(notification(NODE_IMPORTED, "a", "apple", name, importedName));
    expected.add(notification(NODE_IMPORTED, "b", "banana", name, importedName));
    expected.add(notification(NODE_IMPORTED, "c", "cat", standard, importedStandard));
    assertEquals(expected, handler.calls);
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) HashSet(java.util.HashSet)

Example 82 with Attr

use of org.w3c.dom.Attr in project nokogiri by sparklemotion.

the class XMLUtils method protectAgainstWrappingAttack.

/**
     * This method is a tree-search to help prevent against wrapping attacks. It checks that no
     * two Elements have ID Attributes that match the "value" argument, if this is the case then
     * "false" is returned. Note that a return value of "true" does not necessarily mean that
     * a matching Element has been found, just that no wrapping attack has been detected.
     */
public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
    Node startParent = startNode.getParentNode();
    Node processedNode;
    Element foundElement = null;
    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }
    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;
            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr) attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue())) {
                        if (foundElement == null) {
                            // Continue searching to find duplicates
                            foundElement = attr.getOwnerElement();
                        } else {
                            //log.debug("Multiple elements with the same 'Id' attribute value!");
                            return false;
                        }
                    }
                }
            }
        }
        processedNode = startNode;
        startNode = startNode.getFirstChild();
        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Example 83 with Attr

use of org.w3c.dom.Attr in project spring-framework by spring-projects.

the class AbstractMarshallerTests method marshalDOMResult.

@Test
public void marshalDOMResult() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document result = builder.newDocument();
    DOMResult domResult = new DOMResult(result);
    marshaller.marshal(flights, domResult);
    Document expected = builder.newDocument();
    Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
    Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
    namespace.setNodeValue("http://samples.springframework.org/flight");
    flightsElement.setAttributeNode(namespace);
    expected.appendChild(flightsElement);
    Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
    flightsElement.appendChild(flightElement);
    Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
    flightElement.appendChild(numberElement);
    Text text = expected.createTextNode("42");
    numberElement.appendChild(text);
    assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Example 84 with Attr

use of org.w3c.dom.Attr in project nokogiri by sparklemotion.

the class XmlAttr method value_set.

@JRubyMethod(name = { "value=", "content=" })
public IRubyObject value_set(ThreadContext context, IRubyObject content) {
    Attr attr = (Attr) node;
    attr.setValue(rubyStringToString(XmlNode.encode_special_chars(context, content)));
    setContent(content);
    return content;
}
Also used : Attr(org.w3c.dom.Attr) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 85 with Attr

use of org.w3c.dom.Attr in project nokogiri by sparklemotion.

the class XmlAttr method unlink.

@Override
public IRubyObject unlink(ThreadContext context) {
    Attr attr = (Attr) node;
    Element parent = attr.getOwnerElement();
    parent.removeAttributeNode(attr);
    return this;
}
Also used : Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Aggregations

Attr (org.w3c.dom.Attr)461 Element (org.w3c.dom.Element)215 NamedNodeMap (org.w3c.dom.NamedNodeMap)194 Node (org.w3c.dom.Node)153 Document (org.w3c.dom.Document)147 NodeList (org.w3c.dom.NodeList)89 ArrayList (java.util.ArrayList)33 DOMException (org.w3c.dom.DOMException)32 QName (javax.xml.namespace.QName)22 HashMap (java.util.HashMap)18 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 Text (org.w3c.dom.Text)14 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)13 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)10 RubyString (org.jruby.RubyString)10 IOException (java.io.IOException)9 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)8 File (java.io.File)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 ParseException (java.text.ParseException)6