Fix bug convertxml cannot work

issue: https://gitee.com/openharmony/js_api_module/issues/I51SDA
Signed-off-by: lihucheng <lihucheng1@huawei.com>
This commit is contained in:
lihucheng
2022-04-08 18:10:51 +08:00
parent e626c79712
commit fe228a826b
2 changed files with 45 additions and 40 deletions
+1
View File
@@ -313,6 +313,7 @@ namespace OHOS::Xml {
Replace(strXml, "\\r", "\r");
Replace(strXml, "\\n", "\n");
Replace(strXml, "\\v", "\v");
Replace(strXml, "\\t", "\t");
Replace(strXml, "]]><![CDATA", "]]> <![CDATA");
size_t len = strXml.size();
doc = xmlParseMemory(strXml.c_str(), len);
+44 -40
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -13,53 +13,55 @@
* limitations under the License.
*/
declare function requireInternal(s : string) : any;
const convertXml = requireInternal("ConvertXML");
interface ReceiveObject{
obj : Object;
spaces ?: string | number;
}
interface NativeConvertXml {
new() : NativeConvertXml;
convert(strXml : string, options ?: Object) : ReceiveObject;
}
interface ConvertXML {
ConvertXml : NativeConvertXml;
}
declare function requireInternal(s : string) : ConvertXML;
const convertXml = requireInternal('ConvertXML');
class ConvertXML {
convertxmlclass : any;
convertxmlclass : NativeConvertXml;
constructor() {
this.convertxmlclass = new convertXml.ConvertXml();
}
convert(strXml : string, options : any) {
convert(strXml : string, options ?: Object) {
strXml = DealXml(strXml);
let converted = this.convertxmlclass.convert(strXml, options);
let space = 0;
if (converted.hasOwnProperty("spaces")) {
space = converted.spaces;
let strEnd : string = '';
if (converted.hasOwnProperty('spaces')) {
let space : string | number | undefined = converted.spaces;
delete converted.spaces;
}
var strEnd = JSON.stringify(converted, null, space);
var idx = 0;
while ((idx = strEnd.indexOf('\\t')) != -1) {
strEnd = strEnd.substring(0, idx) + '\t' + strEnd.substring(idx + 2);
}
while ((idx = strEnd.indexOf('\\n')) != -1) {
strEnd = strEnd.substring(0, idx) + '\n' + strEnd.substring(idx + 2);
}
while ((idx = strEnd.indexOf('\\')) != -1) {
strEnd = strEnd.substring(0, idx) + '' + strEnd.substring(idx + 1);
}
return strEnd;
return converted;
}
}
function DealXml(strXml : string)
{
var idx = 0;
var idxSec = 0;
var idxThir = 0;
var idxCData = 0;
var idxCDataSec = 0;
while ((idx = strXml.indexOf(']]><![CDATA')) != -1) {
let idx : number = 0;
let idxSec : number = 0;
let idxThir : number = 0;
let idxCData : number = 0;
let idxCDataSec : number = 0;
while ((idx = strXml.indexOf(']]><![CDATA')) !== -1) {
strXml = strXml.substring(0, idx + 3) + ' ' + strXml.substring(idx + 3);
}
while ((idx = strXml.indexOf('>', idxSec)) != -1) {
while ((idx = strXml.indexOf('>', idxSec)) !== -1) {
idxThir = strXml.indexOf('<', idx);
strXml = DealPriorReplace(strXml, idx, idxThir);
if (strXml.indexOf('<', idx) != -1) {
if (strXml.indexOf('<', idx) !== -1) {
idxCData = strXml.indexOf('<![CDATA', idxCDataSec);
idxSec = strXml.indexOf('<', idx);
if (idxSec == idxCData) {
if (idxSec === idxCData) {
idxSec = strXml.indexOf(']]>', idxCData);
strXml = DealLaterReplace(strXml, idx, idxThir);
idxCDataSec = idxSec;
@@ -72,20 +74,20 @@ function DealXml(strXml : string)
return strXml;
}
function DealPriorReplace(strXml : string, idx : any, idxThir : any)
function DealPriorReplace(strXml : string, idx : number, idxThir : number)
{
var i = idx + 1;
let i : number = idx + 1;
for (; i < idxThir ; i++) {
var cXml = strXml.charAt(i);
if (cXml != '\n' && cXml != '\v' && cXml != '\t' && cXml != ' ')
let cXml : string = strXml.charAt(i);
if (cXml !== '\n' && cXml !== '\v' && cXml !== '\t' && cXml !== ' ')
{
break;
}
}
var j = idx + 1;
let j : number = idx + 1;
for (; j < strXml.indexOf('<', idx) ; j++) {
var cXml = strXml.charAt(j);
if (i != idxThir) {
let cXml : string = strXml.charAt(j);
if (i !== idxThir) {
switch (cXml) {
case '\n':
strXml = strXml.substring(0, j) + '\\n' + strXml.substring(j + 1);
@@ -107,11 +109,11 @@ function DealPriorReplace(strXml : string, idx : any, idxThir : any)
return strXml;
}
function DealLaterReplace(strXml : string, idx : any, idxThir : any)
function DealLaterReplace(strXml : string, idx : number, idxThir : number)
{
var i = idx + 1;
let i : number = idx + 1;
for (; i < idxThir ; i++) {
var cXml = strXml.charAt(i)
let cXml : string = strXml.charAt(i)
switch (cXml) {
case '\n':
strXml = strXml.substring(0, i) + '\\n' + strXml.substring(i + 1);
@@ -129,4 +131,6 @@ function DealLaterReplace(strXml : string, idx : any, idxThir : any)
return strXml;
}
export default ConvertXML
export default {
ConvertXML : ConvertXML
}