package com.thoughtworks.xstream.io.xml; import com.thoughtworks.xstream.core.util.FastStack; import com.thoughtworks.xstream.io.AttributeNameIterator; import com.thoughtworks.xstream.io.naming.NameCoder; import java.util.Iterator; /* loaded from: classes.dex */ public abstract class AbstractPullReader extends AbstractXmlReader { protected static final int COMMENT = 4; protected static final int END_NODE = 2; protected static final int OTHER = 0; protected static final int START_NODE = 1; protected static final int TEXT = 3; private final FastStack elementStack; private final FastStack lookahead; private final FastStack lookback; private boolean marked; private final FastStack pool; private static class Event { int type; String value; private Event() { } } protected AbstractPullReader(NameCoder nameCoder) { super(nameCoder); this.elementStack = new FastStack(16); this.pool = new FastStack(16); this.lookahead = new FastStack(4); this.lookback = new FastStack(4); } private void move() { Event readEvent = readEvent(); this.pool.push(readEvent); int i = readEvent.type; if (i == 1) { this.elementStack.push(pullElementName()); } else { if (i != 2) { return; } this.elementStack.pop(); } } private Event readEvent() { return this.marked ? this.lookback.hasStuff() ? (Event) this.lookahead.push(this.lookback.pop()) : (Event) this.lookahead.push(readRealEvent()) : this.lookback.hasStuff() ? (Event) this.lookback.pop() : readRealEvent(); } private Event readRealEvent() { Event event = this.pool.hasStuff() ? (Event) this.pool.pop() : new Event(); event.type = pullNextEvent(); int i = event.type; if (i == 3) { event.value = pullText(); } else if (i == 1) { event.value = pullElementName(); } else { event.value = null; } return event; } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public Iterator getAttributeNames() { return new AttributeNameIterator(this); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getNodeName() { return unescapeXmlName((String) this.elementStack.peek()); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getValue() { mark(); Event readEvent = readEvent(); StringBuffer stringBuffer = null; String str = null; while (true) { int i = readEvent.type; if (i == 3) { String str2 = readEvent.value; if (str2 != null && str2.length() > 0) { if (str == null) { str = str2; } else { if (stringBuffer == null) { stringBuffer = new StringBuffer(str); } stringBuffer.append(str2); } } } else if (i != 4) { break; } readEvent = readEvent(); } reset(); return stringBuffer != null ? stringBuffer.toString() : str == null ? "" : str; } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public boolean hasMoreChildren() { int i; mark(); do { i = readEvent().type; if (i == 1) { reset(); return true; } } while (i != 2); reset(); return false; } public void mark() { this.marked = true; } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public void moveDown() { int size = this.elementStack.size(); while (this.elementStack.size() <= size) { move(); if (this.elementStack.size() < size) { throw new RuntimeException(); } } } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public void moveUp() { int size = this.elementStack.size(); while (this.elementStack.size() >= size) { move(); } } @Override // com.thoughtworks.xstream.io.AbstractReader, com.thoughtworks.xstream.io.ExtendedHierarchicalStreamReader public String peekNextChild() { int i; mark(); do { Event readEvent = readEvent(); i = readEvent.type; if (i == 1) { reset(); return readEvent.value; } } while (i != 2); reset(); return null; } protected abstract String pullElementName(); protected abstract int pullNextEvent(); protected abstract String pullText(); public void reset() { while (this.lookahead.hasStuff()) { this.lookback.push(this.lookahead.pop()); } this.marked = false; } protected AbstractPullReader(XmlFriendlyReplacer xmlFriendlyReplacer) { this((NameCoder) xmlFriendlyReplacer); } }