xref: /trunk/main/l10ntools/source/xmlparse.cxx (revision 7950f2af)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_l10ntools.hxx"
26 #include <iterator> /* std::iterator*/
27 
28 #include <stdio.h>
29 #include <sal/alloca.h>
30 
31 #include "xmlparse.hxx"
32 #include <fstream>
33 #include <iostream>
34 #include <osl/mutex.hxx>
35 #include <osl/thread.hxx>
36 #ifdef __MINGW32__
37 #include <tools/prewin.h>
38 #include <tools/postwin.h>
39 #endif
40 using namespace std;
41 using namespace osl;
42 
43 //
44 // class XMLChildNode
45 //
46 
47 /*****************************************************************************/
XMLChildNode(XMLParentNode * pPar)48 XMLChildNode::XMLChildNode( XMLParentNode *pPar )
49 /*****************************************************************************/
50 				: pParent( pPar )
51 {
52 	if ( pParent )
53 		pParent->AddChild( this );
54 }
55 
56 
57 /*****************************************************************************/
XMLChildNode(const XMLChildNode & obj)58 XMLChildNode::XMLChildNode( const XMLChildNode& obj)
59 /*****************************************************************************/
60 :   XMLNode(obj),
61     pParent(obj.pParent){}
62 
63 /*****************************************************************************/
operator =(const XMLChildNode & obj)64 XMLChildNode& XMLChildNode::operator=(const XMLChildNode& obj){
65 /*****************************************************************************/
66 	if(this != &obj){
67         pParent=obj.pParent;
68     }
69     return *this;
70 }
71 //
72 // class XMLParentNode
73 //
74 
75 
76 /*****************************************************************************/
~XMLParentNode()77 XMLParentNode::~XMLParentNode()
78 /*****************************************************************************/
79 {
80 	if( pChildList ){
81             RemoveAndDeleteAllChilds();
82             delete pChildList;
83 			pChildList = NULL;
84     }
85 	pChildList = NULL;
86 }
87 /*****************************************************************************/
XMLParentNode(const XMLParentNode & obj)88 XMLParentNode::XMLParentNode( const XMLParentNode& obj)
89 /*****************************************************************************/
90 : XMLChildNode( obj )
91 {
92 	if( obj.pChildList ){
93         pChildList=new XMLChildNodeList();
94         XMLChildNode* pNode = NULL;
95         for ( sal_uLong i = 0; i < obj.pChildList->Count(); i++ ){
96 			pNode = obj.pChildList->GetObject( i );
97             if( pNode != NULL){
98                 switch(pNode->GetNodeType()){
99                     case XML_NODE_TYPE_ELEMENT:
100                         AddChild( new XMLElement( *static_cast<XMLElement* >(pNode) ) ); break;
101                     case XML_NODE_TYPE_DATA:
102                         AddChild( new XMLData   ( *static_cast<XMLData* >   (pNode) ) ); break;
103                     case XML_NODE_TYPE_COMMENT:
104                         AddChild( new XMLComment( *static_cast<XMLComment* >(pNode) ) ); break;
105                     case XML_NODE_TYPE_DEFAULT:
106                         AddChild( new XMLDefault( *static_cast<XMLDefault* >(pNode) ) ); break;
107                     default:    fprintf(stdout,"XMLParentNode::XMLParentNode( const XMLParentNode& obj) strange obj");
108                 }
109             }
110         }
111     }else pChildList = NULL;
112 }
113 /*****************************************************************************/
operator =(const XMLParentNode & obj)114 XMLParentNode& XMLParentNode::operator=(const XMLParentNode& obj){
115 /*****************************************************************************/
116 	if(this!=&obj){
117         XMLChildNode::operator=(obj);
118         if( pChildList ){
119             RemoveAndDeleteAllChilds();
120             delete pChildList;
121 			pChildList = NULL;
122         }
123         if( obj.pChildList ){
124             pChildList=new XMLChildNodeList();
125             for ( sal_uLong i = 0; i < obj.pChildList->Count(); i++ )
126 			    AddChild( obj.pChildList->GetObject( i ) );
127         }else pChildList = NULL;
128 
129     }
130     return *this;
131 }
132 /*****************************************************************************/
AddChild(XMLChildNode * pChild)133 void XMLParentNode::AddChild( XMLChildNode *pChild )
134 /*****************************************************************************/
135 {
136 	if ( !pChildList )
137 		pChildList = new XMLChildNodeList();
138 	pChildList->Insert( pChild, LIST_APPEND );
139 }
140 
141 /*****************************************************************************/
AddChild(XMLChildNode * pChild,int pos)142 void XMLParentNode::AddChild( XMLChildNode *pChild , int pos )
143 /*****************************************************************************/
144 {
145 	if ( !pChildList )
146 		pChildList = new XMLChildNodeList();
147     pChildList->Insert( pChild, pos );
148 }
149 
150 /*****************************************************************************/
GetPosition(ByteString id)151 int XMLParentNode::GetPosition( ByteString id ){
152 /*****************************************************************************/
153     XMLElement* a;
154 
155     static const ByteString sEnusStr = ByteString(String::CreateFromAscii(ENGLISH_US_ISO).ToLowerAscii() , RTL_TEXTENCODING_ASCII_US ).ToLowerAscii();
156     static const ByteString sDeStr   = ByteString(String::CreateFromAscii(GERMAN_ISO2).ToLowerAscii()    , RTL_TEXTENCODING_ASCII_US ).ToLowerAscii();
157 
158     if ( pChildList ){
159         for ( sal_uLong i = 0; i < pChildList->Count(); i++ ) {
160 		    XMLChildNode *pChild = pChildList->GetObject( i );
161             if ( pChild->GetNodeType() == XML_NODE_TYPE_ELEMENT ){
162 			    a = static_cast<XMLElement* >(pChild);
163                 ByteString elemid( a->GetId() );
164                 elemid.ToLowerAscii();
165                 if (   elemid.Equals( id.ToLowerAscii() ) ){
166                     ByteString elemLID( a->GetLanguageId() );
167                     elemLID.ToLowerAscii();
168                     if( elemLID.Equals( sEnusStr) ) {
169                         return i;
170                     }
171                     else if( elemLID.Equals( sDeStr) ) {
172                         return i;
173                     }
174                 }
175             }
176         }
177     }
178     return -1;
179 }
180 
181 /*****************************************************************************/
RemoveChild(XMLElement * pRefElement)182 int XMLParentNode::RemoveChild( XMLElement *pRefElement )
183 /*****************************************************************************/
184 {
185     XMLElement* a;
186     if ( pChildList ){
187         for ( sal_uLong i = 0; i < pChildList->Count(); i++ ) {
188 		    XMLChildNode *pChild = pChildList->GetObject( i );
189             if ( pChild->GetNodeType() == XML_NODE_TYPE_ELEMENT ){
190 			    a = static_cast<XMLElement* >(pChild);
191                 ByteString elemid( a->GetId() );
192                 elemid.ToLowerAscii();
193                 ByteString elemLID( a->GetLanguageId() );
194                 elemLID.ToLowerAscii();
195                 ByteString pRefLID( pRefElement->GetLanguageId() );
196                 pRefLID.ToLowerAscii();
197                 if ( elemid.Equals(pRefElement->GetId())
198                     && elemLID.Equals( pRefLID ) )
199 			    {
200                     if( pRefElement->ToOString().compareTo( a->ToOString() )==0 ){
201 						pChildList->Remove( i );
202                         delete a; // Test
203                         return i;
204                     }
205                 }
206             }
207 
208         }
209     }
210     return -1;
211 }
212 
213 /*****************************************************************************/
RemoveAndDeleteAllChilds()214 void XMLParentNode::RemoveAndDeleteAllChilds(){
215 /*****************************************************************************/
216 	if ( pChildList ) {
217 		for ( sal_uLong i = 0; i < pChildList->Count(); i++ )
218 			delete pChildList->GetObject( i );
219 		pChildList->Clear();
220 	}
221 }
222 
223 /*****************************************************************************/
GetChildElement(XMLElement * pRefElement)224 XMLElement *XMLParentNode::GetChildElement( XMLElement *pRefElement )
225 /*****************************************************************************/
226 {
227 	for ( sal_uLong i = 0; i < pChildList->Count(); i++ ) {
228 		XMLChildNode *pChild = pChildList->GetObject( i );
229 		if ( pChild->GetNodeType() == XML_NODE_TYPE_ELEMENT )
230 			if ((( XMLElement * ) pChild )->GetName() ==
231 				pRefElement->GetName())
232 			{
233 				XMLAttributeList *pList = pRefElement->GetAttributeList();
234 				if ( !pList )
235 					return ( XMLElement * ) pChild;
236 
237 				sal_Bool bMatch = sal_False;
238 				for ( sal_uLong j = 0; j < pList->Count() && bMatch; j++ ) {
239 					XMLAttribute *pAttribute = pList->GetObject( j );
240 					XMLAttribute *pCandidate =
241 						(( XMLElement * ) pChild )->GetAttribute(
242 							*pAttribute );
243 					if ( !pCandidate || !pAttribute->IsEqual( *pCandidate ))
244 						bMatch = sal_False;
245 				}
246 				if ( bMatch )
247 					return ( XMLElement * ) pChild;
248 			}
249 	}
250 	return NULL;
251 }
252 
253 //
254 // class XMLFile
255 //
256 
257 /*****************************************************************************/
GetNodeType()258 sal_uInt16 XMLFile::GetNodeType()
259 /*****************************************************************************/
260 {
261 	return XML_NODE_TYPE_FILE;
262 }
263 
264 /*****************************************************************************/
Write(ByteString & aFilename)265 sal_Bool XMLFile::Write( ByteString &aFilename )
266 /*****************************************************************************/
267 {
268 
269     if ( aFilename.Len()) {
270 		// retry harder if there is a NFS problem,
271         for( int x = 1 ; x < 3 ; x++ ){	// this looks strange...yes!
272 			ofstream aFStream( aFilename.GetBuffer() , ios::out | ios::trunc );
273 
274             if( !aFStream )		// From time to time the stream can not be opened the first time on NFS volumes,
275 			{					// I wasn't able to track this down. I think this is an NFS issue .....
276                 //cerr << "ERROR: - helpex - Can't write to tempfile " << aFilename.GetBuffer() << " No#" << x << "\n";
277                 TimeValue aTime;
278                 aTime.Seconds = 3;
279                 aTime.Nanosec = 0;
280 
281                 osl::Thread::wait( aTime );
282             }
283             else
284             {
285                 // write out
286 				Write( aFStream );
287                 aFStream.close();
288 
289 				// check!
290 				DirEntry aTarget( aFilename );
291                 FileStat aFileStat( aTarget );
292 
293 				if( aFileStat.GetSize() < 1 )
294 				{
295                     //retry
296 					//cerr << "WARNING: - helpex - Can't create file " << aFilename.GetBuffer() << " No#" << x << "\n";
297                     aTarget.Kill();
298                 }
299 				else
300 				{
301                     //everything ok!
302 					return true;
303                 }
304             }
305 	    }
306 		cerr << "ERROR: - helpex - Can't create file " << aFilename.GetBuffer() << "\nPossible reason: Disk full ? Mounted NFS volume broken ? Wrong permissions ?\n";
307         exit( -1 );
308     }
309     cerr << "ERROR: - helpex - Empty file name\n";
310     exit( -1 );
311 }
312 
313 
314 
WriteString(ofstream & rStream,const String & sString)315 void XMLFile::WriteString( ofstream &rStream, const String &sString )
316 {
317 	ByteString sText( sString, RTL_TEXTENCODING_UTF8 );
318 	rStream << sText.GetBuffer();
319 }
320 
321 
Write(ofstream & rStream,XMLNode * pCur)322 sal_Bool XMLFile::Write( ofstream &rStream , XMLNode *pCur )
323 {
324     XMLUtil& xmlutil = XMLUtil::Instance();
325     (void) xmlutil;
326 
327     if ( !pCur )
328 		Write( rStream, this );
329 	else {
330 		switch( pCur->GetNodeType()) {
331 			case XML_NODE_TYPE_FILE: {
332 				if( GetChildList())
333 					for ( sal_uLong i = 0; i < GetChildList()->Count(); i++ )
334 						Write( rStream, GetChildList()->GetObject( i ));
335 			}
336 			break;
337 			case XML_NODE_TYPE_ELEMENT: {
338 				XMLElement *pElement = ( XMLElement * ) pCur;
339 				rStream  << "<";
340 				WriteString( rStream, pElement->GetName());
341 				if ( pElement->GetAttributeList())
342 					for ( sal_uLong j = 0; j < pElement->GetAttributeList()->Count(); j++ ) {
343 						rStream << " ";
344 						String sData(*pElement->GetAttributeList()->GetObject( j ));
345 						xmlutil.QuotHTML( sData );
346 						WriteString( rStream , sData );
347 						rStream << "=\"";
348 						sData=pElement->GetAttributeList()->GetObject( j )->GetValue();
349 						xmlutil.QuotHTML(  sData );
350 						WriteString( rStream , sData  );
351 						rStream << "\"";
352 					}
353 				if ( !pElement->GetChildList())
354 					rStream << "/>";
355 				else {
356 					rStream << ">";
357 					for ( sal_uLong k = 0; k < pElement->GetChildList()->Count(); k++ )
358 						Write( rStream, pElement->GetChildList()->GetObject( k ));
359 					rStream << "</";
360 					WriteString( rStream, pElement->GetName());
361 					rStream << ">";
362    				}
363 			}
364 			break;
365 			case XML_NODE_TYPE_DATA: {
366 				XMLData *pData = ( XMLData * ) pCur;
367 				String sData( pData->GetData());
368                 xmlutil.QuotHTML( sData );
369 				WriteString( rStream, sData );
370 			}
371 			break;
372 			case XML_NODE_TYPE_COMMENT: {
373 				XMLComment *pComment = ( XMLComment * ) pCur;
374 				rStream << "<!--";
375 				WriteString( rStream, pComment->GetComment());
376 				rStream << "-->";
377 			}
378 			break;
379 			case XML_NODE_TYPE_DEFAULT: {
380 				XMLDefault *pDefault = ( XMLDefault * ) pCur;
381 				WriteString( rStream, pDefault->GetDefault());
382 			}
383 			break;
384 		}
385 	}
386 	return sal_True;
387 }
388 
389 
Print(XMLNode * pCur,sal_uInt16 nLevel)390 void XMLFile::Print( XMLNode *pCur, sal_uInt16 nLevel )
391 {
392 
393 	if ( !pCur )
394 		Print( this );
395 	else {
396 		switch( pCur->GetNodeType()) {
397 			case XML_NODE_TYPE_FILE: {
398 				if( GetChildList())
399 					for ( sal_uLong i = 0; i < GetChildList()->Count(); i++ )
400 						Print( GetChildList()->GetObject( i ));
401 			}
402 			break;
403 			case XML_NODE_TYPE_ELEMENT: {
404 				XMLElement *pElement = ( XMLElement * ) pCur;
405 
406 				fprintf( stdout, "<%s", ByteString( pElement->GetName(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
407 				if ( pElement->GetAttributeList())
408 					for ( sal_uLong j = 0; j < pElement->GetAttributeList()->Count(); j++ ){
409 						ByteString aAttrName( *pElement->GetAttributeList()->GetObject( j ), RTL_TEXTENCODING_UTF8 );
410                         if( !aAttrName.EqualsIgnoreCaseAscii( XML_LANG ) ) {
411                             fprintf( stdout, " %s=\"%s\"",
412                                 aAttrName.GetBuffer(),
413 							    ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),
414 								    RTL_TEXTENCODING_UTF8 ).GetBuffer());
415                         }
416                     }
417 				if ( !pElement->GetChildList())
418 					fprintf( stdout, "/>" );
419 				else {
420 					fprintf( stdout, ">" );
421 					for ( sal_uLong k = 0; k < pElement->GetChildList()->Count(); k++ )
422 						Print( pElement->GetChildList()->GetObject( k ), nLevel + 1 );
423 					fprintf( stdout, "</%s>", ByteString( pElement->GetName(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
424 				}
425 			}
426 			break;
427 			case XML_NODE_TYPE_DATA: {
428 				XMLData *pData = ( XMLData * ) pCur;
429 				String sData = pData->GetData();
430 				fprintf( stdout, "%s", ByteString( sData, RTL_TEXTENCODING_UTF8 ).GetBuffer());
431 			}
432 			break;
433 			case XML_NODE_TYPE_COMMENT: {
434 				XMLComment *pComment = ( XMLComment * ) pCur;
435 				fprintf( stdout, "<!--%s-->", ByteString( pComment->GetComment(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
436 			}
437 			break;
438 			case XML_NODE_TYPE_DEFAULT: {
439 				XMLDefault *pDefault = ( XMLDefault * ) pCur;
440 				fprintf( stdout, "%s", ByteString( pDefault->GetDefault(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
441 			}
442 			break;
443 		}
444 	}
445 }
~XMLFile()446 XMLFile::~XMLFile()
447 {
448 	if( XMLStrings != NULL ){
449 		XMLHashMap::iterator pos = XMLStrings->begin();
450 		for( ; pos != XMLStrings->end() ; ++pos ){
451 			delete pos->second;				// Check and delete content also ?
452 		}
453 		delete XMLStrings;
454 		XMLStrings = NULL;
455 	}
456 }
457 /*****************************************************************************/
XMLFile(const String & rFileName)458 XMLFile::XMLFile( const String &rFileName ) // the file name, empty if created from memory stream
459 /*****************************************************************************/
460 				: XMLParentNode( NULL ),
461 				  sFileName    ( rFileName ),
462 				  ID           ( "id" ),
463                   OLDREF       ( "oldref" ),
464 				  XML_LANG     ( "xml-lang" ),
465 				  XMLStrings   ( NULL )
466 
467 {
468 //	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("bookmark_value"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
469 	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("bookmark"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
470     nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("variable"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
471 	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("paragraph"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
472 	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("alt"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
473 	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("caption"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
474 	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("title"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
475 	nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("link"),RTL_TEXTENCODING_ASCII_US) , sal_True) );
476 }
477 /*****************************************************************************/
Extract(XMLFile * pCur)478 void XMLFile::Extract( XMLFile *pCur )
479 /*****************************************************************************/
480 {
481 	if( XMLStrings != NULL ) delete XMLStrings; // Elements ?
482 
483 	XMLStrings = new XMLHashMap();
484    	if ( !pCur )
485         SearchL10NElements( this );
486 	else {
487 		if( pCur->GetNodeType()==XML_NODE_TYPE_FILE) {
488             SearchL10NElements(pCur);
489 		}
490 	}
491 }
492 
493 /*****************************************************************************/
View()494 void XMLFile::View(){
495 /*****************************************************************************/
496 	XMLElement* cur;
497 	for(XMLHashMap::iterator pos=XMLStrings->begin(); pos!=XMLStrings->end();++pos){
498 		fprintf(stdout,"\nid=%s\n",(pos->first).GetBuffer());
499 		LangHashMap* elem=pos->second;
500 		for(LangHashMap::iterator pos2=elem->begin(); pos2!=elem->end();++pos2){
501 			fprintf( stdout,"\nlanguage=%s\n",(pos2->first).GetBuffer() );
502 			cur=pos2->second;
503 			fprintf(stdout,"\n%s\n",((XMLElement*)cur)->ToOString().getStr());
504 
505 		}
506 	}
507 }
508 
509 /*****************************************************************************/
InsertL10NElement(XMLElement * pElement)510 void XMLFile::InsertL10NElement( XMLElement* pElement ){
511 /*****************************************************************************/
512 	ByteString tmpStr,id,oldref,language("");
513 	LangHashMap* elem;
514 
515     if( pElement->GetAttributeList() != NULL ){
516         for ( sal_uLong j = 0; j < pElement->GetAttributeList()->Count(); j++ ){
517 		    tmpStr=ByteString( *pElement->GetAttributeList()->GetObject( j ),RTL_TEXTENCODING_UTF8 );
518 		    if( tmpStr.CompareTo(ID)==COMPARE_EQUAL  ){	// Get the "id" Attribute
519 			    id = ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
520 		    }
521 		    if( tmpStr.CompareTo( XML_LANG ) == COMPARE_EQUAL ){	// Get the "xml-lang" Attribute
522 			    language = ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
523 		    }
524 
525 	    }
526     }else{
527         fprintf(stdout,"XMLFile::InsertL10NElement: No AttributeList found");
528         fprintf(stdout,"++++++++++++++++++++++++++++++++++++++++++++++++++");
529         Print( pElement , 0 );
530         fprintf(stdout,"++++++++++++++++++++++++++++++++++++++++++++++++++");
531     }
532 
533 	XMLHashMap::iterator pos = XMLStrings->find( id );
534 	if( pos == XMLStrings->end() ){				// No instanze , create new one
535         elem = new LangHashMap();
536         (*elem)[ language ]=pElement;
537 		XMLStrings->insert( XMLHashMap::value_type( id , elem ) );
538         order.push_back( id );
539 	}else{									// Already there
540         elem=pos->second;
541         if ( (*elem)[ language ] )
542         {
543             fprintf(stdout,"Error: Duplicated entry. ID = %s  LANG = %s in File %s\n", id.GetBuffer(), language.GetBuffer(), ByteString( sFullName,RTL_TEXTENCODING_ASCII_US ).GetBuffer() );
544             exit( -1 );
545         }
546         (*elem)[ language ]=pElement;
547 	}
548 }
549 /*****************************************************************************/
showType(XMLParentNode * node)550 void XMLFile::showType(XMLParentNode* node){
551 /*****************************************************************************/
552 	switch (node->GetNodeType()){
553 		case XML_NODE_TYPE_ELEMENT: fprintf(stdout,"ELEMENT\n") ;break;
554 		case XML_NODE_TYPE_FILE:    fprintf(stdout,"FILE\n")    ;break;
555 		case XML_NODE_TYPE_COMMENT: fprintf(stdout,"COMMENT\n") ;break;
556 		case XML_NODE_TYPE_DATA:    fprintf(stdout,"DATA\n")    ;break;
557 		case XML_NODE_TYPE_DEFAULT: fprintf(stdout,"DEFAULT\n") ;break;
558 		default: break;
559 	}
560 }
XMLFile()561 XMLFile::XMLFile()
562 /*****************************************************************************/
563 				: XMLParentNode( NULL ),
564 				  ID           ( "id" ),
565                   OLDREF       ( "oldref" ),
566 				  XML_LANG     ( "xml-lang" ),
567 				  XMLStrings   ( NULL ){};
568 
569 
XMLFile(const XMLFile & obj)570 XMLFile::XMLFile( const XMLFile& obj )
571 /*****************************************************************************/
572 				: XMLParentNode( obj ),
573 				  sFileName    ( obj.sFileName ),
574 				  ID           ( "id" ),
575                   OLDREF       ( "oldref" ),
576 				  XML_LANG     ( "xml-lang" ),
577 				  XMLStrings   ( NULL )
578 {
579 	if( this!=&obj )
580 	{
581         nodes_localize  =obj.nodes_localize;
582         order           =obj.order;
583 
584 	}
585 }
586 /*****************************************************************************/
operator =(const XMLFile & obj)587 XMLFile& XMLFile::operator=(const XMLFile& obj){
588 /*****************************************************************************/
589     if( this!=&obj ){
590 
591         XMLParentNode::operator=(obj);
592 
593         nodes_localize  =obj.nodes_localize;
594         order           =obj.order;
595 
596 		if( XMLStrings )    delete XMLStrings;
597 
598 		if( obj.XMLStrings )
599         {
600             XMLStrings = new XMLHashMap();
601 	        for( XMLHashMap::iterator pos = obj.XMLStrings->begin() ; pos != obj.XMLStrings->end() ; ++pos )
602             {
603 		        LangHashMap* elem=pos->second;
604 		        LangHashMap* newelem = new LangHashMap();
605                 for(LangHashMap::iterator pos2=elem->begin(); pos2!=elem->end();++pos2){
606                     (*newelem)[ pos2->first ] = new XMLElement( *pos2->second );
607                     printf("*");
608 		        }
609                 (*XMLStrings)[ pos->first ] = newelem;
610             }
611         }
612     }
613     printf("done!\n");
614     return *this;
615 }
616 
617 
618 /*****************************************************************************/
SearchL10NElements(XMLParentNode * pCur,int pos)619 void XMLFile::SearchL10NElements( XMLParentNode *pCur , int pos)
620 /*****************************************************************************/
621 {
622 	static const ByteString LOCALIZE("localize");
623 	static const ByteString THEID("id");
624 	bool bInsert	= true;
625 	if ( !pCur )
626 		SearchL10NElements( this  );
627 	else {
628 		switch( pCur->GetNodeType()) {
629 			case XML_NODE_TYPE_FILE: {
630                 XMLParentNode* pElement;
631                 if( GetChildList()){
632                     for ( sal_uLong i = 0; i < GetChildList()->Count(); i++ ){
633                         pElement = (XMLParentNode*) GetChildList()->GetObject( i );
634                         if( pElement->GetNodeType() ==  XML_NODE_TYPE_ELEMENT ) SearchL10NElements( pElement , i);
635                     }
636                 }
637             }
638 			break;
639 			case XML_NODE_TYPE_ELEMENT: {
640 				XMLElement *pElement = ( XMLElement * ) pCur;
641         		ByteString sName(pElement->GetName(),RTL_TEXTENCODING_ASCII_US);
642                 ByteString language,tmpStrVal,oldref;
643 				if ( pElement->GetAttributeList()){
644 					for ( sal_uLong j = 0 , cnt = pElement->GetAttributeList()->Count(); j < cnt && bInsert; j++ ){
645 						const ByteString tmpStr( *pElement->GetAttributeList()->GetObject( j ),RTL_TEXTENCODING_UTF8 );
646 						if( tmpStr.CompareTo(THEID)==COMPARE_EQUAL  ){	// Get the "id" Attribute
647 							tmpStrVal=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
648                             //printf("Checking id = %s\n",tmpStrVal.GetBuffer() );
649 						}
650 						if( tmpStr.CompareTo(LOCALIZE)==COMPARE_EQUAL  ){	// Get the "localize" Attribute
651 							bInsert=false;
652 						}
653 						if( tmpStr.CompareTo(XML_LANG)==COMPARE_EQUAL ){	// Get the "xml-lang" Attribute
654 							language=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
655 						}
656                         if( tmpStr.CompareTo(OLDREF)==COMPARE_EQUAL ){	// Get the "oldref" Attribute
657 		                    oldref=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
658 		                }
659 					}
660 					pElement->SetLanguageId ( language );
661 					pElement->SetId         ( tmpStrVal.GetBuffer() );
662                     pElement->SetOldRef     ( oldref  );
663                     pElement->SetPos( pos );
664 				}
665 
666 				if ( bInsert && ( nodes_localize.find( sName.ToLowerAscii() ) != nodes_localize.end() ) )
667 					InsertL10NElement(pElement);
668 				else if ( bInsert && pElement->GetChildList() ){
669 					for ( sal_uLong k = 0; k < pElement->GetChildList()->Count(); k++ )
670 						SearchL10NElements( (XMLParentNode*) pElement->GetChildList()->GetObject( k ) , k);
671 				}
672             }
673 			break;
674 			case XML_NODE_TYPE_DATA: {
675 			}
676 			break;
677 			case XML_NODE_TYPE_COMMENT: {
678 			}
679 			break;
680 			case XML_NODE_TYPE_DEFAULT: {
681 			}
682 			break;
683 		}
684 	}
685 }
686 
687 /*****************************************************************************/
CheckExportStatus(XMLParentNode * pCur)688 bool XMLFile::CheckExportStatus( XMLParentNode *pCur )
689 /*****************************************************************************/
690 {
691     static bool bStatusExport = true;
692     const ByteString LOCALIZE("localize");
693     const ByteString STATUS("status");
694     const ByteString PUBLISH("PUBLISH");
695     const ByteString DEPRECATED("DEPRECATED");
696 
697     const ByteString TOPIC("topic");
698 	bool bInsert	= true;
699 	if ( !pCur )
700 		CheckExportStatus( this );
701 	else {
702         switch( pCur->GetNodeType()) {
703 			case XML_NODE_TYPE_FILE: {
704                 XMLParentNode* pElement;
705                 if( GetChildList()){
706                     for ( sal_uLong i = 0; i < GetChildList()->Count(); i++ ){
707                         pElement = (XMLParentNode*) GetChildList()->GetObject( i );
708                         if( pElement->GetNodeType() ==  XML_NODE_TYPE_ELEMENT ) CheckExportStatus( pElement );//, i);
709                     }
710                 }
711             }
712 			break;
713 			case XML_NODE_TYPE_ELEMENT: {
714 				XMLElement *pElement = ( XMLElement * ) pCur;
715         		ByteString sName(pElement->GetName(),RTL_TEXTENCODING_ASCII_US);
716                 if( sName.EqualsIgnoreCaseAscii( TOPIC ) ){
717 				    if ( pElement->GetAttributeList()){
718 					    for ( sal_uLong j = 0 , cnt = pElement->GetAttributeList()->Count(); j < cnt && bInsert; j++ ){
719 						    const ByteString tmpStr( *pElement->GetAttributeList()->GetObject( j ),RTL_TEXTENCODING_UTF8 );
720                             if( tmpStr.EqualsIgnoreCaseAscii( STATUS ) ){
721 							    ByteString tmpStrVal=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
722                                 if( !tmpStrVal.EqualsIgnoreCaseAscii( PUBLISH )  &&
723                                     !tmpStrVal.EqualsIgnoreCaseAscii( DEPRECATED )){
724                                     bStatusExport = false;
725                                 }
726 						    }
727 
728                         }
729                     }
730                 }
731 				else if ( pElement->GetChildList() ){
732 					for ( sal_uLong k = 0; k < pElement->GetChildList()->Count(); k++ )
733 						CheckExportStatus( (XMLParentNode*) pElement->GetChildList()->GetObject( k ) );//, k);
734 				}
735             }
736 			break;
737 		}
738 	}
739     return bStatusExport;
740 }
741 
742 /*****************************************************************************/
GetNodeType()743 sal_uInt16 XMLElement::GetNodeType()
744 /*****************************************************************************/
745 {
746 	return XML_NODE_TYPE_ELEMENT;
747 }
748 
749 /*****************************************************************************/
XMLElement(const XMLElement & obj)750 XMLElement::XMLElement(const XMLElement& obj)
751 /*****************************************************************************/
752 	:
753                 XMLParentNode   ( obj ),
754 				sElementName    ( obj.sElementName ),
755 				pAttributes     ( NULL ),
756 				project         ( obj.project ),
757 				filename        ( obj.filename ),
758 				id              ( obj.id ),
759 				sOldRef         ( obj.sOldRef ),
760 				resourceType    ( obj.resourceType ),
761 				languageId      ( obj.languageId ),
762                 nPos            ( obj.nPos )
763 
764 {
765     if ( obj.pAttributes ){
766         pAttributes = new XMLAttributeList();
767         for ( sal_uLong i = 0; i < obj.pAttributes->Count(); i++ )
768 			AddAttribute( *obj.pAttributes->GetObject( i ) , obj.pAttributes->GetObject( i )->GetValue() );
769     }
770 }
771 
772 /*****************************************************************************/
operator =(const XMLElement & obj)773 XMLElement& XMLElement::operator=(const XMLElement& obj){
774 /*****************************************************************************/
775     if( this!=&obj ){
776         XMLParentNode::operator=(obj);
777         sElementName    =obj.sElementName;
778 		project         =obj.project;
779 		filename        =obj.filename;
780 		id              =obj.id;
781 		sOldRef         =obj.sOldRef;
782 		resourceType    =obj.resourceType;
783 		languageId      =obj.languageId;
784         nPos            =obj.nPos;
785 
786         if ( pAttributes ){
787             for ( sal_uLong i = 0; i < pAttributes->Count(); i++ )
788 			    delete pAttributes->GetObject( i );
789 	        delete pAttributes;
790 	    }
791         if ( obj.pAttributes ){
792             pAttributes         =new XMLAttributeList();
793             for ( sal_uLong i = 0; i < obj.pAttributes->Count(); i++ )
794 			    AddAttribute( *obj.pAttributes->GetObject( i ) , obj.pAttributes->GetObject( i )->GetValue() );
795         }
796     }
797     return *this;
798 }
799 
800 /*****************************************************************************/
AddAttribute(const String & rAttribute,const String & rValue)801 void XMLElement::AddAttribute( const String &rAttribute, const String &rValue )
802 /*****************************************************************************/
803 {
804 	if ( !pAttributes )
805 		pAttributes = new XMLAttributeList();
806 	pAttributes->Insert( new XMLAttribute( rAttribute, rValue ), LIST_APPEND );
807 }
808 
809 /*****************************************************************************/
ChangeLanguageTag(const String & rValue)810 void XMLElement::ChangeLanguageTag( const String &rValue ){
811 /*****************************************************************************/
812     static const String rName = String::CreateFromAscii("xml-lang");
813     SetLanguageId( ByteString(rValue,RTL_TEXTENCODING_UTF8) );
814     if ( pAttributes ){
815         for ( sal_uLong i = 0; i < pAttributes->Count(); i++ ){
816             if ( *pAttributes->GetObject( i ) == rName ){
817                 pAttributes->GetObject( i )->setValue(rValue);
818             }
819         }
820     }
821     XMLChildNode* pNode  = NULL;
822     XMLElement*   pElem  = NULL;
823     XMLChildNodeList* pCList = GetChildList();
824 
825     if( pCList != NULL ){
826         for ( sal_uLong i = 0; i < pCList->Count(); i++ ){
827 			pNode = pCList->GetObject( i );
828             if( pNode != NULL && pNode->GetNodeType() == XML_NODE_TYPE_ELEMENT ){
829                 pElem = static_cast< XMLElement* >(pNode);
830                 pElem->ChangeLanguageTag( rValue );
831                 pElem->SetLanguageId( ByteString(rValue,RTL_TEXTENCODING_UTF8) );
832                 pElem  = NULL;
833                 pNode  = NULL;
834             }
835         }
836         pCList = NULL;
837     }
838 }
839 /*****************************************************************************/
GetAttribute(const String & rName)840 XMLAttribute *XMLElement::GetAttribute( const String &rName	)
841 /*****************************************************************************/
842 {
843 	if ( pAttributes )
844 		for ( sal_uLong i = 0; i < pAttributes->Count(); i++ )
845 			if ( *pAttributes->GetObject( i ) == rName )
846 				return pAttributes->GetObject( i );
847 
848 	return NULL;
849 }
850 
851 /*****************************************************************************/
~XMLElement()852 XMLElement::~XMLElement()
853 /*****************************************************************************/
854 {
855 	if ( pAttributes ) {
856 		for ( sal_uLong i = 0; i < pAttributes->Count(); i++ )
857 			delete pAttributes->GetObject( i );
858 
859 		delete pAttributes;
860 		pAttributes = NULL;
861 	}
862 }
863 /*****************************************************************************/
Equals(OUString refStr)864 bool	XMLElement::Equals(OUString refStr){
865 /*****************************************************************************/
866 	return  refStr.equals( ToOUString() );
867 }
868 
869 /*****************************************************************************/
ToOString()870 OString XMLElement::ToOString(){
871 /*****************************************************************************/
872     OUString ouEmpty;
873 
874 	OUStringBuffer* buffer = new OUStringBuffer();
875 	Print( this, *buffer , true );
876 
877 	OString result( (sal_Unicode* )buffer->getStr(), buffer->getLength() , RTL_TEXTENCODING_UTF8 );
878 	delete buffer;
879 	return result;
880 }
881 /*****************************************************************************/
ToOUString()882 OUString XMLElement::ToOUString(){
883 /*****************************************************************************/
884 	OUStringBuffer* buffer = new OUStringBuffer();
885 	Print(this,*buffer,true);
886 	OUString result=buffer->makeStringAndClear();
887 	String xy(result.getStr());
888     result=OUString(xy);
889 	delete buffer;
890 	return result;
891 }
892 /*****************************************************************************/
Print(XMLNode * pCur,OUStringBuffer & buffer,bool rootelement)893 void XMLElement::Print(XMLNode *pCur, OUStringBuffer& buffer , bool rootelement ){
894 /*****************************************************************************/
895     //YD FIXME somewhere COMMENT is defined as 4!
896     static const String _COMMENT = String::CreateFromAscii("comment");
897     static const OUString XML_LANG ( OUString::createFromAscii("xml-lang") );
898 
899     if(pCur!=NULL){
900         if(rootelement){
901             XMLElement *pElement = ( XMLElement * ) pCur;
902             if ( pElement->GetAttributeList()){
903                 if ( pElement->GetChildList()){
904                     XMLChildNode* tmp=NULL;
905                     for ( sal_uLong k = 0; k < pElement->GetChildList()->Count(); k++ ){
906 				        tmp=pElement->GetChildList()->GetObject( k );
907                         Print( tmp, buffer , false);
908                     }
909 				}
910 			}
911         }
912         else{
913 
914         switch( pCur->GetNodeType()) {
915 			case XML_NODE_TYPE_ELEMENT: {
916 				XMLElement *pElement = ( XMLElement * ) pCur;
917 
918                 if(  !pElement->GetName().EqualsIgnoreCaseAscii( _COMMENT ) ){
919                     buffer.append( OUString::createFromAscii("\\<") );
920 				    buffer.append( pElement->GetName() );
921 				    if ( pElement->GetAttributeList()){
922 					    for ( sal_uLong j = 0; j < pElement->GetAttributeList()->Count(); j++ ){
923 
924                             OUString aAttrName( *pElement->GetAttributeList()->GetObject( j ) );
925                             if( !aAttrName.equalsIgnoreAsciiCase( XML_LANG ) ) {
926                                 buffer.append( OUString::createFromAscii(" ") );
927                                 buffer.append( aAttrName );
928                                 buffer.append( OUString::createFromAscii("=") );
929 						        buffer.append( OUString::createFromAscii("\\\"") );
930 					            buffer.append( pElement->GetAttributeList()->GetObject( j )->GetValue() );
931 						        buffer.append( OUString::createFromAscii("\\\"") );
932                             }
933 					    }
934                     }
935 				    if ( !pElement->GetChildList())
936 					    buffer.append( OUString::createFromAscii("/\\>") );
937 				    else {
938 					    buffer.append( OUString::createFromAscii("\\>") );
939                         XMLChildNode* tmp=NULL;
940                         for ( sal_uLong k = 0; k < pElement->GetChildList()->Count(); k++ ){
941 						    tmp=pElement->GetChildList()->GetObject( k );
942                             Print( tmp, buffer , false);
943                         }
944 					    buffer.append( OUString::createFromAscii("\\</") );
945 				        buffer.append( pElement->GetName() );
946                         buffer.append( OUString::createFromAscii("\\>") );
947 	                }
948                 }
949 			}
950 			break;
951 			case XML_NODE_TYPE_DATA: {
952 				XMLData *pData = ( XMLData * ) pCur;
953 				String sData = pData->GetData();
954                 buffer.append( sData );
955 			}
956 			break;
957 			case XML_NODE_TYPE_COMMENT: {
958 				XMLComment *pComment = ( XMLComment * ) pCur;
959 				buffer.append( OUString::createFromAscii("<!--") );
960 			    buffer.append( pComment->GetComment() );
961 			    buffer.append( OUString::createFromAscii("-->") );
962 			}
963 			break;
964 			case XML_NODE_TYPE_DEFAULT: {
965 				XMLDefault *pDefault = ( XMLDefault * ) pCur;
966 				buffer.append( pDefault->GetDefault() );
967 			}
968 			break;
969 		}
970         }
971     }else {
972         fprintf(stdout,"\n#+------Error: NULL Pointer in XMLELement::Print------+#\n");
973         return;
974     }
975 }
976 
977 
978 //
979 // class XMLData
980 //
981 /*****************************************************************************/
XMLData(const XMLData & obj)982 XMLData::XMLData(const XMLData& obj)
983 /*****************************************************************************/
984 	: XMLChildNode( obj ),
985       sData( obj.sData ) ,
986       isNewCreated ( obj.isNewCreated ){}
987 
988 /*****************************************************************************/
operator =(const XMLData & obj)989 XMLData& XMLData::operator=(const XMLData& obj){
990 /*****************************************************************************/
991 	if( this!=&obj ){
992         XMLChildNode::operator=( obj );
993         sData           = obj.sData;
994         isNewCreated    = obj.isNewCreated;
995     }
996     return *this;
997 }
998 /*****************************************************************************/
AddData(const String & rData)999 void XMLData::AddData( const String &rData) {
1000 /*****************************************************************************/
1001 	sData += rData;
1002 }
1003 
1004 /*****************************************************************************/
GetNodeType()1005 sal_uInt16 XMLData::GetNodeType()
1006 /*****************************************************************************/
1007 {
1008 	return XML_NODE_TYPE_DATA;
1009 }
1010 
1011 //
1012 // class XMLComment
1013 //
1014 
1015 /*****************************************************************************/
GetNodeType()1016 sal_uInt16 XMLComment::GetNodeType()
1017 /*****************************************************************************/
1018 {
1019 	return XML_NODE_TYPE_COMMENT;
1020 }
1021 /*****************************************************************************/
XMLComment(const XMLComment & obj)1022 XMLComment::XMLComment(const XMLComment& obj)
1023 /*****************************************************************************/
1024 	: XMLChildNode( obj ),
1025       sComment( obj.sComment ){}
1026 
1027 /*****************************************************************************/
operator =(const XMLComment & obj)1028 XMLComment& XMLComment::operator=(const XMLComment& obj){
1029 /*****************************************************************************/
1030 	if( this!=&obj ){
1031         XMLChildNode::operator=( obj );
1032         sComment        = obj.sComment;
1033     }
1034     return *this;
1035 }
1036 
1037 //
1038 // class XMLDefault
1039 //
1040 
1041 /*****************************************************************************/
GetNodeType()1042 sal_uInt16 XMLDefault::GetNodeType()
1043 /*****************************************************************************/
1044 {
1045 	return XML_NODE_TYPE_DEFAULT;
1046 }
1047 /*****************************************************************************/
XMLDefault(const XMLDefault & obj)1048 XMLDefault::XMLDefault(const XMLDefault& obj)
1049 /*****************************************************************************/
1050 	: XMLChildNode( obj ),
1051       sDefault( obj.sDefault){}
1052 
1053 /*****************************************************************************/
operator =(const XMLDefault & obj)1054 XMLDefault& XMLDefault::operator=(const XMLDefault& obj){
1055 /*****************************************************************************/
1056 	if( this!=&obj ){
1057         XMLChildNode::operator=( obj );
1058         sDefault        = obj.sDefault;
1059     }
1060     return *this;
1061 }
1062 
1063 
1064 //
1065 // class SimpleXMLParser
1066 //
1067 
1068 #define XML_CHAR_TO_OUSTRING(x) OStringToOUString(OString(x), RTL_TEXTENCODING_UTF8)
1069 #define XML_CHAR_N_TO_OUSTRING(x,n) OStringToOUString(OString(x,n), RTL_TEXTENCODING_UTF8 )
1070 
1071 
1072 /*****************************************************************************/
SimpleXMLParser()1073 SimpleXMLParser::SimpleXMLParser()
1074 /*****************************************************************************/
1075 				: pXMLFile( NULL )
1076 {
1077     aParser = XML_ParserCreate( NULL );
1078 	XML_SetUserData( aParser, this );
1079 	XML_SetElementHandler( aParser, (XML_StartElementHandler) StartElementHandler, (XML_EndElementHandler) EndElementHandler );
1080 	XML_SetCharacterDataHandler( aParser, (XML_CharacterDataHandler) CharacterDataHandler );
1081 	XML_SetCommentHandler( aParser, (XML_CommentHandler) CommentHandler );
1082 	XML_SetDefaultHandler( aParser, (XML_DefaultHandler) DefaultHandler );
1083 }
1084 
1085 /*****************************************************************************/
~SimpleXMLParser()1086 SimpleXMLParser::~SimpleXMLParser()
1087 /*****************************************************************************/
1088 {
1089 	XML_ParserFree( aParser );
1090 }
1091 
1092 /*****************************************************************************/
StartElementHandler(void * userData,const XML_Char * name,const XML_Char ** atts)1093 void SimpleXMLParser::StartElementHandler(
1094 	void *userData, const XML_Char *name, const XML_Char **atts )
1095 /*****************************************************************************/
1096 {
1097 	(( SimpleXMLParser * ) userData )->StartElement( name, atts );
1098 }
1099 
1100 
1101 /*****************************************************************************/
EndElementHandler(void * userData,const XML_Char * name)1102 void SimpleXMLParser::EndElementHandler(
1103 	void *userData, const XML_Char *name )
1104 /*****************************************************************************/
1105 {
1106 	(( SimpleXMLParser * ) userData )->EndElement( name );
1107 }
1108 
1109 /*****************************************************************************/
CharacterDataHandler(void * userData,const XML_Char * s,int len)1110 void SimpleXMLParser::CharacterDataHandler(
1111 	void *userData, const XML_Char *s, int len )
1112 /*****************************************************************************/
1113 {
1114 	(( SimpleXMLParser * ) userData )->CharacterData( s, len );
1115 }
1116 
1117 /*****************************************************************************/
CommentHandler(void * userData,const XML_Char * data)1118 void SimpleXMLParser::CommentHandler(
1119 	void *userData, const XML_Char *data )
1120 /*****************************************************************************/
1121 {
1122 	(( SimpleXMLParser * ) userData )->Comment( data );
1123 }
1124 
1125 /*****************************************************************************/
DefaultHandler(void * userData,const XML_Char * s,int len)1126 void SimpleXMLParser::DefaultHandler(
1127 	void *userData, const XML_Char *s, int len )
1128 /*****************************************************************************/
1129 {
1130 	(( SimpleXMLParser * ) userData )->Default( s, len );
1131 }
1132 
1133 /*****************************************************************************/
StartElement(const XML_Char * name,const XML_Char ** atts)1134 void SimpleXMLParser::StartElement(
1135 	const XML_Char *name, const XML_Char **atts )
1136 /*****************************************************************************/
1137 {
1138 	String sElementName = String( XML_CHAR_TO_OUSTRING( name ));
1139 	XMLElement *pElement = new XMLElement( sElementName, ( XMLParentNode * ) pCurNode );
1140 	pCurNode = pElement;
1141 	pCurData = NULL;
1142 
1143 	int i = 0;
1144 	while( atts[i] ) {
1145 		pElement->AddAttribute(
1146 			String( XML_CHAR_TO_OUSTRING( atts[ i ] )),
1147 			String( XML_CHAR_TO_OUSTRING( atts[ i + 1 ] )));
1148 		i += 2;
1149 	}
1150 }
1151 
1152 /*****************************************************************************/
EndElement(const XML_Char * name)1153 void SimpleXMLParser::EndElement( const XML_Char *name )
1154 /*****************************************************************************/
1155 {
1156 	// This variable is not used at all, but the sax C interface can't be changed
1157     // To prevent warnings this dummy assignment is used
1158     // +++
1159     (void) name;
1160 
1161     pCurNode = pCurNode->GetParent();
1162 	pCurData = NULL;
1163 }
1164 
1165 /*****************************************************************************/
CharacterData(const XML_Char * s,int len)1166 void SimpleXMLParser::CharacterData(
1167 	const XML_Char *s, int len )
1168 /*****************************************************************************/
1169 {
1170 	if ( !pCurData ){
1171 		String x=String( XML_CHAR_N_TO_OUSTRING( s, len ));
1172 		XMLUtil::UnQuotHTML(x);
1173 		pCurData = new XMLData( x , pCurNode );
1174 	}else{
1175 		String x=String( XML_CHAR_N_TO_OUSTRING( s, len ));
1176 		XMLUtil::UnQuotHTML(x);
1177 		pCurData->AddData( x );
1178 
1179 	}
1180 }
1181 
1182 /*****************************************************************************/
Comment(const XML_Char * data)1183 void SimpleXMLParser::Comment(
1184 	const XML_Char *data )
1185 /*****************************************************************************/
1186 {
1187 	pCurData = NULL;
1188 		new XMLComment( String( XML_CHAR_TO_OUSTRING( data )), pCurNode );
1189 }
1190 
1191 /*****************************************************************************/
Default(const XML_Char * s,int len)1192 void SimpleXMLParser::Default(
1193 	const XML_Char *s, int len )
1194 /*****************************************************************************/
1195 {
1196 	pCurData = NULL;
1197 	new XMLDefault(
1198 		String( XML_CHAR_N_TO_OUSTRING( s, len )), pCurNode );
1199 }
1200 
1201 /*****************************************************************************/
Execute(const String & rFullFileName,const String & rFileName,XMLFile * pXMLFileIn)1202 XMLFile *SimpleXMLParser::Execute( const String &rFullFileName , const String &rFileName, XMLFile* pXMLFileIn )
1203 /*****************************************************************************/
1204 {
1205 //	printf("DBG: SimpleXMLParser::Execute( %s )", ByteString( rFileName , RTL_TEXTENCODING_ASCII_US ).GetBuffer() );
1206     aErrorInformation.eCode = XML_ERROR_NONE;
1207 	aErrorInformation.nLine = 0;
1208 	aErrorInformation.nColumn = 0;
1209 	aErrorInformation.sMessage = String::CreateFromAscii( "ERROR: Unable to open file " );
1210 	aErrorInformation.sMessage += rFileName;
1211 
1212 	SvFileStream aStream( rFileName, STREAM_STD_READ );
1213 
1214 	if ( !aStream.IsOpen())
1215 		return NULL;
1216 
1217 	SvMemoryStream aMemStream;
1218 	aStream >> aMemStream;
1219 	aMemStream.Seek( 0 );
1220 
1221 	aStream.Close();
1222 
1223     pXMLFile = pXMLFileIn;
1224     pXMLFile->SetName( rFileName );
1225     pXMLFile->SetFullName( rFullFileName );
1226 
1227 	return Execute( &aMemStream );
1228 }
1229 
1230 /*****************************************************************************/
Execute(SvMemoryStream * pStream)1231 XMLFile *SimpleXMLParser::Execute( SvMemoryStream *pStream )
1232 /*****************************************************************************/
1233 {
1234 	if ( !pXMLFile )
1235 		pXMLFile = new XMLFile( String());
1236 
1237 	pCurNode = pXMLFile;
1238 	pCurData = NULL;
1239 
1240 	sal_uLong nPos = pStream->Tell();
1241 	pStream->Seek( STREAM_SEEK_TO_END );
1242 
1243 	aErrorInformation.eCode = XML_ERROR_NONE;
1244 	aErrorInformation.nLine = 0;
1245 	aErrorInformation.nColumn = 0;
1246 	if ( pXMLFile->GetName().Len()) {
1247 		aErrorInformation.sMessage = String::CreateFromAscii( "File " );
1248 		aErrorInformation.sMessage += pXMLFile->GetName();
1249 		aErrorInformation.sMessage += String::CreateFromAscii( " parsed successfully" );
1250 	}
1251 	else
1252 		aErrorInformation.sMessage = String::CreateFromAscii( "XML-File parsed successfully" );
1253 
1254 	if ( !XML_Parse(
1255 		aParser, ( char * ) pStream->GetData() + nPos, pStream->Tell() - nPos, sal_True ))
1256 	{
1257 		aErrorInformation.eCode = XML_GetErrorCode( aParser );
1258 		aErrorInformation.nLine = XML_GetErrorLineNumber( aParser );
1259 		aErrorInformation.nColumn = XML_GetErrorColumnNumber( aParser );
1260 
1261 		aErrorInformation.sMessage = String::CreateFromAscii( "ERROR: " );
1262 		if ( pXMLFile->GetName().Len())
1263 			aErrorInformation.sMessage += pXMLFile->GetName();
1264 		else
1265 			aErrorInformation.sMessage += String::CreateFromAscii( "XML-File" );
1266 		aErrorInformation.sMessage += String::CreateFromAscii( " (" );
1267 		aErrorInformation.sMessage += String::CreateFromInt64( aErrorInformation.nLine );
1268 		aErrorInformation.sMessage += String::CreateFromAscii( "," );
1269 		aErrorInformation.sMessage += String::CreateFromInt64( aErrorInformation.nColumn );
1270 		aErrorInformation.sMessage += String::CreateFromAscii( "): " );
1271 
1272 		switch( aErrorInformation.eCode ) {
1273   			case XML_ERROR_NO_MEMORY: aErrorInformation.sMessage += String::CreateFromAscii( "No memory" ); break;
1274   			case XML_ERROR_SYNTAX: aErrorInformation.sMessage += String::CreateFromAscii( "Syntax" ); break;
1275   			case XML_ERROR_NO_ELEMENTS: aErrorInformation.sMessage += String::CreateFromAscii( "No elements" ); break;
1276   			case XML_ERROR_INVALID_TOKEN: aErrorInformation.sMessage += String::CreateFromAscii( "Invalid token" ); break;
1277   			case XML_ERROR_UNCLOSED_TOKEN: aErrorInformation.sMessage += String::CreateFromAscii( "Unclosed token" ); break;
1278   			case XML_ERROR_PARTIAL_CHAR: aErrorInformation.sMessage += String::CreateFromAscii( "Partial char" ); break;
1279   			case XML_ERROR_TAG_MISMATCH: aErrorInformation.sMessage += String::CreateFromAscii( "Tag mismatch" ); break;
1280   			case XML_ERROR_DUPLICATE_ATTRIBUTE: aErrorInformation.sMessage += String::CreateFromAscii( "Dublicat attribute" ); break;
1281   			case XML_ERROR_JUNK_AFTER_DOC_ELEMENT: aErrorInformation.sMessage += String::CreateFromAscii( "Junk after doc element" ); break;
1282   			case XML_ERROR_PARAM_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Param entity ref" ); break;
1283   			case XML_ERROR_UNDEFINED_ENTITY: aErrorInformation.sMessage += String::CreateFromAscii( "Undefined entity" ); break;
1284   			case XML_ERROR_RECURSIVE_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Recursive entity ref" ); break;
1285   			case XML_ERROR_ASYNC_ENTITY: aErrorInformation.sMessage += String::CreateFromAscii( "Async_entity" ); break;
1286   			case XML_ERROR_BAD_CHAR_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Bad char ref" ); break;
1287   			case XML_ERROR_BINARY_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Binary entity" ); break;
1288   			case XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Attribute external entity ref" ); break;
1289   			case XML_ERROR_MISPLACED_XML_PI: aErrorInformation.sMessage += String::CreateFromAscii( "Misplaced xml pi" ); break;
1290   			case XML_ERROR_UNKNOWN_ENCODING: aErrorInformation.sMessage += String::CreateFromAscii( "Unknown encoding" ); break;
1291   			case XML_ERROR_INCORRECT_ENCODING: aErrorInformation.sMessage += String::CreateFromAscii( "Incorrect encoding" ); break;
1292   			case XML_ERROR_UNCLOSED_CDATA_SECTION: aErrorInformation.sMessage += String::CreateFromAscii( "Unclosed cdata section" ); break;
1293   			case XML_ERROR_EXTERNAL_ENTITY_HANDLING: aErrorInformation.sMessage += String::CreateFromAscii( "External entity handling" ); break;
1294   			case XML_ERROR_NOT_STANDALONE: aErrorInformation.sMessage += String::CreateFromAscii( "Not standalone" ); break;
1295             case XML_ERROR_NONE: break;
1296             default:
1297                 break;
1298 
1299 		}
1300 		delete pXMLFile;
1301 		pXMLFile = NULL;
1302 	}
1303 	pStream->Seek( nPos );
1304 
1305 	return pXMLFile;
1306 }
1307 
1308 /*****************************************************************************/
QuotHTML(String & rString)1309 void XMLUtil::QuotHTML( String &rString )
1310 /*****************************************************************************/
1311 {
1312 	OUStringBuffer sReturn;
1313 	static const String LT(String::CreateFromAscii("<"));
1314 	static const String QLT(String::CreateFromAscii("&lt;"));
1315 	static const String GT(String::CreateFromAscii(">"));
1316 	static const String QGT(String::CreateFromAscii("&gt;"));
1317 	static const String QUOT(String::CreateFromAscii("\\"));
1318 	static const String QQUOT(String::CreateFromAscii("&quot;"));
1319 	static const String APOS(String::CreateFromAscii("\""));
1320 	static const String QAPOS(String::CreateFromAscii("&apos;"));
1321 	static const String AMP(String::CreateFromAscii("&"));
1322 	static const String QAMP(String::CreateFromAscii("&amp;"));
1323 	static const String SLASH(String::CreateFromAscii("\\"));
1324 
1325 	for ( sal_uInt16 i = 0; i < rString.Len(); i++) {
1326 		if ( i < rString.Len()) {
1327 			switch ( rString.GetChar( i )) {
1328 				case '\\': if( i+1 <= rString.Len() ){
1329 							switch( rString.GetChar( i+1 ) ){
1330                              case '<':  sReturn.append( LT );i++;break;
1331                              case '>':  sReturn.append( GT );i++;break;
1332                              case '\\': sReturn.append( QUOT );i++;break;
1333                              case '\"': sReturn.append( APOS );i++;break;
1334                              //case '\'': sReturn += "\'";i++;break;
1335                              //case '&' : sRetrun += "&";i++;break;
1336                              default:   sReturn.append( SLASH );break;
1337 
1338                            }
1339                           }
1340                         break;
1341 
1342                 case '<':
1343                     sReturn.append( QLT );
1344 					break;
1345 
1346 				case '>':
1347                     sReturn.append( QGT );
1348 					break;
1349 
1350 				case '\"':
1351                     sReturn.append( QQUOT );
1352 					break;
1353 
1354 /*				case '\'':
1355                     sReturn += "&apos;";
1356 					break;
1357 */
1358 				case '&':
1359 					if (
1360 						  ( ( i + 4 ) < rString.Len()) &&
1361 						  ( String( rString.Copy( i, 5 ) ).Equals( QAMP ) )
1362 					   )
1363 						sReturn.append( rString.GetChar( i ) );
1364 					else
1365 						sReturn.append( QAMP );
1366 				break;
1367 
1368 				default:
1369 					sReturn.append( rString.GetChar( i ) );
1370 				break;
1371 			}
1372 		}
1373 	}
1374 	rString = String( sReturn.makeStringAndClear() );
1375 }
1376 
UnQuotHTML(String & rString)1377 void XMLUtil::UnQuotHTML( String &rString ){
1378     UnQuotData( rString );
1379 }
1380 
UnQuotData(String & rString_in)1381 void XMLUtil::UnQuotData( String &rString_in ){
1382 	ByteString sReturn;
1383     ByteString sString( rString_in , RTL_TEXTENCODING_UTF8 );
1384 	while ( sString.Len()) {
1385 	    if ( sString.Copy( 0, 1 ) == "\\" ) {
1386 			sReturn += "\\\\";
1387 			sString.Erase( 0, 1 );
1388 		}
1389 		else if ( sString.Copy( 0, 5 ) == "&amp;" ) {
1390 			sReturn += "&";
1391 			sString.Erase( 0, 5 );
1392 		}
1393 		else if ( sString.Copy( 0, 4 ) == "&lt;" ) {
1394 			sReturn += "<";
1395 			sString.Erase( 0, 4 );
1396 		}
1397 		else if ( sString.Copy( 0, 4 ) == "&gt;" ) {
1398 			sReturn += ">";
1399 			sString.Erase( 0, 4 );
1400 		}
1401 		else if ( sString.Copy( 0, 6 ) == "&quot;" ) {
1402 			sReturn += "\"";
1403 			sString.Erase( 0, 6 );
1404 		}
1405 		else if ( sString.Copy( 0, 6 ) == "&apos;" ) {
1406 			sReturn += "\'";
1407 			sString.Erase( 0, 6 );
1408 		}
1409 		else {
1410 			sReturn += sString.GetChar( 0 );
1411 			sString.Erase( 0, 1 );
1412 		}
1413 	}
1414     rString_in = String(sReturn , RTL_TEXTENCODING_UTF8 );
1415 
1416 
1417 }
1418 
XMLUtil()1419 XMLUtil::XMLUtil(){
1420 }
1421 
1422 
1423 /*****************************************************************************/
dump()1424 void XMLUtil::dump(){
1425 /*****************************************************************************/
1426 	int cnt=1;
1427     printf("size=%lu\n",static_cast<unsigned long>(lMap.size()));
1428     for(HashMap::iterator pos = lMap.begin(); pos != lMap.end() ; ++pos){
1429         fprintf(stdout,"key=%s , value=%d , no=%d\n",pos->first.GetBuffer(),pos->second,cnt++);
1430     }
1431 }
1432 /*****************************************************************************/
Instance()1433 XMLUtil&  XMLUtil::Instance(){
1434 /*****************************************************************************/
1435 	static XMLUtil instance;
1436 	return instance;
1437 }
1438 /*****************************************************************************/
~XMLUtil()1439 XMLUtil::~XMLUtil(){}
1440 /*****************************************************************************/
1441 /*****************************************************************************/
GetIsoLangByIndex(sal_uInt16 nIndex)1442 ByteString XMLUtil::GetIsoLangByIndex( sal_uInt16 nIndex )
1443 /*****************************************************************************/
1444 {
1445 	if(nIndex > 0 && MAX_LANGUAGES >= nIndex )
1446 		return isoArray[nIndex];
1447 	return "";
1448 }
1449 
1450