• 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  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      const inputType: string = 'utf-8';
62      this.xmlSerializerClass = new XML.XmlSerializer(obj, inputType);
63    } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'string' && inputStr.length !== 0)) {
64      let strTemp: string = inputStr;
65      if (strTemp.toLowerCase() !== 'utf-8') {
66        throw new Error('Just support utf-8');
67      }
68      this.xmlSerializerClass = new XML.XmlSerializer(obj, inputStr);
69    } else {
70      throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`);
71    }
72    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
73    if (errStr.length !== 0) {
74      throw new Error(errStr);
75    }
76  }
77  setAttributes(name: string, value: string): void {
78    if (typeof name !== 'string' || name.length === 0) {
79      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
80    }
81    if (typeof value !== 'string') {
82      throw new BusinessError(`Parameter error.The type of ${value} must be string`);
83    }
84
85    this.xmlSerializerClass.setAttributes(name, value);
86    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
87    if (errStr.length !== 0) {
88      throw new Error(errStr);
89    }
90  }
91  addEmptyElement(name: string): void {
92    if (typeof name !== 'string' || name.length === 0) {
93      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
94    }
95    this.xmlSerializerClass.addEmptyElement(name);
96    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
97    if (errStr.length !== 0) {
98      throw new Error(errStr);
99    }
100  }
101  setDeclaration(): void {
102    this.xmlSerializerClass.setDeclaration();
103    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
104    if (errStr.length !== 0) {
105      throw new Error(errStr);
106    }
107  }
108  startElement(name: string): void {
109    if (typeof name !== 'string' || name.length === 0) {
110      throw new BusinessError(`Parameter error.The type of ${name} must be string`);
111    }
112    this.xmlSerializerClass.startElement(name);
113    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
114    if (errStr.length !== 0) {
115      throw new Error(errStr);
116    }
117  }
118  endElement(): void {
119    this.xmlSerializerClass.endElement();
120    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
121    if (errStr.length !== 0) {
122      throw new Error(errStr);
123    }
124  }
125  setNamespace(prefix: string, ns: string): void {
126    if (typeof prefix !== 'string' || prefix.length === 0) {
127      throw new BusinessError(`Parameter error.The type of ${prefix} must be string`);
128    }
129    if (typeof ns !== 'string' || ns.length === 0) {
130      throw new BusinessError(`Parameter error.The type of ${ns} must be string`);
131    }
132    this.xmlSerializerClass.setNamespace(prefix, ns);
133    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
134    if (errStr.length !== 0) {
135      throw new Error(errStr);
136    }
137  }
138  setComment(text: string): void {
139    if (typeof text !== 'string' || text.length === 0) {
140      let error = new BusinessError(`Parameter error.The type of ${text} must be string`);
141      throw error;
142    }
143    this.xmlSerializerClass.setComment(text);
144    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
145    if (errStr.length !== 0) {
146      throw new Error(errStr);
147    }
148  }
149  setCDATA(text: string): void {
150    if (typeof text !== 'string' || text.length === 0) {
151      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
152    }
153    this.xmlSerializerClass.setCDATA(text);
154    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
155    if (errStr.length !== 0) {
156      throw new Error(errStr);
157    }
158  }
159  setText(text: string): void {
160    if (typeof text !== 'string' || text.length === 0) {
161      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
162    }
163    this.xmlSerializerClass.setText(text);
164    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
165    if (errStr.length !== 0) {
166      throw new Error(errStr);
167    }
168  }
169  setDocType(text: string): void {
170    if (typeof text !== 'string' || text.length === 0) {
171      throw new BusinessError(`Parameter error.The type of ${text} must be string`);
172    }
173    this.xmlSerializerClass.setDocType(text);
174    let errStr: string = this.xmlSerializerClass.XmlSerializerError();
175    if (errStr.length !== 0) {
176      throw new Error(errStr);
177    }
178  }
179}
180
181class XmlPullParser {
182  xmlPullParserClass: NativeXmlPullParser;
183  constructor(obj: object, inputStr: string) {
184    if (typeof obj !== 'object') {
185      throw new BusinessError(`Parameter error.The type of ${obj} must be object`);
186    }
187    if (arguments.length === 1) {
188      let str: string = 'utf-8';
189      this.xmlPullParserClass = new XML.XmlPullParser(obj, str);
190    } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr ===
191      'string' && inputStr.length !== 0)) {
192      let strTemp: string = inputStr;
193      if (strTemp.toLowerCase() !== 'utf-8') {
194        throw new Error('Just support utf-8');
195      }
196      this.xmlPullParserClass = new XML.XmlPullParser(obj, inputStr);
197    } else {
198      throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`);
199    }
200    let errStr: string = this.xmlPullParserClass.XmlPullParserError();
201    if (errStr.length !== 0) {
202      throw new Error(errStr);
203    }
204  }
205  parse(options: object): void {
206    if (typeof options !== 'object') {
207      throw new BusinessError(`Parameter error.The type of ${options} must be object`);
208    }
209    this.xmlPullParserClass.parse(options);
210    let errStr: string = this.xmlPullParserClass.XmlPullParserError();
211    if (errStr.length !== 0) {
212      throw new Error(errStr);
213    }
214  }
215}
216
217enum EventType {
218  START_DOCUMENT,
219  END_DOCUMENT,
220  START_TAG,
221  END_TAG,
222  TEXT,
223  CDSECT,
224  COMMENT,
225  DOCDECL,
226  INSTRUCTION,
227  ENTITY_REFERENCE,
228  WHITESPACE
229}
230
231export default {
232  XmlSerializer: XmlSerializer,
233  XmlPullParser: XmlPullParser,
234  EventType,
235}
236