// Generated by CoffeeScript 1.12.7 (function() {

var XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStreamWriter, XMLText, XMLWriterBase,
  extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  hasProp = {}.hasOwnProperty;

XMLDeclaration = require('./XMLDeclaration');

XMLDocType = require('./XMLDocType');

XMLCData = require('./XMLCData');

XMLComment = require('./XMLComment');

XMLElement = require('./XMLElement');

XMLRaw = require('./XMLRaw');

XMLText = require('./XMLText');

XMLProcessingInstruction = require('./XMLProcessingInstruction');

XMLDTDAttList = require('./XMLDTDAttList');

XMLDTDElement = require('./XMLDTDElement');

XMLDTDEntity = require('./XMLDTDEntity');

XMLDTDNotation = require('./XMLDTDNotation');

XMLWriterBase = require('./XMLWriterBase');

module.exports = XMLStreamWriter = (function(superClass) {
  extend(XMLStreamWriter, superClass);

  function XMLStreamWriter(stream, options) {
    XMLStreamWriter.__super__.constructor.call(this, options);
    this.stream = stream;
  }

  XMLStreamWriter.prototype.document = function(doc) {
    var child, i, j, len, len1, ref, ref1, results;
    ref = doc.children;
    for (i = 0, len = ref.length; i < len; i++) {
      child = ref[i];
      child.isLastRootNode = false;
    }
    doc.children[doc.children.length - 1].isLastRootNode = true;
    ref1 = doc.children;
    results = [];
    for (j = 0, len1 = ref1.length; j < len1; j++) {
      child = ref1[j];
      switch (false) {
        case !(child instanceof XMLDeclaration):
          results.push(this.declaration(child));
          break;
        case !(child instanceof XMLDocType):
          results.push(this.docType(child));
          break;
        case !(child instanceof XMLComment):
          results.push(this.comment(child));
          break;
        case !(child instanceof XMLProcessingInstruction):
          results.push(this.processingInstruction(child));
          break;
        default:
          results.push(this.element(child));
      }
    }
    return results;
  };

  XMLStreamWriter.prototype.attribute = function(att) {
    return this.stream.write(' ' + att.name + '="' + att.value + '"');
  };

  XMLStreamWriter.prototype.cdata = function(node, level) {
    return this.stream.write(this.space(level) + '<![CDATA[' + node.text + ']]>' + this.endline(node));
  };

  XMLStreamWriter.prototype.comment = function(node, level) {
    return this.stream.write(this.space(level) + '<!-- ' + node.text + ' -->' + this.endline(node));
  };

  XMLStreamWriter.prototype.declaration = function(node, level) {
    this.stream.write(this.space(level));
    this.stream.write('<?xml version="' + node.version + '"');
    if (node.encoding != null) {
      this.stream.write(' encoding="' + node.encoding + '"');
    }
    if (node.standalone != null) {
      this.stream.write(' standalone="' + node.standalone + '"');
    }
    this.stream.write(this.spacebeforeslash + '?>');
    return this.stream.write(this.endline(node));
  };

  XMLStreamWriter.prototype.docType = function(node, level) {
    var child, i, len, ref;
    level || (level = 0);
    this.stream.write(this.space(level));
    this.stream.write('<!DOCTYPE ' + node.root().name);
    if (node.pubID && node.sysID) {
      this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
    } else if (node.sysID) {
      this.stream.write(' SYSTEM "' + node.sysID + '"');
    }
    if (node.children.length > 0) {
      this.stream.write(' [');
      this.stream.write(this.endline(node));
      ref = node.children;
      for (i = 0, len = ref.length; i < len; i++) {
        child = ref[i];
        switch (false) {
          case !(child instanceof XMLDTDAttList):
            this.dtdAttList(child, level + 1);
            break;
          case !(child instanceof XMLDTDElement):
            this.dtdElement(child, level + 1);
            break;
          case !(child instanceof XMLDTDEntity):
            this.dtdEntity(child, level + 1);
            break;
          case !(child instanceof XMLDTDNotation):
            this.dtdNotation(child, level + 1);
            break;
          case !(child instanceof XMLCData):
            this.cdata(child, level + 1);
            break;
          case !(child instanceof XMLComment):
            this.comment(child, level + 1);
            break;
          case !(child instanceof XMLProcessingInstruction):
            this.processingInstruction(child, level + 1);
            break;
          default:
            throw new Error("Unknown DTD node type: " + child.constructor.name);
        }
      }
      this.stream.write(']');
    }
    this.stream.write(this.spacebeforeslash + '>');
    return this.stream.write(this.endline(node));
  };

  XMLStreamWriter.prototype.element = function(node, level) {
    var att, child, i, len, name, ref, ref1, space;
    level || (level = 0);
    space = this.space(level);
    this.stream.write(space + '<' + node.name);
    ref = node.attributes;
    for (name in ref) {
      if (!hasProp.call(ref, name)) continue;
      att = ref[name];
      this.attribute(att);
    }
    if (node.children.length === 0 || node.children.every(function(e) {
      return e.value === '';
    })) {
      if (this.allowEmpty) {
        this.stream.write('></' + node.name + '>');
      } else {
        this.stream.write(this.spacebeforeslash + '/>');
      }
    } else if (this.pretty && node.children.length === 1 && (node.children[0].value != null)) {
      this.stream.write('>');
      this.stream.write(node.children[0].value);
      this.stream.write('</' + node.name + '>');
    } else {
      this.stream.write('>' + this.newline);
      ref1 = node.children;
      for (i = 0, len = ref1.length; i < len; i++) {
        child = ref1[i];
        switch (false) {
          case !(child instanceof XMLCData):
            this.cdata(child, level + 1);
            break;
          case !(child instanceof XMLComment):
            this.comment(child, level + 1);
            break;
          case !(child instanceof XMLElement):
            this.element(child, level + 1);
            break;
          case !(child instanceof XMLRaw):
            this.raw(child, level + 1);
            break;
          case !(child instanceof XMLText):
            this.text(child, level + 1);
            break;
          case !(child instanceof XMLProcessingInstruction):
            this.processingInstruction(child, level + 1);
            break;
          default:
            throw new Error("Unknown XML node type: " + child.constructor.name);
        }
      }
      this.stream.write(space + '</' + node.name + '>');
    }
    return this.stream.write(this.endline(node));
  };

  XMLStreamWriter.prototype.processingInstruction = function(node, level) {
    this.stream.write(this.space(level) + '<?' + node.target);
    if (node.value) {
      this.stream.write(' ' + node.value);
    }
    return this.stream.write(this.spacebeforeslash + '?>' + this.endline(node));
  };

  XMLStreamWriter.prototype.raw = function(node, level) {
    return this.stream.write(this.space(level) + node.value + this.endline(node));
  };

  XMLStreamWriter.prototype.text = function(node, level) {
    return this.stream.write(this.space(level) + node.value + this.endline(node));
  };

  XMLStreamWriter.prototype.dtdAttList = function(node, level) {
    this.stream.write(this.space(level) + '<!ATTLIST ' + node.elementName + ' ' + node.attributeName + ' ' + node.attributeType);
    if (node.defaultValueType !== '#DEFAULT') {
      this.stream.write(' ' + node.defaultValueType);
    }
    if (node.defaultValue) {
      this.stream.write(' "' + node.defaultValue + '"');
    }
    return this.stream.write(this.spacebeforeslash + '>' + this.endline(node));
  };

  XMLStreamWriter.prototype.dtdElement = function(node, level) {
    this.stream.write(this.space(level) + '<!ELEMENT ' + node.name + ' ' + node.value);
    return this.stream.write(this.spacebeforeslash + '>' + this.endline(node));
  };

  XMLStreamWriter.prototype.dtdEntity = function(node, level) {
    this.stream.write(this.space(level) + '<!ENTITY');
    if (node.pe) {
      this.stream.write(' %');
    }
    this.stream.write(' ' + node.name);
    if (node.value) {
      this.stream.write(' "' + node.value + '"');
    } else {
      if (node.pubID && node.sysID) {
        this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
      } else if (node.sysID) {
        this.stream.write(' SYSTEM "' + node.sysID + '"');
      }
      if (node.nData) {
        this.stream.write(' NDATA ' + node.nData);
      }
    }
    return this.stream.write(this.spacebeforeslash + '>' + this.endline(node));
  };

  XMLStreamWriter.prototype.dtdNotation = function(node, level) {
    this.stream.write(this.space(level) + '<!NOTATION ' + node.name);
    if (node.pubID && node.sysID) {
      this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
    } else if (node.pubID) {
      this.stream.write(' PUBLIC "' + node.pubID + '"');
    } else if (node.sysID) {
      this.stream.write(' SYSTEM "' + node.sysID + '"');
    }
    return this.stream.write(this.spacebeforeslash + '>' + this.endline(node));
  };

  XMLStreamWriter.prototype.endline = function(node) {
    if (!node.isLastRootNode) {
      return this.newline;
    } else {
      return '';
    }
  };

  return XMLStreamWriter;

})(XMLWriterBase);

}).call(this);