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  XmlPullParserError(): string;
20}
21
22interface NativeXMLSerializer {
23  new(value: object, strEncoding?: string): NativeXMLSerializer;
24  setAttributes(name: string, value: string): void;
25  addEmptyElement(name: string): void;
26  setDeclaration(): void;
27  startElement(name: string): void;
28  endElement(): void;
29  setNamespace(prefix: string, namespace: string): void;
30  setComment(text: string): void;
31  setCDATA(text: string): void;
32  setText(text: string): void;
33  setDocType(text: string): void;
34  XmlSerializerError(): string;
35}
36
37interface Xml {
38  XmlSerializer: NativeXMLSerializer;
39  XmlPullParser: NativeXmlPullParser;
40}
41const ARGUMENT_LENGTH_TWO = 2;
42const TypeErrorCode = 401;
43class BusinessError extends Error {
44  code: number;
45  constructor(msg: string) {
46    super(msg);
47    this.name = 'BusinessError';
48    this.code = TypeErrorCode;
49  }
50}
51
52declare function requireInternal(s: string): Xml;
53const XML = requireInternal('xml');
54class XmlSerializer {
55  xmlSerializerClass: NativeXMLSerializer;
56  constructor(obj: object, inputStr: string) {
57    if (typeof obj !== 'object') {
58      throw new BusinessError(`Parameter error.The type of ${obj} must be object`);
59    }
60    if (arguments.length === 1 ||
61        (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) {
62      const inputType: string = 'utf-8';
63      this.xmlSerializerClass = new XML.XmlSerializer(obj, inputType);
64    } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'string' && inputStr.length !== 0)) {
65      let strTemp: string = inputStr;
66      if (strTemp.toLowerCase() !== 'utf-8') {
67        throw new BusinessError('Parameter error.Just support utf-8');
68      }
69      this.xmlSerializerClass = new XML.XmlSerializer(obj, inputStr);
70    } else {
71      throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`);
72    }
73    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
74    if (errStr.length !== 0) {
75      throw new BusinessError(errStr);
76    }
77  }
78
79  setAttributes(name: string, value: string): void {
80    if (typeof name !== 'string') {
81      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
82    }
83    if (name.length === 0) {
84      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
85    }
86    if (typeof value !== 'string') {
87      throw new BusinessError(`Parameter error.The type of ${value} must be string`);
88    }
89
90    this.xmlSerializerClass.setAttributes(name, value);
91    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
92    if (errStr.length !== 0) {
93      throw new BusinessError(errStr);
94    }
95  }
96
97  addEmptyElement(name: string): void {
98    if (typeof name !== 'string') {
99      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
100    }
101    if (name.length === 0) {
102      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
103    }
104
105    this.xmlSerializerClass.addEmptyElement(name);
106    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
107    if (errStr.length !== 0) {
108      throw new BusinessError(errStr);
109    }
110  }
111
112  setDeclaration(): void {
113    this.xmlSerializerClass.setDeclaration();
114    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
115    if (errStr.length !== 0) {
116      throw new BusinessError(errStr);
117    }
118  }
119
120  startElement(name: string): void {
121    if (typeof name !== 'string') {
122      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
123    }
124    if (name.length === 0) {
125      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
126    }
127    this.xmlSerializerClass.startElement(name);
128    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
129    if (errStr.length !== 0) {
130      throw new BusinessError(errStr);
131    }
132  }
133
134  endElement(): void {
135    this.xmlSerializerClass.endElement();
136    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
137    if (errStr.length !== 0) {
138      throw new BusinessError(errStr);
139    }
140  }
141
142  setNamespace(prefix: string, ns: string): void {
143    if (typeof prefix !== 'string') {
144      throw new BusinessError(`Parameter error.The type of ${prefix} must be string`);
145    }
146    if (prefix.length === 0) {
147      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
148    }
149    if (typeof ns !== 'string' || ns.length === 0) {
150      throw new BusinessError(`Parameter error.The type of ${ns} must be string`);
151    }
152    this.xmlSerializerClass.setNamespace(prefix, ns);
153    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
154    if (errStr.length !== 0) {
155      throw new BusinessError(errStr);
156    }
157  }
158
159  setComment(text: string): void {
160    if (typeof text !== 'string') {
161      let error = new BusinessError(`Parameter error.The type of ${text} must be string`);
162      throw error;
163    }
164    if (text.length === 0) {
165      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
166    }
167    this.xmlSerializerClass.setComment(text);
168    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
169    if (errStr.length !== 0) {
170      throw new BusinessError(errStr);
171    }
172  }
173
174  setCDATA(text: string): void {
175    if (typeof text !== 'string') {
176      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
177    }
178    if (text.length === 0) {
179      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
180    }
181    this.xmlSerializerClass.setCDATA(text);
182    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
183    if (errStr.length !== 0) {
184      throw new BusinessError(errStr);
185    }
186  }
187
188  setText(text: string): void {
189    if (typeof text !== 'string') {
190      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
191    }
192    if (text.length === 0) {
193      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
194    }
195    this.xmlSerializerClass.setText(text);
196    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
197    if (errStr.length !== 0) {
198      throw new BusinessError(errStr);
199    }
200  }
201
202  setDocType(text: string): void {
203    if (typeof text !== 'string') {
204      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
205    }
206    if (text.length === 0) {
207      throw new BusinessError(`Parameter error. Parameter cannot be empty`);
208    }
209    this.xmlSerializerClass.setDocType(text);
210    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
211    if (errStr.length !== 0) {
212      throw new BusinessError(errStr);
213    }
214  }
215}
216
217class XmlPullParser {
218  xmlPullParserClass: NativeXmlPullParser;
219  constructor(obj: object, inputStr: string) {
220    if (typeof obj !== 'object') {
221      throw new BusinessError(`Parameter error.The type of ${obj} must be object`);
222    }
223    if (arguments.length === 1 ||
224        (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) {
225      let str: string = 'utf-8';
226      this.xmlPullParserClass = new XML.XmlPullParser(obj, str);
227    } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr ===
228      'string' && inputStr.length !== 0)) {
229      let strTemp: string = inputStr;
230      if (strTemp.toLowerCase() !== 'utf-8') {
231        throw new BusinessError('Parameter error.Just support utf-8');
232      }
233      this.xmlPullParserClass = new XML.XmlPullParser(obj, inputStr);
234    } else {
235      throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`);
236    }
237    let errStr: string = this.xmlPullParserClass.XmlPullParserError();
238    if (errStr.length !== 0) {
239      throw new BusinessError(errStr);
240    }
241  }
242  parse(options: object): void {
243    if (typeof options !== 'object') {
244      throw new BusinessError(`Parameter error.The type of ${options} must be object`);
245    }
246    this.xmlPullParserClass.parse(options);
247    let errStr: string = this.xmlPullParserClass.XmlPullParserError();
248    if (errStr.length !== 0) {
249      throw new BusinessError(errStr);
250    }
251  }
252}
253
254enum EventType {
255  START_DOCUMENT,
256  END_DOCUMENT,
257  START_TAG,
258  END_TAG,
259  TEXT,
260  CDSECT,
261  COMMENT,
262  DOCDECL,
263  INSTRUCTION,
264  ENTITY_REFERENCE,
265  WHITESPACE
266}
267
268export default {
269  XmlSerializer: XmlSerializer,
270  XmlPullParser: XmlPullParser,
271  EventType,
272};
273