Salome HOME
53b07024f1b9dac0ed5864c83061276863665a29
[modules/shaper.git] / src / Config / Config_XMLReader.h
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef CONFIG_XMLREADER_H_
22 #define CONFIG_XMLREADER_H_
23
24 #include <Config_def.h>
25
26 #include <cstdarg>
27 #include <string>
28 #include <map>
29
30 //>> Forward declaration of xmlNodePtr.
31 typedef struct _xmlNode xmlNode;
32 typedef xmlNode *xmlNodePtr;
33 struct _xmlNode;
34 //<<
35
36 //>> Forward declaration of xmlDocPtr.
37 typedef struct _xmlDoc xmlDoc;
38 typedef xmlDoc *xmlDocPtr;
39 struct _xmlDoc;
40 //<<
41
42 /*!
43  * \class Config_XMLReader
44  * \ingroup Config
45  * \brief Base class for all libxml readers. Provides high-level API
46  * for all xml operations.
47 */
48 class Config_XMLReader
49 {
50  public:
51   /*!
52    * Constructor
53    * \param theXmlFile - full path to the xml file which will be processed by the reader
54    */
55   CONFIG_EXPORT Config_XMLReader(const std::string& theXmlFile);
56   CONFIG_EXPORT virtual ~Config_XMLReader();
57   /*!
58    * Returns a path to the plugins.xml file (created from ROOT_DIR environment variable)
59    * \return string value
60    */
61   CONFIG_EXPORT static std::string pluginConfigFile();
62   /*!
63    * Read all nodes in associated xml file,
64    * recursively if processChildren(xmlNode) is true for the xmlNode.
65    * For each read node the processNode will be called.
66    */
67   CONFIG_EXPORT void readAll();
68   /*!
69    * Returns xmlNodePtr to the root of reader's document
70    * or NULL if not found
71    */
72   CONFIG_EXPORT xmlNodePtr findRoot();
73
74   CONFIG_EXPORT const char* encoding() const;
75
76  protected:
77   /*!
78    * \brief Allows to customize reader's behavior for a node. Virtual.
79    * The default implementation process "source" and "validator" nodes.
80    */
81   virtual void processNode(xmlNodePtr aNode);
82
83   /*!
84    * This method gives an ability to finalize processing of a node,
85    * when reader is about to leave the node (node and all it's children are processed)
86    */
87   virtual void cleanup(xmlNodePtr aNode);
88   /*!
89    * \brief Defines which nodes should be processed recursively. Virtual.
90    * The default impl is to read all nodes.
91    */
92   virtual bool processChildren(xmlNodePtr aNode);
93   /*!
94    * Calls processNode() for each child (for some - recursively)
95    * of the given node.
96    * \sa ReadAll()
97    */
98   void readRecursively(xmlNodePtr theParent);
99   /*!
100    * \brief void* -> xmlNodePtr
101    */
102   xmlNodePtr node(void* theNode);
103   /// Gets xml node name
104   std::string getNodeName(xmlNodePtr theNode);
105   /// Stores an attribute in internal map for later use.
106   /// Key is "Node_Name:Node_Attribute" and value is getProperty(theNodeAttribute)
107   void storeAttribute(xmlNodePtr theNode, const char* theNodeAttribute, bool doClean = false);
108   /// Restores an attribute from internal map.
109   std::string restoreAttribute(xmlNodePtr theNode, const char* theNodeAttribute);
110   /// Restores an attribute from internal map.
111   std::string restoreAttribute(const char* theNodeName, const char* theNodeAttribute);
112   /// Cleanups attribute from cache
113   bool cleanupAttribute(xmlNodePtr theNode, const char* theNodeAttribute);
114   /// Cleanups attribute from cache
115   bool cleanupAttribute(const char* theNodeName, const char* theNodeAttribute);
116
117  protected:
118   std::string myDocumentPath; ///< Path to the xml document
119   xmlDocPtr myXmlDoc; ///< Root of the xml document
120   /// A map to store all parent's attributes.
121   /// The key has from "Node_Name:Node_Attribute"
122   std::map<std::string, std::string> myCachedAttributes;
123 };
124
125 #endif /* CONFIG_XMLREADER_H_ */