3 var XMLBuilder, XMLDeclaration, XMLDocType, XMLElement, XMLStringifier;
5 XMLStringifier = require(
'./XMLStringifier');
7 XMLDeclaration = require(
'./XMLDeclaration');
9 XMLDocType = require(
'./XMLDocType');
11 XMLElement = require(
'./XMLElement');
13 module.exports = XMLBuilder = (
function() {
14 function XMLBuilder(name, options) {
17 throw new Error(
"Root element needs a name");
19 if (options == null) {
22 this.options = options;
23 this.stringify =
new XMLStringifier(options);
24 temp =
new XMLElement(
this,
'doc');
25 root = temp.element(name);
27 root.documentObject =
this;
28 this.rootObject = root;
29 if (!options.headless) {
30 root.declaration(options);
31 if ((options.pubID != null) || (options.sysID != null)) {
32 root.doctype(options);
37 XMLBuilder.prototype.root =
function() {
38 return this.rootObject;
41 XMLBuilder.prototype.end =
function(options) {
42 return this.toString(options);
45 XMLBuilder.prototype.toString =
function(options) {
46 var indent, newline, offset, pretty, r, ref, ref1, ref2;
47 pretty = (options != null ? options.pretty :
void 0) ||
false;
48 indent = (ref = options != null ? options.indent :
void 0) != null ? ref :
' ';
49 offset = (ref1 = options != null ? options.offset :
void 0) != null ? ref1 : 0;
50 newline = (ref2 = options != null ? options.newline :
void 0) != null ? ref2 :
'\n';
52 if (this.xmldec != null) {
53 r += this.xmldec.toString(options);
55 if (this.doctype != null) {
56 r += this.doctype.toString(options);
58 r += this.rootObject.toString(options);
59 if (pretty && r.slice(-newline.length) === newline) {
60 r = r.slice(0, -newline.length);