turn gb_CustomTarget_get_workdir into error and remove old _repo_targets
[LibreOffice.git] / l10ntools / inc / xmlparse.hxx
blob8e7d320affc5ec32b7f5998e73673e6f98443a9d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <cstddef>
26 #include <memory>
27 #include <utility>
28 #include <vector>
30 #include <expat.h>
32 #include <rtl/string.hxx>
33 #include <rtl/strbuf.hxx>
34 #include <unordered_map>
36 class XMLParentNode;
37 class XMLElement;
39 enum class XMLNodeType{
40 XFILE = 0x001,
41 ELEMENT = 0x002,
42 DATA = 0x003,
43 COMMENT = 0x004,
44 DEFAULT = 0x005
47 /** Holds data of Attributes
49 class XMLAttribute
51 private:
52 OString m_sName;
53 OString m_sValue;
55 public:
56 /// creates an attribute
57 XMLAttribute(
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
74 class XMLNode
76 protected:
77 XMLNode(){}
79 public:
80 virtual XMLNodeType GetNodeType() const = 0;
81 virtual ~XMLNode(){}
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
94 private:
95 XMLParentNode *m_pParent;
97 protected:
98 XMLChildNode( XMLParentNode *pPar );
99 XMLChildNode( const XMLChildNode& rObj);
100 XMLChildNode& operator=(const XMLChildNode& rObj);
101 public:
102 /// returns the parent of this node
103 XMLParentNode *GetParent() { return m_pParent; }
106 typedef std::vector< XMLChildNode* > XMLChildNodeList;
108 class XMLData;
110 /** Virtual base to handle different kinds of parent nodes
113 class XMLParentNode : public XMLChildNode
115 private:
116 std::unique_ptr<XMLChildNodeList> m_pChildList;
118 protected:
119 XMLParentNode( XMLParentNode *pPar )
120 : XMLChildNode( pPar ) {}
122 XMLParentNode( const XMLParentNode& );
124 XMLParentNode& operator=(const XMLParentNode& rObj);
125 virtual ~XMLParentNode() override;
127 public:
128 /// returns child list of this node
129 XMLChildNodeList *GetChildList() { return m_pChildList.get(); }
131 /// adds a new child
132 void AddChild(
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
149 public:
150 XMLFile(
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 );
158 void Extract();
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; }
175 private:
177 void InsertL10NElement( XMLElement* pElement);
179 // DATA
180 OString m_sFileName;
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
191 class XMLUtil
193 public:
194 /// Quot the XML characters
195 static OString QuotHTML( const OString& rString );
199 /** Hold information of an element node
201 class XMLElement : public XMLParentNode
203 private:
204 OString m_sElementName;
205 std::unique_ptr<XMLAttributeList> m_pAttributes;
207 protected:
208 void Print(XMLNode *pCur, OStringBuffer& rBuffer, bool bRootelement) const;
209 public:
210 /// create an element node
211 XMLElement(
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
234 OString ToOString();
237 /** Holds character data
239 class XMLData : public XMLChildNode
241 private:
242 OString m_sData;
244 public:
245 /// create a data node
246 XMLData(
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; }
255 /// returns the 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; }
262 /** Holds comments
264 class XMLComment final : public XMLChildNode
266 private:
267 OString m_sComment;
269 public:
270 /// create a comment node
271 XMLComment(
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
289 private:
290 OString m_sDefault;
292 public:
293 /// create a comment node
294 XMLDefault(
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
310 struct XMLError {
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
322 private:
323 XML_Parser m_aParser;
324 XMLError m_aErrorInformation;
326 XMLParentNode *m_pCurNode;
327 XMLData *m_pCurData;
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 );
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 );
343 public:
344 /// creates a new parser
345 SimpleXMLParser();
346 ~SimpleXMLParser();
348 /// parse a file, return false on critical errors
349 bool Execute(
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: */