// Generated by CoffeeScript 1.12.7 (function() {

var XMLAttribute, XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDocumentCB, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStringWriter, XMLStringifier, XMLText, isFunction, isObject, isPlainObject, ref,
  hasProp = {}.hasOwnProperty;

ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction, isPlainObject = ref.isPlainObject;

XMLElement = require('./XMLElement');

XMLCData = require('./XMLCData');

XMLComment = require('./XMLComment');

XMLRaw = require('./XMLRaw');

XMLText = require('./XMLText');

XMLProcessingInstruction = require('./XMLProcessingInstruction');

XMLDeclaration = require('./XMLDeclaration');

XMLDocType = require('./XMLDocType');

XMLDTDAttList = require('./XMLDTDAttList');

XMLDTDEntity = require('./XMLDTDEntity');

XMLDTDElement = require('./XMLDTDElement');

XMLDTDNotation = require('./XMLDTDNotation');

XMLAttribute = require('./XMLAttribute');

XMLStringifier = require('./XMLStringifier');

XMLStringWriter = require('./XMLStringWriter');

module.exports = XMLDocumentCB = (function() {
  function XMLDocumentCB(options, onData, onEnd) {
    var writerOptions;
    options || (options = {});
    if (!options.writer) {
      options.writer = new XMLStringWriter(options);
    } else if (isPlainObject(options.writer)) {
      writerOptions = options.writer;
      options.writer = new XMLStringWriter(writerOptions);
    }
    this.options = options;
    this.writer = options.writer;
    this.stringify = new XMLStringifier(options);
    this.onDataCallback = onData || function() {};
    this.onEndCallback = onEnd || function() {};
    this.currentNode = null;
    this.currentLevel = -1;
    this.openTags = {};
    this.documentStarted = false;
    this.documentCompleted = false;
    this.root = null;
  }

  XMLDocumentCB.prototype.node = function(name, attributes, text) {
    var ref1;
    if (name == null) {
      throw new Error("Missing node name");
    }
    if (this.root && this.currentLevel === -1) {
      throw new Error("Document can only have one root node");
    }
    this.openCurrent();
    name = name.valueOf();
    if (attributes == null) {
      attributes = {};
    }
    attributes = attributes.valueOf();
    if (!isObject(attributes)) {
      ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
    }
    this.currentNode = new XMLElement(this, name, attributes);
    this.currentNode.children = false;
    this.currentLevel++;
    this.openTags[this.currentLevel] = this.currentNode;
    if (text != null) {
      this.text(text);
    }
    return this;
  };

  XMLDocumentCB.prototype.element = function(name, attributes, text) {
    if (this.currentNode && this.currentNode instanceof XMLDocType) {
      return this.dtdElement.apply(this, arguments);
    } else {
      return this.node(name, attributes, text);
    }
  };

  XMLDocumentCB.prototype.attribute = function(name, value) {
    var attName, attValue;
    if (!this.currentNode || this.currentNode.children) {
      throw new Error("att() can only be used immediately after an ele() call in callback mode");
    }
    if (name != null) {
      name = name.valueOf();
    }
    if (isObject(name)) {
      for (attName in name) {
        if (!hasProp.call(name, attName)) continue;
        attValue = name[attName];
        this.attribute(attName, attValue);
      }
    } else {
      if (isFunction(value)) {
        value = value.apply();
      }
      if (!this.options.skipNullAttributes || (value != null)) {
        this.currentNode.attributes[name] = new XMLAttribute(this, name, value);
      }
    }
    return this;
  };

  XMLDocumentCB.prototype.text = function(value) {
    var node;
    this.openCurrent();
    node = new XMLText(this, value);
    this.onData(this.writer.text(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.cdata = function(value) {
    var node;
    this.openCurrent();
    node = new XMLCData(this, value);
    this.onData(this.writer.cdata(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.comment = function(value) {
    var node;
    this.openCurrent();
    node = new XMLComment(this, value);
    this.onData(this.writer.comment(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.raw = function(value) {
    var node;
    this.openCurrent();
    node = new XMLRaw(this, value);
    this.onData(this.writer.raw(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.instruction = function(target, value) {
    var i, insTarget, insValue, len, node;
    this.openCurrent();
    if (target != null) {
      target = target.valueOf();
    }
    if (value != null) {
      value = value.valueOf();
    }
    if (Array.isArray(target)) {
      for (i = 0, len = target.length; i < len; i++) {
        insTarget = target[i];
        this.instruction(insTarget);
      }
    } else if (isObject(target)) {
      for (insTarget in target) {
        if (!hasProp.call(target, insTarget)) continue;
        insValue = target[insTarget];
        this.instruction(insTarget, insValue);
      }
    } else {
      if (isFunction(value)) {
        value = value.apply();
      }
      node = new XMLProcessingInstruction(this, target, value);
      this.onData(this.writer.processingInstruction(node, this.currentLevel + 1));
    }
    return this;
  };

  XMLDocumentCB.prototype.declaration = function(version, encoding, standalone) {
    var node;
    this.openCurrent();
    if (this.documentStarted) {
      throw new Error("declaration() must be the first node");
    }
    node = new XMLDeclaration(this, version, encoding, standalone);
    this.onData(this.writer.declaration(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.doctype = function(root, pubID, sysID) {
    this.openCurrent();
    if (root == null) {
      throw new Error("Missing root node name");
    }
    if (this.root) {
      throw new Error("dtd() must come before the root node");
    }
    this.currentNode = new XMLDocType(this, pubID, sysID);
    this.currentNode.rootNodeName = root;
    this.currentNode.children = false;
    this.currentLevel++;
    this.openTags[this.currentLevel] = this.currentNode;
    return this;
  };

  XMLDocumentCB.prototype.dtdElement = function(name, value) {
    var node;
    this.openCurrent();
    node = new XMLDTDElement(this, name, value);
    this.onData(this.writer.dtdElement(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.attList = function(elementName, attributeName, attributeType, defaultValueType, defaultValue) {
    var node;
    this.openCurrent();
    node = new XMLDTDAttList(this, elementName, attributeName, attributeType, defaultValueType, defaultValue);
    this.onData(this.writer.dtdAttList(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.entity = function(name, value) {
    var node;
    this.openCurrent();
    node = new XMLDTDEntity(this, false, name, value);
    this.onData(this.writer.dtdEntity(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.pEntity = function(name, value) {
    var node;
    this.openCurrent();
    node = new XMLDTDEntity(this, true, name, value);
    this.onData(this.writer.dtdEntity(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.notation = function(name, value) {
    var node;
    this.openCurrent();
    node = new XMLDTDNotation(this, name, value);
    this.onData(this.writer.dtdNotation(node, this.currentLevel + 1));
    return this;
  };

  XMLDocumentCB.prototype.up = function() {
    if (this.currentLevel < 0) {
      throw new Error("The document node has no parent");
    }
    if (this.currentNode) {
      if (this.currentNode.children) {
        this.closeNode(this.currentNode);
      } else {
        this.openNode(this.currentNode);
      }
      this.currentNode = null;
    } else {
      this.closeNode(this.openTags[this.currentLevel]);
    }
    delete this.openTags[this.currentLevel];
    this.currentLevel--;
    return this;
  };

  XMLDocumentCB.prototype.end = function() {
    while (this.currentLevel >= 0) {
      this.up();
    }
    return this.onEnd();
  };

  XMLDocumentCB.prototype.openCurrent = function() {
    if (this.currentNode) {
      this.currentNode.children = true;
      return this.openNode(this.currentNode);
    }
  };

  XMLDocumentCB.prototype.openNode = function(node) {
    if (!node.isOpen) {
      if (!this.root && this.currentLevel === 0 && node instanceof XMLElement) {
        this.root = node;
      }
      this.onData(this.writer.openNode(node, this.currentLevel));
      return node.isOpen = true;
    }
  };

  XMLDocumentCB.prototype.closeNode = function(node) {
    if (!node.isClosed) {
      this.onData(this.writer.closeNode(node, this.currentLevel));
      return node.isClosed = true;
    }
  };

  XMLDocumentCB.prototype.onData = function(chunk) {
    this.documentStarted = true;
    return this.onDataCallback(chunk);
  };

  XMLDocumentCB.prototype.onEnd = function() {
    this.documentCompleted = true;
    return this.onEndCallback();
  };

  XMLDocumentCB.prototype.ele = function() {
    return this.element.apply(this, arguments);
  };

  XMLDocumentCB.prototype.nod = function(name, attributes, text) {
    return this.node(name, attributes, text);
  };

  XMLDocumentCB.prototype.txt = function(value) {
    return this.text(value);
  };

  XMLDocumentCB.prototype.dat = function(value) {
    return this.cdata(value);
  };

  XMLDocumentCB.prototype.com = function(value) {
    return this.comment(value);
  };

  XMLDocumentCB.prototype.ins = function(target, value) {
    return this.instruction(target, value);
  };

  XMLDocumentCB.prototype.dec = function(version, encoding, standalone) {
    return this.declaration(version, encoding, standalone);
  };

  XMLDocumentCB.prototype.dtd = function(root, pubID, sysID) {
    return this.doctype(root, pubID, sysID);
  };

  XMLDocumentCB.prototype.e = function(name, attributes, text) {
    return this.element(name, attributes, text);
  };

  XMLDocumentCB.prototype.n = function(name, attributes, text) {
    return this.node(name, attributes, text);
  };

  XMLDocumentCB.prototype.t = function(value) {
    return this.text(value);
  };

  XMLDocumentCB.prototype.d = function(value) {
    return this.cdata(value);
  };

  XMLDocumentCB.prototype.c = function(value) {
    return this.comment(value);
  };

  XMLDocumentCB.prototype.r = function(value) {
    return this.raw(value);
  };

  XMLDocumentCB.prototype.i = function(target, value) {
    return this.instruction(target, value);
  };

  XMLDocumentCB.prototype.att = function() {
    if (this.currentNode && this.currentNode instanceof XMLDocType) {
      return this.attList.apply(this, arguments);
    } else {
      return this.attribute.apply(this, arguments);
    }
  };

  XMLDocumentCB.prototype.a = function() {
    if (this.currentNode && this.currentNode instanceof XMLDocType) {
      return this.attList.apply(this, arguments);
    } else {
      return this.attribute.apply(this, arguments);
    }
  };

  XMLDocumentCB.prototype.ent = function(name, value) {
    return this.entity(name, value);
  };

  XMLDocumentCB.prototype.pent = function(name, value) {
    return this.pEntity(name, value);
  };

  XMLDocumentCB.prototype.not = function(name, value) {
    return this.notation(name, value);
  };

  return XMLDocumentCB;

})();

}).call(this);