mirror of
https://github.com/openharmony/js_api_module.git
synced 2026-08-24 22:21:26 -04:00
@@ -283,18 +283,23 @@ void ConvertXml::SetSpacesInfo(napi_value &object)
|
||||
|
||||
napi_value ConvertXml::convert(std::string strXml)
|
||||
{
|
||||
xmlDocPtr doc = NULL;
|
||||
xmlNodePtr curNode = NULL;
|
||||
xmlDocPtr doc = nullptr;
|
||||
xmlNodePtr curNode = nullptr;
|
||||
napi_status status = napi_ok;
|
||||
napi_value object = nullptr;
|
||||
status = napi_create_object(env_, &object);
|
||||
if (status != napi_ok) {
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
Replace(strXml, "\\r", "\r");
|
||||
Replace(strXml, "\\n", "\n");
|
||||
Replace(strXml, "\\v", "\v");
|
||||
Replace(strXml, "]]><![CDATA", "]]> <![CDATA");
|
||||
size_t len = strXml.size();
|
||||
doc = xmlParseMemory(strXml.c_str(), len);
|
||||
if (!doc) {
|
||||
xmlFreeDoc(doc);
|
||||
DealSingleLine(strXml, object);
|
||||
return object;
|
||||
}
|
||||
napi_value subObject = nullptr;
|
||||
@@ -307,14 +312,15 @@ napi_value ConvertXml::convert(std::string strXml)
|
||||
if (doc != nullptr && doc->encoding != nullptr) {
|
||||
SetKeyValue(subSubObject, "encoding", (const char*)doc->encoding);
|
||||
}
|
||||
|
||||
if (!m_Options.ignoreDeclaration && strXml.find("xml") != std::string::npos) {
|
||||
napi_set_named_property(env_, subObject, m_Options.attributes.c_str(), subSubObject);
|
||||
napi_set_named_property(env_, object, m_Options.declaration.c_str(), subObject);
|
||||
}
|
||||
curNode = xmlDocGetRootElement(doc);
|
||||
GetPrevNodeList(curNode);
|
||||
GetXMLInfo(curNode, object, 0);
|
||||
if (doc != nullptr) {
|
||||
curNode = xmlDocGetRootElement(doc);
|
||||
GetPrevNodeList(curNode);
|
||||
GetXMLInfo(curNode, object, 0);
|
||||
}
|
||||
SetSpacesInfo(object);
|
||||
return object;
|
||||
}
|
||||
@@ -459,4 +465,118 @@ void ConvertXml::DealOptions(napi_value napi_obj)
|
||||
}
|
||||
DealIgnore(napi_obj);
|
||||
DealSpaces(napi_obj);
|
||||
}
|
||||
|
||||
void ConvertXml::DealSingleLine(std::string &strXml, napi_value &object)
|
||||
{
|
||||
size_t iXml = 0;
|
||||
if ((iXml = strXml.find("xml")) != std::string::npos) {
|
||||
m_XmlInfo.bXml = true;
|
||||
napi_value declObj = nullptr;
|
||||
napi_create_object(env_, &declObj);
|
||||
napi_value attrObj = nullptr;
|
||||
bool bFlag = false;
|
||||
napi_create_object(env_, &attrObj);
|
||||
if (strXml.find("version=") != std::string::npos) {
|
||||
m_XmlInfo.bVersion = true;
|
||||
SetKeyValue(attrObj, "version", "1.0");
|
||||
bFlag = true;
|
||||
}
|
||||
if (strXml.find("encoding=") != std::string::npos) {
|
||||
m_XmlInfo.bEncoding = false;
|
||||
SetKeyValue(attrObj, "encoding", "utf-8");
|
||||
bFlag = true;
|
||||
}
|
||||
if (bFlag) {
|
||||
napi_set_named_property(env_, declObj, m_Options.attributes.c_str(), attrObj);
|
||||
napi_set_named_property(env_, object, m_Options.declaration.c_str(), declObj);
|
||||
} else {
|
||||
napi_set_named_property(env_, object, m_Options.declaration.c_str(), declObj);
|
||||
}
|
||||
if (strXml.find(">", iXml) == strXml.size() - 1) {
|
||||
strXml = "";
|
||||
} else {
|
||||
strXml = strXml.substr(0, strXml.rfind("<", iXml)) + strXml.substr(strXml.find(">", iXml) + 1);
|
||||
}
|
||||
}
|
||||
size_t iCount = 0;
|
||||
size_t iLen = strXml.size();
|
||||
for (; iCount < iLen; ++iCount) {
|
||||
if (strXml[iCount] != ' ' && strXml[iCount] != '\v' &&
|
||||
strXml[iCount] != '\t' && strXml[iCount] != '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (iCount < iLen) {
|
||||
DealComplex(strXml, object);
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertXml::DealComplex(std::string &strXml, napi_value &object)
|
||||
{
|
||||
if (strXml.find("<!DOCTYPE") != std::string::npos) {
|
||||
strXml = strXml + "<node></node>";
|
||||
} else {
|
||||
strXml = "<node>" + strXml + "</node>";
|
||||
}
|
||||
xmlDocPtr doc = nullptr;
|
||||
xmlNodePtr curNode = nullptr;
|
||||
size_t len = strXml.size();
|
||||
doc = xmlParseMemory(strXml.c_str(), len);
|
||||
if (!doc) {
|
||||
xmlFreeDoc(doc);
|
||||
}
|
||||
if (doc) {
|
||||
curNode = xmlDocGetRootElement(doc);
|
||||
curNode = curNode->children;
|
||||
napi_value elements = nullptr;
|
||||
napi_create_array(env_, &elements);
|
||||
bool bHasEle = false;
|
||||
int index = 0;
|
||||
bool bCData = false;
|
||||
if (strXml.find("<![CDATA") != strXml.rfind("<![CDATA")) {
|
||||
bCData = true;
|
||||
}
|
||||
while (curNode != nullptr) {
|
||||
napi_value elementsObject = nullptr;
|
||||
napi_create_object(env_, &elementsObject);
|
||||
SetNodeInfo(curNode, elementsObject);
|
||||
SetXmlElementType(curNode, elementsObject, bHasEle);
|
||||
SetEndInfo(curNode, elementsObject, bHasEle);
|
||||
napi_set_element(env_, elements, index++, elementsObject);
|
||||
DealCDataInfo(bCData, curNode);
|
||||
}
|
||||
if (bHasEle) {
|
||||
napi_set_named_property(env_, object, m_Options.elements.c_str(), elements);
|
||||
}
|
||||
xmlFreeDoc(doc);
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertXml::Replace(std::string &str, const std::string src, const std::string dst)
|
||||
{
|
||||
size_t index = 0;
|
||||
while ((index = str.find(src)) != std::string::npos) {
|
||||
str.replace(index, src.size(), dst);
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertXml::DealCDataInfo(bool bCData, xmlNodePtr &curNode)
|
||||
{
|
||||
if (bCData && curNode->type == xmlElementType::XML_CDATA_SECTION_NODE &&
|
||||
curNode->next && curNode->next->type == xmlElementType::XML_TEXT_NODE &&
|
||||
curNode->next->next && curNode->next->next->type == xmlElementType::XML_CDATA_SECTION_NODE) {
|
||||
if (xmlNodeGetContent(curNode->next) != nullptr) {
|
||||
std::string strTemp = (char*)xmlNodeGetContent(curNode->next);
|
||||
Replace(strTemp, " ", "");
|
||||
Replace(strTemp, "\v", "");
|
||||
Replace(strTemp, "\t", "");
|
||||
Replace(strTemp, "\n", "");
|
||||
if (strTemp == "") {
|
||||
curNode = curNode->next->next;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
curNode = curNode->next;
|
||||
}
|
||||
}
|
||||
+40
-27
@@ -30,33 +30,41 @@ enum class SpaceType {
|
||||
};
|
||||
|
||||
struct Options {
|
||||
std::string declaration = "_declaration";
|
||||
std::string instruction = "_instruction";
|
||||
std::string attributes = "_attributes";
|
||||
std::string text = "_text";
|
||||
std::string cdata = "_cdata";
|
||||
std::string doctype = "_doctype";
|
||||
std::string comment = "_comment";
|
||||
std::string parent = "_parent";
|
||||
std::string type = "_type";
|
||||
std::string name = "_name";
|
||||
std::string elements = "_elements";
|
||||
bool compact = false;
|
||||
bool trim = false;
|
||||
bool nativetype = false;
|
||||
bool nativetypeattributes = false;
|
||||
bool addparent = false;
|
||||
bool alwaysArray = false;
|
||||
bool alwaysChildren = false;
|
||||
bool instructionHasAttributes = false;
|
||||
bool ignoreDeclaration = false;
|
||||
bool ignoreInstruction = false;
|
||||
bool ignoreAttributes = false;
|
||||
bool ignoreComment = false;
|
||||
bool ignoreCdata = false;
|
||||
bool ignoreDoctype = false;
|
||||
bool ignoreText = false;
|
||||
bool spaces = false;
|
||||
std::string declaration = "_declaration";
|
||||
std::string instruction = "_instruction";
|
||||
std::string attributes = "_attributes";
|
||||
std::string text = "_text";
|
||||
std::string cdata = "_cdata";
|
||||
std::string doctype = "_doctype";
|
||||
std::string comment = "_comment";
|
||||
std::string parent = "_parent";
|
||||
std::string type = "_type";
|
||||
std::string name = "_name";
|
||||
std::string elements = "_elements";
|
||||
bool compact = false;
|
||||
bool trim = false;
|
||||
bool nativetype = false;
|
||||
bool nativetypeattributes = false;
|
||||
bool addparent = false;
|
||||
bool alwaysArray = false;
|
||||
bool alwaysChildren = false;
|
||||
bool instructionHasAttributes = false;
|
||||
bool ignoreDeclaration = false;
|
||||
bool ignoreInstruction = false;
|
||||
bool ignoreAttributes = false;
|
||||
bool ignoreComment = false;
|
||||
bool ignoreCdata = false;
|
||||
bool ignoreDoctype = false;
|
||||
bool ignoreText = false;
|
||||
bool spaces = false;
|
||||
};
|
||||
|
||||
struct XmlInfo {
|
||||
bool bXml = false;
|
||||
bool bVersion = false;
|
||||
std::string strVersion = "";
|
||||
bool bEncoding = false;
|
||||
std::string strEncoding = "";
|
||||
};
|
||||
|
||||
class ConvertXml {
|
||||
@@ -80,6 +88,10 @@ public:
|
||||
void SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1);
|
||||
void SetDefaultKey(size_t i, std::string strRecv);
|
||||
void SetSpacesInfo(napi_value &object);
|
||||
void DealSingleLine(std::string &strXml, napi_value &object);
|
||||
void DealComplex(std::string &strXml, napi_value &object);
|
||||
void Replace(std::string &str, const std::string src, const std::string dst);
|
||||
void DealCDataInfo(bool bCData, xmlNodePtr &curNode);
|
||||
private:
|
||||
napi_env env_;
|
||||
SpaceType m_SpaceType;
|
||||
@@ -87,5 +99,6 @@ private:
|
||||
std::string m_strSpace;
|
||||
Options m_Options;
|
||||
std::vector<napi_value> m_prevObj;
|
||||
XmlInfo m_XmlInfo;
|
||||
};
|
||||
#endif
|
||||
+70
-31
@@ -28,52 +28,56 @@ class ConvertXml {
|
||||
space = converted.spaces;
|
||||
delete converted.spaces;
|
||||
}
|
||||
return JSON.stringify(converted, null, space);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
function DealXml(strXml)
|
||||
{
|
||||
var idx = -1;
|
||||
var idx = 0;
|
||||
var idxSec = 0;
|
||||
var idxThir = 0;
|
||||
var idxCData = 0;
|
||||
var idxCDataSec = 0;
|
||||
while ((idx = strXml.indexOf(']]><![CDATA')) != -1) {
|
||||
strXml = strXml.substring(0, idx + 3) + ' ' + strXml.substring(idx + 3);
|
||||
}
|
||||
while ((idx = strXml.indexOf('>', idxSec)) != -1) {
|
||||
idxThir = strXml.indexOf('<', idx);
|
||||
var i = idx + 1;
|
||||
for (; i < idxThir ; i++) {
|
||||
var cXml = strXml.charAt(i);
|
||||
if (cXml != '\n' && cXml != '\v' && cXml != '\t' && cXml != ' ')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
var j = idx + 1;
|
||||
for (; j < strXml.indexOf('<', idx) ; j++) {
|
||||
var cXml = strXml.charAt(j);
|
||||
if (i != idxThir) {
|
||||
switch (cXml) {
|
||||
case '\n':
|
||||
strXml = strXml.substring(0, j) + '\\n' + strXml.substring(j + 1);
|
||||
break;
|
||||
case '\v':
|
||||
strXml = strXml.substring(0, j) + '\\v' + strXml.substring(j + 1);
|
||||
break;
|
||||
case '\t':
|
||||
strXml = strXml.substring(0, j) + '\\t' + strXml.substring(j + 1);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
strXml = strXml.substring(0, j) + strXml.substring(j + 1);
|
||||
--j;
|
||||
}
|
||||
}
|
||||
strXml = DealReplace(strXml, idx, idxThir);
|
||||
if (strXml.indexOf('<', idx) != -1) {
|
||||
idxCData = strXml.indexOf('<![CDATA', idxCDataSec);
|
||||
idxSec = strXml.indexOf('<', idx);
|
||||
if (idxSec == idxCData) {
|
||||
idxSec = strXml.indexOf(']]', idxCData);
|
||||
idxSec = strXml.indexOf(']]>', idxCData);
|
||||
var i = idx + 1;
|
||||
for (; i < idxThir ; i++) {
|
||||
var cXml = strXml.charAt(i)
|
||||
switch (cXml) {
|
||||
case '\n':
|
||||
strXml = strXml.substring(0, i) + '\\n' + strXml.substring(i + 1);
|
||||
break;
|
||||
case '\v':
|
||||
strXml = strXml.substring(0, i) + '\\v' + strXml.substring(i + 1);
|
||||
break;
|
||||
case '\t':
|
||||
strXml = strXml.substring(0, i) + '\\t' + strXml.substring(i + 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
idxCDataSec = idxSec;
|
||||
}
|
||||
}
|
||||
@@ -84,6 +88,41 @@ function DealXml(strXml)
|
||||
return strXml;
|
||||
}
|
||||
|
||||
function DealReplace(strXml, idx, idxThir)
|
||||
{
|
||||
var i = idx + 1;
|
||||
for (; i < idxThir ; i++) {
|
||||
var cXml = strXml.charAt(i);
|
||||
if (cXml != '\n' && cXml != '\v' && cXml != '\t' && cXml != ' ')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
var j = idx + 1;
|
||||
for (; j < strXml.indexOf('<', idx) ; j++) {
|
||||
var cXml = strXml.charAt(j);
|
||||
if (i != idxThir) {
|
||||
switch (cXml) {
|
||||
case '\n':
|
||||
strXml = strXml.substring(0, j) + '\\n' + strXml.substring(j + 1);
|
||||
break;
|
||||
case '\v':
|
||||
strXml = strXml.substring(0, j) + '\\v' + strXml.substring(j + 1);
|
||||
break;
|
||||
case '\t':
|
||||
strXml = strXml.substring(0, j) + '\\t' + strXml.substring(j + 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
strXml = strXml.substring(0, j) + strXml.substring(j + 1);
|
||||
--j;
|
||||
}
|
||||
}
|
||||
return strXml;
|
||||
}
|
||||
|
||||
export default {
|
||||
ConvertXml : ConvertXml
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user