/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.File
 *  java.io.FileWriter
 *  java.io.IOException
 *  java.io.OutputStream
 *  java.io.StringWriter
 *  java.io.Writer
 *  java.lang.Object
 *  java.lang.RuntimeException
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.Throwable
 *  java.lang.UnsupportedOperationException
 *  java.util.Iterator
 *  java.util.List
 *  javax.xml.namespace.QName
 *  javax.xml.stream.XMLEventFactory
 *  javax.xml.stream.XMLEventWriter
 *  javax.xml.stream.XMLOutputFactory
 *  javax.xml.stream.XMLStreamException
 *  javax.xml.stream.events.Attribute
 *  javax.xml.stream.events.Characters
 *  javax.xml.stream.events.Comment
 *  javax.xml.stream.events.DTD
 *  javax.xml.stream.events.EndDocument
 *  javax.xml.stream.events.EndElement
 *  javax.xml.stream.events.EntityDeclaration
 *  javax.xml.stream.events.EntityReference
 *  javax.xml.stream.events.Namespace
 *  javax.xml.stream.events.ProcessingInstruction
 *  javax.xml.stream.events.StartDocument
 *  javax.xml.stream.events.StartElement
 *  javax.xml.stream.events.XMLEvent
 *  javax.xml.stream.util.XMLEventConsumer
 */
package org.dom4j.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.DTD;
import javax.xml.stream.events.EndDocument;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.EntityDeclaration;
import javax.xml.stream.events.EntityReference;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.util.XMLEventConsumer;
import org.dom4j.Attribute;
import org.dom4j.Branch;
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.QName;
import org.dom4j.Text;

public class STAXEventWriter {
    private XMLEventConsumer consumer;
    private XMLEventFactory factory;
    private XMLOutputFactory outputFactory;

    public STAXEventWriter() {
        this.factory = XMLEventFactory.newInstance();
        this.outputFactory = XMLOutputFactory.newInstance();
    }

    public STAXEventWriter(File file) throws XMLStreamException, IOException {
        this.factory = XMLEventFactory.newInstance();
        this.outputFactory = XMLOutputFactory.newInstance();
        this.consumer = this.outputFactory.createXMLEventWriter((Writer)new FileWriter(file));
    }

    public STAXEventWriter(OutputStream outputStream) throws XMLStreamException {
        this.factory = XMLEventFactory.newInstance();
        this.outputFactory = XMLOutputFactory.newInstance();
        this.consumer = this.outputFactory.createXMLEventWriter(outputStream);
    }

    public STAXEventWriter(Writer writer) throws XMLStreamException {
        this.factory = XMLEventFactory.newInstance();
        this.outputFactory = XMLOutputFactory.newInstance();
        this.consumer = this.outputFactory.createXMLEventWriter(writer);
    }

    public STAXEventWriter(XMLEventConsumer xMLEventConsumer) {
        this.factory = XMLEventFactory.newInstance();
        this.outputFactory = XMLOutputFactory.newInstance();
        this.consumer = xMLEventConsumer;
    }

    static XMLEventFactory access$000(STAXEventWriter sTAXEventWriter) {
        return sTAXEventWriter.factory;
    }

    private EntityReference createEntityReference(Entity entity) {
        return this.factory.createEntityReference(entity.getName(), null);
    }

    public javax.xml.stream.events.Attribute createAttribute(Attribute attribute) {
        javax.xml.namespace.QName qName = this.createQName(attribute.getQName());
        String string2 = attribute.getValue();
        return this.factory.createAttribute(qName, string2);
    }

    public Characters createCharacters(CDATA cDATA) {
        return this.factory.createCData(cDATA.getText());
    }

    public Characters createCharacters(Text text) {
        return this.factory.createCharacters(text.getText());
    }

    public javax.xml.stream.events.Comment createComment(Comment comment) {
        return this.factory.createComment(comment.getText());
    }

    public DTD createDTD(DocumentType documentType) {
        StringWriter stringWriter = new StringWriter();
        try {
            documentType.write((Writer)stringWriter);
        }
        catch (IOException var3_3) {
            throw new RuntimeException("Error writing DTD", (Throwable)var3_3);
        }
        return this.factory.createDTD(stringWriter.toString());
    }

    public EndDocument createEndDocument(Document document) {
        return this.factory.createEndDocument();
    }

    public EndElement createEndElement(Element element) {
        javax.xml.namespace.QName qName = this.createQName(element.getQName());
        NamespaceIterator namespaceIterator = new NamespaceIterator((STAXEventWriter)this, element.declaredNamespaces().iterator());
        return this.factory.createEndElement(qName, (Iterator)namespaceIterator);
    }

    public javax.xml.stream.events.Namespace createNamespace(Namespace namespace) {
        String string2 = namespace.getPrefix();
        String string3 = namespace.getURI();
        return this.factory.createNamespace(string2, string3);
    }

    public javax.xml.stream.events.ProcessingInstruction createProcessingInstruction(ProcessingInstruction processingInstruction) {
        String string2 = processingInstruction.getTarget();
        String string3 = processingInstruction.getText();
        return this.factory.createProcessingInstruction(string2, string3);
    }

