00001
00002 (function() {
00003 var XMLBuilder, XMLDeclaration, XMLDocType, XMLElement, XMLStringifier;
00004
00005 XMLStringifier = require('./XMLStringifier');
00006
00007 XMLDeclaration = require('./XMLDeclaration');
00008
00009 XMLDocType = require('./XMLDocType');
00010
00011 XMLElement = require('./XMLElement');
00012
00013 module.exports = XMLBuilder = (function() {
00014 function XMLBuilder(name, options) {
00015 var root, temp;
00016 if (name == null) {
00017 throw new Error("Root element needs a name");
00018 }
00019 if (options == null) {
00020 options = {};
00021 }
00022 this.options = options;
00023 this.stringify = new XMLStringifier(options);
00024 temp = new XMLElement(this, 'doc');
00025 root = temp.element(name);
00026 root.isRoot = true;
00027 root.documentObject = this;
00028 this.rootObject = root;
00029 if (!options.headless) {
00030 root.declaration(options);
00031 if ((options.pubID != null) || (options.sysID != null)) {
00032 root.doctype(options);
00033 }
00034 }
00035 }
00036
00037 XMLBuilder.prototype.root = function() {
00038 return this.rootObject;
00039 };
00040
00041 XMLBuilder.prototype.end = function(options) {
00042 return this.toString(options);
00043 };
00044
00045 XMLBuilder.prototype.toString = function(options) {
00046 var indent, newline, offset, pretty, r, ref, ref1, ref2;
00047 pretty = (options != null ? options.pretty : void 0) || false;
00048 indent = (ref = options != null ? options.indent : void 0) != null ? ref : ' ';
00049 offset = (ref1 = options != null ? options.offset : void 0) != null ? ref1 : 0;
00050 newline = (ref2 = options != null ? options.newline : void 0) != null ? ref2 : '\n';
00051 r = '';
00052 if (this.xmldec != null) {
00053 r += this.xmldec.toString(options);
00054 }
00055 if (this.doctype != null) {
00056 r += this.doctype.toString(options);
00057 }
00058 r += this.rootObject.toString(options);
00059 if (pretty && r.slice(-newline.length) === newline) {
00060 r = r.slice(0, -newline.length);
00061 }
00062 return r;
00063 };
00064
00065 return XMLBuilder;
00066
00067 })();
00068
00069 }).call(this);