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