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;
88 /** Virtual base to handle different kinds of child nodes
90 class XMLChildNode
: public XMLNode
93 XMLParentNode
*m_pParent
;
96 XMLChildNode( XMLParentNode
*pPar
);
97 XMLChildNode( const XMLChildNode
& rObj
);
98 XMLChildNode
& operator=(const XMLChildNode
& rObj
);
100 /// returns the parent of this node
101 XMLParentNode
*GetParent() { return m_pParent
; }
104 typedef std::vector
< XMLChildNode
* > XMLChildNodeList
;
108 /** Virtual base to handle different kinds of parent nodes
111 class XMLParentNode
: public XMLChildNode
114 std::unique_ptr
<XMLChildNodeList
> m_pChildList
;
117 XMLParentNode( XMLParentNode
*pPar
)
118 : XMLChildNode( pPar
) {}
120 XMLParentNode( const XMLParentNode
& );
122 XMLParentNode
& operator=(const XMLParentNode
& rObj
);
123 virtual ~XMLParentNode() override
;
126 /// returns child list of this node
127 XMLChildNodeList
*GetChildList() { return m_pChildList
.get(); }
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
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
);
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
; }
175 void InsertL10NElement( XMLElement
* pElement
);
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
192 /// Quot the XML characters
193 static OString
QuotHTML( const OString
& rString
);
197 /** Hold information of an element node
199 class XMLElement
: public XMLParentNode
202 OString m_sElementName
;
203 std::unique_ptr
<XMLAttributeList
> m_pAttributes
;
205 OString m_sLanguageId
;
208 void Print(XMLNode
*pCur
, OStringBuffer
& rBuffer
, bool bRootelement
) const;
210 /// create an element node
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
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
248 /// create a data node
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
; }
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
: public XMLChildNode
274 /// create a comment node
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
297 /// create a comment node
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
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, returns NULL on criticall errors
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: */