tdf#140726 Change func func(<?>) to func <?>(<?>) in Math
[LibreOffice.git] / l10ntools / inc / xmlparse.hxx
bloba9993f460b5cb1d9e9a01888300e2971d17687c0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
21 #define INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
23 #include <sal/config.h>
25 #include <cstddef>
26 #include <memory>
27 #include <vector>
29 #include <signal.h>
31 #include <libxml/xmlexports.h>
32 #include <expat.h>
34 #include <rtl/string.hxx>
35 #include <rtl/strbuf.hxx>
36 #include "export.hxx"
37 #include <unordered_map>
39 class XMLParentNode;
40 class XMLElement;
42 enum class XMLNodeType{
43 XFILE = 0x001,
44 ELEMENT = 0x002,
45 DATA = 0x003,
46 COMMENT = 0x004,
47 DEFAULT = 0x005
50 /** Holds data of Attributes
52 class XMLAttribute
54 private:
55 OString m_sName;
56 OString m_sValue;
58 public:
59 /// creates an attribute
60 XMLAttribute(
61 const OString &rName, // attributes name
62 const OString &rValue // attributes data
64 : m_sName( rName ), m_sValue( rValue ) {}
66 const OString& GetName() const { return m_sName; }
67 const OString& GetValue() const { return m_sValue; }
69 void setValue( const OString &rValue ){ m_sValue = rValue; }
73 typedef std::vector< XMLAttribute* > XMLAttributeList;
75 /** Virtual base to handle different kinds of XML nodes
77 class XMLNode
79 protected:
80 XMLNode(){}
82 public:
83 virtual XMLNodeType GetNodeType() const = 0;
84 virtual ~XMLNode(){}
86 XMLNode(XMLNode const &) = default;
87 XMLNode(XMLNode &&) = default;
88 XMLNode & operator =(XMLNode const &) = default;
89 XMLNode & operator =(XMLNode &&) = default;
93 /** Virtual base to handle different kinds of child nodes
95 class XMLChildNode : public XMLNode
97 private:
98 XMLParentNode *m_pParent;
100 protected:
101 XMLChildNode( XMLParentNode *pPar );
102 XMLChildNode( const XMLChildNode& rObj);
103 XMLChildNode& operator=(const XMLChildNode& rObj);
104 public:
105 /// returns the parent of this node
106 XMLParentNode *GetParent() { return m_pParent; }
109 typedef std::vector< XMLChildNode* > XMLChildNodeList;
111 class XMLData;
113 /** Virtual base to handle different kinds of parent nodes
116 class XMLParentNode : public XMLChildNode
118 private:
119 std::unique_ptr<XMLChildNodeList> m_pChildList;
121 protected:
122 XMLParentNode( XMLParentNode *pPar )
123 : XMLChildNode( pPar ) {}
125 XMLParentNode( const XMLParentNode& );
127 XMLParentNode& operator=(const XMLParentNode& rObj);
128 virtual ~XMLParentNode() override;
130 public:
131 /// returns child list of this node
132 XMLChildNodeList *GetChildList() { return m_pChildList.get(); }
134 /// adds a new child
135 void AddChild(
136 XMLChildNode *pChild /// the new child
139 void RemoveAndDeleteAllChildren();
142 /// Mapping numeric Language code <-> XML Element
143 typedef std::unordered_map<OString, XMLElement*> LangHashMap;
145 /// Mapping XML Element string identifier <-> Language Map
146 typedef std::unordered_map<OString, LangHashMap*> XMLHashMap;
148 /** Holds information of a XML file, is root node of tree
150 class XMLFile final : public XMLParentNode
152 public:
153 XMLFile(
154 const OString &rFileName // the file name, empty if created from memory stream
156 XMLFile( const XMLFile& rObj ) ;
157 virtual ~XMLFile() override;
159 void Print( XMLNode *pCur, sal_uInt16 nLevel = 0 );
160 void SearchL10NElements( XMLChildNode *pCur );
161 void Extract();
163 XMLHashMap* GetStrings(){ return m_pXMLStrings.get(); }
164 void Write( OString const &rFilename );
165 void Write( std::ofstream &rStream, XMLNode *pCur = nullptr );
167 bool CheckExportStatus( XMLParentNode *pCur = nullptr );
169 XMLFile& operator=(const XMLFile& rObj);
171 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::XFILE; }
173 /// returns file name
174 const OString& GetName() const { return m_sFileName; }
175 void SetName( const OString &rFilename ) { m_sFileName = rFilename; }
176 const std::vector<OString>& getOrder() const { return m_vOrder; }
178 private:
180 void InsertL10NElement( XMLElement* pElement);
182 // DATA
183 OString m_sFileName;
185 /// Mapping XML tag names <-> have localizable strings
186 std::unordered_map<OString, bool> m_aNodes_localize;
188 std::unique_ptr<XMLHashMap> m_pXMLStrings;
190 std::vector <OString> m_vOrder;
193 /// A Utility class for XML
194 class XMLUtil
196 public:
197 /// Quot the XML characters
198 static OString QuotHTML( const OString& rString );
202 /** Hold information of an element node
204 class XMLElement : public XMLParentNode
206 private:
207 OString m_sElementName;
208 std::unique_ptr<XMLAttributeList> m_pAttributes;
210 protected:
211 void Print(XMLNode *pCur, OStringBuffer& rBuffer, bool bRootelement) const;
212 public:
213 /// create an element node
214 XMLElement(
215 const OString &rName, // the element name
216 XMLParentNode *pParent // parent node of this element
219 virtual ~XMLElement() override;
220 XMLElement(const XMLElement&);
222 XMLElement& operator=(const XMLElement& rObj);
223 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::ELEMENT; }
225 /// returns element name
226 const OString& GetName() const { return m_sElementName; }
228 /// returns list of attributes of this element
229 XMLAttributeList *GetAttributeList() { return m_pAttributes.get(); }
231 /// adds a new attribute to this element, typically used by parser
232 void AddAttribute( const OString &rAttribute, const OString &rValue );
234 void ChangeLanguageTag( const OString &rValue );
236 /// Return a Unicode String representation of this object
237 OString ToOString();
240 /** Holds character data
242 class XMLData : public XMLChildNode
244 private:
245 OString m_sData;
247 public:
248 /// create a data node
249 XMLData(
250 const OString &rData, // the initial data
251 XMLParentNode *pParent // the parent node of this data, typically an element node
253 : XMLChildNode( pParent ), m_sData( rData ) {}
255 // Default copy constructor and copy operator work well.
257 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::DATA; }
259 /// returns the data
260 const OString& GetData() const { return m_sData; }
262 /// adds new character data to the existing one
263 void AddData( const OString &rData ) { m_sData += rData; }
266 /** Holds comments
268 class XMLComment final : public XMLChildNode
270 private:
271 OString m_sComment;
273 public:
274 /// create a comment node
275 XMLComment(
276 const OString &rComment, // the comment
277 XMLParentNode *pParent // the parent node of this comment, typically an element node
279 : XMLChildNode( pParent ), m_sComment( rComment ) {}
281 // Default copy constructor and copy operator work well.
283 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::COMMENT; }
285 /// returns the comment
286 const OString& GetComment() const { return m_sComment; }
289 /** Holds additional file content like those for which no handler exists
291 class XMLDefault final : public XMLChildNode
293 private:
294 OString m_sDefault;
296 public:
297 /// create a comment node
298 XMLDefault(
299 const OString &rDefault, // the comment
300 XMLParentNode *pParent // the parent node of this comment, typically an element node
302 : XMLChildNode( pParent ), m_sDefault( rDefault ) {}
304 // Default copy constructor and copy operator work well.
306 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::DEFAULT; }
308 /// returns the comment
309 const OString& GetDefault() const { return m_sDefault; }
312 /** struct for error information, used by class SimpleXMLParser
314 struct XMLError {
315 XML_Error m_eCode; ///< the error code
316 std::size_t m_nLine; ///< error line number
317 std::size_t m_nColumn; ///< error column number
318 OString m_sMessage; ///< readable error message
321 /** validating xml parser, creates a document tree with xml nodes
324 class SimpleXMLParser
326 private:
327 XML_Parser m_aParser;
328 XMLError m_aErrorInformation;
330 XMLParentNode *m_pCurNode;
331 XMLData *m_pCurData;
334 static void StartElementHandler( void *userData, const XML_Char *name, const XML_Char **atts );
335 static void EndElementHandler( void *userData, const XML_Char *name );
336 static void CharacterDataHandler( void *userData, const XML_Char *s, int len );
337 static void CommentHandler( void *userData, const XML_Char *data );
338 static void DefaultHandler( void *userData, const XML_Char *s, int len );
341 void StartElement( const XML_Char *name, const XML_Char **atts );
342 void EndElement();
343 void CharacterData( const XML_Char *s, int len );
344 void Comment( const XML_Char *data );
345 void Default( const XML_Char *s, int len );
347 public:
348 /// creates a new parser
349 SimpleXMLParser();
350 ~SimpleXMLParser();
352 /// parse a file, return false on critical errors
353 bool Execute(
354 const OString &rFileName, // the file name
355 XMLFile* pXMLFile // the XMLFile
358 /// returns an error struct
359 const XMLError &GetError() const { return m_aErrorInformation; }
362 #endif // INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */