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 parseXml(options: object): boolean; 20 XmlPullParserError(): string; 21} 22 23interface NativeXMLSerializer { 24 new(value: object, strEncoding?: string): NativeXMLSerializer; 25 setAttributes(name: string, value: string): void; 26 addEmptyElement(name: string): void; 27 setDeclaration(): void; 28 startElement(name: string): void; 29 endElement(): void; 30 setNamespace(prefix: string, namespace: string): void; 31 setComment(text: string): void; 32 setCDATA(text: string): void; 33 setText(text: string): void; 34 setDocType(text: string): void; 35 XmlSerializerError(): string; 36} 37 38interface NativeXMLDynamicSerializer { 39 new(strEncoding?: string): NativeXMLDynamicSerializer; 40 setAttributes(name: string, value: string): void; 41 addEmptyElement(name: string): void; 42 setDeclaration(): void; 43 startElement(name: string): void; 44 endElement(): void; 45 setNamespace(prefix: string, namespace: string): void; 46 setComment(text: string): void; 47 setCDATA(text: string): void; 48 setText(text: string): void; 49 setDocType(text: string): void; 50 getOutput(): ArrayBuffer | undefined; 51} 52 53interface Xml { 54 XmlSerializer: NativeXMLSerializer; 55 XmlPullParser: NativeXmlPullParser; 56 XmlDynamicSerializer: NativeXMLDynamicSerializer; 57} 58 59const ARGUMENT_LENGTH_ONE = 1; 60const ARGUMENT_LENGTH_TWO = 2; 61const TypeErrorCode = 401; 62const EmptyErrorCode = 10200064; 63const EncodingErrorCode = 10200066; 64class BusinessError extends Error { 65 code: number; 66 constructor(msg: string, errCode?: number) { 67 super(msg); 68 this.name = 'BusinessError'; 69 this.code = errCode ?? TypeErrorCode; 70 } 71} 72 73declare function requireInternal(s: string): Xml; 74const XML = requireInternal('xml'); 75class XmlSerializer { 76 xmlSerializerClass: NativeXMLSerializer; 77 constructor(obj: object, inputStr: string) { 78 if (typeof obj !== 'object') { 79 throw new BusinessError(`Parameter error.The type of ${obj} must be object`); 80 } 81 if (arguments.length === 1 || 82 (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) { 83 const inputType: string = 'utf-8'; 84 this.xmlSerializerClass = new XML.XmlSerializer(obj, inputType); 85 } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'string' && inputStr.length !== 0)) { 86 let strTemp: string = inputStr; 87 if (strTemp.toLowerCase() !== 'utf-8') { 88 throw new BusinessError('Parameter error.Just support utf-8'); 89 } 90 this.xmlSerializerClass = new XML.XmlSerializer(obj, inputStr); 91 } else { 92 throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`); 93 } 94 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 95 if (errStr.length !== 0) { 96 throw new BusinessError(errStr); 97 } 98 } 99 100 setAttributes(name: string, value: string): void { 101 if (typeof name !== 'string') { 102 throw new BusinessError(`Parameter error.The type of ${name} must be string`); 103 } 104 if (name.length === 0) { 105 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 106 } 107 if (typeof value !== 'string') { 108 throw new BusinessError(`Parameter error.The type of ${value} must be string`); 109 } 110 111 this.xmlSerializerClass.setAttributes(name, value); 112 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 113 if (errStr.length !== 0) { 114 throw new BusinessError(errStr); 115 } 116 } 117 118 addEmptyElement(name: string): void { 119 if (typeof name !== 'string') { 120 throw new BusinessError(`Parameter error.The type of ${name} must be string`); 121 } 122 if (name.length === 0) { 123 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 124 } 125 126 this.xmlSerializerClass.addEmptyElement(name); 127 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 128 if (errStr.length !== 0) { 129 throw new BusinessError(errStr); 130 } 131 } 132 133 setDeclaration(): void { 134 this.xmlSerializerClass.setDeclaration(); 135 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 136 if (errStr.length !== 0) { 137 throw new BusinessError(errStr); 138 } 139 } 140 141 startElement(name: string): void { 142 if (typeof name !== 'string') { 143 throw new BusinessError(`Parameter error.The type of ${name} must be string`); 144 } 145 if (name.length === 0) { 146 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 147 } 148 this.xmlSerializerClass.startElement(name); 149 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 150 if (errStr.length !== 0) { 151 throw new BusinessError(errStr); 152 } 153 } 154 155 endElement(): void { 156 this.xmlSerializerClass.endElement(); 157 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 158 if (errStr.length !== 0) { 159 throw new BusinessError(errStr); 160 } 161 } 162 163 setNamespace(prefix: string, ns: string): void { 164 if (typeof prefix !== 'string') { 165 throw new BusinessError(`Parameter error.The type of ${prefix} must be string`); 166 } 167 if (prefix.length === 0) { 168 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 169 } 170 if (typeof ns !== 'string' || ns.length === 0) { 171 throw new BusinessError(`Parameter error.The type of ${ns} must be string`); 172 } 173 this.xmlSerializerClass.setNamespace(prefix, ns); 174 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 175 if (errStr.length !== 0) { 176 throw new BusinessError(errStr); 177 } 178 } 179 180 setComment(text: string): void { 181 if (typeof text !== 'string') { 182 let error = new BusinessError(`Parameter error.The type of ${text} must be string`); 183 throw error; 184 } 185 if (text.length === 0) { 186 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 187 } 188 this.xmlSerializerClass.setComment(text); 189 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 190 if (errStr.length !== 0) { 191 throw new BusinessError(errStr); 192 } 193 } 194 195 setCDATA(text: string): void { 196 if (typeof text !== 'string') { 197 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 198 } 199 if (text.length === 0) { 200 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 201 } 202 this.xmlSerializerClass.setCDATA(text); 203 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 204 if (errStr.length !== 0) { 205 throw new BusinessError(errStr); 206 } 207 } 208 209 setText(text: string): void { 210 if (typeof text !== 'string') { 211 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 212 } 213 if (text.length === 0) { 214 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 215 } 216 this.xmlSerializerClass.setText(text); 217 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 218 if (errStr.length !== 0) { 219 throw new BusinessError(errStr); 220 } 221 } 222 223 setDocType(text: string): void { 224 if (typeof text !== 'string') { 225 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 226 } 227 if (text.length === 0) { 228 throw new BusinessError(`Parameter error. Parameter cannot be empty`); 229 } 230 this.xmlSerializerClass.setDocType(text); 231 let errStr: string = this.xmlSerializerClass.XmlSerializerError(); 232 if (errStr.length !== 0) { 233 throw new BusinessError(errStr); 234 } 235 } 236} 237 238class XmlDynamicSerializer { 239 xmlSerializerClass: NativeXMLDynamicSerializer; 240 constructor(encoding?: string) { 241 let input: string = 'utf-8'; 242 if (arguments.length === ARGUMENT_LENGTH_ONE) { 243 if (typeof encoding !== 'string') { 244 throw new BusinessError(`Parameter error.The type of ${encoding} must be string`); 245 } 246 if (encoding.toLowerCase() !== 'utf-8') { 247 throw new BusinessError('Incorrect encoding format, only support utf-8.', EncodingErrorCode); 248 } 249 } 250 this.xmlSerializerClass = new XML.XmlDynamicSerializer(input); 251 } 252 253 getOutput(): ArrayBuffer { 254 let result = this.xmlSerializerClass.getOutput(); 255 if (result === undefined) { 256 return new ArrayBuffer(0); 257 } 258 return result; 259 } 260 261 setAttributes(name: string, value: string): void { 262 if (typeof name !== 'string') { 263 throw new BusinessError(`Parameter error.The type of ${name} must be string`); 264 } 265 if (name.length === 0) { 266 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 267 } 268 if (typeof value !== 'string') { 269 throw new BusinessError(`Parameter error.The type of ${value} must be string`); 270 } 271 this.xmlSerializerClass.setAttributes(name, value); 272 } 273 274 addEmptyElement(name: string): void { 275 if (typeof name !== 'string') { 276 throw new BusinessError(`Parameter error.The type of ${name} must be string`); 277 } 278 if (name.length === 0) { 279 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 280 } 281 this.xmlSerializerClass.addEmptyElement(name); 282 } 283 284 setDeclaration(): void { 285 this.xmlSerializerClass.setDeclaration(); 286 } 287 288 startElement(name: string): void { 289 if (typeof name !== 'string') { 290 throw new BusinessError(`Parameter error.The type of ${name} must be string`); 291 } 292 if (name.length === 0) { 293 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 294 } 295 this.xmlSerializerClass.startElement(name); 296 } 297 298 endElement(): void { 299 this.xmlSerializerClass.endElement(); 300 } 301 302 setNamespace(prefix: string, ns: string): void { 303 if (typeof prefix !== 'string') { 304 throw new BusinessError(`Parameter error.The type of ${prefix} must be string`); 305 } 306 if (typeof ns !== 'string') { 307 throw new BusinessError(`Parameter error.The type of ${ns} must be string`); 308 } 309 if (prefix.length === 0 || ns.length === 0) { 310 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 311 } 312 this.xmlSerializerClass.setNamespace(prefix, ns); 313 } 314 315 setComment(text: string): void { 316 if (typeof text !== 'string') { 317 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 318 } 319 if (text.length === 0) { 320 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 321 } 322 this.xmlSerializerClass.setComment(text); 323 } 324 325 setCdata(text: string): void { 326 if (typeof text !== 'string') { 327 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 328 } 329 if (text.length === 0) { 330 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 331 } 332 this.xmlSerializerClass.setCDATA(text); 333 } 334 335 setText(text: string): void { 336 if (typeof text !== 'string') { 337 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 338 } 339 if (text.length === 0) { 340 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 341 } 342 this.xmlSerializerClass.setText(text); 343 } 344 345 setDocType(text: string): void { 346 if (typeof text !== 'string') { 347 throw new BusinessError(`Parameter error.The type of ${text} must be string`); 348 } 349 if (text.length === 0) { 350 throw new BusinessError('Parameter error. Cannot be an empty string.', EmptyErrorCode); 351 } 352 this.xmlSerializerClass.setDocType(text); 353 } 354} 355 356class XmlPullParser { 357 xmlPullParserClass: NativeXmlPullParser; 358 constructor(obj: object, inputStr: string) { 359 if (typeof obj !== 'object') { 360 throw new BusinessError(`Parameter error.The type of ${obj} must be object`); 361 } 362 if (arguments.length === 1 || 363 (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 'undefined' || inputStr === null))) { 364 let str: string = 'utf-8'; 365 this.xmlPullParserClass = new XML.XmlPullParser(obj, str); 366 } else if (arguments.length === ARGUMENT_LENGTH_TWO && (typeof inputStr === 367 'string' && inputStr.length !== 0)) { 368 let strTemp: string = inputStr; 369 if (strTemp.toLowerCase() !== 'utf-8') { 370 throw new BusinessError('Parameter error.Just support utf-8'); 371 } 372 this.xmlPullParserClass = new XML.XmlPullParser(obj, inputStr); 373 } else { 374 throw new BusinessError(`Parameter error.The type of ${inputStr} must be string`); 375 } 376 let errStr: string = this.xmlPullParserClass.XmlPullParserError(); 377 if (errStr.length !== 0) { 378 throw new BusinessError(errStr); 379 } 380 } 381 parse(options: object): void { 382 if (typeof options !== 'object') { 383 throw new BusinessError(`Parameter error.The type of ${options} must be object`); 384 } 385 this.xmlPullParserClass.parse(options); 386 let errStr: string = this.xmlPullParserClass.XmlPullParserError(); 387 if (errStr.length !== 0) { 388 throw new BusinessError(errStr); 389 } 390 } 391 392 parseXml(options: object): void { 393 if (typeof options !== 'object') { 394 throw new BusinessError(`Parameter error.The type of ${options} must be object`); 395 } 396 if (this.xmlPullParserClass.parseXml(options)) { 397 let errStr: string = this.xmlPullParserClass.XmlPullParserError(); 398 throw new BusinessError(errStr); 399 } 400 } 401} 402 403enum EventType { 404 START_DOCUMENT, 405 END_DOCUMENT, 406 START_TAG, 407 END_TAG, 408 TEXT, 409 CDSECT, 410 COMMENT, 411 DOCDECL, 412 INSTRUCTION, 413 ENTITY_REFERENCE, 414 WHITESPACE 415} 416 417export default { 418 XmlSerializer: XmlSerializer, 419 XmlPullParser: XmlPullParser, 420 XmlDynamicSerializer: XmlDynamicSerializer, 421 EventType, 422}; 423