artdaq_node_server  v1_00_09
 All Classes Namespaces Files Variables Pages
XMLStringifier.js
1 // Generated by CoffeeScript 1.10.0
2 (function() {
3  var XMLStringifier, camelCase, kebabCase, ref, snakeCase, titleCase,
4  bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
5  hasProp = {}.hasOwnProperty;
6 
7  ref = require('./Utility'), camelCase = ref.camelCase, titleCase = ref.titleCase, kebabCase = ref.kebabCase, snakeCase = ref.snakeCase;
8 
9  module.exports = XMLStringifier = (function() {
10  function XMLStringifier(options) {
11  this.assertLegalChar = bind(this.assertLegalChar, this);
12  var key, ref1, value;
13  options || (options = {});
14  this.allowSurrogateChars = options.allowSurrogateChars;
15  this.noDoubleEncoding = options.noDoubleEncoding;
16  this.textCase = options.textCase;
17  ref1 = options.stringify || {};
18  for (key in ref1) {
19  if (!hasProp.call(ref1, key)) continue;
20  value = ref1[key];
21  this[key] = value;
22  }
23  }
24 
25  XMLStringifier.prototype.eleName = function(val) {
26  val = '' + val || '';
27  val = this.applyCase(val);
28  return this.assertLegalChar(val);
29  };
30 
31  XMLStringifier.prototype.eleText = function(val) {
32  val = '' + val || '';
33  return this.assertLegalChar(this.elEscape(val));
34  };
35 
36  XMLStringifier.prototype.cdata = function(val) {
37  val = '' + val || '';
38  val = val.replace(']]>', ']]]]><![CDATA[>');
39  return this.assertLegalChar(val);
40  };
41 
42  XMLStringifier.prototype.comment = function(val) {
43  val = '' + val || '';
44  if (val.match(/--/)) {
45  throw new Error("Comment text cannot contain double-hypen: " + val);
46  }
47  return this.assertLegalChar(val);
48  };
49 
50  XMLStringifier.prototype.raw = function(val) {
51  return '' + val || '';
52  };
53 
54  XMLStringifier.prototype.attName = function(val) {
55  val = '' + val || '';
56  return val = this.applyCase(val);
57  };
58 
59  XMLStringifier.prototype.attValue = function(val) {
60  val = '' + val || '';
61  return this.attEscape(val);
62  };
63 
64  XMLStringifier.prototype.insTarget = function(val) {
65  return '' + val || '';
66  };
67 
68  XMLStringifier.prototype.insValue = function(val) {
69  val = '' + val || '';
70  if (val.match(/\?>/)) {
71  throw new Error("Invalid processing instruction value: " + val);
72  }
73  return val;
74  };
75 
76  XMLStringifier.prototype.xmlVersion = function(val) {
77  val = '' + val || '';
78  if (!val.match(/1\.[0-9]+/)) {
79  throw new Error("Invalid version number: " + val);
80  }
81  return val;
82  };
83 
84  XMLStringifier.prototype.xmlEncoding = function(val) {
85  val = '' + val || '';
86  if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-]|-)*$/)) {
87  throw new Error("Invalid encoding: " + val);
88  }
89  return val;
90  };
91 
92  XMLStringifier.prototype.xmlStandalone = function(val) {
93  if (val) {
94  return "yes";
95  } else {
96  return "no";
97  }
98  };
99 
100  XMLStringifier.prototype.dtdPubID = function(val) {
101  return '' + val || '';
102  };
103 
104  XMLStringifier.prototype.dtdSysID = function(val) {
105  return '' + val || '';
106  };
107 
108  XMLStringifier.prototype.dtdElementValue = function(val) {
109  return '' + val || '';
110  };
111 
112  XMLStringifier.prototype.dtdAttType = function(val) {
113  return '' + val || '';
114  };
115 
116  XMLStringifier.prototype.dtdAttDefault = function(val) {
117  if (val != null) {
118  return '' + val || '';
119  } else {
120  return val;
121  }
122  };
123 
124  XMLStringifier.prototype.dtdEntityValue = function(val) {
125  return '' + val || '';
126  };
127 
128  XMLStringifier.prototype.dtdNData = function(val) {
129  return '' + val || '';
130  };
131 
132  XMLStringifier.prototype.convertAttKey = '@';
133 
134  XMLStringifier.prototype.convertPIKey = '?';
135 
136  XMLStringifier.prototype.convertTextKey = '#text';
137 
138  XMLStringifier.prototype.convertCDataKey = '#cdata';
139 
140  XMLStringifier.prototype.convertCommentKey = '#comment';
141 
142  XMLStringifier.prototype.convertRawKey = '#raw';
143 
144  XMLStringifier.prototype.assertLegalChar = function(str) {
145  var chars, chr;
146  if (this.allowSurrogateChars) {
147  chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uFFFE-\uFFFF]/;
148  } else {
149  chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/;
150  }
151  chr = str.match(chars);
152  if (chr) {
153  throw new Error("Invalid character (" + chr + ") in string: " + str + " at index " + chr.index);
154  }
155  return str;
156  };
157 
158  XMLStringifier.prototype.applyCase = function(str) {
159  switch (this.textCase) {
160  case "camel":
161  return camelCase(str);
162  case "title":
163  return titleCase(str);
164  case "kebab":
165  case "lower":
166  return kebabCase(str);
167  case "snake":
168  return snakeCase(str);
169  case "upper":
170  return kebabCase(str).toUpperCase();
171  default:
172  return str;
173  }
174  };
175 
176  XMLStringifier.prototype.elEscape = function(str) {
177  var ampregex;
178  ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
179  return str.replace(ampregex, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\r/g, '&#xD;');
180  };
181 
182  XMLStringifier.prototype.attEscape = function(str) {
183  var ampregex;
184  ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
185  return str.replace(ampregex, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/\t/g, '&#x9;').replace(/\n/g, '&#xA;').replace(/\r/g, '&#xD;');
186  };
187 
188  return XMLStringifier;
189 
190  })();
191 
192 }).call(this);