UNPKG

3.46 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Plist = void 0;
7var _options = require("./options.js");
8var _util = require("./util.js");
9var _array = require("./value/array.js");
10const xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>';
11const xmlDoctype = '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
12
13/**
14 * Plist object.
15 */
16class Plist {
17 /**
18 * Default XML declaration.
19 */
20 static XML_DECLARATION = xmlDeclaration;
21
22 /**
23 * Default XML doctype.
24 */
25 static XML_DOCTYPE = xmlDoctype;
26
27 /**
28 * XML declaration.
29 */
30 xmlDeclaration = xmlDeclaration;
31
32 /**
33 * XML doctype.
34 */
35 xmlDoctype = xmlDoctype;
36
37 /**
38 * Value element.
39 */
40 value = null;
41
42 /**
43 * Plist constructor.
44 *
45 * @param value The value.
46 */
47 constructor(value = null) {
48 this.value = value;
49 }
50
51 /**
52 * Get value or throw if null.
53 *
54 * @returns The value.
55 */
56 getValue() {
57 const {
58 value
59 } = this;
60 if (!value) {
61 throw new Error('Value is null');
62 }
63 return value;
64 }
65
66 /**
67 * Cast to specific type or null.
68 *
69 * @param Type Type constructor.
70 * @returns The object or null.
71 */
72 valueCastTo(Type) {
73 const {
74 value
75 } = this;
76 return value ? value.castTo(Type) : null;
77 }
78
79 /**
80 * Cast to specific type or throw.
81 *
82 * @param Type Type constructor.
83 * @returns The object.
84 */
85 valueCastAs(Type) {
86 const casted = this.valueCastTo(Type);
87 if (!casted) {
88 throw new Error(`Cannot cast value to type '${Type.TYPE}'`);
89 }
90 return casted;
91 }
92
93 /**
94 * Decode document from string.
95 *
96 * @param xml XML string.
97 */
98 fromXml(xml) {
99 const {
100 declaration,
101 doctype,
102 documentElement
103 } = (0, _util.xmlDecode)(xml);
104 this.fromXmlElement(documentElement, declaration, doctype);
105 }
106
107 /**
108 * Decode document from element.
109 *
110 * @param element XML element.
111 * @param declaration XML declaration.
112 * @param doctype XML doctype.
113 */
114 fromXmlElement(element, declaration = null, doctype = null) {
115 const {
116 tagName
117 } = element;
118 if (tagName !== 'plist') {
119 throw new Error(`Unexpected root plist tag name: ${tagName}`);
120 }
121 const childElements = (0, _util.xmlElementChildElements)(element);
122 const childElementsL = childElements.length;
123 if (childElementsL > 1) {
124 throw new Error(`Multiple root plist child tag: ${childElementsL}`);
125 }
126 this.value = childElementsL ? this.childFromXmlElement(childElements[0]) : null;
127 this.xmlDeclaration = declaration || '';
128 this.xmlDoctype = doctype || '';
129 }
130
131 /**
132 * Decode child element from XML element.
133 *
134 * @param element XML element.
135 * @returns Value element.
136 */
137 childFromXmlElement(element) {
138 const a = new _array.ValueArray();
139 return a.childFromXmlElement(element);
140 }
141
142 /**
143 * Encode documents to string.
144 *
145 * @param options Options object.
146 * @returns XML string.
147 */
148 toXml(options = null) {
149 const ir = options?.indentRoot ?? _options.INDENT_ROOT;
150 const n = options?.newlineString ?? _options.NEWLINE_STRING;
151 const v = this.value;
152 return [...[this.xmlDeclaration, this.xmlDoctype].filter(s => s), '<plist version="1.0">', ...(v ? [v.toXml(options, ir ? 1 : 0)] : []), '</plist>', ''].join(n);
153 }
154}
155exports.Plist = Plist;
156//# sourceMappingURL=plist.js.map
\No newline at end of file