what-the-bank/sources/org/simpleframework/xml/stream/NodeWriter.java

164 lines
5.1 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.simpleframework.xml.stream;
import java.io.Writer;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/* loaded from: classes.dex */
class NodeWriter {
private final Set active;
private final OutputStack stack;
private final boolean verbose;
private final Formatter writer;
public NodeWriter(Writer writer) {
this(writer, new Format());
}
public NodeWriter(Writer writer, Format format) {
this(writer, format, false);
}
private NodeWriter(Writer writer, Format format, boolean z) {
this.writer = new Formatter(writer, format);
HashSet hashSet = new HashSet();
this.active = hashSet;
this.stack = new OutputStack(hashSet);
this.verbose = z;
}
public OutputNode writeRoot() throws Exception {
OutputDocument outputDocument = new OutputDocument(this, this.stack);
if (this.stack.isEmpty()) {
this.writer.writeProlog();
}
return outputDocument;
}
public boolean isRoot(OutputNode outputNode) {
return this.stack.bottom() == outputNode;
}
public boolean isCommitted(OutputNode outputNode) {
return !this.active.contains(outputNode);
}
public void commit(OutputNode outputNode) throws Exception {
if (this.stack.contains(outputNode)) {
OutputNode pVar = this.stack.top();
if (!isCommitted(pVar)) {
writeStart(pVar);
}
while (this.stack.top() != outputNode) {
writeEnd(this.stack.pop());
}
writeEnd(outputNode);
this.stack.pop();
}
}
public void remove(OutputNode outputNode) throws Exception {
if (this.stack.top() != outputNode) {
throw new NodeException("Cannot remove node");
}
this.stack.pop();
}
public OutputNode writeElement(OutputNode outputNode, String str) throws Exception {
if (this.stack.isEmpty()) {
return writeStart(outputNode, str);
}
if (!this.stack.contains(outputNode)) {
return null;
}
OutputNode pVar = this.stack.top();
if (!isCommitted(pVar)) {
writeStart(pVar);
}
while (this.stack.top() != outputNode) {
writeEnd(this.stack.pop());
}
if (!this.stack.isEmpty()) {
writeValue(outputNode);
}
return writeStart(outputNode, str);
}
private OutputNode writeStart(OutputNode outputNode, String str) throws Exception {
OutputElement outputElement = new OutputElement(outputNode, this, str);
if (str == null) {
throw new NodeException("Can not have a null name");
}
return this.stack.push(outputElement);
}
private void writeStart(OutputNode outputNode) throws Exception {
writeComment(outputNode);
writeName(outputNode);
writeAttributes(outputNode);
writeNamespaces(outputNode);
}
private void writeComment(OutputNode outputNode) throws Exception {
String comment = outputNode.getComment();
if (comment != null) {
this.writer.writeComment(comment);
}
}
private void writeName(OutputNode outputNode) throws Exception {
String prefix = outputNode.getPrefix(this.verbose);
String name = outputNode.getName();
if (name != null) {
this.writer.writeStart(name, prefix);
}
}
private void writeValue(OutputNode outputNode) throws Exception {
Mode mode = outputNode.getMode();
String value = outputNode.getValue();
if (value != null) {
Iterator<OutputNode> it = this.stack.iterator();
while (it.hasNext()) {
OutputNode next = it.next();
if (mode != Mode.INHERIT) {
break;
} else {
mode = next.getMode();
}
}
this.writer.writeText(value, mode);
}
outputNode.setValue(null);
}
private void writeEnd(OutputNode outputNode) throws Exception {
String name = outputNode.getName();
String prefix = outputNode.getPrefix(this.verbose);
if (outputNode.getValue() != null) {
writeValue(outputNode);
}
if (name != null) {
this.writer.writeEnd(name, prefix);
this.writer.flush();
}
}
private void writeAttributes(OutputNode outputNode) throws Exception {
NodeMap<OutputNode> attributes = outputNode.getAttributes();
for (String str : attributes) {
OutputNode outputNode2 = attributes.get(str);
this.writer.writeAttribute(str, outputNode2.getValue(), outputNode2.getPrefix(this.verbose));
}
this.active.remove(outputNode);
}
private void writeNamespaces(OutputNode outputNode) throws Exception {
NamespaceMap namespaces = outputNode.getNamespaces();
for (String str : namespaces) {
this.writer.writeNamespace(str, namespaces.getPrefix(str));
}
}
}