/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.BufferedWriter
 *  java.io.IOException
 *  java.io.OutputStream
 *  java.io.OutputStreamWriter
 *  java.io.PrintStream
 *  java.io.UnsupportedEncodingException
 *  java.io.Writer
 *  java.lang.Character
 *  java.lang.Exception
 *  java.lang.NullPointerException
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  java.util.HashMap
 *  java.util.List
 *  java.util.Map
 *  java.util.Map$Entry
 *  java.util.Set
 *  java.util.StringTokenizer
 *  org.xml.sax.Attributes
 *  org.xml.sax.InputSource
 *  org.xml.sax.Locator
 *  org.xml.sax.SAXException
 *  org.xml.sax.SAXNotRecognizedException
 *  org.xml.sax.SAXNotSupportedException
 *  org.xml.sax.XMLReader
 *  org.xml.sax.ext.LexicalHandler
 *  org.xml.sax.helpers.XMLFilterImpl
 */
package org.dom4j.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.Text;
import org.dom4j.io.OutputFormat;
import org.dom4j.tree.NamespaceStack;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.XMLFilterImpl;

public class XMLWriter
extends XMLFilterImpl
implements LexicalHandler {
    protected static final OutputFormat DEFAULT_FORMAT;
    protected static final String[] LEXICAL_HANDLER_NAMES;
    private static final String PAD_TEXT = " ";
    private boolean autoFlush;
    private StringBuffer buffer;
    private boolean charsAdded;
    private boolean escapeText;
    private OutputFormat format;
    private boolean inDTD;
    private int indentLevel;
    private char lastChar;
    private boolean lastElementClosed;
    protected int lastOutputNodeType;
    private LexicalHandler lexicalHandler;
    private int maximumAllowedCharacter;
    private NamespaceStack namespaceStack;
    private Map namespacesMap;
    protected boolean preserve;
    private boolean resolveEntityRefs;
    private boolean showCommentsInDTDs;
    protected Writer writer;

    static {
        LEXICAL_HANDLER_NAMES = new String[]{"http://xml.org/sax/properties/lexical-handler", "http://xml.org/sax/handlers/LexicalHandler"};
        DEFAULT_FORMAT = new OutputFormat();
    }

    public XMLWriter() {
        this.resolveEntityRefs = true;
        this.lastElementClosed = false;
        this.preserve = false;
        this.namespaceStack = new NamespaceStack();
        this.escapeText = true;
        this.indentLevel = 0;
        this.buffer = new StringBuffer();
        this.charsAdded = false;
        this.format = DEFAULT_FORMAT;
        this.writer = new BufferedWriter((Writer)new OutputStreamWriter((OutputStream)System.out));
        this.autoFlush = true;
        this.namespaceStack.push(Namespace.NO_NAMESPACE);
    }

    public XMLWriter(OutputStream outputStream) throws UnsupportedEncodingException {
        this.resolveEntityRefs = true;
        this.lastElementClosed = false;
        this.preserve = false;
        this.namespaceStack = new NamespaceStack();
        this.escapeText = true;
        this.indentLevel = 0;
        this.buffer = new StringBuffer();
        this.charsAdded = false;
        this.format = DEFAULT_FORMAT;
        this.writer = this.createWriter(outputStream, this.format.getEncoding());
        this.autoFlush = true;
        this.namespaceStack.push(Namespace.NO_NAMESPACE);
    }

    public XMLWriter(OutputStream outputStream, OutputFormat outputFormat) throws UnsupportedEncodingException {
        this.resolveEntityRefs = true;
        this.lastElementClosed = false;
        this.preserve = false;
        this.namespaceStack = new NamespaceStack();
        this.escapeText = true;
        this.indentLevel = 0;
        this.buffer = new StringBuffer();
        this.charsAdded = false;
        this.format = outputFormat;
        this.writer = this.createWriter(outputStream, outputFormat.getEncoding());
        this.autoFlush = true;
        this.namespaceStack.push(Namespace.NO_NAMESPACE);
    }

    public XMLWriter(Writer writer) {
        super(writer, DEFAULT_FORMAT);
    }

    public XMLWriter(Writer writer, OutputFormat outputFormat) {
        this.resolveEntityRefs = true;
        this.lastElementClosed = false;
        this.preserve = false;
        this.namespaceStack = new NamespaceStack();
        this.escapeText = true;
        this.indentLevel = 0;
        this.buffer = new StringBuffer();
        this.charsAdded = false;
        this.writer = writer;
        this.format = outputFormat;
        this.namespaceStack.push(Namespace.NO_NAMESPACE);
    }

    public XMLWriter(OutputFormat outputFormat) throws UnsupportedEncodingException {
        this.resolveEntityRefs = true;
        this.lastElementClosed = false;
        this.preserve = false;
        this.namespaceStack = new NamespaceStack();
        this.escapeText = true;
        this.indentLevel = 0;
        this.buffer = new StringBuffer();
        this.charsAdded = false;
        this.format = outputFormat;
        this.writer = this.createWriter((OutputStream)System.out, outputFormat.getEncoding());
        this.autoFlush = true;
        this.namespaceStack.push(Namespace.NO_NAMESPACE);
    }

    /*
     * Exception decompiling
     */
    public void characters(char[] var1, int var2_3, int var3_2) throws SAXException {
        // This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
        // java.util.ConcurrentModificationException
        // java.util.LinkedList$ReverseLinkIterator.next(LinkedList.java:217)
        // org.benf.cfr.reader.bytecode.analysis.structured.statement.Block.extractLabelledBlocks(Block.java:212)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement$LabelledBlockExtractor.transform(Op04StructuredStatement.java:485)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.transform(Op04StructuredStatement.java:639)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.insertLabelledBlocks(Op04StructuredStatement.java:649)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:816)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:220)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:165)
        // org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:91)
        // org.benf.cfr.reader.entities.Method.analyse(Method.java:354)
        // org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:751)
        // org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:683)
        // org.benf.cfr.reader.Main.doJar(Main.java:128)
        // com.njlabs.showjava.processor.JavaExtractor$1.run(JavaExtractor.java:84)
        // java.lang.Thread.run(Thread.java:818)
        throw new IllegalStateException("Decompilation failed");
    }

    public void close() throws IOException {
        this.writer.close();
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void comment(char[] arrc, int n, int n2) throws SAXException {
        if (this.showCommentsInDTDs || !this.inDTD) {
            try {
                this.charsAdded = false;
                this.writeComment(new String(arrc, n, n2));
            }
            catch (IOException var4_4) {
                this.handleException(var4_4);
            }
        }
        if (this.lexicalHandler != null) {
            this.lexicalHandler.comment(arrc, n, n2);
        }
    }

    protected Writer createWriter(OutputStream outputStream, String string2) throws UnsupportedEncodingException {
        return new BufferedWriter((Writer)new OutputStreamWriter(outputStream, string2));
    }

    protected int defaultMaximumAllowedCharacter() {
        String string2 = this.format.getEncoding();
        if (string2 != null && string2.equals((Object)"US-ASCII")) {
            return 127;
        }
        return -1;
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void endCDATA() throws SAXException {
        try {
            this.writer.write("]]>");
        }
        catch (IOException var1_1) {
            this.handleException(var1_1);
        }
        if (this.lexicalHandler != null) {
            this.lexicalHandler.endCDATA();
        }
    }

    public void endDTD() throws SAXException {
        this.inDTD = false;
        if (this.lexicalHandler != null) {
            this.lexicalHandler.endDTD();
        }
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public void endDocument() throws SAXException {
        super.endDocument();
        if (!this.autoFlush) return;
        try {
            this.flush();
            return;
        }
        catch (IOException var1_1) {
            return;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void endElement(String string2, String string3, String string4) throws SAXException {
        try {
            this.charsAdded = false;
            this.indentLevel = -1 + this.indentLevel;
            if (this.lastElementClosed) {
                this.writePrintln();
                this.indent();
            }
            if (true) {
                this.writeClose(string4);
            } else {
                this.writeEmptyElementClose(string4);
            }
            this.lastOutputNodeType = 1;
            this.lastElementClosed = true;
            super.endElement(string2, string3, string4);
            return;
        }
        catch (IOException var4_4) {
            this.handleException(var4_4);
            return;
        }
    }

    public void endEntity(String string2) throws SAXException {
        if (this.lexicalHandler != null) {
            this.lexicalHandler.endEntity(string2);
        }
    }

    public void endPrefixMapping(String string2) throws SAXException {
        super.endPrefixMapping(string2);
    }

    /*
     * Enabled aggressive block sorting
     */
    protected String escapeAttributeEntities(String string2) {
        int n;
        char c = this.format.getAttributeQuoteCharacter();
        Object object = null;
        int n2 = 0;
        int n3 = string2.length();
        block8 : for (n = 0; n < n3; ++n) {
            char c2 = string2.charAt(n);
            String string3 = null;
            switch (c2) {
                default: {
                    if (c2 >= ' ') {
                        boolean bl = this.shouldEncodeChar(c2);
                        string3 = null;
                        if (!bl) continue block8;
                    }
                    string3 = new StringBuffer().append("&#").append((int)c2).append(";").toString();
                    break;
                }
                case '<': {
                    string3 = "&lt;";
                    break;
                }
                case '>': {
                    string3 = "&gt;";
                    break;
                }
                case '\'': {
                    string3 = null;
                    if (c != '\'') continue block8;
                    string3 = "&apos;";
                    break;
                }
                case '\"': {
                    string3 = null;
                    if (c != '\"') continue block8;
                    string3 = "&quot;";
                }
                case '\t': 
                case '\n': 
                case '\r': {
                    break;
                }
                case '&': {
                    string3 = "&amp;";
                }
            }
            if (string3 == null) continue;
            if (object == null) {
                object = string2.toCharArray();
            }
            this.buffer.append((char[])object, n2, n - n2);
            this.buffer.append(string3);
            n2 = n + 1;
        }
        if (n2 == 0) {
            return string2;
        }
        if (n2 < n3) {
            if (object == null) {
                object = string2.toCharArray();
            }
            this.buffer.append((char[])object, n2, n - n2);
        }
        String string4 = this.buffer.toString();
        this.buffer.setLength(0);
        return string4;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected String escapeElementEntities(String string2) {
        int n;
        Object object = null;
        int n2 = 0;
        int n3 = string2.length();
        block6 : for (n = 0; n < n3; ++n) {
            String string3;
            char c = string2.charAt(n);
            switch (c) {
                default: {
                    if (c >= ' ') {
                        boolean bl = this.shouldEncodeChar(c);
                        string3 = null;
                        if (!bl) continue block6;
                    }
                    string3 = new StringBuffer().append("&#").append((int)c).append(";").toString();
                    break;
                }
                case '<': {
                    string3 = "&lt;";
                    break;
                }
                case '>': {
                    string3 = "&gt;";
                    break;
                }
                case '&': {
                    string3 = "&amp;";
                    break;
                }
                case '\t': 
                case '\n': 
                case '\r': {
                    boolean bl = this.preserve;
                    string3 = null;
                    if (!bl) continue block6;
                    string3 = String.valueOf((char)c);
                }
            }
            if (string3 == null) continue;
            if (object == null) {
                object = string2.toCharArray();
            }
            this.buffer.append((char[])object, n2, n - n2);
            this.buffer.append(string3);
            n2 = n + 1;
        }
        if (n2 == 0) {
            return string2;
        }
        if (n2 < n3) {
            if (object == null) {
                object = string2.toCharArray();
            }
            this.buffer.append((char[])object, n2, n - n2);
        }
        String string4 = this.buffer.toString();
        this.buffer.setLength(0);
        return string4;
    }

    public void flush() throws IOException {
        this.writer.flush();
    }

    public LexicalHandler getLexicalHandler() {
        return this.lexicalHandler;
    }

    public int getMaximumAllowedCharacter() {
        if (this.maximumAllowedCharacter == 0) {
            this.maximumAllowedCharacter = this.defaultMaximumAllowedCharacter();
        }
        return this.maximumAllowedCharacter;
    }

    protected OutputFormat getOutputFormat() {
        return this.format;
    }

    public Object getProperty(String string2) throws SAXNotRecognizedException, SAXNotSupportedException {
        for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; ++i) {
            if (!LEXICAL_HANDLER_NAMES[i].equals((Object)string2)) continue;
            return this.getLexicalHandler();
        }
        return super.getProperty(string2);
    }

    protected void handleException(IOException iOException) throws SAXException {
        throw new SAXException((Exception)iOException);
    }

    public void ignorableWhitespace(char[] arrc, int n, int n2) throws SAXException {
        super.ignorableWhitespace(arrc, n, n2);
    }

    protected void indent() throws IOException {
        String string2 = this.format.getIndent();
        if (string2 != null && string2.length() > 0) {
            for (int i = 0; i < this.indentLevel; ++i) {
                this.writer.write(string2);
            }
        }
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    protected void installLexicalHandler() {
        var1_1 = this.getParent();
        if (var1_1 == null) {
            throw new NullPointerException("No parent for filter");
        }
        var2_2 = 0;
        while (var2_2 < XMLWriter.LEXICAL_HANDLER_NAMES.length) {
            try {
                var1_1.setProperty(XMLWriter.LEXICAL_HANDLER_NAMES[var2_2], (Object)this);
                return;
            }
            catch (SAXNotSupportedException var4_3) {}
            ** GOTO lbl-1000
            catch (SAXNotRecognizedException var3_4) {}
lbl-1000: // 2 sources:
            {
                ++var2_2;
                continue;
            }
        }
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected final boolean isElementSpacePreserved(Element element) {
        Attribute attribute = element.attribute("space");
        boolean bl = this.preserve;
        if (attribute == null) return bl;
        if (!"xml".equals((Object)attribute.getNamespacePrefix())) return false;
        if (!"preserve".equals((Object)attribute.getText())) return false;
        return true;
    }

    public boolean isEscapeText() {
        return this.escapeText;
    }

    protected boolean isExpandEmptyElements() {
        return this.format.isExpandEmptyElements();
    }

    protected boolean isNamespaceDeclaration(Namespace namespace) {
        if (namespace != null && namespace != Namespace.XML_NAMESPACE && namespace.getURI() != null && !this.namespaceStack.contains(namespace)) {
            return true;
        }
        return false;
    }

    public void notationDecl(String string2, String string3, String string4) throws SAXException {
        super.notationDecl(string2, string3, string4);
    }

    public void parse(InputSource inputSource) throws IOException, SAXException {
        this.installLexicalHandler();
        super.parse(inputSource);
    }

    public void println() throws IOException {
        this.writer.write(this.format.getLineSeparator());
    }

    public void processingInstruction(String string2, String string3) throws SAXException {
        try {
            this.indent();
            this.writer.write("<?");
            this.writer.write(string2);
            this.writer.write(" ");
            this.writer.write(string3);
            this.writer.write("?>");
            this.writePrintln();
            this.lastOutputNodeType = 7;
            super.processingInstruction(string2, string3);
            return;
        }
        catch (IOException var3_3) {
            this.handleException(var3_3);
            return;
        }
    }

    public boolean resolveEntityRefs() {
        return this.resolveEntityRefs;
    }

    public void setDocumentLocator(Locator locator) {
        super.setDocumentLocator(locator);
    }

    public void setEscapeText(boolean bl) {
        this.escapeText = bl;
    }

    public void setIndentLevel(int n) {
        this.indentLevel = n;
    }

    public void setLexicalHandler(LexicalHandler lexicalHandler) {
        if (lexicalHandler == null) {
            throw new NullPointerException("Null lexical handler");
        }
        this.lexicalHandler = lexicalHandler;
    }

    public void setMaximumAllowedCharacter(int n) {
        this.maximumAllowedCharacter = n;
    }

    public void setOutputStream(OutputStream outputStream) throws UnsupportedEncodingException {
        this.writer = this.createWriter(outputStream, this.format.getEncoding());
        this.autoFlush = true;
    }

    public void setProperty(String string2, Object object) throws SAXNotRecognizedException, SAXNotSupportedException {
        for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; ++i) {
            if (!LEXICAL_HANDLER_NAMES[i].equals((Object)string2)) continue;
            this.setLexicalHandler((LexicalHandler)object);
            return;
        }
        super.setProperty(string2, object);
    }

    public void setResolveEntityRefs(boolean bl) {
        this.resolveEntityRefs = bl;
    }

    public void setWriter(Writer writer) {
        this.writer = writer;
        this.autoFlush = false;
    }

    protected boolean shouldEncodeChar(char c) {
        int n = this.getMaximumAllowedCharacter();
        if (n > 0 && c > n) {
            return true;
        }
        return false;
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void startCDATA() throws SAXException {
        try {
            this.writer.write("<![CDATA[");
        }
        catch (IOException var1_1) {
            this.handleException(var1_1);
        }
        if (this.lexicalHandler != null) {
            this.lexicalHandler.startCDATA();
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void startDTD(String string2, String string3, String string4) throws SAXException {
        this.inDTD = true;
        try {
            this.writeDocType(string2, string3, string4);
        }
        catch (IOException var4_4) {
            this.handleException(var4_4);
        }
        if (this.lexicalHandler != null) {
            this.lexicalHandler.startDTD(string2, string3, string4);
        }
    }

    public void startDocument() throws SAXException {
        try {
            this.writeDeclaration();
            super.startDocument();
            return;
        }
        catch (IOException var1_1) {
            this.handleException(var1_1);
            return;
        }
    }

    public void startElement(String string2, String string3, String string4, Attributes attributes) throws SAXException {
        try {
            this.charsAdded = false;
            this.writePrintln();
            this.indent();
            this.writer.write("<");
            this.writer.write(string4);
            this.writeNamespaces();
            this.writeAttributes(attributes);
            this.writer.write(">");
            this.indentLevel = 1 + this.indentLevel;
            this.lastOutputNodeType = 1;
            this.lastElementClosed = false;
            super.startElement(string2, string3, string4, attributes);
            return;
        }
        catch (IOException var5_5) {
            this.handleException(var5_5);
            return;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void startEntity(String string2) throws SAXException {
        try {
            this.writeEntityRef(string2);
        }
        catch (IOException var2_2) {
            this.handleException(var2_2);
        }
        if (this.lexicalHandler != null) {
            this.lexicalHandler.startEntity(string2);
        }
    }

    public void startPrefixMapping(String string2, String string3) throws SAXException {
        if (this.namespacesMap == null) {
            this.namespacesMap = new HashMap();
        }
        this.namespacesMap.put((Object)string2, (Object)string3);
        super.startPrefixMapping(string2, string3);
    }

    public void unparsedEntityDecl(String string2, String string3, String string4, String string5) throws SAXException {
        super.unparsedEntityDecl(string2, string3, string4, string5);
    }

    /*
     * Enabled aggressive block sorting
     */
    public void write(Object object) throws IOException {
        if (object instanceof Node) {
            this.write((Node)object);
            return;
        } else {
            if (object instanceof String) {
                this.write((String)object);
                return;
            }
            if (object instanceof List) {
                List list = (List)object;
                int n = list.size();
                for (int i = 0; i < n; ++i) {
                    this.write(list.get(i));
                }
                return;
            } else {
                if (object == null) return;
                {
                    throw new IOException(new StringBuffer().append("Invalid object: ").append(object).toString());
                }
            }
        }
    }

    public void write(String string2) throws IOException {
        this.writeString(string2);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Attribute attribute) throws IOException {
        this.writeAttribute(attribute);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(CDATA cDATA) throws IOException {
        this.writeCDATA(cDATA.getText());
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Comment comment) throws IOException {
        this.writeComment(comment.getText());
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Document document) throws IOException {
        this.writeDeclaration();
        if (document.getDocType() != null) {
            this.indent();
            this.writeDocType(document.getDocType());
        }
        int n = document.nodeCount();
        for (int i = 0; i < n; ++i) {
            this.writeNode(document.node(i));
        }
        this.writePrintln();
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(DocumentType documentType) throws IOException {
        this.writeDocType(documentType);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Element element) throws IOException {
        this.writeElement(element);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Entity entity) throws IOException {
        this.writeEntity(entity);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Namespace namespace) throws IOException {
        this.writeNamespace(namespace);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Node node) throws IOException {
        this.writeNode(node);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(ProcessingInstruction processingInstruction) throws IOException {
        this.writeProcessingInstruction(processingInstruction);
        if (this.autoFlush) {
            this.flush();
        }
    }

    public void write(Text text) throws IOException {
        this.writeString(text.getText());
        if (this.autoFlush) {
            this.flush();
        }
    }

    protected void writeAttribute(Attribute attribute) throws IOException {
        this.writer.write(" ");
        this.writer.write(attribute.getQualifiedName());
        this.writer.write("=");
        char c = this.format.getAttributeQuoteCharacter();
        this.writer.write((int)c);
        this.writeEscapeAttributeEntities(attribute.getValue());
        this.writer.write((int)c);
        this.lastOutputNodeType = 2;
    }

    protected void writeAttribute(Attributes attributes, int n) throws IOException {
        char c = this.format.getAttributeQuoteCharacter();
        this.writer.write(" ");
        this.writer.write(attributes.getQName(n));
        this.writer.write("=");
        this.writer.write((int)c);
        this.writeEscapeAttributeEntities(attributes.getValue(n));
        this.writer.write((int)c);
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void writeAttributes(Element element) throws IOException {
        int n = 0;
        int n2 = element.attributeCount();
        while (n < n2) {
            String string2;
            Attribute attribute = element.attribute(n);
            Namespace namespace = attribute.getNamespace();
            if (namespace != null && namespace != Namespace.NO_NAMESPACE && namespace != Namespace.XML_NAMESPACE) {
                String string3 = namespace.getPrefix();
                String string4 = this.namespaceStack.getURI(string3);
                if (!namespace.getURI().equals((Object)string4)) {
                    this.writeNamespace(namespace);
                    this.namespaceStack.push(namespace);
                }
            }
            if ((string2 = attribute.getName()).startsWith("xmlns:")) {
                String string5 = string2.substring(6);
                if (this.namespaceStack.getNamespaceForPrefix(string5) == null) {
                    String string6 = attribute.getValue();
                    this.namespaceStack.push(string5, string6);
                    this.writeNamespace(string5, string6);
                }
            } else if (string2.equals((Object)"xmlns")) {
                if (this.namespaceStack.getDefaultNamespace() == null) {
                    String string7 = attribute.getValue();
                    this.namespaceStack.push(null, string7);
                    this.writeNamespace(null, string7);
                }
            } else {
                char c = this.format.getAttributeQuoteCharacter();
                this.writer.write(" ");
                this.writer.write(attribute.getQualifiedName());
                this.writer.write("=");
                this.writer.write((int)c);
                this.writeEscapeAttributeEntities(attribute.getValue());
                this.writer.write((int)c);
            }
            ++n;
        }
        return;
    }

    protected void writeAttributes(Attributes attributes) throws IOException {
        int n = attributes.getLength();
        for (int i = 0; i < n; ++i) {
            this.writeAttribute(attributes, i);
        }
    }

    protected void writeCDATA(String string2) throws IOException {
        this.writer.write("<![CDATA[");
        if (string2 != null) {
            this.writer.write(string2);
        }
        this.writer.write("]]>");
        this.lastOutputNodeType = 4;
    }

    protected void writeClose(String string2) throws IOException {
        this.writer.write("</");
        this.writer.write(string2);
        this.writer.write(">");
    }

    public void writeClose(Element element) throws IOException {
        this.writeClose(element.getQualifiedName());
    }

    protected void writeComment(String string2) throws IOException {
        if (this.format.isNewlines()) {
            this.println();
            this.indent();
        }
        this.writer.write("<!--");
        this.writer.write(string2);
        this.writer.write("-->");
        this.lastOutputNodeType = 8;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void writeDeclaration() throws IOException {
        String string2 = this.format.getEncoding();
        if (!this.format.isSuppressDeclaration()) {
            if (string2.equals((Object)"UTF8")) {
                this.writer.write("<?xml version=\"1.0\"");
                if (!this.format.isOmitEncoding()) {
                    this.writer.write(" encoding=\"UTF-8\"");
                }
                this.writer.write("?>");
            } else {
                this.writer.write("<?xml version=\"1.0\"");
                if (!this.format.isOmitEncoding()) {
                    this.writer.write(new StringBuffer().append(" encoding=\"").append(string2).append("\"").toString());
                }
                this.writer.write("?>");
            }
            if (this.format.isNewLineAfterDeclaration()) {
                this.println();
            }
        }
    }

    protected void writeDocType(String string2, String string3, String string4) throws IOException {
        this.writer.write("<!DOCTYPE ");
        this.writer.write(string2);
        boolean bl = false;
        if (string3 != null) {
            boolean bl2 = string3.equals((Object)"");
            bl = false;
            if (!bl2) {
                this.writer.write(" PUBLIC \"");
                this.writer.write(string3);
                this.writer.write("\"");
                bl = true;
            }
        }
        if (string4 != null && !string4.equals((Object)"")) {
            if (!bl) {
                this.writer.write(" SYSTEM");
            }
            this.writer.write(" \"");
            this.writer.write(string4);
            this.writer.write("\"");
        }
        this.writer.write(">");
        this.writePrintln();
    }

    protected void writeDocType(DocumentType documentType) throws IOException {
        if (documentType != null) {
            documentType.write(this.writer);
            this.writePrintln();
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void writeElement(Element element) throws IOException {
        int n = element.nodeCount();
        String string2 = element.getQualifiedName();
        this.writePrintln();
        this.indent();
        this.writer.write("<");
        this.writer.write(string2);
        int n2 = this.namespaceStack.size();
        Namespace namespace = element.getNamespace();
        if (this.isNamespaceDeclaration(namespace)) {
            this.namespaceStack.push(namespace);
            this.writeNamespace(namespace);
        }
        boolean bl = true;
        for (int i = 0; i < n; ++i) {
            Node node = element.node(i);
            if (node instanceof Namespace) {
                Namespace namespace2 = (Namespace)node;
                if (!this.isNamespaceDeclaration(namespace2)) continue;
                this.namespaceStack.push(namespace2);
                this.writeNamespace(namespace2);
                continue;
            }
            if (node instanceof Element) {
                bl = false;
                continue;
            }
            if (!(node instanceof Comment)) continue;
            bl = false;
        }
        this.writeAttributes(element);
        this.lastOutputNodeType = 1;
        if (n <= 0) {
            this.writeEmptyElementClose(string2);
        } else {
            this.writer.write(">");
            if (bl) {
                this.writeElementContent(element);
            } else {
                this.indentLevel = 1 + this.indentLevel;
                this.writeElementContent(element);
                this.indentLevel = -1 + this.indentLevel;
                this.writePrintln();
                this.indent();
            }
            this.writer.write("</");
            this.writer.write(string2);
            this.writer.write(">");
        }
        do {
            if (this.namespaceStack.size() <= n2) {
                this.lastOutputNodeType = 1;
                return;
            }
            this.namespaceStack.pop();
        } while (true);
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Lifted jumps to return sites
     */
    protected void writeElementContent(Element var1) throws IOException {
        var2_2 = this.format.isTrimText();
        var3_3 = this.preserve;
        if (var2_2) {
            this.preserve = this.isElementSpacePreserved(var1);
            var2_2 = this.preserve == false;
        }
        if (!var2_2) {
            var4_14 = null;
            var5_15 = 0;
            var6_16 = var1.nodeCount();
        } else {
            var9_4 = null;
            var10_5 = null;
            var11_6 = true;
            var13_8 = var1.nodeCount();
            for (var12_7 = 0; var12_7 < var13_8; ++var12_7) {
                var15_11 = var1.node(var12_7);
                if (var15_11 instanceof Text) {
                    if (var9_4 == null) {
                        var9_4 = (Text)var15_11;
                        continue;
                    }
                    if (var10_5 == null) {
                        var10_5 = new StringBuffer(var9_4.getText());
                    }
                    var10_5.append(((Text)var15_11).getText());
                    continue;
                }
                if (!var11_6 && this.format.isPadText()) {
                    var18_12 = 'a';
                    if (var10_5 != null) {
                        var18_12 = var10_5.charAt(0);
                    } else if (var9_4 != null) {
                        var18_12 = var9_4.getText().charAt(0);
                    }
                    if (Character.isWhitespace((char)var18_12)) {
                        this.writer.write(" ");
                    }
                }
                if (var9_4 != null) {
                    if (var10_5 != null) {
                        this.writeString(var10_5.toString());
                        var10_5 = null;
                    } else {
                        this.writeString(var9_4.getText());
                    }
                    if (this.format.isPadText()) {
                        var16_10 = 'a';
                        if (var10_5 != null) {
                            var16_10 = var10_5.charAt(-1 + var10_5.length());
                        } else if (var9_4 != null) {
                            var17_9 = var9_4.getText();
                            var16_10 = var17_9.charAt(-1 + var17_9.length());
                        }
                        if (Character.isWhitespace((char)var16_10)) {
                            this.writer.write(" ");
                        }
                    }
                    var9_4 = null;
                }
                this.writeNode(var15_11);
                var11_6 = false;
            }
            if (var9_4 != null) {
                if (!var11_6 && this.format.isPadText()) {
                    var14_13 = var10_5 != null ? var10_5.charAt(0) : var9_4.getText().charAt(0);
                    if (Character.isWhitespace((char)var14_13)) {
                        this.writer.write(" ");
                    }
                }
                if (var10_5 != null) {
                    this.writeString(var10_5.toString());
                } else {
                    this.writeString(var9_4.getText());
                }
            }
            do {
                this.preserve = var3_3;
                return;
                break;
            } while (true);
        }
        do {
            if (var5_15 >= var6_16) ** continue;
            var7_18 = var1.node(var5_15);
            if (var7_18 instanceof Text) {
                this.writeNode(var7_18);
                var4_14 = var7_18;
            } else {
                if (var4_14 != null && this.format.isPadText() && Character.isWhitespace((char)(var8_17 = var4_14.getText()).charAt(-1 + var8_17.length()))) {
                    this.writer.write(" ");
                }
                this.writeNode(var7_18);
                var4_14 = null;
            }
            ++var5_15;
        } while (true);
    }

    protected void writeEmptyElementClose(String string2) throws IOException {
        if (!this.format.isExpandEmptyElements()) {
            this.writer.write("/>");
            return;
        }
        this.writer.write("></");
        this.writer.write(string2);
        this.writer.write(">");
    }

    protected void writeEntity(Entity entity) throws IOException {
        if (!this.resolveEntityRefs()) {
            this.writeEntityRef(entity.getName());
            return;
        }
        this.writer.write(entity.getText());
    }

    protected void writeEntityRef(String string2) throws IOException {
        this.writer.write("&");
        this.writer.write(string2);
        this.writer.write(";");
        this.lastOutputNodeType = 5;
    }

    protected void writeEscapeAttributeEntities(String string2) throws IOException {
        if (string2 != null) {
            String string3 = this.escapeAttributeEntities(string2);
            this.writer.write(string3);
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void writeNamespace(String string2, String string3) throws IOException {
        if (string2 != null && string2.length() > 0) {
            this.writer.write(" xmlns:");
            this.writer.write(string2);
            this.writer.write("=\"");
        } else {
            this.writer.write(" xmlns=\"");
        }
        this.writer.write(string3);
        this.writer.write("\"");
    }

    protected void writeNamespace(Namespace namespace) throws IOException {
        if (namespace != null) {
            this.writeNamespace(namespace.getPrefix(), namespace.getURI());
        }
    }

    protected void writeNamespaces() throws IOException {
        if (this.namespacesMap != null) {
            for (Map.Entry entry : this.namespacesMap.entrySet()) {
                this.writeNamespace((String)entry.getKey(), (String)entry.getValue());
            }
            this.namespacesMap = null;
        }
    }

    protected void writeNode(Node node) throws IOException {
        switch (node.getNodeType()) {
            default: {
                throw new IOException(new StringBuffer().append("Invalid node type: ").append((Object)node).toString());
            }
            case 1: {
                this.writeElement((Element)node);
            }
            case 13: {
                return;
            }
            case 2: {
                this.writeAttribute((Attribute)node);
                return;
            }
            case 3: {
                this.writeNodeText(node);
                return;
            }
            case 4: {
                this.writeCDATA(node.getText());
                return;
            }
            case 5: {
                this.writeEntity((Entity)node);
                return;
            }
            case 7: {
                this.writeProcessingInstruction((ProcessingInstruction)node);
                return;
            }
            case 8: {
                this.writeComment(node.getText());
                return;
            }
            case 9: {
                this.write((Document)node);
                return;
            }
            case 10: 
        }
        this.writeDocType((DocumentType)node);
    }

    protected void writeNodeText(Node node) throws IOException {
        String string2 = node.getText();
        if (string2 != null && string2.length() > 0) {
            if (this.escapeText) {
                string2 = this.escapeElementEntities(string2);
            }
            this.lastOutputNodeType = 3;
            this.writer.write(string2);
            this.lastChar = string2.charAt(-1 + string2.length());
        }
    }

    public void writeOpen(Element element) throws IOException {
        this.writer.write("<");
        this.writer.write(element.getQualifiedName());
        this.writeAttributes(element);
        this.writer.write(">");
    }

    protected void writePrintln() throws IOException {
        String string2;
        if (this.format.isNewlines() && this.lastChar != (string2 = this.format.getLineSeparator()).charAt(-1 + string2.length())) {
            this.writer.write(this.format.getLineSeparator());
        }
    }

    protected void writeProcessingInstruction(ProcessingInstruction processingInstruction) throws IOException {
        this.writer.write("<?");
        this.writer.write(processingInstruction.getName());
        this.writer.write(" ");
        this.writer.write(processingInstruction.getText());
        this.writer.write("?>");
        this.writePrintln();
        this.lastOutputNodeType = 7;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected void writeString(String string2) throws IOException {
        if (string2 == null || string2.length() <= 0) return;
        {
            if (this.escapeText) {
                string2 = this.escapeElementEntities(string2);
            }
            if (!this.format.isTrimText()) {
                this.lastOutputNodeType = 3;
                this.writer.write(string2);
                this.lastChar = string2.charAt(-1 + string2.length());
                return;
            } else {
                boolean bl = true;
                StringTokenizer stringTokenizer = new StringTokenizer(string2);
                while (stringTokenizer.hasMoreTokens()) {
                    String string3 = stringTokenizer.nextToken();
                    if (bl) {
                        int n = this.lastOutputNodeType;
                        bl = false;
                        if (n == 3) {
                            this.writer.write(" ");
                        }
                    } else {
                        this.writer.write(" ");
                    }
                    this.writer.write(string3);
                    this.lastOutputNodeType = 3;
                    this.lastChar = string3.charAt(-1 + string3.length());
                }
            }
        }
    }
}

