Blame view
node_modules/xmlcreate/lib/nodes/XmlDtd.js
15.6 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright (C) 2016 Michael Kourlas * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var options_1 = require("../options"); var utils_1 = require("../utils"); var validate_1 = require("../validate"); var XmlComment_1 = require("./XmlComment"); var XmlDtdAttlist_1 = require("./XmlDtdAttlist"); var XmlDtdElement_1 = require("./XmlDtdElement"); var XmlDtdEntity_1 = require("./XmlDtdEntity"); var XmlDtdNotation_1 = require("./XmlDtdNotation"); var XmlDtdParamEntityRef_1 = require("./XmlDtdParamEntityRef"); var XmlNode_1 = require("./XmlNode"); var XmlProcInst_1 = require("./XmlProcInst"); /** * Represents an XML document type definition (DTD). * * An XML document type definition is structured as follows, where `{name}` is * the name of the DTD, `{sysId}` is the system identifier of the DTD, * `{pubId}` is the public identifier of the DTD, and `{intSubset}` is the * internal subset of the DTD: * * ```xml * <!DOCTYPE {name} SYSTEM "{sysId}" PUBLIC "{pubId}" [ * {intSubset} * ]> * ``` * * The `{name}`, `{pubId}`, and `{sysId}` values are properties of the node, * while the `{intSubset}` value consists of the children of this node. * * XmlDtd nodes can have an unlimited number of {@link XmlComment}, * {@link XmlDtdAttlist}, {@link XmlDtdElement}, {@link XmlDtdEntity}, * {@link XmlDtdNotation}, {@link XmlDtdParamEntityRef}, and * {@link XmlProcInst} nodes. */ var XmlDtd = (function (_super) { __extends(XmlDtd, _super); /** * Initializes a new instance of the {@link XmlDtd} class. * * @param name The name of the DTD. * @param sysId The system identifier of the DTD, excluding quotation marks. * @param pubId The public identifier of the DTD, excluding quotation marks. * If a public identifier is provided, a system identifier * must be provided as well. */ function XmlDtd(name, sysId, pubId) { var _this = _super.call(this) || this; _this.name = name; _this.sysId = sysId; _this.pubId = pubId; return _this; } Object.defineProperty(XmlDtd.prototype, "name", { /** * Gets the name of the DTD. * * @returns The name of the DTD. */ get: function () { return this._name; }, /** * Sets the name of the DTD. * * @param name The name of the DTD. */ set: function (name) { if (!utils_1.isString(name)) { throw new TypeError("name should be a string"); } else if (!validate_1.validateName(name)) { throw new Error("name should not contain characters not" + " allowed in XML names"); } this._name = name; }, enumerable: true, configurable: true }); Object.defineProperty(XmlDtd.prototype, "pubId", { /** * Gets the public identifier of the DTD, excluding quotation marks. * * @returns The public identifier of the DTD, excluding quotation marks. * This value may be undefined. */ get: function () { return this._pubId; }, /** * Sets the public identifier of the DTD, excluding quotation marks. If a * public identifier is provided, a system identifier must be provided as * well. * * @param pubId The public identifier of the DTD, excluding quotation marks. * This value may be undefined. */ set: function (pubId) { if (utils_1.isString(pubId)) { if (!/^(\u0020|\u000D|\u000A|[a-zA-Z0-9]|[-'()+,./:=?;!*#@$_%])*$/ .test(pubId)) { throw new Error("pubId should not contain characters not" + " allowed in public identifiers"); } else if (utils_1.isUndefined(this.sysId)) { throw new Error("pubId should not be defined if sysId is" + " undefined"); } } else if (!utils_1.isUndefined(pubId)) { throw new TypeError("pubId should be a string or undefined"); } this._pubId = pubId; }, enumerable: true, configurable: true }); Object.defineProperty(XmlDtd.prototype, "sysId", { /** * Gets the system identifier of the DTD, excluding quotation marks. * * @returns The system identifier of the DTD, excluding quotation marks. * This value may be undefined. */ get: function () { return this._sysId; }, /** * Sets the system identifier of the DTD, excluding quotation marks. * * @param sysId The system identifier of the DTD, excluding quotation marks. * This value may be undefined. */ set: function (sysId) { if (utils_1.isString(sysId)) { if (!validate_1.validateChar(sysId)) { throw new Error("sysId should not contain characters not" + " allowed in XML"); } else if (sysId.indexOf("'") !== -1 && sysId.indexOf("\"") !== -1) { throw new Error("sysId should not contain both single quotes" + " and double quotes"); } } else if (utils_1.isUndefined(sysId)) { if (!utils_1.isUndefined(this.pubId)) { throw new Error("sysId should not be undefined if pubId is" + " defined"); } } else { throw new TypeError("sysId should be a string or undefined"); } this._sysId = sysId; }, enumerable: true, configurable: true }); /** * Inserts a new attribute-list declaration at the specified index. If no * index is specified, the node is inserted at the end of this node's * children. * * @param text The text of the attribute-list declaration. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this node's * children. * * @returns The newly created attribute-list declaration. */ XmlDtd.prototype.attlist = function (text, index) { var attlist = new XmlDtdAttlist_1.default(text); this.insertChild(attlist, index); return attlist; }; /** * Inserts a new comment at the specified index. If no index is specified, * the node is inserted at the end of this node's children. * * @param content The data of the comment. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this node's * children. * * @returns The newly created comment. */ XmlDtd.prototype.comment = function (content, index) { var comment = new XmlComment_1.default(content); this.insertChild(comment, index); return comment; }; /** * Inserts a new element declaration at the specified index. If no index is * specified, the node is inserted at the end of this node's children. * * @param text The text of the element declaration. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this node's * children. * * @returns The newly created element declaration. */ XmlDtd.prototype.element = function (text, index) { var element = new XmlDtdElement_1.default(text); this.insertChild(element, index); return element; }; /** * Inserts a new entity declaration at the specified index. If no index is * specified, the node is inserted at the end of this node's children. * * @param text The text of the entity declaration. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this node's * children. * * @returns The newly created entity declaration. */ XmlDtd.prototype.entity = function (text, index) { var entity = new XmlDtdEntity_1.default(text); this.insertChild(entity, index); return entity; }; /** * Inserts the specified node into this node's children at the specified * index. The node is not inserted if it is already present. If this node * already has a parent, it is removed from that parent. * * Only {@link XmlComment}, {@link XmlDtdAttlist}, {@link XmlDtdElement}, * {@link XmlDtdEntity}, {@link XmlDtdNotation}, and {@link XmlProcInst} * nodes can be inserted; otherwise an exception will be thrown. * * @param node The node to insert. * @param index The index at which to insert the node. Nodes at or after * the index are shifted to the right. If no index is * specified, the node is inserted at the end. * * @returns The node inserted into this node's children, or undefined if no * node was inserted. */ XmlDtd.prototype.insertChild = function (node, index) { if (!(node instanceof XmlComment_1.default || node instanceof XmlDtdAttlist_1.default || node instanceof XmlDtdElement_1.default || node instanceof XmlDtdEntity_1.default || node instanceof XmlDtdNotation_1.default || node instanceof XmlDtdParamEntityRef_1.default || node instanceof XmlProcInst_1.default)) { throw new TypeError("node should be an instance of XmlComment," + " XmlDtdAttlist, XmlDtdElement, XmlDtdEntity," + " XmlDtdNotation, XmlDtdParamEntityRef, or" + " XmlProcInst"); } return _super.prototype.insertChild.call(this, node, index); }; /** * Inserts a new notation declaration at the specified index. If no index is * specified, the node is inserted at the end of this node's children. * * @param text The text of the notation declaration. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this * node's children. * * @returns The newly created notation declaration. */ XmlDtd.prototype.notation = function (text, index) { var notation = new XmlDtdNotation_1.default(text); this.insertChild(notation, index); return notation; }; /** * Inserts a new parameter entity reference at the specified index. If no * index is specified, the node is inserted at the end of this node's * children. * * @param entity The entity to reference. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this * node's children. * * @returns The newly created parameter entity reference. */ XmlDtd.prototype.paramEntityRef = function (entity, index) { var paramEntity = new XmlDtdParamEntityRef_1.default(entity); this.insertChild(paramEntity, index); return paramEntity; }; /** * Inserts a new processing instruction at the specified index. If no index * is specified, the node is inserted at the end of this node's children. * * @param target The target of the processing instruction. * @param content The data of the processing instruction, or undefined if * there is no target. * @param index The index at which the node should be inserted. If no index * is specified, the node is inserted at the end of this node's * children. * * @returns The newly created processing instruction. */ XmlDtd.prototype.procInst = function (target, content, index) { var procInst = new XmlProcInst_1.default(target, content); this.insertChild(procInst, index); return procInst; }; /** * Returns an XML string representation of this node. * * @param options Formatting options for the string representation. * * @returns An XML string representation of this node. */ XmlDtd.prototype.toString = function (options) { if (options === void 0) { options = {}; } var optionsObj = new options_1.StringOptions(options); var str = "<!DOCTYPE " + this.name; if (utils_1.isUndefined(this.pubId)) { if (!utils_1.isUndefined(this.sysId)) { str += " "; str = appendId("SYSTEM", this.sysId, str, optionsObj); } } else { str += " "; str = appendId("PUBLIC", this.pubId, str, optionsObj); str = appendId("", this.sysId, str, optionsObj); } if (this._children.length !== 0) { str += " ["; for (var _i = 0, _a = this._children; _i < _a.length; _i++) { var node = _a[_i]; if (optionsObj.pretty) { str += optionsObj.newline + optionsObj.indent; } str += node.toString(options); } if (optionsObj.pretty) { str += optionsObj.newline; } str += "]>"; } else { str += ">"; } return str; }; return XmlDtd; }(XmlNode_1.default)); exports.default = XmlDtd; /** * Appends the XML string representation of a public or system identifier to * an existing string. * * @param type "SYSTEM", "PUBLIC", or "". * @param value The value of the identifier. * @param str The string to which the string representation should be appended. * @param options Formatting options for the string representation. * * @returns The updated string. * * @private */ function appendId(type, value, str, options) { str += type + " "; if (options.doubleQuotes) { if (value.indexOf("\"") !== -1) { throw new Error("options.doubleQuotes inconsistent with" + " sysId or pubId"); } str += "\"" + value + "\""; } else { if (value.indexOf("'") !== -1) { throw new Error("options.doubleQuotes inconsistent with" + " sysId or pubId"); } str += "'" + value + "'"; } return str; } |