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