/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.IOException
 *  java.lang.Boolean
 *  java.lang.Class
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.util.HashMap
 *  java.util.Iterator
 *  java.util.List
 *  java.util.Map
 *  org.xml.sax.Attributes
 *  org.xml.sax.ContentHandler
 *  org.xml.sax.DTDHandler
 *  org.xml.sax.EntityResolver
 *  org.xml.sax.ErrorHandler
 *  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.AttributesImpl
 *  org.xml.sax.helpers.LocatorImpl
 */
package org.dom4j.io;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Branch;
import org.dom4j.CDATA;
import org.dom4j.CharacterData;
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.DocumentInputSource;
import org.dom4j.tree.NamespaceStack;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
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.AttributesImpl;
import org.xml.sax.helpers.LocatorImpl;

public class SAXWriter
implements XMLReader {
    protected static final String FEATURE_NAMESPACES = "http://xml.org/sax/features/namespaces";
    protected static final String FEATURE_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    protected static final String[] LEXICAL_HANDLER_NAMES = new String[]{"http://xml.org/sax/properties/lexical-handler", "http://xml.org/sax/handlers/LexicalHandler"};
    private AttributesImpl attributes;
    private ContentHandler contentHandler;
    private boolean declareNamespaceAttributes;
    private DTDHandler dtdHandler;
    private EntityResolver entityResolver;
    private ErrorHandler errorHandler;
    private Map features;
    private LexicalHandler lexicalHandler;
    private Map properties;

    public SAXWriter() {
        this.attributes = new AttributesImpl();
        this.features = new HashMap();
        this.properties = new HashMap();
        this.properties.put((Object)"http://xml.org/sax/features/namespace-prefixes", (Object)Boolean.FALSE);
        this.properties.put((Object)"http://xml.org/sax/features/namespace-prefixes", (Object)Boolean.TRUE);
    }

    public SAXWriter(ContentHandler contentHandler) {
        this.contentHandler = contentHandler;
    }

    public SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler) {
        this.contentHandler = contentHandler;
        this.lexicalHandler = lexicalHandler;
    }

    public SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler, EntityResolver entityResolver) {
        this.contentHandler = contentHandler;
        this.lexicalHandler = lexicalHandler;
        this.entityResolver = entityResolver;
    }

    protected AttributesImpl addNamespaceAttribute(AttributesImpl attributesImpl, Namespace namespace) {
        if (this.declareNamespaceAttributes) {
            if (attributesImpl == null) {
                attributesImpl = new AttributesImpl();
            }
            String string2 = namespace.getPrefix();
            String string3 = "xmlns";
            if (string2 != null && string2.length() > 0) {
                string3 = new StringBuffer().append("xmlns:").append(string2).toString();
            }
            String string4 = namespace.getURI();
            attributesImpl.addAttribute("", string2, string3, "CDATA", string4);
        }
        return attributesImpl;
    }

    protected void checkForNullHandlers() {
    }

    protected Attributes createAttributes(Element element, Attributes attributes) throws SAXException {
        this.attributes.clear();
        if (attributes != null) {
            this.attributes.setAttributes(attributes);
        }
        Iterator iterator = element.attributeIterator();
        while (iterator.hasNext()) {
            Attribute attribute = (Attribute)iterator.next();
            this.attributes.addAttribute(attribute.getNamespaceURI(), attribute.getName(), attribute.getQualifiedName(), "CDATA", attribute.getValue());
        }
        return this.attributes;
    }

    protected void documentLocator(Document document) throws SAXException {
        LocatorImpl locatorImpl = new LocatorImpl();
        DocumentType documentType = document.getDocType();
        String string2 = null;
        String string3 = null;
        if (documentType != null) {
            string2 = documentType.getPublicID();
            string3 = documentType.getSystemID();
        }
        if (string2 != null) {
            locatorImpl.setPublicId(string2);
        }
        if (string3 != null) {
            locatorImpl.setSystemId(string3);
        }
        locatorImpl.setLineNumber(-1);
        locatorImpl.setColumnNumber(-1);
        this.contentHandler.setDocumentLocator((Locator)locatorImpl);
    }

    protected void dtdHandler(Document document) throws SAXException {
    }

    protected void endDocument() throws SAXException {
        this.contentHandler.endDocument();
    }

    protected void endElement(Element element) throws SAXException {
        this.contentHandler.endElement(element.getNamespaceURI(), element.getName(), element.getQualifiedName());
    }

    protected void endPrefixMapping(NamespaceStack namespaceStack, int n) throws SAXException {
        while (namespaceStack.size() > n) {
            Namespace namespace = namespaceStack.pop();
            if (namespace == null) continue;
            this.contentHandler.endPrefixMapping(namespace.getPrefix());
        }
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected void entityResolver(Document document) throws SAXException {
        DocumentType documentType;
        if (this.entityResolver == null || (documentType = document.getDocType()) == null) return;
        String string2 = documentType.getPublicID();
        String string3 = documentType.getSystemID();
        if (string2 == null && string3 == null) return;
        try {
            this.entityResolver.resolveEntity(string2, string3);
            return;
        }
        catch (IOException var5_5) {
            throw new SAXException(new StringBuffer().append("Could not resolve publicID: ").append(string2).append(" systemID: ").append(string3).toString(), (Exception)var5_5);
        }
    }

    public ContentHandler getContentHandler() {
        return this.contentHandler;
    }

    public DTDHandler getDTDHandler() {
        return this.dtdHandler;
    }

    public EntityResolver getEntityResolver() {
        return this.entityResolver;
    }

    public ErrorHandler getErrorHandler() {
        return this.errorHandler;
    }

    public boolean getFeature(String string2) throws SAXNotRecognizedException, SAXNotSupportedException {
        Boolean bl = (Boolean)this.features.get((Object)string2);
        if (bl != null && bl.booleanValue()) {
            return true;
        }
        return false;
    }

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

    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 this.properties.get((Object)string2);
    }

    public boolean isDeclareNamespaceAttributes() {
        return this.declareNamespaceAttributes;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected boolean isIgnoreableNamespace(Namespace namespace, NamespaceStack namespaceStack) {
        String string2;
        if (namespace.equals(Namespace.NO_NAMESPACE) || namespace.equals(Namespace.XML_NAMESPACE) || (string2 = namespace.getURI()) == null || string2.length() <= 0) {
            return true;
        }
        return namespaceStack.contains(namespace);
    }

    public void parse(String string2) throws SAXNotSupportedException {
        throw new SAXNotSupportedException("This XMLReader can only accept <dom4j> InputSource objects");
    }

    public void parse(InputSource inputSource) throws SAXException {
        if (inputSource instanceof DocumentInputSource) {
            this.write(((DocumentInputSource)inputSource).getDocument());
            return;
        }
        throw new SAXNotSupportedException("This XMLReader can only accept <dom4j> InputSource objects");
    }

    public void setContentHandler(ContentHandler contentHandler) {
        this.contentHandler = contentHandler;
    }

    public void setDTDHandler(DTDHandler dTDHandler) {
        this.dtdHandler = dTDHandler;
    }

    public void setDeclareNamespaceAttributes(boolean bl) {
        this.declareNamespaceAttributes = bl;
    }

    public void setEntityResolver(EntityResolver entityResolver) {
        this.entityResolver = entityResolver;
    }

    public void setErrorHandler(ErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }

    /*
     * Enabled aggressive block sorting
     */
    public void setFeature(String string2, boolean bl) throws SAXNotRecognizedException, SAXNotSupportedException {
        if ("http://xml.org/sax/features/namespace-prefixes".equals((Object)string2)) {
            this.setDeclareNamespaceAttributes(bl);
        } else if ("http://xml.org/sax/features/namespace-prefixes".equals((Object)string2) && !bl) {
            throw new SAXNotSupportedException("Namespace feature is always supported in dom4j");
        }
        Map map = this.features;
        Boolean bl2 = bl ? Boolean.TRUE : Boolean.FALSE;
        map.put((Object)string2, (Object)bl2);
    }

    public void setLexicalHandler(LexicalHandler lexicalHandler) {
        this.lexicalHandler = lexicalHandler;
    }

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

    public void setXMLReader(XMLReader xMLReader) {
        this.setContentHandler(xMLReader.getContentHandler());
        this.setDTDHandler(xMLReader.getDTDHandler());
        this.setEntityResolver(xMLReader.getEntityResolver());
        this.setErrorHandler(xMLReader.getErrorHandler());
    }

    protected void startDocument() throws SAXException {
        this.contentHandler.startDocument();
    }

    protected void startElement(Element element, AttributesImpl attributesImpl) throws SAXException {
        this.contentHandler.startElement(element.getNamespaceURI(), element.getName(), element.getQualifiedName(), this.createAttributes(element, (Attributes)attributesImpl));
    }

    protected AttributesImpl startPrefixMapping(Element element, NamespaceStack namespaceStack) throws SAXException {
        Namespace namespace = element.getNamespace();
        AttributesImpl attributesImpl = null;
        if (namespace != null) {
            boolean bl = this.isIgnoreableNamespace(namespace, namespaceStack);
            attributesImpl = null;
            if (!bl) {
                namespaceStack.push(namespace);
                this.contentHandler.startPrefixMapping(namespace.getPrefix(), namespace.getURI());
                attributesImpl = this.addNamespaceAttribute(null, namespace);
            }
        }
        List list = element.declaredNamespaces();
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            Namespace namespace2 = (Namespace)list.get(i);
            if (this.isIgnoreableNamespace(namespace2, namespaceStack)) continue;
            namespaceStack.push(namespace2);
            this.contentHandler.startPrefixMapping(namespace2.getPrefix(), namespace2.getURI());
            attributesImpl = this.addNamespaceAttribute(attributesImpl, namespace2);
        }
        return attributesImpl;
    }

    public void write(String string2) throws SAXException {
        if (string2 != null) {
            char[] arrc = string2.toCharArray();
            this.contentHandler.characters(arrc, 0, arrc.length);
        }
    }

    public void write(CDATA cDATA) throws SAXException {
        String string2 = cDATA.getText();
        if (this.lexicalHandler != null) {
            this.lexicalHandler.startCDATA();
            this.write(string2);
            this.lexicalHandler.endCDATA();
            return;
        }
        this.write(string2);
    }

    public void write(Comment comment) throws SAXException {
        if (this.lexicalHandler != null) {
            char[] arrc = comment.getText().toCharArray();
            this.lexicalHandler.comment(arrc, 0, arrc.length);
        }
    }

    public void write(Document document) throws SAXException {
        if (document != null) {
            this.checkForNullHandlers();
            this.documentLocator(document);
            this.startDocument();
            this.entityResolver(document);
            this.dtdHandler(document);
            this.writeContent(document, new NamespaceStack());
            this.endDocument();
        }
    }

    public void write(Element element) throws SAXException {
        this.write(element, new NamespaceStack());
    }

    protected void write(Element element, NamespaceStack namespaceStack) throws SAXException {
        int n = namespaceStack.size();
        this.startElement(element, this.startPrefixMapping(element, namespaceStack));
        this.writeContent(element, namespaceStack);
        this.endElement(element);
        this.endPrefixMapping(namespaceStack, n);
    }

    public void write(Entity entity) throws SAXException {
        String string2 = entity.getText();
        if (this.lexicalHandler != null) {
            String string3 = entity.getName();
            this.lexicalHandler.startEntity(string3);
            this.write(string2);
            this.lexicalHandler.endEntity(string3);
            return;
        }
        this.write(string2);
    }

    public void write(Node node) throws SAXException {
        switch (node.getNodeType()) {
            default: {
                throw new SAXException(new StringBuffer().append("Invalid node type: ").append((Object)node).toString());
            }
            case 1: {
                this.write((Element)node);
            }
            case 13: {
                return;
            }
            case 2: {
                this.write((Attribute)node);
                return;
            }
            case 3: {
                this.write(node.getText());
                return;
            }
            case 4: {
                this.write((CDATA)node);
                return;
            }
            case 5: {
                this.write((Entity)node);
                return;
            }
            case 7: {
                this.write((ProcessingInstruction)node);
                return;
            }
            case 8: {
                this.write((Comment)node);
                return;
            }
            case 9: {
                this.write((Document)node);
                return;
            }
            case 10: 
        }
        this.write((DocumentType)node);
    }

    public void write(ProcessingInstruction processingInstruction) throws SAXException {
        String string2 = processingInstruction.getTarget();
        String string3 = processingInstruction.getText();
        this.contentHandler.processingInstruction(string2, string3);
    }

    public void writeClose(Element element) throws SAXException {
        this.endElement(element);
    }

    protected void writeContent(Branch branch, NamespaceStack namespaceStack) throws SAXException {
        Iterator iterator = branch.nodeIterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object instanceof Element) {
                this.write((Element)object, namespaceStack);
                continue;
            }
            if (object instanceof CharacterData) {
                if (object instanceof Text) {
                    this.write(((Text)object).getText());
                    continue;
                }
                if (object instanceof CDATA) {
                    this.write((CDATA)object);
                    continue;
                }
                if (object instanceof Comment) {
                    this.write((Comment)object);
                    continue;
                }
                throw new SAXException(new StringBuffer().append("Invalid Node in DOM4J content: ").append(object).append(" of type: ").append((Object)object.getClass()).toString());
            }
            if (object instanceof String) {
                this.write((String)object);
                continue;
            }
            if (object instanceof Entity) {
                this.write((Entity)object);
                continue;
            }
            if (object instanceof ProcessingInstruction) {
                this.write((ProcessingInstruction)object);
                continue;
            }
            if (object instanceof Namespace) {
                this.write((Namespace)object);
                continue;
            }
            throw new SAXException(new StringBuffer().append("Invalid Node in DOM4J content: ").append(object).toString());
        }
    }

    public void writeOpen(Element element) throws SAXException {
        this.startElement(element, null);
    }
}

