Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / l10ntools / inc / xmlparse.hxx
blobe8e2d203801d4652132fefe4f5c3ea2c1ee88a7f
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(){}
88 /** Virtual base to handle different kinds of child nodes
90 class XMLChildNode : public XMLNode
92 private:
93 XMLParentNode *m_pParent;
95 protected:
96 XMLChildNode( XMLParentNode *pPar );
97 XMLChildNode( const XMLChildNode& rObj);
98 XMLChildNode& operator=(const XMLChildNode& rObj);
99 public:
100 /// returns the parent of this node
101 XMLParentNode *GetParent() { return m_pParent; }
104 typedef std::vector< XMLChildNode* > XMLChildNodeList;
106 class XMLData;
108 /** Virtual base to handle different kinds of parent nodes
111 class XMLParentNode : public XMLChildNode
113 private:
114 std::unique_ptr<XMLChildNodeList> m_pChildList;
116 protected:
117 XMLParentNode( XMLParentNode *pPar )
118 : XMLChildNode( pPar ) {}
120 XMLParentNode( const XMLParentNode& );
122 XMLParentNode& operator=(const XMLParentNode& rObj);
123 virtual ~XMLParentNode() override;
125 public:
126 /// returns child list of this node
127 XMLChildNodeList *GetChildList() { return m_pChildList.get(); }
129 /// adds a new child
130 void AddChild(
131 XMLChildNode *pChild /// the new child
134 void RemoveAndDeleteAllChildren();
137 /// Mapping numeric Language code <-> XML Element
138 typedef std::unordered_map<OString, XMLElement*> LangHashMap;
140 /// Mapping XML Element string identifier <-> Language Map
141 typedef std::unordered_map<OString, LangHashMap*> XMLHashMap;
143 /** Holds information of a XML file, is root node of tree
145 class XMLFile final : public XMLParentNode
147 public:
148 XMLFile(
149 const OString &rFileName // the file name, empty if created from memory stream
151 XMLFile( const XMLFile& rObj ) ;
152 virtual ~XMLFile() override;
154 void Print( XMLNode *pCur, sal_uInt16 nLevel = 0 );
155 void SearchL10NElements( XMLChildNode *pCur );
156 void Extract();
158 XMLHashMap* GetStrings(){ return m_pXMLStrings.get(); }
159 void Write( OString const &rFilename );
160 bool Write( std::ofstream &rStream, XMLNode *pCur = nullptr );
162 bool CheckExportStatus( XMLParentNode *pCur = nullptr );
164 XMLFile& operator=(const XMLFile& rObj);
166 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::XFILE; }
168 /// returns file name
169 const OString& GetName() const { return m_sFileName; }
170 void SetName( const OString &rFilename ) { m_sFileName = rFilename; }
171 const std::vector<OString>& getOrder() const { return m_vOrder; }
173 private:
175 void InsertL10NElement( XMLElement* pElement);
177 // DATA
178 OString m_sFileName;
180 /// Mapping XML tag names <-> have localizable strings
181 std::unordered_map<OString, bool> m_aNodes_localize;
183 std::unique_ptr<XMLHashMap> m_pXMLStrings;
185 std::vector <OString> m_vOrder;
188 /// An Utility class for XML
189 class XMLUtil
191 public:
192 /// Quot the XML characters
193 static OString QuotHTML( const OString& rString );
197 /** Hold information of an element node
199 class XMLElement : public XMLParentNode
201 private:
202 OString m_sElementName;
203 std::unique_ptr<XMLAttributeList> m_pAttributes;
204 OString m_sId;
205 OString m_sLanguageId;
207 protected:
208 void Print(XMLNode *pCur, OStringBuffer& rBuffer, bool bRootelement) const;
209 public:
210 /// create an element node
211 XMLElement(
212 const OString &rName, // the element name
213 XMLParentNode *pParent // parent node of this element
216 virtual ~XMLElement() override;
217 XMLElement(const XMLElement&);
219 XMLElement& operator=(const XMLElement& rObj);
220 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::ELEMENT; }
222 /// returns element name
223 const OString& GetName() const { return m_sElementName; }
225 /// returns list of attributes of this element
226 XMLAttributeList *GetAttributeList() { return m_pAttributes.get(); }
228 /// adds a new attribute to this element, typically used by parser
229 void AddAttribute( const OString &rAttribute, const OString &rValue );
231 void ChangeLanguageTag( const OString &rValue );
233 /// Return a Unicode String representation of this object
234 OString ToOString();
236 void SetId ( OString const & sTheId ) { m_sId = sTheId; }
237 void SetLanguageId ( OString const & sLangId ) { m_sLanguageId = sLangId; }
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 a 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 : 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 comemnt, typically a 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 : 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 comemnt, typically a 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, returns NULL on criticall errors
353 XMLFile *Execute(
354 const OString &rFileName, // the file name
355 XMLFile *pXMLFileIn // 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: */