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"
30 #include <libxml/xmlexports.h>
33 #include <rtl/string.hxx>
34 #include <rtl/strbuf.hxx>
36 #include <unordered_map>
41 #define XML_NODE_TYPE_FILE 0x001
42 #define XML_NODE_TYPE_ELEMENT 0x002
43 #define XML_NODE_TYPE_DATA 0x003
44 #define XML_NODE_TYPE_COMMENT 0x004
45 #define XML_NODE_TYPE_DEFAULT 0x005
47 /** Holds data of Attributes
56 /// creates an attribute
58 const OString
&rName
, // attributes name
59 const OString
&rValue
// attributes data
61 : m_sName( rName
), m_sValue( rValue
) {}
63 OString
GetName() const { return m_sName
; }
64 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 sal_uInt16
GetNodeType() const = 0;
85 /** Virtual base to handle different kinds of child nodes
87 class XMLChildNode
: public XMLNode
90 XMLParentNode
*m_pParent
;
93 XMLChildNode( XMLParentNode
*pPar
);
94 XMLChildNode(): m_pParent( NULL
){};
95 XMLChildNode( const XMLChildNode
& rObj
);
96 XMLChildNode
& operator=(const XMLChildNode
& rObj
);
98 /// returns the parent of this node
99 XMLParentNode
*GetParent() { return m_pParent
; }
100 virtual ~XMLChildNode(){};
103 typedef std::vector
< XMLChildNode
* > XMLChildNodeList
;
107 /** Virtual base to handle different kinds of parent nodes
110 class XMLParentNode
: public XMLChildNode
113 XMLChildNodeList
* m_pChildList
;
116 XMLParentNode( XMLParentNode
*pPar
)
117 : XMLChildNode( pPar
), m_pChildList( NULL
){}
118 XMLParentNode(): m_pChildList(NULL
){}
120 XMLParentNode( const XMLParentNode
& );
122 XMLParentNode
& operator=(const XMLParentNode
& rObj
);
123 virtual ~XMLParentNode();
126 /// returns child list of this node
127 XMLChildNodeList
*GetChildList() { return m_pChildList
; }
131 XMLChildNode
*pChild
/// the new child
134 void RemoveAndDeleteAllChildren();
137 /// Mapping numeric Language code <-> XML Element
138 typedef std::unordered_map
<OString
, XMLElement
*, OStringHash
> LangHashMap
;
140 /// Mapping XML Element string identifier <-> Language Map
141 typedef std::unordered_map
<OString
, LangHashMap
*, OStringHash
> XMLHashMap
;
143 /// Mapping XML tag names <-> have localizable strings
144 typedef std::unordered_map
<OString
, sal_Bool
, OStringHash
> TagMap
;
146 /** Holds information of a XML file, is root node of tree
148 class XMLFile
: public XMLParentNode
152 const OString
&rFileName
// the file name, empty if created from memory stream
154 XMLFile( const XMLFile
& rObj
) ;
157 void Print( XMLNode
*pCur
= NULL
, sal_uInt16 nLevel
= 0 );
158 void SearchL10NElements( XMLChildNode
*pCur
, int pos
= 0 );
159 void Extract( XMLFile
*pCur
= NULL
);
161 XMLHashMap
* GetStrings(){ return m_pXMLStrings
; }
162 void Write( OString
const &rFilename
);
163 bool Write( std::ofstream
&rStream
, XMLNode
*pCur
= NULL
);
165 bool CheckExportStatus( XMLParentNode
*pCur
= NULL
);
167 XMLFile
& operator=(const XMLFile
& rObj
);
169 virtual sal_uInt16
GetNodeType() const SAL_OVERRIDE
{ return XML_NODE_TYPE_FILE
; }
171 /// returns file name
172 OString
GetName() const { return m_sFileName
; }
173 void SetName( const OString
&rFilename
) { m_sFileName
= rFilename
; }
174 const std::vector
<OString
>& getOrder() const { return m_vOrder
; }
178 void InsertL10NElement( XMLElement
* pElement
);
183 TagMap m_aNodes_localize
;
184 XMLHashMap
* m_pXMLStrings
;
186 std::vector
<OString
> m_vOrder
;
189 /// An Utility class for XML
193 /// Quot the XML characters
194 static OString
QuotHTML( const OString
& rString
);
198 /** Hold information of an element node
200 class XMLElement
: public XMLParentNode
203 OString m_sElementName
;
204 XMLAttributeList
*m_pAttributes
;
209 OString m_sResourceType
;
210 OString m_sLanguageId
;
214 void Print(XMLNode
*pCur
, OStringBuffer
& rBuffer
, bool bRootelement
) const;
216 /// create an element node
219 const OString
&rName
, // the element name
220 XMLParentNode
*pParent
// parent node of this element
223 virtual ~XMLElement();
224 XMLElement(const XMLElement
&);
226 XMLElement
& operator=(const XMLElement
& rObj
);
227 virtual sal_uInt16
GetNodeType() const SAL_OVERRIDE
{ return XML_NODE_TYPE_ELEMENT
; }
229 /// returns element name
230 OString
GetName() const { return m_sElementName
; }
232 /// returns list of attributes of this element
233 XMLAttributeList
*GetAttributeList() { return m_pAttributes
; }
235 /// adds a new attribute to this element, typically used by parser
236 void AddAttribute( const OString
&rAttribute
, const OString
&rValue
);
238 void ChangeLanguageTag( const OString
&rValue
);
240 /// Return a Unicode String representation of this object
243 void SetProject ( OString
const & sPrj
) { m_sProject
= sPrj
; }
244 void SetFileName ( OString
const & sFileName
) { m_sFilename
= sFileName
; }
245 void SetId ( OString
const & sTheId
) { m_sId
= sTheId
; }
246 void SetResourceType ( OString
const & sResType
) { m_sResourceType
= sResType
; }
247 void SetLanguageId ( OString
const & sLangId
) { m_sLanguageId
= sLangId
; }
248 void SetPos ( int nPos
) { m_nPos
= nPos
; }
249 void SetOldRef ( OString
const & sOldRef
) { m_sOldRef
= sOldRef
; }
251 int GetPos() { return m_nPos
; }
252 OString
GetProject() const { return m_sProject
; }
253 OString
GetFileName() const { return m_sFilename
; }
254 OString
GetId() const { return m_sId
; }
255 OString
GetOldref() const { return m_sOldRef
; }
256 OString
GetResourceType() const { return m_sResourceType
; }
257 OString
GetLanguageId() const { return m_sLanguageId
; }
262 /** Holds character data
264 class XMLData
: public XMLChildNode
268 bool m_bIsNewCreated
;
271 /// create a data node
273 const OString
&rData
, // the initial data
274 XMLParentNode
*pParent
, // the parent node of this data, typically a element node
275 bool bNewCreated
= false
277 : XMLChildNode( pParent
), m_sData( rData
), m_bIsNewCreated( bNewCreated
){}
279 // Default copy constructor and copy operator work well.
281 virtual sal_uInt16
GetNodeType() const SAL_OVERRIDE
{ return XML_NODE_TYPE_DATA
; }
284 OString
GetData() const { return m_sData
; }
286 bool isNew() const { return m_bIsNewCreated
; }
288 /// adds new character data to the existing one
289 void AddData( const OString
&rData
) { m_sData
+= rData
; }
294 class XMLComment
: public XMLChildNode
300 /// create a comment node
302 const OString
&rComment
, // the comment
303 XMLParentNode
*pParent
// the parent node of this comemnt, typically a element node
305 : XMLChildNode( pParent
), m_sComment( rComment
) {}
307 // Default copy constructor and copy operator work well.
309 virtual sal_uInt16
GetNodeType() const SAL_OVERRIDE
{ return XML_NODE_TYPE_COMMENT
; }
311 /// returns the comment
312 OString
GetComment() const { return m_sComment
; }
315 /** Holds additional file content like those for which no handler exists
317 class XMLDefault
: public XMLChildNode
323 /// create a comment node
325 const OString
&rDefault
, // the comment
326 XMLParentNode
*pParent
// the parent node of this comemnt, typically a element node
328 : XMLChildNode( pParent
), m_sDefault( rDefault
) {}
330 // Default copy constructor and copy operator work well.
332 virtual sal_uInt16
GetNodeType() const SAL_OVERRIDE
{ return XML_NODE_TYPE_DEFAULT
; }
334 /// returns the comment
335 OString
GetDefault() const { return m_sDefault
; }
338 /** struct for error information, used by class SimpleXMLParser
341 XML_Error m_eCode
; ///< the error code
342 std::size_t m_nLine
; ///< error line number
343 std::size_t m_nColumn
; ///< error column number
344 OString m_sMessage
; ///< readable error message
347 /** validating xml parser, creates a document tree with xml nodes
350 class SimpleXMLParser
353 XML_Parser m_aParser
;
354 XMLError m_aErrorInformation
;
357 XMLParentNode
*m_pCurNode
;
361 static void StartElementHandler( void *userData
, const XML_Char
*name
, const XML_Char
**atts
);
362 static void EndElementHandler( void *userData
, const XML_Char
*name
);
363 static void CharacterDataHandler( void *userData
, const XML_Char
*s
, int len
);
364 static void CommentHandler( void *userData
, const XML_Char
*data
);
365 static void DefaultHandler( void *userData
, const XML_Char
*s
, int len
);
368 void StartElement( const XML_Char
*name
, const XML_Char
**atts
);
369 void EndElement( const XML_Char
*name
);
370 void CharacterData( const XML_Char
*s
, int len
);
371 void Comment( const XML_Char
*data
);
372 void Default( const XML_Char
*s
, int len
);
375 /// creates a new parser
379 /// parse a file, returns NULL on criticall errors
381 const OString
&rFileName
, // the file name
382 XMLFile
*pXMLFileIn
// the XMLFile
385 /// returns an error struct
386 const XMLError
&GetError() const { return m_aErrorInformation
; }
389 #endif // INCLUDED_L10NTOOLS_INC_XMLPARSE_HXX
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */