14d6c458bSopenharmony_ci/* 24d6c458bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 34d6c458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44d6c458bSopenharmony_ci * you may not use this file except in compliance with the License. 54d6c458bSopenharmony_ci * You may obtain a copy of the License at 64d6c458bSopenharmony_ci * 74d6c458bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84d6c458bSopenharmony_ci * 94d6c458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104d6c458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114d6c458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124d6c458bSopenharmony_ci * See the License for the specific language governing permissions and 134d6c458bSopenharmony_ci * limitations under the License. 144d6c458bSopenharmony_ci */ 154d6c458bSopenharmony_ci 164d6c458bSopenharmony_ciinterface NativeXmlPullParser { 174d6c458bSopenharmony_ci new(value: object, strEncoding?: string): NativeXmlPullParser; 184d6c458bSopenharmony_ci parse(options: object): void; 194d6c458bSopenharmony_ci XmlPullParserError(): string; 204d6c458bSopenharmony_ci} 214d6c458bSopenharmony_ci 224d6c458bSopenharmony_ciinterface NativeXMLSerializer { 234d6c458bSopenharmony_ci new(value: object, strEncoding?: string): NativeXMLSerializer; 244d6c458bSopenharmony_ci setAttributes(name: string, value: string): void; 254d6c458bSopenharmony_ci addEmptyElement(name: string): void; 264d6c458bSopenharmony_ci setDeclaration(): void; 274d6c458bSopenharmony_ci startElement(name: string): void; 284d6c458bSopenharmony_ci endElement(): void; 294d6c458bSopenharmony_ci setNamespace(prefix: string, namespace: string): void; 304d6c458bSopenharmony_ci setComment(text: string): void; 314d6c458bSopenharmony_ci setCDATA(text: string): void; 324d6c458bSopenharmony_ci setText(text: string): void; 334d6c458bSopenharmony_ci setDocType(text: string): void; 344d6c458bSopenharmony_ci XmlSerializerError(): string; 354d6c458bSopenharmony_ci} 364d6c458bSopenharmony_ci 374d6c458bSopenharmony_ciinterface Xml { 384d6c458bSopenharmony_ci XmlSerializer: NativeXMLSerializer; 394d6c458bSopenharmony_ci XmlPullParser: NativeXmlPullParser; 404d6c458bSopenharmony_ci} 414d6c458bSopenharmony_ciconst ARGUMENT_LENGTH_TWO = 2; 424d6c458bSopenharmony_ciconst TypeErrorCode = 401; 434d6c458bSopenharmony_ciclass BusinessError extends Error { 444d6c458bSopenharmony_ci code: number; 454d6c458bSopenharmony_ci constructor(msg: string) { 464d6c458bSopenharmony_ci super(msg); 474d6c458bSopenharmony_ci this.name = 'BusinessError'; 484d6c458bSopenharmony_ci this.code = TypeErrorCode; 494d6c458bSopenharmony_ci } 504d6c458bSopenharmony_ci} 514d6c458bSopenharmony_ci 524d6c458bSopenharmony_cideclare function requireInternal(s: string): Xml; 534d6c458bSopenharmony_ciconst XML = requireInternal('xml'); 544d6c458bSopenharmony_ciclass XmlSerializer { 554d6c458bSopenharmony_ci xmlSerializerClass: NativeXMLSerializer; 564d6c458bSopenharmony_ci constructor(obj: object, inputStr: string) { 574d6c458bSopenharmony_ci if (typeof obj !== 'object') { 584d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${obj} must be object`); 594d6c458bSopenharmony_ci } 604d6c458bSopenharmony_ci if (arguments.length === 1 || 614d6c458bSopenharmony_ci (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) { 624d6c458bSopenharmony_ci const inputType: string = 'utf-8'; 634d6c458bSopenharmony_ci this.xmlSerializerClass = new XML.XmlSerializer(obj, inputType); 644d6c458bSopenharmony_ci } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'string' && inputStr.length !== 0)) { 654d6c458bSopenharmony_ci let strTemp: string = inputStr; 664d6c458bSopenharmony_ci if (strTemp.toLowerCase() !== 'utf-8') { 674d6c458bSopenharmony_ci throw new BusinessError('Parameter error.Just support utf-8'); 684d6c458bSopenharmony_ci } 694d6c458bSopenharmony_ci this.xmlSerializerClass = new XML.XmlSerializer(obj, inputStr); 704d6c458bSopenharmony_ci } else { 714d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`); 724d6c458bSopenharmony_ci } 734d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 744d6c458bSopenharmony_ci if (errStr.length !== 0) { 754d6c458bSopenharmony_ci throw new BusinessError(errStr); 764d6c458bSopenharmony_ci } 774d6c458bSopenharmony_ci } 784d6c458bSopenharmony_ci 794d6c458bSopenharmony_ci setAttributes(name: string, value: string): void { 804d6c458bSopenharmony_ci if (typeof name !== 'string') { 814d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${name} must be string`); 824d6c458bSopenharmony_ci } 834d6c458bSopenharmony_ci if (name.length === 0) { 844d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 854d6c458bSopenharmony_ci } 864d6c458bSopenharmony_ci if (typeof value !== 'string') { 874d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${value} must be string`); 884d6c458bSopenharmony_ci } 894d6c458bSopenharmony_ci 904d6c458bSopenharmony_ci this.xmlSerializerClass.setAttributes(name, value); 914d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 924d6c458bSopenharmony_ci if (errStr.length !== 0) { 934d6c458bSopenharmony_ci throw new BusinessError(errStr); 944d6c458bSopenharmony_ci } 954d6c458bSopenharmony_ci } 964d6c458bSopenharmony_ci 974d6c458bSopenharmony_ci addEmptyElement(name: string): void { 984d6c458bSopenharmony_ci if (typeof name !== 'string') { 994d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${name} must be string`); 1004d6c458bSopenharmony_ci } 1014d6c458bSopenharmony_ci if (name.length === 0) { 1024d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 1034d6c458bSopenharmony_ci } 1044d6c458bSopenharmony_ci 1054d6c458bSopenharmony_ci this.xmlSerializerClass.addEmptyElement(name); 1064d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1074d6c458bSopenharmony_ci if (errStr.length !== 0) { 1084d6c458bSopenharmony_ci throw new BusinessError(errStr); 1094d6c458bSopenharmony_ci } 1104d6c458bSopenharmony_ci } 1114d6c458bSopenharmony_ci 1124d6c458bSopenharmony_ci setDeclaration(): void { 1134d6c458bSopenharmony_ci this.xmlSerializerClass.setDeclaration(); 1144d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1154d6c458bSopenharmony_ci if (errStr.length !== 0) { 1164d6c458bSopenharmony_ci throw new BusinessError(errStr); 1174d6c458bSopenharmony_ci } 1184d6c458bSopenharmony_ci } 1194d6c458bSopenharmony_ci 1204d6c458bSopenharmony_ci startElement(name: string): void { 1214d6c458bSopenharmony_ci if (typeof name !== 'string') { 1224d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${name} must be string`); 1234d6c458bSopenharmony_ci } 1244d6c458bSopenharmony_ci if (name.length === 0) { 1254d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 1264d6c458bSopenharmony_ci } 1274d6c458bSopenharmony_ci this.xmlSerializerClass.startElement(name); 1284d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1294d6c458bSopenharmony_ci if (errStr.length !== 0) { 1304d6c458bSopenharmony_ci throw new BusinessError(errStr); 1314d6c458bSopenharmony_ci } 1324d6c458bSopenharmony_ci } 1334d6c458bSopenharmony_ci 1344d6c458bSopenharmony_ci endElement(): void { 1354d6c458bSopenharmony_ci this.xmlSerializerClass.endElement(); 1364d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1374d6c458bSopenharmony_ci if (errStr.length !== 0) { 1384d6c458bSopenharmony_ci throw new BusinessError(errStr); 1394d6c458bSopenharmony_ci } 1404d6c458bSopenharmony_ci } 1414d6c458bSopenharmony_ci 1424d6c458bSopenharmony_ci setNamespace(prefix: string, ns: string): void { 1434d6c458bSopenharmony_ci if (typeof prefix !== 'string') { 1444d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${prefix} must be string`); 1454d6c458bSopenharmony_ci } 1464d6c458bSopenharmony_ci if (prefix.length === 0) { 1474d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 1484d6c458bSopenharmony_ci } 1494d6c458bSopenharmony_ci if (typeof ns !== 'string' || ns.length === 0) { 1504d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${ns} must be string`); 1514d6c458bSopenharmony_ci } 1524d6c458bSopenharmony_ci this.xmlSerializerClass.setNamespace(prefix, ns); 1534d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1544d6c458bSopenharmony_ci if (errStr.length !== 0) { 1554d6c458bSopenharmony_ci throw new BusinessError(errStr); 1564d6c458bSopenharmony_ci } 1574d6c458bSopenharmony_ci } 1584d6c458bSopenharmony_ci 1594d6c458bSopenharmony_ci setComment(text: string): void { 1604d6c458bSopenharmony_ci if (typeof text !== 'string') { 1614d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error.The type of ${text} must be string`); 1624d6c458bSopenharmony_ci throw error; 1634d6c458bSopenharmony_ci } 1644d6c458bSopenharmony_ci if (text.length === 0) { 1654d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 1664d6c458bSopenharmony_ci } 1674d6c458bSopenharmony_ci this.xmlSerializerClass.setComment(text); 1684d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1694d6c458bSopenharmony_ci if (errStr.length !== 0) { 1704d6c458bSopenharmony_ci throw new BusinessError(errStr); 1714d6c458bSopenharmony_ci } 1724d6c458bSopenharmony_ci } 1734d6c458bSopenharmony_ci 1744d6c458bSopenharmony_ci setCDATA(text: string): void { 1754d6c458bSopenharmony_ci if (typeof text !== 'string') { 1764d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${text} must be string`); 1774d6c458bSopenharmony_ci } 1784d6c458bSopenharmony_ci if (text.length === 0) { 1794d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 1804d6c458bSopenharmony_ci } 1814d6c458bSopenharmony_ci this.xmlSerializerClass.setCDATA(text); 1824d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1834d6c458bSopenharmony_ci if (errStr.length !== 0) { 1844d6c458bSopenharmony_ci throw new BusinessError(errStr); 1854d6c458bSopenharmony_ci } 1864d6c458bSopenharmony_ci } 1874d6c458bSopenharmony_ci 1884d6c458bSopenharmony_ci setText(text: string): void { 1894d6c458bSopenharmony_ci if (typeof text !== 'string') { 1904d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${text} must be string`); 1914d6c458bSopenharmony_ci } 1924d6c458bSopenharmony_ci if (text.length === 0) { 1934d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 1944d6c458bSopenharmony_ci } 1954d6c458bSopenharmony_ci this.xmlSerializerClass.setText(text); 1964d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 1974d6c458bSopenharmony_ci if (errStr.length !== 0) { 1984d6c458bSopenharmony_ci throw new BusinessError(errStr); 1994d6c458bSopenharmony_ci } 2004d6c458bSopenharmony_ci } 2014d6c458bSopenharmony_ci 2024d6c458bSopenharmony_ci setDocType(text: string): void { 2034d6c458bSopenharmony_ci if (typeof text !== 'string') { 2044d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${text} must be string`); 2054d6c458bSopenharmony_ci } 2064d6c458bSopenharmony_ci if (text.length === 0) { 2074d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. Parameter cannot be empty`); 2084d6c458bSopenharmony_ci } 2094d6c458bSopenharmony_ci this.xmlSerializerClass.setDocType(text); 2104d6c458bSopenharmony_ci let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 2114d6c458bSopenharmony_ci if (errStr.length !== 0) { 2124d6c458bSopenharmony_ci throw new BusinessError(errStr); 2134d6c458bSopenharmony_ci } 2144d6c458bSopenharmony_ci } 2154d6c458bSopenharmony_ci} 2164d6c458bSopenharmony_ci 2174d6c458bSopenharmony_ciclass XmlPullParser { 2184d6c458bSopenharmony_ci xmlPullParserClass: NativeXmlPullParser; 2194d6c458bSopenharmony_ci constructor(obj: object, inputStr: string) { 2204d6c458bSopenharmony_ci if (typeof obj !== 'object') { 2214d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${obj} must be object`); 2224d6c458bSopenharmony_ci } 2234d6c458bSopenharmony_ci if (arguments.length === 1 || 2244d6c458bSopenharmony_ci (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) { 2254d6c458bSopenharmony_ci let str: string = 'utf-8'; 2264d6c458bSopenharmony_ci this.xmlPullParserClass = new XML.XmlPullParser(obj, str); 2274d6c458bSopenharmony_ci } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 2284d6c458bSopenharmony_ci 'string' && inputStr.length !== 0)) { 2294d6c458bSopenharmony_ci let strTemp: string = inputStr; 2304d6c458bSopenharmony_ci if (strTemp.toLowerCase() !== 'utf-8') { 2314d6c458bSopenharmony_ci throw new BusinessError('Parameter error.Just support utf-8'); 2324d6c458bSopenharmony_ci } 2334d6c458bSopenharmony_ci this.xmlPullParserClass = new XML.XmlPullParser(obj, inputStr); 2344d6c458bSopenharmony_ci } else { 2354d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`); 2364d6c458bSopenharmony_ci } 2374d6c458bSopenharmony_ci let errStr: string = this.xmlPullParserClass.XmlPullParserError(); 2384d6c458bSopenharmony_ci if (errStr.length !== 0) { 2394d6c458bSopenharmony_ci throw new BusinessError(errStr); 2404d6c458bSopenharmony_ci } 2414d6c458bSopenharmony_ci } 2424d6c458bSopenharmony_ci parse(options: object): void { 2434d6c458bSopenharmony_ci if (typeof options !== 'object') { 2444d6c458bSopenharmony_ci throw new BusinessError(`Parameter error.The type of ${options} must be object`); 2454d6c458bSopenharmony_ci } 2464d6c458bSopenharmony_ci this.xmlPullParserClass.parse(options); 2474d6c458bSopenharmony_ci let errStr: string = this.xmlPullParserClass.XmlPullParserError(); 2484d6c458bSopenharmony_ci if (errStr.length !== 0) { 2494d6c458bSopenharmony_ci throw new BusinessError(errStr); 2504d6c458bSopenharmony_ci } 2514d6c458bSopenharmony_ci } 2524d6c458bSopenharmony_ci} 2534d6c458bSopenharmony_ci 2544d6c458bSopenharmony_cienum EventType { 2554d6c458bSopenharmony_ci START_DOCUMENT, 2564d6c458bSopenharmony_ci END_DOCUMENT, 2574d6c458bSopenharmony_ci START_TAG, 2584d6c458bSopenharmony_ci END_TAG, 2594d6c458bSopenharmony_ci TEXT, 2604d6c458bSopenharmony_ci CDSECT, 2614d6c458bSopenharmony_ci COMMENT, 2624d6c458bSopenharmony_ci DOCDECL, 2634d6c458bSopenharmony_ci INSTRUCTION, 2644d6c458bSopenharmony_ci ENTITY_REFERENCE, 2654d6c458bSopenharmony_ci WHITESPACE 2664d6c458bSopenharmony_ci} 2674d6c458bSopenharmony_ci 2684d6c458bSopenharmony_ciexport default { 2694d6c458bSopenharmony_ci XmlSerializer: XmlSerializer, 2704d6c458bSopenharmony_ci XmlPullParser: XmlPullParser, 2714d6c458bSopenharmony_ci EventType, 2724d6c458bSopenharmony_ci}; 273