This commit is contained in:
Lana Steuck 2015-09-26 09:22:33 -07:00
commit 3d26475704
4 changed files with 831 additions and 393 deletions

View File

@ -0,0 +1,103 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Copyright (c) 2015 World Wide Web Consortium,
*
* (Massachusetts Institute of Technology, European Research Consortium for
* Informatics and Mathematics, Keio University, Beihang). All Rights Reserved.
* This work is distributed under the W3C(r) Software License [1] in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* [1] http://www.w3.org/Consortium/Legal/copyright-software
*/
package org.w3c.dom;
/**
* The {@code ElementTraversal} interface is a set of read-only attributes
* which allow an author to easily navigate between elements in a document.
* <p>
* In conforming implementations of Element Traversal, all objects that
* implement {@link Element} must also implement the {@code ElementTraversal}
* interface. Four of the methods,
* {@link #getFirstElementChild}, {@link #getLastElementChild},
* {@link #getPreviousElementSibling}, and {@link #getNextElementSibling},
* each provides a live reference to another element with the defined
* relationship to the current element, if the related element exists. The
* fifth method, {@link #getChildElementCount}, exposes the number of child
* elements of an element, for preprocessing before navigation.
*
* @see
* <a href='http://www.w3.org/TR/ElementTraversal/'><cite>Element Traversal Specification</cite></a>.
*
* @since 9
*/
public interface ElementTraversal {
/**
* Returns a reference to the first child node of the element which is of
* the {@link Element} type.
*
* @return a reference to an element child, {@code null} if the element has
* no child of the {@link Element} type.
*/
Element getFirstElementChild();
/**
* Returns a reference to the last child node of the element which is of
* the {@link Element} type.
*
* @return a reference to an element child, {@code null} if the element has
* no child of the {@link Element} type.
*/
Element getLastElementChild();
/**
* Returns a reference to the sibling node of the element which most immediately
* precedes the element in document order, and which is of the {@link Element} type.
*
* @return a reference to an element child, {@code null} if the element has
* no sibling node of the {@link Element} type that comes before this one.
*/
Element getPreviousElementSibling();
/**
* Returns a reference to the sibling node of the element which most immediately
* follows the element in document order, and which is of the {@link Element} type.
*
* @return a reference to an element child, {@code null} if the element has
* no sibling node of the {@link Element} type that comes after this one.
*/
Element getNextElementSibling();
/**
* Returns the current number of child nodes of the element which are of
* the {@link Element} type.
*
* @return the number of element children, or {@code 0} if the element has
* no element children.
*/
int getChildElementCount();
}

View File

@ -0,0 +1,112 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package dom;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/*
* @bug 8135283
* @summary Tests for the Element Traversal interface.
*/
public class ElementTraversal {
/*
Verifies the ElementTraversal interface by exercising all of its five
methods while reading through the xml document.
*/
@Test(dataProvider = "doc")
public void test(Document doc) {
org.w3c.dom.ElementTraversal et = (org.w3c.dom.ElementTraversal)doc.getDocumentElement();
//4 toys are listed
Assert.assertEquals(et.getChildElementCount(), 4);
//The 1st is the Martian
Element toy1 = et.getFirstElementChild();
verify(toy1, "1", "The Martian");
//toy1 has no previous element
Element noE = ((org.w3c.dom.ElementTraversal)toy1).getPreviousElementSibling();
Assert.assertEquals(noE, null);
//The 1st toy's next element is toy2, the Doll
Element toy2 = ((org.w3c.dom.ElementTraversal)toy1).getNextElementSibling();
verify(toy2, "2", "The Doll");
//The last toy is toy4, the Spaceship
Element toy4 = et.getLastElementChild();
verify(toy4, "4", "The Spaceship");
//toy4 has no next element
noE = ((org.w3c.dom.ElementTraversal)toy4).getNextElementSibling();
Assert.assertEquals(noE, null);
//toy4's previous element is toy3, Transformer X
//toy3 is also an EntityReference
Element toy3 = ((org.w3c.dom.ElementTraversal)toy4).getPreviousElementSibling();
verify(toy3, "3", "Transformer X");
}
/**
* Verifies that the values matches the specified element.
* @param id the value of the id attribute
* @param name the value of its name element
*/
void verify(Element e, String id, String name) {
Assert.assertEquals(e.getAttribute("id"), id);
Element toyName = ((org.w3c.dom.ElementTraversal)e).getFirstElementChild();
Assert.assertEquals(toyName.getTextContent(), name);
}
/*
* DataProvider: a Document object
*/
@DataProvider(name = "doc")
Object[][] getXPath() {
return new Object[][]{{getDoc()}};
}
Document getDoc() {
InputStream xmlFile = getClass().getResourceAsStream("ElementTraversal.xml");
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(xmlFile);
} catch (ParserConfigurationException | SAXException | IOException e) {
System.out.println("fail: " + e.getMessage());
}
return doc;
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE toys [
<!ENTITY toy3 "<name>Transformer X</name><price>519</price>">
]>
<toys>
<toy id="1">
<name>The Martian</name>
<price>98470</price>
</toy>
<toy id="2">
<name>The Doll</name>
<price>345</price>
</toy>
<toy id="3">
&toy3;
</toy>
<toy id="4">
<name>The Spaceship</name>
<price>725</price>
</toy>
</toys>