• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16interface NativeXmlPullParser {
17  new(value: object, strEncoding?: string): NativeXmlPullParser;
18  parse(options: object): void;
19  parseXml(options: object): boolean;
20  XmlPullParserError(): string;
21}
22
23interface NativeXMLSerializer {
24  new(value: object, strEncoding?: string): NativeXMLSerializer;
25  setAttributes(name: string, value: string): void;
26  addEmptyElement(name: string): void;
27  setDeclaration(): void;
28  startElement(name: string): void;
29  endElement(): void;
30  setNamespace(prefix: string, namespace: string): void;
31  setComment(text: string): void;
32  setCDATA(text: string): void;
33  setText(text: string): void;
34  setDocType(text: string): void;
35  XmlSerializerError(): string;
36}
37
38interface NativeXMLDynamicSerializer {
39  new(strEncoding?: string): NativeXMLDynamicSerializer;
40  setAttributes(name: string, value: string): void;
41  addEmptyElement(name: string): void;
42  setDeclaration(): void;
43  startElement(name: string): void;
44  endElement(): void;
45  setNamespace(prefix: string, namespace: string): void;
46  setComment(text: string): void;
47  setCDATA(text: string): void;
48  setText(text: string): void;
49  setDocType(text: string): void;
50  getOutput(): ArrayBuffer | undefined;
51}
52
53interface Xml {
54  XmlSerializer: NativeXMLSerializer;
55  XmlPullParser: NativeXmlPullParser;
56  XmlDynamicSerializer: NativeXMLDynamicSerializer;
57}
58
59const ARGUMENT_LENGTH_ONE = 1;
60const ARGUMENT_LENGTH_TWO = 2;
61const TypeErrorCode = 401;
62class BusinessError extends Error {
63  code: number;
64  constructor(msg: string) {
65    super(msg);
66    this.name = 'BusinessError';
67    this.code = TypeErrorCode;
68  }
69}
70
71declare function requireInternal(s: string): Xml;
72const XML = requireInternal('xml');
73class XmlSerializer {
74  xmlSerializerClass: NativeXMLSerializer;
75  constructor(obj: object, inputStr: string) {
76    if (typeof obj !== 'object') {
77      throw new BusinessError(`Parameter error.The type of ${obj} must be object`);
78    }
79    if (arguments.length === 1 ||
80        (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) {
81      const inputType: string = 'utf-8';
82      this.xmlSerializerClass = new XML.XmlSerializer(obj, inputType);
83    } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'string' && inputStr.length !== 0)) {
84      let strTemp: string = inputStr;
85      if (strTemp.toLowerCase() !== 'utf-8') {
86        throw new BusinessError('Parameter error.Just support utf-8');
87      }
88      this.xmlSerializerClass = new XML.XmlSerializer(obj, inputStr);
89    } else {
90      throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`);
91    }
92    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
93    if (errStr.length !== 0) {
94      throw new BusinessError(errStr);
95    }
96  }
97
98  setAttributes(name: string, value: string): void {
99    if (typeof name !== 'string') {
100      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
101    }
102    if (name.length === 0) {
103      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
104    }
105    if (typeof value !== 'string') {
106      throw new BusinessError(`Parameter error.The type of ${value} must be string`);
107    }
108
109    this.xmlSerializerClass.setAttributes(name, value);
110    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
111    if (errStr.length !== 0) {
112      throw new BusinessError(errStr);
113    }
114  }
115
116  addEmptyElement(name: string): void {
117    if (typeof name !== 'string') {
118      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
119    }
120    if (name.length === 0) {
121      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
122    }
123
124    this.xmlSerializerClass.addEmptyElement(name);
125    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
126    if (errStr.length !== 0) {
127      throw new BusinessError(errStr);
128    }
129  }
130
131  setDeclaration(): void {
132    this.xmlSerializerClass.setDeclaration();
133    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
134    if (errStr.length !== 0) {
135      throw new BusinessError(errStr);
136    }
137  }
138
139  startElement(name: string): void {
140    if (typeof name !== 'string') {
141      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
142    }
143    if (name.length === 0) {
144      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
145    }
146    this.xmlSerializerClass.startElement(name);
147    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
148    if (errStr.length !== 0) {
149      throw new BusinessError(errStr);
150    }
151  }
152
153  endElement(): void {
154    this.xmlSerializerClass.endElement();
155    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
156    if (errStr.length !== 0) {
157      throw new BusinessError(errStr);
158    }
159  }
160
161  setNamespace(prefix: string, ns: string): void {
162    if (typeof prefix !== 'string') {
163      throw new BusinessError(`Parameter error.The type of ${prefix} must be string`);
164    }
165    if (prefix.length === 0) {
166      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
167    }
168    if (typeof ns !== 'string' || ns.length === 0) {
169      throw new BusinessError(`Parameter error.The type of ${ns} must be string`);
170    }
171    this.xmlSerializerClass.setNamespace(prefix, ns);
172    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
173    if (errStr.length !== 0) {
174      throw new BusinessError(errStr);
175    }
176  }
177
178  setComment(text: string): void {
179    if (typeof text !== 'string') {
180      let error = new BusinessError(`Parameter error.The type of ${text} must be string`);
181      throw error;
182    }
183    if (text.length === 0) {
184      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
185    }
186    this.xmlSerializerClass.setComment(text);
187    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
188    if (errStr.length !== 0) {
189      throw new BusinessError(errStr);
190    }
191  }
192
193  setCDATA(text: string): void {
194    if (typeof text !== 'string') {
195      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
196    }
197    if (text.length === 0) {
198      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
199    }
200    this.xmlSerializerClass.setCDATA(text);
201    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
202    if (errStr.length !== 0) {
203      throw new BusinessError(errStr);
204    }
205  }
206
207  setText(text: string): void {
208    if (typeof text !== 'string') {
209      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
210    }
211    if (text.length === 0) {
212      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
213    }
214    this.xmlSerializerClass.setText(text);
215    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
216    if (errStr.length !== 0) {
217      throw new BusinessError(errStr);
218    }
219  }
220
221  setDocType(text: string): void {
222    if (typeof text !== 'string') {
223      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
224    }
225    if (text.length === 0) {
226      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
227    }
228    this.xmlSerializerClass.setDocType(text);
229    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
230    if (errStr.length !== 0) {
231      throw new BusinessError(errStr);
232    }
233  }
234}
235
236class XmlDynamicSerializer {
237  xmlSerializerClass: NativeXMLDynamicSerializer;
238  constructor(encoding?: string) {
239    let input: string = 'utf-8';
240    if (arguments.length === ARGUMENT_LENGTH_ONE) {
241      if (typeof encoding !== 'string') {
242        throw new BusinessError(`Parameter error.The type of ${encoding} must be string`);
243      }
244      if (encoding.length === 0 || encoding.toLowerCase() !== 'utf-8') {
245        throw new BusinessError('Parameter error.Just support utf-8');
246      }
247    }
248    this.xmlSerializerClass = new XML.XmlDynamicSerializer(input);
249  }
250
251  getOutput(): ArrayBuffer | undefined {
252    return this.xmlSerializerClass.getOutput();
253  }
254
255  setAttributes(name: string, value: string): void {
256    if (typeof name !== 'string') {
257      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
258    }
259    if (name.length === 0) {
260      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
261    }
262    if (typeof value !== 'string') {
263      throw new BusinessError(`Parameter error.The type of ${value} must be string`);
264    }
265    this.xmlSerializerClass.setAttributes(name, value);
266  }
267
268  addEmptyElement(name: string): void {
269    if (typeof name !== 'string') {
270      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
271    }
272    if (name.length === 0) {
273      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
274    }
275    this.xmlSerializerClass.addEmptyElement(name);
276  }
277
278  setDeclaration(): void {
279    this.xmlSerializerClass.setDeclaration();
280  }
281
282  startElement(name: string): void {
283    if (typeof name !== 'string') {
284      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
285    }
286    if (name.length === 0) {
287      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
288    }
289    this.xmlSerializerClass.startElement(name);
290  }
291
292  endElement(): void {
293    this.xmlSerializerClass.endElement();
294  }
295
296  setNamespace(prefix: string, ns: string): void {
297    if (typeof prefix !== 'string') {
298      throw new BusinessError(`Parameter error.The type of ${prefix} must be string`);
299    }
300    if (typeof ns !== 'string') {
301      throw new BusinessError(`Parameter error.The type of ${ns} must be string`);
302    }
303    if (prefix.length === 0 || ns.length === 0) {
304      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
305    }
306    this.xmlSerializerClass.setNamespace(prefix, ns);
307  }
308
309  setComment(text: string): void {
310    if (typeof text !== 'string') {
311      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
312    }
313    if (text.length === 0) {
314      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
315    }
316    this.xmlSerializerClass.setComment(text);
317  }
318
319  setCDATA(text: string): void {
320    if (typeof text !== 'string') {
321      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
322    }
323    if (text.length === 0) {
324      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
325    }
326    this.xmlSerializerClass.setCDATA(text);
327  }
328
329  setText(text: string): void {
330    if (typeof text !== 'string') {
331      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
332    }
333    if (text.length === 0) {
334      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
335    }
336    this.xmlSerializerClass.setText(text);
337  }
338
339  setDocType(text: string): void {
340    if (typeof text !== 'string') {
341      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
342    }
343    if (text.length === 0) {
344      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
345    }
346    this.xmlSerializerClass.setDocType(text);
347  }
348}
349
350class XmlPullParser {
351  xmlPullParserClass: NativeXmlPullParser;
352  constructor(obj: object, inputStr: string) {
353    if (typeof obj !== 'object') {
354      throw new BusinessError(`Parameter error.The type of ${obj} must be object`);
355    }
356    if (arguments.length === 1 ||
357        (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) {
358      let str: string = 'utf-8';
359      this.xmlPullParserClass = new XML.XmlPullParser(obj, str);
360    } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr ===
361      'string' && inputStr.length !== 0)) {
362      let strTemp: string = inputStr;
363      if (strTemp.toLowerCase() !== 'utf-8') {
364        throw new BusinessError('Parameter error.Just support utf-8');
365      }
366      this.xmlPullParserClass = new XML.XmlPullParser(obj, inputStr);
367    } else {
368      throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`);
369    }
370    let errStr: string = this.xmlPullParserClass.XmlPullParserError();
371    if (errStr.length !== 0) {
372      throw new BusinessError(errStr);
373    }
374  }
375  parse(options: object): void {
376    if (typeof options !== 'object') {
377      throw new BusinessError(`Parameter error.The type of ${options} must be object`);
378    }
379    this.xmlPullParserClass.parse(options);
380    let errStr: string = this.xmlPullParserClass.XmlPullParserError();
381    if (errStr.length !== 0) {
382      throw new BusinessError(errStr);
383    }
384  }
385
386  parseXml(options: object): void {
387    if (typeof options !== 'object') {
388      throw new BusinessError(`Parameter error.The type of ${options} must be object`);
389    }
390    if (this.xmlPullParserClass.parseXml(options)) {
391      let errStr: string = this.xmlPullParserClass.XmlPullParserError();
392      throw new BusinessError(errStr);
393    }
394  }
395}
396
397enum EventType {
398  START_DOCUMENT,
399  END_DOCUMENT,
400  START_TAG,
401  END_TAG,
402  TEXT,
403  CDSECT,
404  COMMENT,
405  DOCDECL,
406  INSTRUCTION,
407  ENTITY_REFERENCE,
408  WHITESPACE
409}
410
411export default {
412  XmlSerializer: XmlSerializer,
413  XmlPullParser: XmlPullParser,
414  XmlDynamicSerializer: XmlDynamicSerializer,
415  EventType,
416};
417