• 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 ReceiveObject {
17  obj: Object;
18  spaces?: string | number;
19}
20
21interface NativeConvertXml {
22  new(): NativeConvertXml;
23  convert(strXml: string, options?: Object): ReceiveObject;
24  convertToJSObject(strXml: string, options?: Object): ReceiveObject;
25}
26interface ConvertXML {
27  ConvertXml: NativeConvertXml;
28}
29declare function requireInternal(s: string): ConvertXML;
30const convertXml = requireInternal('convertxml');
31
32const LESS_SIGN_INDEX = 3;
33const TypeErrorCode = 401;
34class BusinessError extends Error {
35  code: number;
36  constructor(msg: string) {
37    super(msg)
38    this.name = 'BusinessError';
39    this.code = TypeErrorCode;
40  }
41}
42
43class ConvertXML {
44  convertxmlclass: NativeConvertXml;
45  constructor() {
46    this.convertxmlclass = new convertXml.ConvertXml();
47  }
48  convert(strXml: string, options?: Object): ReceiveObject {
49    strXml = dealXml(strXml);
50    let converted: ReceiveObject = this.convertxmlclass.convert(strXml, options);
51    let strEnd: string = '';
52    if (converted.hasOwnProperty('spaces')) {
53      let space: string | number | undefined = converted.spaces;
54      delete converted.spaces;
55    }
56    return converted;
57  }
58
59  convertToJSObject(strXml: string, options?: Object): ReceiveObject {
60    if (typeof strXml !== 'string') {
61      throw new BusinessError(`Parameter error.The type of ${strXml} must be string`);
62    }
63    if (options && !(typeof options === 'undefined' || options === null) && typeof options !== 'object') {
64      throw new BusinessError(`Parameter error.The type of ${options} must be object`);
65    }
66    strXml = dealXml(strXml);
67    let converted: ReceiveObject;
68    if (arguments.length === 1) {
69      converted = this.convertxmlclass.convert(strXml);
70    } else {
71      converted = this.convertxmlclass.convert(strXml, options);
72    }
73    let strEnd: string = '';
74    if (converted.hasOwnProperty('spaces')) {
75      let space: string | number | undefined = converted.spaces;
76      delete converted.spaces;
77    }
78    return converted;
79  }
80}
81
82function dealXml(strXml: string): string {
83  let idx: number = 0;
84  let idxSec: number = 0;
85  let idxThir: number = 0;
86  let idxCData: number = 0;
87  let idxCDataSec: number = 0;
88  while ((idx = strXml.indexOf(']]><![CDATA')) !== -1) {
89    strXml = strXml.substring(0, idx + LESS_SIGN_INDEX) + ' ' + strXml.substring(idx + LESS_SIGN_INDEX);
90  }
91  while ((idx = strXml.indexOf('>', idxSec)) !== -1) {
92    idxThir = strXml.indexOf('<', idx);
93    strXml = dealPriorReplace(strXml, idx, idxThir);
94    idxSec = strXml.indexOf('<', idx);
95    if (idxSec !== -1) {
96      idxCData = strXml.indexOf('<![CDATA', idxCDataSec);
97      if (idxSec === idxCData) {
98        idxSec = strXml.indexOf(']]>', idxCData);
99        strXml = dealLaterReplace(strXml, idxCData, idxSec);
100        idxCDataSec = idxSec;
101      }
102    } else {
103      break;
104    }
105  }
106  return strXml;
107}
108
109function dealPriorReplace(strXml: string, idx: number, idxThir: number): string {
110  let i: number = idx + 1;
111  for (; i < idxThir; i++) {
112    let cXml: string = strXml.charAt(i);
113    if (cXml !== '\r' && cXml !== '\n' && cXml !== '\v' && cXml !== '\t' && cXml !== ' ') {
114      break;
115    }
116  }
117  if (i === idxThir) {
118    strXml = strXml.substring(0, idx + 1) + strXml.substring(idxThir);
119  }
120  return strXml;
121}
122
123function dealLaterReplace(strXml: string, idx: number, idxThir: number): string {
124  let i: number = idx + 1;
125  let res = strXml.substring(i, idxThir);
126  res = res.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/\r/g, '\\r')
127    .replace(/\t/g, '\\t').replace(/\v/g, '\\v');
128  strXml = strXml.substring(0, i) + res + strXml.substring(idxThir);
129  return strXml;
130}
131
132export default {
133  ConvertXML: ConvertXML
134};
135