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>
32 #include <rtl/string.hxx>
33 #include <rtl/strbuf.hxx>
34 #include <unordered_map>
39 enum class XMLNodeType
{
47 /** Holds data of Attributes
56 /// creates an attribute
58 OString _sName
, // attributes name
59 OString _sValue
// attributes data
61 : m_sName( std::move(_sName
) ), m_sValue( std::move(_sValue
) ) {}
63 const OString
& GetName() const { return m_sName
; }
64 const OString
& GetValue() const { return m_sValue
; }
66 void setValue( const OString
&rValue
){ m_sValue
= rValue
; }
70 typedef std::vector
< XMLAttribute
* > XMLAttributeList
;
72 /** Virtual base to handle different kinds of XML nodes
80 virtual XMLNodeType
GetNodeType() const = 0;
83 XMLNode(XMLNode
const &) = default;
84 XMLNode(XMLNode
&&) = default;
85 XMLNode
& operator =(XMLNode
const &) = default;
86 XMLNode
& operator =(XMLNode
&&) = default;
90 /** Virtual base to handle different kinds of child nodes
92 class XMLChildNode
: public XMLNode
95 XMLParentNode
*m_pParent
;
98 XMLChildNode( XMLParentNode
*pPar
);
99 XMLChildNode( const XMLChildNode
& rObj
);
100 XMLChildNode
& operator=(const XMLChildNode
& rObj
);
102 /// returns the parent of this node
103 XMLParentNode
*GetParent() { return m_pParent
; }
106 typedef std::vector
< XMLChildNode
* > XMLChildNodeList
;
110 /** Virtual base to handle different kinds of parent nodes
113 class XMLParentNode
: public XMLChildNode
116 std::unique_ptr
<XMLChildNodeList
> m_pChildList
;
119 XMLParentNode( XMLParentNode
*pPar
)
120 : XMLChildNode( pPar
) {}
122 XMLParentNode( const XMLParentNode
& );
124 XMLParentNode
& operator=(const XMLParentNode
& rObj
);
125 virtual ~XMLParentNode() override
;
128 /// returns child list of this node
129 XMLChildNodeList
*GetChildList() { return m_pChildList
.get(); }
133 XMLChildNode
*pChild
/// the new child
136 void RemoveAndDeleteAllChildren();
139 /// Mapping numeric Language code <-> XML Element
140 typedef std::unordered_map
<OString
, XMLElement
*> LangHashMap
;
142 /// Mapping XML Element string identifier <-> Language Map
143 typedef std::unordered_map
<OString
, LangHashMap
*> XMLHashMap
;
145 /** Holds information of a XML file, is root node of tree
147 class XMLFile final
: public XMLParentNode
151 OString sFileName
// the file name, empty if created from memory stream
153 XMLFile( const XMLFile
& rObj
) ;
154 virtual ~XMLFile() override
;
156 void Print( XMLNode
*pCur
, sal_uInt16 nLevel
= 0 );
157 void SearchL10NElements( XMLChildNode
*pCur
);
160 XMLHashMap
* GetStrings(){ return m_pXMLStrings
.get(); }
161 void Write( OString
const &rFilename
);
162 void Write( std::ofstream
&rStream
, XMLNode
*pCur
= nullptr );
164 bool CheckExportStatus( XMLChildNode
*pCur
= nullptr );
166 XMLFile
& operator=(const XMLFile
& rObj
);
168 virtual XMLNodeType
GetNodeType() const override
{ return XMLNodeType::XFILE
; }
170 /// returns file name
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
; }
177 void InsertL10NElement( XMLElement
* pElement
);
182 /// Mapping XML tag names <-> have localizable strings
183 std::unordered_map
<OString
, bool> m_aNodes_localize
;
185 std::unique_ptr
<XMLHashMap
> m_pXMLStrings
;
187 std::vector
<OString
> m_vOrder
;
190 /// A Utility class for XML
194 /// Quot the XML characters
195 static OString
QuotHTML( const OString
& rString
);
199 /** Hold information of an element node
201 class XMLElement
: public XMLParentNode
204 OString m_sElementName
;
205 std::unique_ptr
<XMLAttributeList
> m_pAttributes
;
208 void Print(XMLNode
*pCur
, OStringBuffer
& rBuffer
, bool bRootelement
) const;
210 /// create an element node
212 OString sName
, // 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
237 /** Holds character data
239 class XMLData
: public XMLChildNode
245 /// create a data node
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
) ) {}
251 // Default copy constructor and copy operator work well.
253 virtual XMLNodeType
GetNodeType() const override
{ return XMLNodeType::DATA
; }
256 const OString
& GetData() const { return m_sData
; }
258 /// adds new character data to the existing one
259 void AddData( const OString
&rData
) { m_sData
+= rData
; }
264 class XMLComment final
: public XMLChildNode
270 /// create a comment node
272 OString _sComment
, // the comment
273 XMLParentNode
*pParent
// the parent node of this comment, typically an element node
275 : XMLChildNode( pParent
), m_sComment( std::move(_sComment
) ) {}
277 // Default copy constructor and copy operator work well.
279 virtual XMLNodeType
GetNodeType() const override
{ return XMLNodeType::COMMENT
; }
281 /// returns the comment
282 const OString
& GetComment() const { return m_sComment
; }
285 /** Holds additional file content like those for which no handler exists
287 class XMLDefault final
: public XMLChildNode
293 /// create a comment node
295 OString _sDefault
, // the comment
296 XMLParentNode
*pParent
// the parent node of this comment, typically an element node
298 : XMLChildNode( pParent
), m_sDefault( std::move(_sDefault
) ) {}
300 // Default copy constructor and copy operator work well.
302 virtual XMLNodeType
GetNodeType() const override
{ return XMLNodeType::DEFAULT
; }
304 /// returns the comment
305 const OString
& GetDefault() const { return m_sDefault
; }
308 /** struct for error information, used by class SimpleXMLParser
311 XML_Error m_eCode
; ///< the error code
312 std::size_t m_nLine
; ///< error line number
313 std::size_t m_nColumn
; ///< error column number
314 OString m_sMessage
; ///< readable error message
317 /** validating xml parser, creates a document tree with xml nodes
320 class SimpleXMLParser
323 XML_Parser m_aParser
;
324 XMLError m_aErrorInformation
;
326 XMLParentNode
*m_pCurNode
;
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
);
337 void StartElement( const XML_Char
*name
, const XML_Char
**atts
);
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
);
344 /// creates a new parser
348 /// parse a file, return false on critical errors
350 const OString
&rFileName
, // the file name
351 XMLFile
* pXMLFile
// the XMLFile
354 /// returns an error struct
355 const XMLError
&GetError() const { return m_aErrorInformation
; }
358 #endif // INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */