• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #ifndef OHOS_RESTOOL_SOLID_XML_H
17 #define OHOS_RESTOOL_SOLID_XML_H
18 
19 #include<fstream>
20 #include<memory>
21 #include<string>
22 #include<vector>
23 #include "libxml/parser.h"
24 #include "xml_key_node.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Restool {
29 class SolidXml {
30 public:
31     explicit SolidXml(const std::string &xmlPath, std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &keys);
32     virtual ~SolidXml();
33     bool GenerateSolidXml(const std::string &filePath);
34     bool FlushNodeKeys(const std::string &filePath,
35         std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys);
36 private:
37     class Node {
38     public:
Node()39         Node(): name_(-1), value_(-1), nameSpace_(-1) {};
~Node()40         virtual ~Node() {};
41         virtual void RawData(std::ofstream &out) const;
42         virtual bool LoadFrom(std::ifstream &in);
43         virtual bool FlushIndex(const std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &oldKeys,
44             std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys);
SetName(int32_t index)45         void SetName(int32_t index)
46         {
47             name_ = index;
48         };
SetValue(int32_t index)49         void SetValue(int32_t index)
50         {
51             value_ = index;
52         };
SetNameSpace(int32_t index)53         void SetNameSpace(int32_t index)
54         {
55             nameSpace_ = index;
56         };
57     protected:
58         int32_t name_;
59         int32_t value_;
60         int32_t nameSpace_;
61     };
62 
63     class XmlNode : public Node {
64     public:
XmlNode()65         XmlNode() : child_(-1), brother_(-1) {};
~XmlNode()66         virtual ~XmlNode() {};
67         void RawData(std::ofstream &out) const override;
68         bool LoadFrom(std::ifstream &in) override;
69         bool FlushIndex(const std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &oldKeys,
70             std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys) override;
SetChild(int32_t index)71         void SetChild(int32_t index)
72         {
73             child_ = index;
74         };
SetBrother(int32_t index)75         void SetBrother(int32_t index)
76         {
77             brother_ = index;
78         };
AddAttribute(int32_t index)79         void AddAttribute(int32_t index)
80         {
81             attributes_.push_back(index);
82         };
83     private:
84         int32_t child_;
85         int32_t brother_;
86         std::vector<int32_t> attributes_;
87     };
88 
89     void Compile(const xmlNodePtr nodePtr, std::shared_ptr<XmlNode> &node);
90     void CompileAttr(const xmlAttrPtr attrPtr, std::shared_ptr<XmlNode> &node);
91     void CompileNameSpace(const xmlNodePtr nodePtr, std::shared_ptr<XmlNode> &node);
92     void AddNampeSpaceDef(int32_t nameSpace, int32_t href);
93     bool SaveToFile(const std::string &filePath) const;
94     void PretreatmentAttr(std::string &value) const;
95     bool LoadFromFile(const std::string &sxmlPath);
96     static bool ChangeToNewKey(const std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &oldKeys,
97         std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys,
98         XmlKeyNode::KeyType keyType, int32_t &keyId);
99     bool FlushXmlnsKey(std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys);
100     const std::string &xmlPath_;
101     std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &keys_;
102     std::vector<std::shared_ptr<Node>> attributes_;
103     std::vector<std::shared_ptr<XmlNode>> nodes_;
104     std::vector<int32_t> nameSpaces_;
105     std::vector<int32_t> hrefs_;
106 
107     static const uint8_t SOLID_XML_MAGIC[];
108     static const uint32_t SOLID_XML_MAGIC_LENGTH;
109     #define CHECK_IO(stream) \
110         if (!stream) { \
111             return false; \
112         }
113 };
114 }
115 }
116 }
117 #endif