Skip to content

Commit

Permalink
Calculate list of child elements in DomReader only once. Closes #342.
Browse files Browse the repository at this point in the history
  • Loading branch information
joehni committed Sep 27, 2023
1 parent 165514e commit 94cb267
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ <h2>Minor changes</h2>
<ul>
<li>GHPR:#331, GHI:#326: Fix handling of empty java.util.concurrent.atomic.AtomicReference (by Alex Blekhman of Atlassian).</li>
<li>GHPR:#334: Fix remaining buffer size calculation in QuickWriter (by Higuchi Yuta).</li>
<li>GHI:#342: Optimize internal handling of children in DomReader avoiding O(n²) access times for siblings (by Shiang-Yun Yang).</li>
</ul>

<h1 id="1.4.20">1.4.20</h1>
Expand Down
23 changes: 21 additions & 2 deletions xstream/src/java/com/thoughtworks/xstream/io/xml/DomReader.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/*
* Copyright (C) 2004, 2005, 2006 Joe Walnes.
* Copyright (C) 2006, 2007, 2009, 2011 XStream Committers.
* Copyright (C) 2006, 2007, 2009, 2011, 2023 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*
* Created on 07. March 2004 by Joe Walnes
*/
package com.thoughtworks.xstream.io.xml;

import com.thoughtworks.xstream.core.util.FastStack;
import com.thoughtworks.xstream.io.naming.NameCoder;

import org.w3c.dom.Attr;
Expand All @@ -23,11 +24,13 @@
import java.util.ArrayList;
import java.util.List;


public class DomReader extends AbstractDocumentReader {

private Element currentElement;
private StringBuffer textBuffer;
private List childElements;
private final FastStack childrenStack;

public DomReader(Element rootElement) {
this(rootElement, new XmlFriendlyNameCoder());
Expand All @@ -43,6 +46,8 @@ public DomReader(Document document) {
public DomReader(Element rootElement, NameCoder nameCoder) {
super(rootElement, nameCoder);
textBuffer = new StringBuffer();
childrenStack = new FastStack(16);
collectChildElements();
}

/**
Expand Down Expand Up @@ -117,6 +122,9 @@ protected int getChildCount() {

protected void reassignCurrentElement(Object current) {
currentElement = (Element) current;
}

private void collectChildElements() {
NodeList childNodes = currentElement.getChildNodes();
childElements = new ArrayList();
for (int i = 0; i < childNodes.getLength(); i++) {
Expand All @@ -127,6 +135,17 @@ protected void reassignCurrentElement(Object current) {
}
}

public void moveDown() {
super.moveDown();
childrenStack.push(childElements);
collectChildElements();
}

public void moveUp() {
childElements = (List) childrenStack.pop();
super.moveUp();
}

public String peekNextChild() {
NodeList childNodes = currentElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Expand Down

0 comments on commit 94cb267

Please sign in to comment.