Path: blob/master/test/micro/org/openjdk/bench/javax/xml/DOM.java
41159 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.javax.xml;2324import org.openjdk.jmh.annotations.Benchmark;25import org.w3c.dom.Document;26import org.w3c.dom.Element;27import org.w3c.dom.Node;28import org.xml.sax.InputSource;29import org.xml.sax.SAXException;3031import javax.xml.parsers.DocumentBuilder;32import javax.xml.parsers.DocumentBuilderFactory;33import javax.xml.parsers.ParserConfigurationException;34import java.io.ByteArrayInputStream;35import java.io.IOException;3637public class DOM extends AbstractXMLMicro {3839@Benchmark40public Document testBuild() throws Exception {41DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();42byte[] bytes = getFileBytesFromResource(doc);43InputSource source = new InputSource();44ByteArrayInputStream bais = new ByteArrayInputStream(bytes);4546source.setByteStream(bais);47return buildDocument(dbf, source);48}4950@Benchmark51public Document testModify() throws Exception {52DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();53byte[] bytes = getFileBytesFromResource(doc);54ByteArrayInputStream bais = new ByteArrayInputStream(bytes);55InputSource source = new InputSource(bais);56Document doc1 = buildDocument(dbf, source);5758modifyElementRecursive(doc1.getDocumentElement());59return doc1;60}6162@Benchmark63public Document testWalk() throws Exception {64DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();65byte[] bytes = getFileBytesFromResource(doc);66ByteArrayInputStream bais = new ByteArrayInputStream(bytes);67InputSource source = new InputSource(bais);68Document doc1 = buildDocument(dbf, source);6970walkElementRecursive(doc1.getDocumentElement());71return doc1;72}7374private Document buildDocument(DocumentBuilderFactory dbf, InputSource source)75throws ParserConfigurationException, SAXException, IOException {76dbf.setValidating(false);77dbf.setNamespaceAware(true);78DocumentBuilder docBuilder = dbf.newDocumentBuilder();79return docBuilder.parse(source);80}8182// TODO Fix so it isn't recursive?83private static void walkElementRecursive(Element element) {84// loop through children85if (element.hasChildNodes()) {86Node child = element.getFirstChild();87while (child != null) {8889// handle child by type90int type = child.getNodeType();91if (type == Node.ELEMENT_NODE) {92walkElementRecursive((Element) child);93}94child = child.getNextSibling();95}96}97}9899// TODO Fix so it isn't recursive?100private void modifyElementRecursive(Element element) {101102// check for children present103if (element.hasChildNodes()) {104105// loop through child nodes106boolean content = false;107108// Should not be null since we already have made a .hasChildNodes()109// check.110Node child = element.getFirstChild();111112do {113// Handle child by node type.114if (child.getNodeType() == Node.TEXT_NODE) {115String trimmed = child.getNodeValue().trim();116if (trimmed.length() == 0) {117// delete child if nothing but whitespace118element.removeChild(child);119} else {120// make sure we have the parent element information121content = true;122Document doc = element.getOwnerDocument();123String uri = element.getNamespaceURI();124String prefix = element.getPrefix();125content = true;126127// Create a "text" element matching parent namespace.128Element text = (uri == null) ? doc.createElement("text") : doc.createElementNS(uri, prefix129+ ":text");130131// wrap the trimmed content with new element132text.appendChild(doc.createTextNode(trimmed));133element.replaceChild(text, child);134135}136} else if (child.getNodeType() == Node.ELEMENT_NODE) {137modifyElementRecursive((Element) child);138}139140} while ((child = child.getNextSibling()) != null);141142// check if we've seen any non-whitespace content for element143if (content) {144String prefix = element.getPrefix();145String uri = element.getNamespaceURI();146// add attribute flagging content found147if (prefix == null || prefix.length() == 0) {148element.setAttribute("text", "true");149} else {150element.setAttributeNS(uri, prefix + ":text", "true");151}152153}154}155}156157}158159160