• 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}
21interface NativeXMLSerializer{
22    new(value : object, strEncoding? : string) : NativeXMLSerializer;
23    setAttributes(name : string, value : string) : void;
24    addEmptyElement(name : string) : void;
25    setDeclaration() : void;
26    startElement(name : string) : void;
27    endElement() : void;
28    setNamespace(prefix : string, namespace : string) : void;
29    setComment(text : string) : void;
30    setCDATA(text : string) : void;
31    setText(text : string) : void;
32    setDocType(text : string) : void;
33    XmlSerializerError() : string;
34}
35interface Xml{
36    XmlSerializer : NativeXMLSerializer;
37    XmlPullParser : NativeXmlPullParser;
38}
39declare function requireInternal(s : string) : Xml;
40const XML = requireInternal('xml');
41class XmlSerializer {
42    xmlSerializerClass : NativeXMLSerializer;
43    constructor() {
44        if(typeof arguments[0] !== 'object') {
45            throw new Error('input type err');
46        }
47        if (arguments.length === 1) {
48            const inputType :string = 'utf-8';
49            this.xmlSerializerClass = new XML.XmlSerializer(arguments[0], inputType);
50        } else if (arguments.length === 2 && (typeof arguments[1] === 'string' && arguments[1].length !== 0)) {
51            var strTemp = arguments[1];
52            if (strTemp.toLowerCase() !== 'utf-8') {
53                throw new Error('Just support utf-8');
54            }
55            this.xmlSerializerClass = new XML.XmlSerializer(arguments[0], arguments[1]);
56        } else {
57            throw new Error('input type err');
58        }
59        let errStr = this.xmlSerializerClass.XmlSerializerError();
60        if (errStr.length !== 0) {
61            throw new Error(errStr);
62        }
63    }
64    setAttributes(name : string, value : string) {
65        if (typeof name !== 'string' || name.length === 0 || typeof value !== 'string' || name.length === 0 ) {
66            throw new Error('name or value type err');
67        }
68        this.xmlSerializerClass.setAttributes(name, value);
69        let errStr = this.xmlSerializerClass.XmlSerializerError();
70        if (errStr.length !== 0) {
71            throw new Error(errStr);
72        }
73    }
74    addEmptyElement(name : string) {
75        if (typeof name !== 'string' || name.length === 0) {
76            throw new Error('name type err');
77        }
78        this.xmlSerializerClass.addEmptyElement(name);
79        let errStr = this.xmlSerializerClass.XmlSerializerError();
80        if (errStr.length !== 0) {
81            throw new Error(errStr);
82        }
83    }
84    setDeclaration() {
85        this.xmlSerializerClass.setDeclaration();
86        let errStr = this.xmlSerializerClass.XmlSerializerError();
87        if (errStr.length !== 0) {
88            throw new Error(errStr);
89        }
90    }
91    startElement(name : string) {
92        if (typeof name !== 'string' || name.length === 0) {
93            throw new Error('name type err');
94        }
95        this.xmlSerializerClass.startElement(name);
96        let errStr = this.xmlSerializerClass.XmlSerializerError();
97        if (errStr.length !== 0) {
98            throw new Error(errStr);
99        }
100    }
101    endElement() {
102        this.xmlSerializerClass.endElement();
103        let errStr = this.xmlSerializerClass.XmlSerializerError();
104        if (errStr.length !== 0) {
105            throw new Error(errStr);
106        }
107    }
108    setNamespace(prefix : string, ns : string) {
109        if (typeof prefix !== 'string' || prefix.length === 0 || typeof ns !== 'string' || ns.length === 0 ) {
110            throw new Error('prefix or namespace type err');
111        }
112        this.xmlSerializerClass.setNamespace(prefix, ns);
113        let errStr = this.xmlSerializerClass.XmlSerializerError();
114        if (errStr.length !== 0) {
115            throw new Error(errStr);
116        }
117    }
118    setComment(text : string) {
119        if (typeof text !== 'string' || text.length === 0) {
120            throw new Error('text type err');
121        }
122        this.xmlSerializerClass.setComment(text);
123        let errStr = this.xmlSerializerClass.XmlSerializerError();
124        if (errStr.length !== 0) {
125            throw new Error(errStr);
126        }
127    }
128    setCDATA(text : string) {
129        if (typeof text !== 'string' || text.length === 0) {
130            throw new Error('text type err');
131        }
132        this.xmlSerializerClass.setCDATA(text);
133        let errStr = this.xmlSerializerClass.XmlSerializerError();
134        if (errStr.length !== 0) {
135            throw new Error(errStr);
136        }
137    }
138    setText(text : string) {
139        if (typeof text !== 'string' || text.length === 0) {
140            throw new Error('text type err');
141        }
142        this.xmlSerializerClass.setText(text);
143        let errStr = this.xmlSerializerClass.XmlSerializerError();
144        if (errStr.length !== 0) {
145            throw new Error(errStr);
146        }
147    }
148    setDocType(text : string) {
149        if (typeof text !== 'string' || text.length === 0) {
150            throw new Error('text type err');
151        }
152        this.xmlSerializerClass.setDocType(text);
153        let errStr = this.xmlSerializerClass.XmlSerializerError();
154        if (errStr.length !== 0) {
155            throw new Error(errStr);
156        }
157    }
158}
159
160class XmlPullParser {
161    xmlPullParserClass : NativeXmlPullParser;
162    constructor() {
163        if(typeof arguments[0] !== 'object') {
164            throw new Error('input type err');
165        }
166        if (arguments.length === 1) {
167            let str = 'utf-8';
168            this.xmlPullParserClass = new XML.XmlPullParser(arguments[0], str);
169        } else if (arguments.length === 2 && (typeof arguments[1] === 'string' && arguments[1].length !== 0)) {
170            var strTemp = arguments[1];
171            if (strTemp.toLowerCase() !== 'utf-8') {
172                throw new Error('Just support utf-8');
173            }
174            this.xmlPullParserClass = new XML.XmlPullParser(arguments[0], arguments[1]);
175        } else {
176            throw new Error('input type err');
177        }
178        let errStr = this.xmlPullParserClass.XmlPullParserError();
179        if (errStr.length !== 0) {
180            throw new Error(errStr);
181        }
182    }
183    parse(options : object) {
184        if(typeof options !== 'object') {
185            throw new Error('options type err');
186        }
187        this.xmlPullParserClass.parse(options);
188        let errStr = this.xmlPullParserClass.XmlPullParserError();
189        if (errStr.length !== 0) {
190            throw new Error(errStr);
191        }
192    }
193}
194
195export default {
196    XmlSerializer : XmlSerializer,
197    XmlPullParser : XmlPullParser,
198}
199