Skip to content

Commit 4e36459

Browse files
committed
avoid creating Attributes twice for the same element
1 parent 1e7e014 commit 4e36459

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/main/java/org/xmlobjects/stream/XMLReader.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class XMLReader implements AutoCloseable {
4343

4444
private final Map<Class<?>, ObjectBuilder<?>> builderCache = new IdentityHashMap<>();
4545
private Object parent;
46+
private Attributes attributes;
47+
private int attributesState = -1;
4648
private boolean createDOMAsFallback;
4749
private Properties properties;
4850
private Transformer transformer;
@@ -113,6 +115,8 @@ public void close() throws XMLReadException {
113115
} finally {
114116
builderCache.clear();
115117
parent = null;
118+
attributes = null;
119+
attributesState = -1;
116120
}
117121
}
118122

@@ -328,18 +332,24 @@ public Attributes getAttributes() throws XMLReadException {
328332
throw new XMLReadException("Illegal to call getAttributes when event is not START_ELEMENT.");
329333
}
330334

331-
int count = reader.getAttributeCount();
332-
if (count == 0) {
333-
return Attributes.empty();
335+
int state = reader.getState();
336+
if (state == attributesState) {
337+
return attributes;
334338
}
335339

336-
Attributes attributes = new Attributes();
337-
for (int i = 0; i < count; i++) {
338-
attributes.add(reader.getAttributeNamespace(i),
339-
reader.getAttributeLocalName(i),
340-
reader.getAttributeValue(i));
340+
int count = reader.getAttributeCount();
341+
if (count == 0) {
342+
attributes = Attributes.empty();
343+
} else {
344+
attributes = new Attributes();
345+
for (int i = 0; i < count; i++) {
346+
attributes.add(reader.getAttributeNamespace(i),
347+
reader.getAttributeLocalName(i),
348+
reader.getAttributeValue(i));
349+
}
341350
}
342351

352+
attributesState = state;
343353
return attributes;
344354
}
345355

0 commit comments

Comments
 (0)