1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Plist = void 0;
|
7 | var _options = require("./options.js");
|
8 | var _util = require("./util.js");
|
9 | var _dict = require("./value/dict.js");
|
10 | const xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>';
|
11 | const xmlDoctype = '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | class Plist {
|
17 | |
18 |
|
19 |
|
20 | static XML_DECLARATION = xmlDeclaration;
|
21 |
|
22 | |
23 |
|
24 |
|
25 | static XML_DOCTYPE = xmlDoctype;
|
26 |
|
27 | |
28 |
|
29 |
|
30 | xmlDeclaration = xmlDeclaration;
|
31 |
|
32 | |
33 |
|
34 |
|
35 | xmlDoctype = xmlDoctype;
|
36 |
|
37 | |
38 |
|
39 |
|
40 | value = null;
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | constructor(value = null) {
|
48 | this.value = value;
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
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 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | valueCastTo(Type) {
|
73 | const {
|
74 | value
|
75 | } = this;
|
76 | return value ? value.castTo(Type) : null;
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
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 |
|
95 |
|
96 |
|
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 |
|
109 |
|
110 |
|
111 |
|
112 |
|
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 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | childFromXmlElement(element) {
|
138 | const a = new _dict.ValueDict();
|
139 | return a.childFromXmlElement(element);
|
140 | }
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
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 | }
|
155 | exports.Plist = Plist;
|
156 |
|
\ | No newline at end of file |