1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
31 #include <libxml/xmlexports.h>
34 #include <rtl/string.hxx>
35 #include <rtl/strbuf.hxx>
37 #include <unordered_map>
42 enum class XMLNodeType
{
50 /** Holds data of Attributes
59 /// creates an attribute
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
83 virtual XMLNodeType
GetNodeType() const = 0;
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
98 XMLParentNode
*m_pParent
;
101 XMLChildNode( XMLParentNode
*pPar
);
102 XMLChildNode( const XMLChildNode
& rObj
);
103 XMLChildNode
& operator=(const XMLChildNode
& rObj
);
105 /// returns the parent of this node
106 XMLParentNode
*GetParent() { return m_pParent
; }
109 typedef std::vector
< XMLChildNode
* > XMLChildNodeList
;
113 /** Virtual base to handle different kinds of parent nodes
116 class XMLParentNode
: public XMLChildNode
119 std::unique_ptr
<XMLChildNodeList
> m_pChildList
;
122 XMLParentNode( XMLParentNode
*pPar
)
123 : XMLChildNode( pPar
) {}
125 XMLParentNode( const XMLParentNode
& );
127 XMLParentNode
& operator=(const XMLParentNode
& rObj
);
128 virtual ~XMLParentNode() override
;
131 /// returns child list of this node
132 XMLChildNodeList
*GetChildList() { return m_pChildList
.get(); }
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
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
);
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
; }
180 void InsertL10NElement( XMLElement
* pElement
);
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
197 /// Quot the XML characters
198 static OString
QuotHTML( const OString
& rString
);
202 /** Hold information of an element node
204 class XMLElement
: public XMLParentNode
207 OString m_sElementName
;
208 std::unique_ptr
<XMLAttributeList
> m_pAttributes
;
211 void Print(XMLNode
*pCur
, OStringBuffer
& rBuffer
, bool bRootelement
) const;
213 /// create an element node
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
240 /** Holds character data
242 class XMLData
: public XMLChildNode
248 /// create a data node
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
; }
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
; }
268 class XMLComment final
: public XMLChildNode
274 /// create a comment node
276 const OString
&rComment
, // the comment
277 XMLParentNode
*pParent
// the parent node of this comemnt, 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
297 /// create a comment node
299 const OString
&rDefault
, // the comment
300 XMLParentNode
*pParent
// the parent node of this comemnt, 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
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
327 XML_Parser m_aParser
;
328 XMLError m_aErrorInformation
;
330 XMLParentNode
*m_pCurNode
;
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
);
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
);
348 /// creates a new parser
352 /// parse a file, return false on critical errors
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: */