    public javax.xml.namespace.QName createQName(QName qName) {
        return new javax.xml.namespace.QName(qName.getNamespaceURI(), qName.getName(), qName.getNamespacePrefix());
    }

    public StartDocument createStartDocument(Document document) {
        String string2 = document.getXMLEncoding();
        if (string2 != null) {
            return this.factory.createStartDocument(string2);
        }
        return this.factory.createStartDocument();
    }

    public StartElement createStartElement(Element element) {
        javax.xml.namespace.QName qName = this.createQName(element.getQName());
        AttributeIterator attributeIterator = new AttributeIterator((STAXEventWriter)this, element.attributeIterator());
        NamespaceIterator namespaceIterator = new NamespaceIterator((STAXEventWriter)this, element.declaredNamespaces().iterator());
        return this.factory.createStartElement(qName, (Iterator)attributeIterator, (Iterator)namespaceIterator);
    }

    public XMLEventConsumer getConsumer() {
        return this.consumer;
    }

    public XMLEventFactory getEventFactory() {
        return this.factory;
    }

    public void setConsumer(XMLEventConsumer xMLEventConsumer) {
        this.consumer = xMLEventConsumer;
    }

    public void setEventFactory(XMLEventFactory xMLEventFactory) {
        this.factory = xMLEventFactory;
    }

    public void writeAttribute(Attribute attribute) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createAttribute(attribute));
    }

    public void writeCDATA(CDATA cDATA) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createCharacters(cDATA));
    }

    public void writeChildNodes(Branch branch) throws XMLStreamException {
        int n = branch.nodeCount();
        for (int i = 0; i < n; ++i) {
            this.writeNode(branch.node(i));
        }
    }

    public void writeComment(Comment comment) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createComment(comment));
    }

    public void writeDocument(Document document) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createStartDocument(document));
        this.writeChildNodes(document);
        this.consumer.add((XMLEvent)this.createEndDocument(document));
    }

    public void writeDocumentType(DocumentType documentType) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createDTD(documentType));
    }

    public void writeElement(Element element) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createStartElement(element));
        this.writeChildNodes(element);
        this.consumer.add((XMLEvent)this.createEndElement(element));
    }

    public void writeEntity(Entity entity) throws XMLStreamException {
        this.consumer.add((XMLEvent)super.createEntityReference(entity));
    }

    public void writeNamespace(Namespace namespace) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createNamespace(namespace));
    }

    public void writeNode(Node node) throws XMLStreamException {
        switch (node.getNodeType()) {
            default: {
                throw new XMLStreamException(new StringBuffer().append("Unsupported DOM4J Node: ").append((Object)node).toString());
            }
            case 1: {
                this.writeElement((Element)node);
                return;
            }
            case 3: {
                this.writeText((Text)node);
                return;
            }
            case 2: {
                this.writeAttribute((Attribute)node);
                return;
            }
            case 13: {
                this.writeNamespace((Namespace)node);
                return;
            }
            case 8: {
                this.writeComment((Comment)node);
                return;
            }
            case 4: {
                this.writeCDATA((CDATA)node);
                return;
            }
            case 7: {
                this.writeProcessingInstruction((ProcessingInstruction)node);
                return;
            }
            case 5: {
                this.writeEntity((Entity)node);
                return;
            }
            case 9: {
                this.writeDocument((Document)node);
                return;
            }
            case 10: 
        }
        this.writeDocumentType((DocumentType)node);
    }

    public void writeProcessingInstruction(ProcessingInstruction processingInstruction) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createProcessingInstruction(processingInstruction));
    }

    public void writeText(Text text) throws XMLStreamException {
        this.consumer.add((XMLEvent)this.createCharacters(text));
    }

    private class AttributeIterator
    implements Iterator {
        private Iterator iter;
        private final STAXEventWriter this$0;

        public AttributeIterator(STAXEventWriter sTAXEventWriter, Iterator iterator) {
            this.this$0 = sTAXEventWriter;
            this.iter = iterator;
        }

        public boolean hasNext() {
            return this.iter.hasNext();
        }

        public Object next() {
            Attribute attribute = (Attribute)this.iter.next();
            javax.xml.namespace.QName qName = this.this$0.createQName(attribute.getQName());
            String string2 = attribute.getValue();
            return STAXEventWriter.access$000(this.this$0).createAttribute(qName, string2);
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    private class NamespaceIterator
    implements Iterator {
        private Iterator iter;
        private final STAXEventWriter this$0;

        public NamespaceIterator(STAXEventWriter sTAXEventWriter, Iterator iterator) {
            this.this$0 = sTAXEventWriter;
            this.iter = iterator;
        }

        public boolean hasNext() {
            return this.iter.hasNext();
        }

        public Object next() {
            Namespace namespace = (Namespace)this.iter.next();
            String string2 = namespace.getPrefix();
            String string3 = namespace.getURI();
            return STAXEventWriter.access$000(this.this$0).createNamespace(string2, string3);
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

}

