merged tag ooo/OOO330_m14
[LibreOffice.git] / l10ntools / inc / xmlparse.hxx
blob69e74a83ec47ea09b9b8dbf96e4d782269e6aef3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #ifndef BOOTSTRP_XMLPARSE_HXX
29 #define BOOTSTRP_XMLPARSE_HXX
31 #include <signal.h>
32 #include <expat.h>
33 #include <rtl/ustring.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include "tools/string.hxx"
36 #include "tools/list.hxx"
37 #define ENABLE_BYTESTRING_STREAM_OPERATORS
38 #include "tools/stream.hxx"
39 #include "tools/isofallback.hxx"
40 #include "export.hxx"
41 #include "xmlutil.hxx"
43 #include <fstream>
44 #include <iostream>
46 class XMLParentNode;
47 class XMLElement;
50 using namespace ::rtl;
51 using namespace std;
53 #include <hash_map> /* std::hashmap*/
54 #include <deque> /* std::deque*/
55 #include <iterator> /* std::iterator*/
56 #include <list> /* std::list*/
57 #include <vector> /* std::vector*/
58 #define XML_NODE_TYPE_FILE 0x001
59 #define XML_NODE_TYPE_ELEMENT 0x002
60 #define XML_NODE_TYPE_DATA 0x003
61 #define XML_NODE_TYPE_COMMENT 0x004
62 #define XML_NODE_TYPE_DEFAULT 0x005
63 #define MAX_LANGUAGES 99
66 //#define TESTDRIVER /* use xml2gsi testclass */
67 //-------------------------------------------------------------------------
69 /** Holds data of Attributes
71 class XMLAttribute : public String
73 private:
74 String sValue;
76 public:
77 /// creates an attribute
78 XMLAttribute(
79 const String &rName, // attributes name
80 const String &rValue // attributes data
82 : String( rName ), sValue( rValue ) {}
84 /// getting value of an attribue
85 const String &GetValue() { return sValue; }
87 void setValue(const String &rValue){sValue=rValue;}
89 /// returns true if two attributes are equal and have the same value
90 BOOL IsEqual(
91 const XMLAttribute &rAttribute // the attribute which has to be equal
94 return (( rAttribute == *this ) && ( rAttribute.sValue == sValue ));
98 DECLARE_LIST( XMLAttributeList, XMLAttribute * )
100 //-------------------------------------------------------------------------
102 /** Virtual base to handle different kinds of XML nodes
104 class XMLNode
106 protected:
107 XMLNode() {}
109 public:
110 virtual USHORT GetNodeType() = 0;
111 virtual ~XMLNode() {}
114 //-------------------------------------------------------------------------
116 /** Virtual base to handle different kinds of child nodes
118 class XMLChildNode : public XMLNode
120 private:
121 XMLParentNode *pParent;
123 protected:
124 XMLChildNode( XMLParentNode *pPar );
125 XMLChildNode():pParent( NULL ){};
126 XMLChildNode( const XMLChildNode& obj);
127 XMLChildNode& operator=(const XMLChildNode& obj);
128 public:
129 virtual USHORT GetNodeType() = 0;
131 /// returns the parent of this node
132 XMLParentNode *GetParent() { return pParent; }
133 virtual ~XMLChildNode(){};
136 DECLARE_LIST( XMLChildNodeList, XMLChildNode * )
138 //-------------------------------------------------------------------------
140 /** Virtual base to handle different kinds of parent nodes
142 class XMLData;
144 class XMLParentNode : public XMLChildNode
146 private:
147 XMLChildNodeList *pChildList;
148 static int dbgcnt;
149 //int nParentPos;
150 protected:
151 XMLParentNode( XMLParentNode *pPar )
152 : XMLChildNode( pPar ), pChildList( NULL )
155 XMLParentNode(): pChildList(NULL){
157 /// Copyconstructor
158 XMLParentNode( const XMLParentNode& );
160 XMLParentNode& operator=(const XMLParentNode& obj);
161 virtual ~XMLParentNode();
164 public:
165 virtual USHORT GetNodeType() = 0;
167 /// returns child list of this node
168 XMLChildNodeList *GetChildList() { return pChildList; }
170 /// adds a new child
171 void AddChild(
172 XMLChildNode *pChild /// the new child
175 void AddChild(
176 XMLChildNode *pChild , int pos /// the new child
179 virtual int GetPosition( ByteString id );
180 int RemoveChild( XMLElement *pRefElement );
181 void RemoveAndDeleteAllChilds();
183 /// returns a child element which matches the given one
184 XMLElement *GetChildElement(
185 XMLElement *pRefElement // the reference elelement
189 //-------------------------------------------------------------------------
191 DECLARE_LIST( XMLStringList, XMLElement* )
193 /// Mapping numeric Language code <-> XML Element
194 typedef std::hash_map< ByteString ,XMLElement* , hashByteString,equalByteString > LangHashMap;
196 /// Mapping XML Element string identifier <-> Language Map
197 typedef std::hash_map<ByteString , LangHashMap* ,
198 hashByteString,equalByteString> XMLHashMap;
200 /// Mapping iso alpha string code <-> iso numeric code
201 typedef std::hash_map<ByteString, int, hashByteString,equalByteString> HashMap;
203 /// Mapping XML tag names <-> have localizable strings
204 typedef std::hash_map<ByteString , BOOL ,
205 hashByteString,equalByteString> TagMap;
207 /** Holds information of a XML file, is root node of tree
211 class XMLFile : public XMLParentNode
213 public:
214 XMLFile() ;
215 XMLFile(
216 const String &rFileName // the file name, empty if created from memory stream
218 XMLFile( const XMLFile& obj ) ;
219 ~XMLFile();
221 ByteString* GetGroupID(std::deque<ByteString> &groupid);
222 void Print( XMLNode *pCur = NULL, USHORT nLevel = 0 );
223 virtual void SearchL10NElements( XMLParentNode *pCur, int pos = 0 );
224 void Extract( XMLFile *pCur = NULL );
225 void View();
226 // void static Signal_handler(int signo);//void*,oslSignalInfo * pInfo);
227 void showType(XMLParentNode* node);
229 XMLHashMap* GetStrings(){return XMLStrings;}
230 BOOL Write( ByteString &rFilename );
231 BOOL Write( ofstream &rStream , XMLNode *pCur = NULL );
233 bool CheckExportStatus( XMLParentNode *pCur = NULL );// , int pos = 0 );
235 XMLFile& operator=(const XMLFile& obj);
237 virtual USHORT GetNodeType();
239 /// returns file name
240 const String &GetName() { return sFileName; }
241 void SetName( const String &rFilename ) { sFileName = rFilename; }
242 void SetFullName( const String &rFullFilename ) { sFullName = rFullFilename; }
243 const std::vector<ByteString> getOrder(){ return order; }
245 protected:
246 // writes a string as UTF8 with dos line ends to a given stream
247 void WriteString( ofstream &rStream, const String &sString );
249 // quotes the given text for writing to a file
250 void QuotHTML( String &rString );
252 void InsertL10NElement( XMLElement* pElement);
254 // DATA
255 String sFileName;
256 String sFullName;
258 const ByteString ID,OLDREF,XML_LANG;
260 TagMap nodes_localize;
261 XMLHashMap* XMLStrings;
263 std::vector <ByteString> order;
266 /// An Utility class for XML
267 /// See RFC 3066 / #i8252# for ISO codes
268 class XMLUtil{
270 public:
271 /// Quot the XML characters and replace \n \t
272 static void QuotHTML( String &rString );
274 /// UnQuot the XML characters and restore \n \t
275 static void UnQuotHTML ( String &rString );
277 /// Return the numeric iso language code
278 //USHORT GetLangByIsoLang( const ByteString &rIsoLang );
280 /// Return the alpha strings representation
281 ByteString GetIsoLangByIndex( USHORT nIndex );
283 static XMLUtil& Instance();
284 ~XMLUtil();
286 void dump();
288 private:
289 /// Mapping iso alpha string code <-> iso numeric code
290 HashMap lMap;
292 /// Mapping iso numeric code <-> iso alpha string code
293 ByteString isoArray[MAX_LANGUAGES];
295 static void UnQuotData( String &rString );
296 static void UnQuotTags( String &rString );
298 XMLUtil();
299 XMLUtil(const XMLUtil&);
305 //-------------------------------------------------------------------------
307 /** Hold information of an element node
309 class XMLElement : public XMLParentNode
311 private:
312 String sElementName;
313 XMLAttributeList *pAttributes;
314 ByteString project,
315 filename,
317 sOldRef,
318 resourceType,
319 languageId;
320 int nPos;
322 protected:
323 void Print(XMLNode *pCur, OUStringBuffer& buffer , bool rootelement);
324 public:
325 /// create a element node
326 XMLElement(){}
327 XMLElement(
328 const String &rName, // the element name
329 XMLParentNode *Parent // parent node of this element
330 ): XMLParentNode( Parent ),
331 sElementName( rName ),
332 pAttributes( NULL ),
333 project(""),
334 filename(""),
335 id(""),
336 sOldRef(""),
337 resourceType(""),
338 languageId(""),
339 nPos(0)
342 ~XMLElement();
343 XMLElement(const XMLElement&);
345 XMLElement& operator=(const XMLElement& obj);
346 /// returns node type XML_NODE_ELEMENT
347 virtual USHORT GetNodeType();
349 /// returns element name
350 const String &GetName() { return sElementName; }
352 /// returns list of attributes of this element
353 XMLAttributeList *GetAttributeList() { return pAttributes; }
355 /// adds a new attribute to this element, typically used by parser
356 void AddAttribute( const String &rAttribute, const String &rValue );
358 void ChangeLanguageTag( const String &rValue );
359 // Return a ASCII String representation of this object
360 OString ToOString();
362 // Return a Unicode String representation of this object
363 OUString ToOUString();
365 bool Equals(OUString refStr);
367 /// returns a attribute
368 XMLAttribute *GetAttribute(
369 const String &rName // the attribute name
371 void SetProject ( ByteString prj ){ project = prj; }
372 void SetFileName ( ByteString fn ){ filename = fn; }
373 void SetId ( ByteString theId ){ id = theId; }
374 void SetResourceType ( ByteString rt ){ resourceType = rt; }
375 void SetLanguageId ( ByteString lid ){ languageId = lid; }
376 void SetPos ( int nPos_in ){ nPos = nPos_in; }
377 void SetOldRef ( ByteString sOldRef_in ){ sOldRef = sOldRef_in; }
379 virtual int GetPos() { return nPos; }
380 ByteString GetProject() { return project; }
381 ByteString GetFileName() { return filename; }
382 ByteString GetId() { return id; }
383 ByteString GetOldref() { return sOldRef; }
384 ByteString GetResourceType(){ return resourceType; }
385 ByteString GetLanguageId() { return languageId; }
389 //-------------------------------------------------------------------------
392 /** Holds character data
394 class XMLData : public XMLChildNode
396 private:
397 String sData;
398 bool isNewCreated;
400 public:
401 /// create a data node
402 XMLData(
403 const String &rData, // the initial data
404 XMLParentNode *Parent // the parent node of this data, typically a element node
406 : XMLChildNode( Parent ), sData( rData ) , isNewCreated ( false ){}
407 XMLData(
408 const String &rData, // the initial data
409 XMLParentNode *Parent, // the parent node of this data, typically a element node
410 bool newCreated
412 : XMLChildNode( Parent ), sData( rData ) , isNewCreated ( newCreated ){}
414 XMLData(const XMLData& obj);
416 XMLData& operator=(const XMLData& obj);
417 virtual USHORT GetNodeType();
419 /// returns the data
420 const String &GetData() { return sData; }
422 bool isNew() { return isNewCreated; }
423 /// adds new character data to the existing one
424 void AddData(
425 const String &rData // the new data
432 //-------------------------------------------------------------------------
434 /** Holds comments
436 class XMLComment : public XMLChildNode
438 private:
439 String sComment;
441 public:
442 /// create a comment node
443 XMLComment(
444 const String &rComment, // the comment
445 XMLParentNode *Parent // the parent node of this comemnt, typically a element node
447 : XMLChildNode( Parent ), sComment( rComment ) {}
449 virtual USHORT GetNodeType();
451 XMLComment( const XMLComment& obj );
453 XMLComment& operator=(const XMLComment& obj);
455 /// returns the comment
456 const String &GetComment() { return sComment; }
459 //-------------------------------------------------------------------------
461 /** Holds additional file content like those for which no handler exists
463 class XMLDefault : public XMLChildNode
465 private:
466 String sDefault;
468 public:
469 /// create a comment node
470 XMLDefault(
471 const String &rDefault, // the comment
472 XMLParentNode *Parent // the parent node of this comemnt, typically a element node
474 : XMLChildNode( Parent ), sDefault( rDefault ) {}
476 XMLDefault(const XMLDefault& obj);
478 XMLDefault& operator=(const XMLDefault& obj);
480 /// returns node type XML_NODE_TYPE_COMMENT
481 virtual USHORT GetNodeType();
483 /// returns the comment
484 const String &GetDefault() { return sDefault; }
487 //-------------------------------------------------------------------------
489 /** struct for error information, used by class SimpleXMLParser
491 struct XMLError {
492 XML_Error eCode; // the error code
493 ULONG nLine; // error line number
494 ULONG nColumn; // error column number
495 String sMessage; // readable error message
498 //-------------------------------------------------------------------------
500 /** validating xml parser, creates a document tree with xml nodes
504 class SimpleXMLParser
506 private:
507 XML_Parser aParser;
508 XMLError aErrorInformation;
510 XMLFile *pXMLFile;
511 XMLParentNode *pCurNode;
512 XMLData *pCurData;
515 static void StartElementHandler( void *userData, const XML_Char *name, const XML_Char **atts );
516 static void EndElementHandler( void *userData, const XML_Char *name );
517 static void CharacterDataHandler( void *userData, const XML_Char *s, int len );
518 static void CommentHandler( void *userData, const XML_Char *data );
519 static void DefaultHandler( void *userData, const XML_Char *s, int len );
522 void StartElement( const XML_Char *name, const XML_Char **atts );
523 void EndElement( const XML_Char *name );
524 void CharacterData( const XML_Char *s, int len );
525 void Comment( const XML_Char *data );
526 void Default( const XML_Char *s, int len );
529 public:
530 /// creates a new parser
531 SimpleXMLParser();
532 ~SimpleXMLParser();
534 /// parse a file, returns NULL on criticall errors
535 XMLFile *Execute(
536 const String &rFullFileName,
537 const String &rFileName, // the file name
538 XMLFile *pXMLFileIn // the XMLFile
541 /// parse a memory stream, returns NULL on criticall errors
542 XMLFile *Execute(
543 SvMemoryStream *pStream // the stream
546 /// returns an error struct
547 const XMLError &GetError() { return aErrorInformation; }
550 #endif