Salome HOME
9f2fd8415f91055828f72f5aa8f20bb53c3d8c9c
[modules/shaper.git] / src / Config / Config_XMLReader.h
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef CONFIG_XMLREADER_H_
21 #define CONFIG_XMLREADER_H_
22
23 #include <Config_def.h>
24
25 #include <cstdarg>
26 #include <string>
27 #include <map>
28
29 //>> Forward declaration of xmlNodePtr.
30 typedef struct _xmlNode xmlNode;
31 typedef xmlNode *xmlNodePtr;
32 struct _xmlNode;
33 //<<
34
35 //>> Forward declaration of xmlDocPtr.
36 typedef struct _xmlDoc xmlDoc;
37 typedef xmlDoc *xmlDocPtr;
38 struct _xmlDoc;
39 //<<
40
41 /*!
42  * \class Config_XMLReader
43  * \ingroup Config
44  * \brief Base class for all libxml readers. Provides high-level API
45  * for all xml operations.
46 */
47 class Config_XMLReader
48 {
49  public:
50   /*!
51    * Constructor
52    * \param theXmlFile - full path to the xml file which will be processed by the reader
53    */
54   CONFIG_EXPORT Config_XMLReader(const std::string& theXmlFile, bool isXMLContent = false);
55   CONFIG_EXPORT virtual ~Config_XMLReader();
56   /*!
57    * Returns a path to resource files (created from ROOT_DIR environment variable)
58    * \return string value
59    */
60   CONFIG_EXPORT static std::string resourcesConfigFile();
61   /*!
62    * Returns a path to the plugins.xml file (created from ROOT_DIR environment variable)
63    * \return string value
64    */
65   CONFIG_EXPORT static std::string pluginConfigFile();
66   /*!
67    * Read all nodes in associated xml file,
68    * recursively if processChildren(xmlNode) is true for the xmlNode.
69    * For each read node the processNode will be called.
70    */
71   CONFIG_EXPORT void readAll();
72   /*!
73    * Returns xmlNodePtr to the root of reader's document or NULL if not found.
74    * If the path to the document to read is empty, uses myDocumentPath.
75    */
76   CONFIG_EXPORT xmlNodePtr findRoot(const std::string theDocumentPath = "");
77
78   CONFIG_EXPORT const char* encoding() const;
79
80   /// Checks all possible paths to configuration file given
81   /// Uses theFindIndex if several solutions can be found (this is the number of solution to find)
82   CONFIG_EXPORT static std::string
83     findConfigFile(const std::string theFileName, const int theFindIndex = 0);
84
85  protected:
86   /*!
87    * \brief Allows to customize reader's behavior for a node. Virtual.
88    * The default implementation process "source" and "validator" nodes.
89    */
90   virtual void processNode(xmlNodePtr aNode);
91
92   /*!
93    * This method gives an ability to finalize processing of a node,
94    * when reader is about to leave the node (node and all it's children are processed)
95    */
96   virtual void cleanup(xmlNodePtr aNode);
97   /*!
98    * \brief Defines which nodes should be processed recursively. Virtual.
99    * The default impl is to read all nodes.
100    */
101   virtual bool processChildren(xmlNodePtr aNode);
102   /*!
103    * Calls processNode() for each child (for some - recursively)
104    * of the given node.
105    * \sa ReadAll()
106    */
107   void readRecursively(xmlNodePtr theParent);
108   /*!
109    * \brief void* -> xmlNodePtr
110    */
111   xmlNodePtr node(void* theNode);
112   /// Gets xml node name
113   std::string getNodeName(xmlNodePtr theNode);
114   /// Stores an attribute in internal map for later use.
115   /// Key is "Node_Name:Node_Attribute" and value is getProperty(theNodeAttribute)
116   void storeAttribute(xmlNodePtr theNode, const char* theNodeAttribute, bool doClean = false);
117   /// Restores an attribute from internal map.
118   std::string restoreAttribute(xmlNodePtr theNode, const char* theNodeAttribute);
119   /// Restores an attribute from internal map.
120   std::string restoreAttribute(const char* theNodeName, const char* theNodeAttribute);
121   /// Cleanups attribute from cache
122   bool cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute);
123   /// Cleanups attribute from cache
124   bool cleanupAttribute(const char* theNodeName, const char* theNodeAttribute);
125
126  protected:
127   std::string myDocumentPath; ///< Path to the xml document
128   xmlDocPtr myXmlDoc; ///< Root of the xml document
129   std::string myRootFileName; ///< name of the root file
130   /// A map to store all parent's attributes.
131   /// The key has from "Node_Name:Node_Attribute"
132   std::map<std::string, std::string> myCachedAttributes;
133
134   bool isFromMemory;
135 };
136
137 #endif /* CONFIG_XMLREADER_H_ */