LibreOffice Module l10ntools (master) 1
xmlparse.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
21#define INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
22
23#include <sal/config.h>
24
25#include <cstddef>
26#include <memory>
27#include <utility>
28#include <vector>
29
30#include <expat.h>
31
32#include <rtl/string.hxx>
33#include <rtl/strbuf.hxx>
34#include <unordered_map>
35
36class XMLParentNode;
37class XMLElement;
38
39enum class XMLNodeType{
40 XFILE = 0x001,
41 ELEMENT = 0x002,
42 DATA = 0x003,
43 COMMENT = 0x004,
44 DEFAULT = 0x005
45};
46
50{
51private:
52 OString m_sName;
53 OString m_sValue;
54
55public:
58 OString _sName, // attributes name
59 OString _sValue // attributes data
60 )
61 : m_sName( std::move(_sName) ), m_sValue( std::move(_sValue) ) {}
62
63 const OString& GetName() const { return m_sName; }
64 const OString& GetValue() const { return m_sValue; }
65
66 void setValue( const OString &rValue ){ m_sValue = rValue; }
67};
68
69
70typedef std::vector< XMLAttribute* > XMLAttributeList;
71
75{
76protected:
78
79public:
80 virtual XMLNodeType GetNodeType() const = 0;
81 virtual ~XMLNode(){}
82
83 XMLNode(XMLNode const &) = default;
84 XMLNode(XMLNode &&) = default;
85 XMLNode & operator =(XMLNode const &) = default;
86 XMLNode & operator =(XMLNode &&) = default;
87};
88
89
92class XMLChildNode : public XMLNode
93{
94private:
96
97protected:
99 XMLChildNode( const XMLChildNode& rObj);
100 XMLChildNode& operator=(const XMLChildNode& rObj);
101public:
104};
105
106typedef std::vector< XMLChildNode* > XMLChildNodeList;
107
108class XMLData;
109
114{
115private:
116 std::unique_ptr<XMLChildNodeList> m_pChildList;
117
118protected:
120 : XMLChildNode( pPar ) {}
121
123
125 virtual ~XMLParentNode() override;
126
127public:
130
132 void AddChild(
133 XMLChildNode *pChild
134 );
135
137};
138
140typedef std::unordered_map<OString, XMLElement*> LangHashMap;
141
143typedef std::unordered_map<OString, LangHashMap*> XMLHashMap;
144
147class XMLFile final : public XMLParentNode
148{
149public:
150 XMLFile(
151 OString sFileName // the file name, empty if created from memory stream
152 );
153 XMLFile( const XMLFile& rObj ) ;
154 virtual ~XMLFile() override;
155
156 void Print( XMLNode *pCur, sal_uInt16 nLevel = 0 );
157 void SearchL10NElements( XMLChildNode *pCur );
158 void Extract();
159
161 void Write( OString const &rFilename );
162 void Write( std::ofstream &rStream, XMLNode *pCur = nullptr );
163
164 bool CheckExportStatus( XMLChildNode *pCur = nullptr );
165
166 XMLFile& operator=(const XMLFile& rObj);
167
168 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::XFILE; }
169
171 const OString& GetName() const { return m_sFileName; }
172 void SetName( const OString &rFilename ) { m_sFileName = rFilename; }
173 const std::vector<OString>& getOrder() const { return m_vOrder; }
174
175private:
176
177 void InsertL10NElement( XMLElement* pElement);
178
179 // DATA
180 OString m_sFileName;
181
183 std::unordered_map<OString, bool> m_aNodes_localize;
184
185 std::unique_ptr<XMLHashMap> m_pXMLStrings;
186
187 std::vector <OString> m_vOrder;
188};
189
192{
193public:
195 static OString QuotHTML( const OString& rString );
196};
197
198
202{
203private:
205 std::unique_ptr<XMLAttributeList> m_pAttributes;
206
207protected:
208 void Print(XMLNode *pCur, OStringBuffer& rBuffer, bool bRootelement) const;
209public:
212 OString sName, // the element name
213 XMLParentNode *pParent // parent node of this element
214 );
215
216 virtual ~XMLElement() override;
217 XMLElement(const XMLElement&);
218
219 XMLElement& operator=(const XMLElement& rObj);
220 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::ELEMENT; }
221
223 const OString& GetName() const { return m_sElementName; }
224
227
229 void AddAttribute( const OString &rAttribute, const OString &rValue );
230
231 void ChangeLanguageTag( const OString &rValue );
232
234 OString ToOString();
235};
236
239class XMLData : public XMLChildNode
240{
241private:
242 OString m_sData;
243
244public:
247 OString _sData, // the initial data
248 XMLParentNode *pParent // the parent node of this data, typically an element node
249 ) : XMLChildNode( pParent ), m_sData( std::move(_sData) ) {}
250
251 // Default copy constructor and copy operator work well.
252
253 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::DATA; }
254
256 const OString& GetData() const { return m_sData; }
257
259 void AddData( const OString &rData ) { m_sData += rData; }
260};
261
264class XMLComment final : public XMLChildNode
265{
266private:
267 OString m_sComment;
268
269public:
272 OString _sComment, // the comment
273 XMLParentNode *pParent // the parent node of this comment, typically an element node
274 )
275 : XMLChildNode( pParent ), m_sComment( std::move(_sComment) ) {}
276
277 // Default copy constructor and copy operator work well.
278
279 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::COMMENT; }
280
282 const OString& GetComment() const { return m_sComment; }
283};
284
287class XMLDefault final : public XMLChildNode
288{
289private:
290 OString m_sDefault;
291
292public:
295 OString _sDefault, // the comment
296 XMLParentNode *pParent // the parent node of this comment, typically an element node
297 )
298 : XMLChildNode( pParent ), m_sDefault( std::move(_sDefault) ) {}
299
300 // Default copy constructor and copy operator work well.
301
302 virtual XMLNodeType GetNodeType() const override { return XMLNodeType::DEFAULT; }
303
305 const OString& GetDefault() const { return m_sDefault; }
306};
307
310struct XMLError {
311 XML_Error m_eCode;
312 std::size_t m_nLine;
313 std::size_t m_nColumn;
314 OString m_sMessage;
315};
316
321{
322private:
323 XML_Parser m_aParser;
325
328
329
330 static void StartElementHandler( void *userData, const XML_Char *name, const XML_Char **atts );
331 static void EndElementHandler( void *userData, const XML_Char *name );
332 static void CharacterDataHandler( void *userData, const XML_Char *s, int len );
333 static void CommentHandler( void *userData, const XML_Char *data );
334 static void DefaultHandler( void *userData, const XML_Char *s, int len );
335
336
337 void StartElement( const XML_Char *name, const XML_Char **atts );
338 void EndElement();
339 void CharacterData( const XML_Char *s, int len );
340 void Comment( const XML_Char *data );
341 void Default( const XML_Char *s, int len );
342
343public:
347
349 bool Execute(
350 const OString &rFileName, // the file name
351 XMLFile* pXMLFile // the XMLFile
352 );
353
355 const XMLError &GetError() const { return m_aErrorInformation; }
356};
357
358#endif // INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
359
360/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
validating xml parser, creates a document tree with xml nodes
Definition: xmlparse.hxx:321
void Default(const XML_Char *s, int len)
Definition: xmlparse.cxx:849
XMLData * m_pCurData
Definition: xmlparse.hxx:327
XML_Parser m_aParser
Definition: xmlparse.hxx:323
static void StartElementHandler(void *userData, const XML_Char *name, const XML_Char **atts)
Definition: xmlparse.cxx:777
static void CommentHandler(void *userData, const XML_Char *data)
Definition: xmlparse.cxx:795
SimpleXMLParser()
creates a new parser
Definition: xmlparse.cxx:760
const XMLError & GetError() const
returns an error struct
Definition: xmlparse.hxx:355
XMLParentNode * m_pCurNode
Definition: xmlparse.hxx:326
static void CharacterDataHandler(void *userData, const XML_Char *s, int len)
Definition: xmlparse.cxx:789
bool Execute(const OString &rFileName, XMLFile *pXMLFile)
parse a file, return false on critical errors
Definition: xmlparse.cxx:855
static void DefaultHandler(void *userData, const XML_Char *s, int len)
Definition: xmlparse.cxx:801
void CharacterData(const XML_Char *s, int len)
Definition: xmlparse.cxx:828
static void EndElementHandler(void *userData, const XML_Char *name)
Definition: xmlparse.cxx:783
void EndElement()
Definition: xmlparse.cxx:822
void Comment(const XML_Char *data)
Definition: xmlparse.cxx:843
void StartElement(const XML_Char *name, const XML_Char **atts)
Definition: xmlparse.cxx:807
XMLError m_aErrorInformation
Definition: xmlparse.hxx:324
Holds data of Attributes.
Definition: xmlparse.hxx:50
OString m_sValue
Definition: xmlparse.hxx:53
OString m_sName
Definition: xmlparse.hxx:52
void setValue(const OString &rValue)
Definition: xmlparse.hxx:66
XMLAttribute(OString _sName, OString _sValue)
creates an attribute
Definition: xmlparse.hxx:57
const OString & GetValue() const
Definition: xmlparse.hxx:64
const OString & GetName() const
Definition: xmlparse.hxx:63
Virtual base to handle different kinds of child nodes.
Definition: xmlparse.hxx:93
XMLChildNode & operator=(const XMLChildNode &rObj)
Definition: xmlparse.cxx:58
XMLParentNode * m_pParent
Definition: xmlparse.hxx:95
XMLChildNode(XMLParentNode *pPar)
Definition: xmlparse.cxx:44
XMLParentNode * GetParent()
returns the parent of this node
Definition: xmlparse.hxx:103
Holds comments.
Definition: xmlparse.hxx:265
virtual XMLNodeType GetNodeType() const override
Definition: xmlparse.hxx:279
OString m_sComment
Definition: xmlparse.hxx:267
XMLComment(OString _sComment, XMLParentNode *pParent)
create a comment node
Definition: xmlparse.hxx:271
const OString & GetComment() const
returns the comment
Definition: xmlparse.hxx:282
Holds character data.
Definition: xmlparse.hxx:240
OString m_sData
Definition: xmlparse.hxx:242
XMLData(OString _sData, XMLParentNode *pParent)
create a data node
Definition: xmlparse.hxx:246
const OString & GetData() const
returns the data
Definition: xmlparse.hxx:256
void AddData(const OString &rData)
adds new character data to the existing one
Definition: xmlparse.hxx:259
virtual XMLNodeType GetNodeType() const override
Definition: xmlparse.hxx:253
Holds additional file content like those for which no handler exists.
Definition: xmlparse.hxx:288
virtual XMLNodeType GetNodeType() const override
Definition: xmlparse.hxx:302
const OString & GetDefault() const
returns the comment
Definition: xmlparse.hxx:305
OString m_sDefault
Definition: xmlparse.hxx:290
XMLDefault(OString _sDefault, XMLParentNode *pParent)
create a comment node
Definition: xmlparse.hxx:294
Hold information of an element node.
Definition: xmlparse.hxx:202
void Print(XMLNode *pCur, OStringBuffer &rBuffer, bool bRootelement) const
Definition: xmlparse.cxx:638
OString m_sElementName
Definition: xmlparse.hxx:204
const OString & GetName() const
returns element name
Definition: xmlparse.hxx:223
void AddAttribute(const OString &rAttribute, const OString &rValue)
adds a new attribute to this element, typically used by parser
Definition: xmlparse.cxx:579
virtual XMLNodeType GetNodeType() const override
Definition: xmlparse.hxx:220
XMLAttributeList * GetAttributeList()
returns list of attributes of this element
Definition: xmlparse.hxx:226
XMLElement & operator=(const XMLElement &rObj)
Definition: xmlparse.cxx:556
void ChangeLanguageTag(const OString &rValue)
Definition: xmlparse.cxx:586
std::unique_ptr< XMLAttributeList > m_pAttributes
Definition: xmlparse.hxx:205
OString ToOString()
Return a Unicode String representation of this object.
Definition: xmlparse.cxx:631
virtual ~XMLElement() override
Definition: xmlparse.cxx:622
XMLElement(OString sName, XMLParentNode *pParent)
create an element node
Definition: xmlparse.cxx:535
Holds information of a XML file, is root node of tree.
Definition: xmlparse.hxx:148
std::unique_ptr< XMLHashMap > m_pXMLStrings
Definition: xmlparse.hxx:185
void Write(OString const &rFilename)
Definition: xmlparse.cxx:147
OString m_sFileName
Definition: xmlparse.hxx:180
const OString & GetName() const
returns file name
Definition: xmlparse.hxx:171
std::vector< OString > m_vOrder
Definition: xmlparse.hxx:187
std::unordered_map< OString, bool > m_aNodes_localize
Mapping XML tag names <-> have localizable strings.
Definition: xmlparse.hxx:183
virtual ~XMLFile() override
Definition: xmlparse.cxx:294
const std::vector< OString > & getOrder() const
Definition: xmlparse.hxx:173
XMLHashMap * GetStrings()
Definition: xmlparse.hxx:160
bool CheckExportStatus(XMLChildNode *pCur=nullptr)
Definition: xmlparse.cxx:477
void Extract()
Definition: xmlparse.cxx:327
void SearchL10NElements(XMLChildNode *pCur)
Definition: xmlparse.cxx:424
virtual XMLNodeType GetNodeType() const override
Definition: xmlparse.hxx:168
void SetName(const OString &rFilename)
Definition: xmlparse.hxx:172
XMLFile & operator=(const XMLFile &rObj)
Definition: xmlparse.cxx:395
void InsertL10NElement(XMLElement *pElement)
Definition: xmlparse.cxx:333
void Print(XMLNode *pCur, sal_uInt16 nLevel=0)
Definition: xmlparse.cxx:229
XMLFile(OString sFileName)
Definition: xmlparse.cxx:305
Virtual base to handle different kinds of XML nodes.
Definition: xmlparse.hxx:75
virtual ~XMLNode()
Definition: xmlparse.hxx:81
XMLNode & operator=(XMLNode const &)=default
XMLNode(XMLNode const &)=default
XMLNode()
Definition: xmlparse.hxx:77
virtual XMLNodeType GetNodeType() const =0
XMLNode(XMLNode &&)=default
Virtual base to handle different kinds of parent nodes.
Definition: xmlparse.hxx:114
void RemoveAndDeleteAllChildren()
Definition: xmlparse.cxx:134
void AddChild(XMLChildNode *pChild)
adds a new child
Definition: xmlparse.cxx:127
XMLParentNode(XMLParentNode *pPar)
Definition: xmlparse.hxx:119
virtual ~XMLParentNode() override
Definition: xmlparse.cxx:70
XMLChildNodeList * GetChildList()
returns child list of this node
Definition: xmlparse.hxx:129
std::unique_ptr< XMLChildNodeList > m_pChildList
Definition: xmlparse.hxx:116
XMLParentNode & operator=(const XMLParentNode &rObj)
Definition: xmlparse.cxx:106
A Utility class for XML.
Definition: xmlparse.hxx:192
static OString QuotHTML(const OString &rString)
Quot the XML characters.
Definition: xmlparse.cxx:1062
struct for error information, used by class SimpleXMLParser
Definition: xmlparse.hxx:310
XML_Error m_eCode
the error code
Definition: xmlparse.hxx:311
std::size_t m_nLine
error line number
Definition: xmlparse.hxx:312
OString m_sMessage
readable error message
Definition: xmlparse.hxx:314
std::size_t m_nColumn
error column number
Definition: xmlparse.hxx:313
#define COMMENT
Definition: tokens.h:29
std::unordered_map< OString, LangHashMap * > XMLHashMap
Mapping XML Element string identifier <-> Language Map.
Definition: xmlparse.hxx:143
XMLNodeType
Definition: xmlparse.hxx:39
std::vector< XMLChildNode * > XMLChildNodeList
Definition: xmlparse.hxx:106
std::vector< XMLAttribute * > XMLAttributeList
Definition: xmlparse.hxx:70
std::unordered_map< OString, XMLElement * > LangHashMap
Mapping numeric Language code <-> XML Element.
Definition: xmlparse.hxx:140