/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.IOException
 *  java.io.OutputStream
 *  java.io.StringWriter
 *  java.io.UnsupportedEncodingException
 *  java.io.Writer
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  java.util.HashSet
 *  java.util.Set
 *  java.util.Stack
 *  org.xml.sax.SAXException
 */
package org.dom4j.io;

import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

public class HTMLWriter
extends XMLWriter {
    protected static final OutputFormat DEFAULT_HTML_FORMAT;
    protected static final HashSet DEFAULT_PREFORMATTED_TAGS;
    private static String lineSeparator;
    private Stack formatStack;
    private String lastText;
    private int newLineAfterNTags;
    private HashSet omitElementCloseSet;
    private HashSet preformattedTags;
    private int tagsOuput;

    static {
        lineSeparator = System.getProperty((String)"line.separator");
        DEFAULT_PREFORMATTED_TAGS = new HashSet();
        DEFAULT_PREFORMATTED_TAGS.add((Object)"PRE");
        DEFAULT_PREFORMATTED_TAGS.add((Object)"SCRIPT");
        DEFAULT_PREFORMATTED_TAGS.add((Object)"STYLE");
        DEFAULT_PREFORMATTED_TAGS.add((Object)"TEXTAREA");
        DEFAULT_HTML_FORMAT = new OutputFormat("  ", true);
        DEFAULT_HTML_FORMAT.setTrimText(true);
        DEFAULT_HTML_FORMAT.setSuppressDeclaration(true);
    }

    public HTMLWriter() throws UnsupportedEncodingException {
        super(DEFAULT_HTML_FORMAT);
        this.formatStack = new Stack();
        this.lastText = "";
        this.tagsOuput = 0;
        this.newLineAfterNTags = -1;
        this.preformattedTags = DEFAULT_PREFORMATTED_TAGS;
    }

    public HTMLWriter(OutputStream outputStream) throws UnsupportedEncodingException {
        super(outputStream, DEFAULT_HTML_FORMAT);
        this.formatStack = new Stack();
        this.lastText = "";
        this.tagsOuput = 0;
        this.newLineAfterNTags = -1;
        this.preformattedTags = DEFAULT_PREFORMATTED_TAGS;
    }

    public HTMLWriter(OutputStream outputStream, OutputFormat outputFormat) throws UnsupportedEncodingException {
        super(outputStream, outputFormat);
        this.formatStack = new Stack();
        this.lastText = "";
        this.tagsOuput = 0;
        this.newLineAfterNTags = -1;
        this.preformattedTags = DEFAULT_PREFORMATTED_TAGS;
    }

    public HTMLWriter(Writer writer) {
        super(writer, DEFAULT_HTML_FORMAT);
        this.formatStack = new Stack();
        this.lastText = "";
        this.tagsOuput = 0;
        this.newLineAfterNTags = -1;
        this.preformattedTags = DEFAULT_PREFORMATTED_TAGS;
    }

    public HTMLWriter(Writer writer, OutputFormat outputFormat) {
        super(writer, outputFormat);
        this.formatStack = new Stack();
        this.lastText = "";
        this.tagsOuput = 0;
        this.newLineAfterNTags = -1;
        this.preformattedTags = DEFAULT_PREFORMATTED_TAGS;
    }

    public HTMLWriter(OutputFormat outputFormat) throws UnsupportedEncodingException {
        super(outputFormat);
        this.formatStack = new Stack();
        this.lastText = "";
        this.tagsOuput = 0;
        this.newLineAfterNTags = -1;
        this.preformattedTags = DEFAULT_PREFORMATTED_TAGS;
    }

    private HashSet internalGetOmitElementCloseSet() {
        if (this.omitElementCloseSet == null) {
            this.omitElementCloseSet = new HashSet();
            this.loadOmitElementCloseSet((Set)this.omitElementCloseSet);
        }
        return this.omitElementCloseSet;
    }

    private String justSpaces(String string2) {
        int n = string2.length();
        StringBuffer stringBuffer = new StringBuffer(n);
        for (int i = 0; i < n; ++i) {
            char c = string2.charAt(i);
            switch (c) {
                default: {
                    stringBuffer.append(c);
                }
                case '\n': 
                case '\r': 
            }
        }
        return stringBuffer.toString();
    }

    private void lazyInitNewLinesAfterNTags() {
        if (this.getOutputFormat().isNewlines()) {
            this.newLineAfterNTags = 0;
            return;
        }
        this.newLineAfterNTags = this.getOutputFormat().getNewLineAfterNTags();
    }

    public static String prettyPrintHTML(String string2) throws IOException, UnsupportedEncodingException, DocumentException {
        return HTMLWriter.prettyPrintHTML(string2, true, true, false, true);
    }

    public static String prettyPrintHTML(String string2, boolean bl, boolean bl2, boolean bl3, boolean bl4) throws IOException, UnsupportedEncodingException, DocumentException {
        StringWriter stringWriter = new StringWriter();
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setNewlines(bl);
        outputFormat.setTrimText(bl2);
        outputFormat.setXHTML(bl3);
        outputFormat.setExpandEmptyElements(bl4);
        HTMLWriter hTMLWriter = new HTMLWriter((Writer)stringWriter, outputFormat);
        hTMLWriter.write(DocumentHelper.parseText(string2));
        hTMLWriter.flush();
        return stringWriter.toString();
    }

    public static String prettyPrintXHTML(String string2) throws IOException, UnsupportedEncodingException, DocumentException {
        return HTMLWriter.prettyPrintHTML(string2, true, true, true, false);
    }

    @Override
    public void endCDATA() throws SAXException {
    }

    public Set getOmitElementCloseSet() {
        return (Set)this.internalGetOmitElementCloseSet().clone();
    }

    public Set getPreformattedTags() {
        return (Set)this.preformattedTags.clone();
    }

    public boolean isPreformattedTag(String string2) {
        if (this.preformattedTags != null && this.preformattedTags.contains((Object)string2.toUpperCase())) {
            return true;
        }
        return false;
    }

    protected void loadOmitElementCloseSet(Set set) {
        set.add((Object)"AREA");
        set.add((Object)"BASE");
        set.add((Object)"BR");
        set.add((Object)"COL");
        set.add((Object)"HR");
        set.add((Object)"IMG");
        set.add((Object)"INPUT");
        set.add((Object)"LINK");
        set.add((Object)"META");
        set.add((Object)"P");
        set.add((Object)"PARAM");
    }

    protected boolean omitElementClose(String string2) {
        return super.internalGetOmitElementCloseSet().contains((Object)string2.toUpperCase());
    }

    public void setOmitElementCloseSet(Set set) {
        this.omitElementCloseSet = new HashSet();
        if (set != null) {
            this.omitElementCloseSet = new HashSet();
            for (Object object : set) {
                if (object == null) continue;
                this.omitElementCloseSet.add((Object)object.toString().toUpperCase());
            }
        }
    }

    public void setPreformattedTags(Set set) {
        this.preformattedTags = new HashSet();
        if (set != null) {
            for (Object object : set) {
                if (object == null) continue;
                this.preformattedTags.add((Object)object.toString().toUpperCase());
            }
        }
    }

    @Override
    public void startCDATA() throws SAXException {
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    protected void writeCDATA(String string2) throws IOException {
        if (this.getOutputFormat().isXHTML()) {
            super.writeCDATA(string2);
        } else {
            this.writer.write(string2);
        }
        this.lastOutputNodeType = 4;
    }

    @Override
    protected void writeClose(String string2) throws IOException {
        if (!this.omitElementClose(string2)) {
            super.writeClose(string2);
        }
    }

    @Override
    protected void writeDeclaration() throws IOException {
    }

    @Override
    protected void writeElement(Element element) throws IOException {
        if (this.newLineAfterNTags == -1) {
            super.lazyInitNewLinesAfterNTags();
        }
        if (this.newLineAfterNTags > 0 && this.tagsOuput > 0 && this.tagsOuput % this.newLineAfterNTags == 0) {
            this.writer.write(lineSeparator);
        }
        this.tagsOuput = 1 + this.tagsOuput;
        String string2 = element.getQualifiedName();
        String string3 = this.lastText;
        element.nodeCount();
        if (this.isPreformattedTag(string2)) {
            OutputFormat outputFormat = this.getOutputFormat();
            boolean bl = outputFormat.isNewlines();
            boolean bl2 = outputFormat.isTrimText();
            String string4 = outputFormat.getIndent();
            this.formatStack.push((Object)new FormatState((HTMLWriter)this, bl, bl2, string4));
            try {
                super.writePrintln();
                if (string3.trim().length() == 0 && string4 != null && string4.length() > 0) {
                    this.writer.write(super.justSpaces(string3));
                }
                outputFormat.setNewlines(false);
                outputFormat.setTrimText(false);
                outputFormat.setIndent("");
                super.writeElement(element);
                return;
            }
            finally {
                FormatState formatState = (FormatState)this.formatStack.pop();
                outputFormat.setNewlines(formatState.isNewlines());
                outputFormat.setTrimText(formatState.isTrimText());
                outputFormat.setIndent(formatState.getIndent());
            }
        }
        super.writeElement(element);
    }

    @Override
    protected void writeEmptyElementClose(String string2) throws IOException {
        if (this.getOutputFormat().isXHTML()) {
            if (this.omitElementClose(string2)) {
                this.writer.write(" />");
                return;
            }
            super.writeEmptyElementClose(string2);
            return;
        }
        if (this.omitElementClose(string2)) {
            this.writer.write(">");
            return;
        }
        super.writeEmptyElementClose(string2);
    }

    @Override
    protected void writeEntity(Entity entity) throws IOException {
        this.writer.write(entity.getText());
        this.lastOutputNodeType = 5;
    }

    @Override
    protected void writeString(String string2) throws IOException {
        if (string2.equals((Object)"\n")) {
            if (!this.formatStack.empty()) {
                super.writeString(lineSeparator);
            }
            return;
        }
        this.lastText = string2;
        if (this.formatStack.empty()) {
            super.writeString(string2.trim());
            return;
        }
        super.writeString(string2);
    }

    private class FormatState {
        private String indent;
        private boolean newlines;
        private final HTMLWriter this$0;
        private boolean trimText;

        public FormatState(HTMLWriter hTMLWriter, boolean bl, boolean bl2, String string2) {
            this.this$0 = hTMLWriter;
            this.newlines = false;
            this.trimText = false;
            this.indent = "";
            this.newlines = bl;
            this.trimText = bl2;
            this.indent = string2;
        }

        public String getIndent() {
            return this.indent;
        }

        public boolean isNewlines() {
            return this.newlines;
        }

        public boolean isTrimText() {
            return this.trimText;
        }
    }

}

