merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / xmlparse.cxx
blob4945a4ff42001f06503976b0dd0497f67339cb9e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlparse.cxx,v $
10 * $Revision: 1.24 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_transex3.hxx"
33 #include <iterator> /* std::iterator*/
35 #include <stdio.h>
36 #include <sal/alloca.h>
38 #include "xmlparse.hxx"
39 #include <fstream>
40 #include <iostream>
41 #include <osl/mutex.hxx>
42 #include <osl/thread.hxx>
43 #ifdef __MINGW32__
44 #include <tools/prewin.h>
45 #include <tools/postwin.h>
46 #endif
47 using namespace std;
48 using namespace osl;
51 // class XMLChildNode
54 /*****************************************************************************/
55 XMLChildNode::XMLChildNode( XMLParentNode *pPar )
56 /*****************************************************************************/
57 : pParent( pPar )
59 if ( pParent )
60 pParent->AddChild( this );
64 /*****************************************************************************/
65 XMLChildNode::XMLChildNode( const XMLChildNode& obj)
66 /*****************************************************************************/
67 : XMLNode(obj),
68 pParent(obj.pParent){}
70 /*****************************************************************************/
71 XMLChildNode& XMLChildNode::operator=(const XMLChildNode& obj){
72 /*****************************************************************************/
73 if(this != &obj){
74 pParent=obj.pParent;
76 return *this;
79 // class XMLParentNode
83 /*****************************************************************************/
84 XMLParentNode::~XMLParentNode()
85 /*****************************************************************************/
87 if( pChildList ){
88 RemoveAndDeleteAllChilds();
89 delete pChildList;
90 pChildList = NULL;
92 pChildList = NULL;
94 /*****************************************************************************/
95 XMLParentNode::XMLParentNode( const XMLParentNode& obj)
96 /*****************************************************************************/
97 : XMLChildNode( obj )
99 if( obj.pChildList ){
100 pChildList=new XMLChildNodeList();
101 XMLChildNode* pNode = NULL;
102 for ( ULONG i = 0; i < obj.pChildList->Count(); i++ ){
103 pNode = obj.pChildList->GetObject( i );
104 if( pNode != NULL){
105 switch(pNode->GetNodeType()){
106 case XML_NODE_TYPE_ELEMENT:
107 AddChild( new XMLElement( *static_cast<XMLElement* >(pNode) ) ); break;
108 case XML_NODE_TYPE_DATA:
109 AddChild( new XMLData ( *static_cast<XMLData* > (pNode) ) ); break;
110 case XML_NODE_TYPE_COMMENT:
111 AddChild( new XMLComment( *static_cast<XMLComment* >(pNode) ) ); break;
112 case XML_NODE_TYPE_DEFAULT:
113 AddChild( new XMLDefault( *static_cast<XMLDefault* >(pNode) ) ); break;
114 default: fprintf(stdout,"XMLParentNode::XMLParentNode( const XMLParentNode& obj) strange obj");
118 }else pChildList = NULL;
120 /*****************************************************************************/
121 XMLParentNode& XMLParentNode::operator=(const XMLParentNode& obj){
122 /*****************************************************************************/
123 if(this!=&obj){
124 XMLChildNode::operator=(obj);
125 if( pChildList ){
126 RemoveAndDeleteAllChilds();
127 delete pChildList;
128 pChildList = NULL;
130 if( obj.pChildList ){
131 pChildList=new XMLChildNodeList();
132 for ( ULONG i = 0; i < obj.pChildList->Count(); i++ )
133 AddChild( obj.pChildList->GetObject( i ) );
134 }else pChildList = NULL;
137 return *this;
139 /*****************************************************************************/
140 void XMLParentNode::AddChild( XMLChildNode *pChild )
141 /*****************************************************************************/
143 if ( !pChildList )
144 pChildList = new XMLChildNodeList();
145 pChildList->Insert( pChild, LIST_APPEND );
148 /*****************************************************************************/
149 void XMLParentNode::AddChild( XMLChildNode *pChild , int pos )
150 /*****************************************************************************/
152 if ( !pChildList )
153 pChildList = new XMLChildNodeList();
154 pChildList->Insert( pChild, pos );
157 /*****************************************************************************/
158 int XMLParentNode::GetPosition( ByteString id ){
159 /*****************************************************************************/
160 XMLElement* a;
162 static const ByteString sEnusStr = ByteString(String::CreateFromAscii(ENGLISH_US_ISO).ToLowerAscii() , RTL_TEXTENCODING_ASCII_US ).ToLowerAscii();
163 static const ByteString sDeStr = ByteString(String::CreateFromAscii(GERMAN_ISO2).ToLowerAscii() , RTL_TEXTENCODING_ASCII_US ).ToLowerAscii();
165 if ( pChildList ){
166 for ( ULONG i = 0; i < pChildList->Count(); i++ ) {
167 XMLChildNode *pChild = pChildList->GetObject( i );
168 if ( pChild->GetNodeType() == XML_NODE_TYPE_ELEMENT ){
169 a = static_cast<XMLElement* >(pChild);
170 ByteString elemid( a->GetId() );
171 elemid.ToLowerAscii();
172 if ( elemid.Equals( id.ToLowerAscii() ) ){
173 ByteString elemLID( a->GetLanguageId() );
174 elemLID.ToLowerAscii();
175 if( elemLID.Equals( sEnusStr) ) {
176 return i;
178 else if( elemLID.Equals( sDeStr) ) {
179 return i;
185 return -1;
188 /*****************************************************************************/
189 int XMLParentNode::RemoveChild( XMLElement *pRefElement )
190 /*****************************************************************************/
192 XMLElement* a;
193 if ( pChildList ){
194 for ( ULONG i = 0; i < pChildList->Count(); i++ ) {
195 XMLChildNode *pChild = pChildList->GetObject( i );
196 if ( pChild->GetNodeType() == XML_NODE_TYPE_ELEMENT ){
197 a = static_cast<XMLElement* >(pChild);
198 ByteString elemid( a->GetId() );
199 elemid.ToLowerAscii();
200 ByteString elemLID( a->GetLanguageId() );
201 elemLID.ToLowerAscii();
202 ByteString pRefLID( pRefElement->GetLanguageId() );
203 pRefLID.ToLowerAscii();
204 if ( elemid.Equals(pRefElement->GetId())
205 && elemLID.Equals( pRefLID ) )
207 if( pRefElement->ToOString().compareTo( a->ToOString() )==0 ){
208 pChildList->Remove( i );
209 delete a; // Test
210 return i;
217 return -1;
220 /*****************************************************************************/
221 void XMLParentNode::RemoveAndDeleteAllChilds(){
222 /*****************************************************************************/
223 if ( pChildList ) {
224 for ( ULONG i = 0; i < pChildList->Count(); i++ )
225 delete pChildList->GetObject( i );
226 pChildList->Clear();
230 /*****************************************************************************/
231 XMLElement *XMLParentNode::GetChildElement( XMLElement *pRefElement )
232 /*****************************************************************************/
234 for ( ULONG i = 0; i < pChildList->Count(); i++ ) {
235 XMLChildNode *pChild = pChildList->GetObject( i );
236 if ( pChild->GetNodeType() == XML_NODE_TYPE_ELEMENT )
237 if ((( XMLElement * ) pChild )->GetName() ==
238 pRefElement->GetName())
240 XMLAttributeList *pList = pRefElement->GetAttributeList();
241 if ( !pList )
242 return ( XMLElement * ) pChild;
244 BOOL bMatch = FALSE;
245 for ( ULONG j = 0; j < pList->Count() && bMatch; j++ ) {
246 XMLAttribute *pAttribute = pList->GetObject( j );
247 XMLAttribute *pCandidate =
248 (( XMLElement * ) pChild )->GetAttribute(
249 *pAttribute );
250 if ( !pCandidate || !pAttribute->IsEqual( *pCandidate ))
251 bMatch = FALSE;
253 if ( bMatch )
254 return ( XMLElement * ) pChild;
257 return NULL;
261 // class XMLFile
264 /*****************************************************************************/
265 USHORT XMLFile::GetNodeType()
266 /*****************************************************************************/
268 return XML_NODE_TYPE_FILE;
271 /*****************************************************************************/
272 BOOL XMLFile::Write( ByteString &aFilename )
273 /*****************************************************************************/
276 if ( aFilename.Len()) {
277 // retry harder if there is a NFS problem,
278 for( int x = 1 ; x < 3 ; x++ ){ // this looks strange...yes!
279 ofstream aFStream( aFilename.GetBuffer() , ios::out | ios::trunc );
281 if( !aFStream ) // From time to time the stream can not be opened the first time on NFS volumes,
282 { // I wasn't able to track this down. I think this is an NFS issue .....
283 //cerr << "ERROR: - helpex - Can't write to tempfile " << aFilename.GetBuffer() << " No#" << x << "\n";
284 TimeValue aTime;
285 aTime.Seconds = 3;
286 aTime.Nanosec = 0;
288 osl::Thread::wait( aTime );
290 else
292 // write out
293 Write( aFStream );
294 aFStream.close();
296 // check!
297 DirEntry aTarget( aFilename );
298 FileStat aFileStat( aTarget );
300 if( aFileStat.GetSize() < 1 )
302 //retry
303 //cerr << "WARNING: - helpex - Can't create file " << aFilename.GetBuffer() << " No#" << x << "\n";
304 aTarget.Kill();
306 else
308 //everything ok!
309 return true;
313 cerr << "ERROR: - helpex - Can't create file " << aFilename.GetBuffer() << "\nPossible reason: Disk full ? Mounted NFS volume broken ? Wrong permissions ?\n";
314 exit( -1 );
316 cerr << "ERROR: - helpex - Empty file name\n";
317 exit( -1 );
322 void XMLFile::WriteString( ofstream &rStream, const String &sString )
324 ByteString sText( sString, RTL_TEXTENCODING_UTF8 );
325 rStream << sText.GetBuffer();
329 BOOL XMLFile::Write( ofstream &rStream , XMLNode *pCur )
331 XMLUtil& xmlutil = XMLUtil::Instance();
332 (void) xmlutil;
334 if ( !pCur )
335 Write( rStream, this );
336 else {
337 switch( pCur->GetNodeType()) {
338 case XML_NODE_TYPE_FILE: {
339 if( GetChildList())
340 for ( ULONG i = 0; i < GetChildList()->Count(); i++ )
341 Write( rStream, GetChildList()->GetObject( i ));
343 break;
344 case XML_NODE_TYPE_ELEMENT: {
345 XMLElement *pElement = ( XMLElement * ) pCur;
346 rStream << "<";
347 WriteString( rStream, pElement->GetName());
348 if ( pElement->GetAttributeList())
349 for ( ULONG j = 0; j < pElement->GetAttributeList()->Count(); j++ ) {
350 rStream << " ";
351 String sData(*pElement->GetAttributeList()->GetObject( j ));
352 xmlutil.QuotHTML( sData );
353 WriteString( rStream , sData );
354 rStream << "=\"";
355 sData=pElement->GetAttributeList()->GetObject( j )->GetValue();
356 xmlutil.QuotHTML( sData );
357 WriteString( rStream , sData );
358 rStream << "\"";
360 if ( !pElement->GetChildList())
361 rStream << "/>";
362 else {
363 rStream << ">";
364 for ( ULONG k = 0; k < pElement->GetChildList()->Count(); k++ )
365 Write( rStream, pElement->GetChildList()->GetObject( k ));
366 rStream << "</";
367 WriteString( rStream, pElement->GetName());
368 rStream << ">";
371 break;
372 case XML_NODE_TYPE_DATA: {
373 XMLData *pData = ( XMLData * ) pCur;
374 String sData( pData->GetData());
375 xmlutil.QuotHTML( sData );
376 WriteString( rStream, sData );
378 break;
379 case XML_NODE_TYPE_COMMENT: {
380 XMLComment *pComment = ( XMLComment * ) pCur;
381 rStream << "<!--";
382 WriteString( rStream, pComment->GetComment());
383 rStream << "-->";
385 break;
386 case XML_NODE_TYPE_DEFAULT: {
387 XMLDefault *pDefault = ( XMLDefault * ) pCur;
388 WriteString( rStream, pDefault->GetDefault());
390 break;
393 return TRUE;
397 void XMLFile::Print( XMLNode *pCur, USHORT nLevel )
400 if ( !pCur )
401 Print( this );
402 else {
403 switch( pCur->GetNodeType()) {
404 case XML_NODE_TYPE_FILE: {
405 if( GetChildList())
406 for ( ULONG i = 0; i < GetChildList()->Count(); i++ )
407 Print( GetChildList()->GetObject( i ));
409 break;
410 case XML_NODE_TYPE_ELEMENT: {
411 XMLElement *pElement = ( XMLElement * ) pCur;
413 fprintf( stdout, "<%s", ByteString( pElement->GetName(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
414 if ( pElement->GetAttributeList())
415 for ( ULONG j = 0; j < pElement->GetAttributeList()->Count(); j++ ){
416 ByteString aAttrName( *pElement->GetAttributeList()->GetObject( j ), RTL_TEXTENCODING_UTF8 );
417 if( !aAttrName.EqualsIgnoreCaseAscii( XML_LANG ) ) {
418 fprintf( stdout, " %s=\"%s\"",
419 aAttrName.GetBuffer(),
420 ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),
421 RTL_TEXTENCODING_UTF8 ).GetBuffer());
424 if ( !pElement->GetChildList())
425 fprintf( stdout, "/>" );
426 else {
427 fprintf( stdout, ">" );
428 for ( ULONG k = 0; k < pElement->GetChildList()->Count(); k++ )
429 Print( pElement->GetChildList()->GetObject( k ), nLevel + 1 );
430 fprintf( stdout, "</%s>", ByteString( pElement->GetName(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
433 break;
434 case XML_NODE_TYPE_DATA: {
435 XMLData *pData = ( XMLData * ) pCur;
436 String sData = pData->GetData();
437 fprintf( stdout, "%s", ByteString( sData, RTL_TEXTENCODING_UTF8 ).GetBuffer());
439 break;
440 case XML_NODE_TYPE_COMMENT: {
441 XMLComment *pComment = ( XMLComment * ) pCur;
442 fprintf( stdout, "<!--%s-->", ByteString( pComment->GetComment(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
444 break;
445 case XML_NODE_TYPE_DEFAULT: {
446 XMLDefault *pDefault = ( XMLDefault * ) pCur;
447 fprintf( stdout, "%s", ByteString( pDefault->GetDefault(), RTL_TEXTENCODING_UTF8 ).GetBuffer());
449 break;
453 XMLFile::~XMLFile()
455 if( XMLStrings != NULL ){
456 XMLHashMap::iterator pos = XMLStrings->begin();
457 for( ; pos != XMLStrings->end() ; ++pos ){
458 delete pos->second; // Check and delete content also ?
460 delete XMLStrings;
461 XMLStrings = NULL;
464 /*****************************************************************************/
465 XMLFile::XMLFile( const String &rFileName ) // the file name, empty if created from memory stream
466 /*****************************************************************************/
467 : XMLParentNode( NULL ),
468 sFileName ( rFileName ),
469 ID ( "id" ),
470 OLDREF ( "oldref" ),
471 XML_LANG ( "xml-lang" ),
472 XMLStrings ( NULL )
475 // nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("bookmark_value"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
476 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("bookmark"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
477 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("variable"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
478 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("paragraph"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
479 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("alt"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
480 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("caption"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
481 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("title"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
482 nodes_localize.insert( TagMap::value_type(ByteString(String::CreateFromAscii("link"),RTL_TEXTENCODING_ASCII_US) , TRUE) );
484 /*****************************************************************************/
485 void XMLFile::Extract( XMLFile *pCur )
486 /*****************************************************************************/
488 if( XMLStrings != NULL ) delete XMLStrings; // Elements ?
490 XMLStrings = new XMLHashMap();
491 if ( !pCur )
492 SearchL10NElements( this );
493 else {
494 if( pCur->GetNodeType()==XML_NODE_TYPE_FILE) {
495 SearchL10NElements(pCur);
500 /*****************************************************************************/
501 void XMLFile::View(){
502 /*****************************************************************************/
503 XMLElement* cur;
504 for(XMLHashMap::iterator pos=XMLStrings->begin(); pos!=XMLStrings->end();++pos){
505 fprintf(stdout,"\nid=%s\n",(pos->first).GetBuffer());
506 LangHashMap* elem=pos->second;
507 for(LangHashMap::iterator pos2=elem->begin(); pos2!=elem->end();++pos2){
508 fprintf( stdout,"\nlanguage=%s\n",(pos2->first).GetBuffer() );
509 cur=pos2->second;
510 fprintf(stdout,"\n%s\n",((XMLElement*)cur)->ToOString().getStr());
516 /*****************************************************************************/
517 void XMLFile::InsertL10NElement( XMLElement* pElement ){
518 /*****************************************************************************/
519 ByteString tmpStr,id,oldref,language("");
520 LangHashMap* elem;
522 if( pElement->GetAttributeList() != NULL ){
523 for ( ULONG j = 0; j < pElement->GetAttributeList()->Count(); j++ ){
524 tmpStr=ByteString( *pElement->GetAttributeList()->GetObject( j ),RTL_TEXTENCODING_UTF8 );
525 if( tmpStr.CompareTo(ID)==COMPARE_EQUAL ){ // Get the "id" Attribute
526 id = ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
528 if( tmpStr.CompareTo( XML_LANG ) == COMPARE_EQUAL ){ // Get the "xml-lang" Attribute
529 language = ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
533 }else{
534 fprintf(stdout,"XMLFile::InsertL10NElement: No AttributeList found");
535 fprintf(stdout,"++++++++++++++++++++++++++++++++++++++++++++++++++");
536 Print( pElement , 0 );
537 fprintf(stdout,"++++++++++++++++++++++++++++++++++++++++++++++++++");
540 XMLHashMap::iterator pos = XMLStrings->find( id );
541 if( pos == XMLStrings->end() ){ // No instanze , create new one
542 elem = new LangHashMap();
543 (*elem)[ language ]=pElement;
544 XMLStrings->insert( XMLHashMap::value_type( id , elem ) );
545 order.push_back( id );
546 }else{ // Already there
547 elem=pos->second;
548 if ( (*elem)[ language ] )
550 fprintf(stdout,"Error: Duplicated entry. ID = %s LANG = %s in File %s\n", id.GetBuffer(), language.GetBuffer(), ByteString( sFullName,RTL_TEXTENCODING_ASCII_US ).GetBuffer() );
551 exit( -1 );
553 (*elem)[ language ]=pElement;
556 /*****************************************************************************/
557 void XMLFile::showType(XMLParentNode* node){
558 /*****************************************************************************/
559 switch (node->GetNodeType()){
560 case XML_NODE_TYPE_ELEMENT: fprintf(stdout,"ELEMENT\n") ;break;
561 case XML_NODE_TYPE_FILE: fprintf(stdout,"FILE\n") ;break;
562 case XML_NODE_TYPE_COMMENT: fprintf(stdout,"COMMENT\n") ;break;
563 case XML_NODE_TYPE_DATA: fprintf(stdout,"DATA\n") ;break;
564 case XML_NODE_TYPE_DEFAULT: fprintf(stdout,"DEFAULT\n") ;break;
565 default: break;
568 XMLFile::XMLFile()
569 /*****************************************************************************/
570 : XMLParentNode( NULL ),
571 ID ( "id" ),
572 OLDREF ( "oldref" ),
573 XML_LANG ( "xml-lang" ),
574 XMLStrings ( NULL ){};
577 XMLFile::XMLFile( const XMLFile& obj )
578 /*****************************************************************************/
579 : XMLParentNode( obj ),
580 sFileName ( obj.sFileName ),
581 ID ( "id" ),
582 OLDREF ( "oldref" ),
583 XML_LANG ( "xml-lang" ),
584 XMLStrings ( NULL )
586 if( this!=&obj )
588 nodes_localize =obj.nodes_localize;
589 order =obj.order;
593 /*****************************************************************************/
594 XMLFile& XMLFile::operator=(const XMLFile& obj){
595 /*****************************************************************************/
596 if( this!=&obj ){
598 XMLParentNode::operator=(obj);
600 nodes_localize =obj.nodes_localize;
601 order =obj.order;
603 if( XMLStrings ) delete XMLStrings;
605 if( obj.XMLStrings )
607 XMLStrings = new XMLHashMap();
608 for( XMLHashMap::iterator pos = obj.XMLStrings->begin() ; pos != obj.XMLStrings->end() ; ++pos )
610 LangHashMap* elem=pos->second;
611 LangHashMap* newelem = new LangHashMap();
612 for(LangHashMap::iterator pos2=elem->begin(); pos2!=elem->end();++pos2){
613 (*newelem)[ pos2->first ] = new XMLElement( *pos2->second );
614 printf("*");
616 (*XMLStrings)[ pos->first ] = newelem;
620 printf("done!\n");
621 return *this;
625 /*****************************************************************************/
626 void XMLFile::SearchL10NElements( XMLParentNode *pCur , int pos)
627 /*****************************************************************************/
629 static const ByteString LOCALIZE("localize");
630 static const ByteString THEID("id");
631 bool bInsert = true;
632 if ( !pCur )
633 SearchL10NElements( this );
634 else {
635 switch( pCur->GetNodeType()) {
636 case XML_NODE_TYPE_FILE: {
637 XMLParentNode* pElement;
638 if( GetChildList()){
639 for ( ULONG i = 0; i < GetChildList()->Count(); i++ ){
640 pElement = (XMLParentNode*) GetChildList()->GetObject( i );
641 if( pElement->GetNodeType() == XML_NODE_TYPE_ELEMENT ) SearchL10NElements( pElement , i);
645 break;
646 case XML_NODE_TYPE_ELEMENT: {
647 XMLElement *pElement = ( XMLElement * ) pCur;
648 ByteString sName(pElement->GetName(),RTL_TEXTENCODING_ASCII_US);
649 ByteString language,tmpStrVal,oldref;
650 if ( pElement->GetAttributeList()){
651 for ( ULONG j = 0 , cnt = pElement->GetAttributeList()->Count(); j < cnt && bInsert; j++ ){
652 const ByteString tmpStr( *pElement->GetAttributeList()->GetObject( j ),RTL_TEXTENCODING_UTF8 );
653 if( tmpStr.CompareTo(THEID)==COMPARE_EQUAL ){ // Get the "id" Attribute
654 tmpStrVal=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
655 //printf("Checking id = %s\n",tmpStrVal.GetBuffer() );
657 if( tmpStr.CompareTo(LOCALIZE)==COMPARE_EQUAL ){ // Get the "localize" Attribute
658 bInsert=false;
660 if( tmpStr.CompareTo(XML_LANG)==COMPARE_EQUAL ){ // Get the "xml-lang" Attribute
661 language=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
663 if( tmpStr.CompareTo(OLDREF)==COMPARE_EQUAL ){ // Get the "oldref" Attribute
664 oldref=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
667 pElement->SetLanguageId ( language );
668 pElement->SetId ( tmpStrVal.GetBuffer() );
669 pElement->SetOldRef ( oldref );
670 pElement->SetPos( pos );
673 if ( bInsert && ( nodes_localize.find( sName.ToLowerAscii() ) != nodes_localize.end() ) )
674 InsertL10NElement(pElement);
675 else if ( bInsert && pElement->GetChildList() ){
676 for ( ULONG k = 0; k < pElement->GetChildList()->Count(); k++ )
677 SearchL10NElements( (XMLParentNode*) pElement->GetChildList()->GetObject( k ) , k);
680 break;
681 case XML_NODE_TYPE_DATA: {
683 break;
684 case XML_NODE_TYPE_COMMENT: {
686 break;
687 case XML_NODE_TYPE_DEFAULT: {
689 break;
694 /*****************************************************************************/
695 bool XMLFile::CheckExportStatus( XMLParentNode *pCur )
696 /*****************************************************************************/
698 static bool bStatusExport = true;
699 const ByteString LOCALIZE("localize");
700 const ByteString STATUS("status");
701 const ByteString PUBLISH("PUBLISH");
702 const ByteString DEPRECATED("DEPRECATED");
704 const ByteString TOPIC("topic");
705 bool bInsert = true;
706 if ( !pCur )
707 CheckExportStatus( this );
708 else {
709 switch( pCur->GetNodeType()) {
710 case XML_NODE_TYPE_FILE: {
711 XMLParentNode* pElement;
712 if( GetChildList()){
713 for ( ULONG i = 0; i < GetChildList()->Count(); i++ ){
714 pElement = (XMLParentNode*) GetChildList()->GetObject( i );
715 if( pElement->GetNodeType() == XML_NODE_TYPE_ELEMENT ) CheckExportStatus( pElement );//, i);
719 break;
720 case XML_NODE_TYPE_ELEMENT: {
721 XMLElement *pElement = ( XMLElement * ) pCur;
722 ByteString sName(pElement->GetName(),RTL_TEXTENCODING_ASCII_US);
723 if( sName.EqualsIgnoreCaseAscii( TOPIC ) ){
724 if ( pElement->GetAttributeList()){
725 for ( ULONG j = 0 , cnt = pElement->GetAttributeList()->Count(); j < cnt && bInsert; j++ ){
726 const ByteString tmpStr( *pElement->GetAttributeList()->GetObject( j ),RTL_TEXTENCODING_UTF8 );
727 if( tmpStr.EqualsIgnoreCaseAscii( STATUS ) ){
728 ByteString tmpStrVal=ByteString( pElement->GetAttributeList()->GetObject( j )->GetValue(),RTL_TEXTENCODING_UTF8 );
729 if( !tmpStrVal.EqualsIgnoreCaseAscii( PUBLISH ) &&
730 !tmpStrVal.EqualsIgnoreCaseAscii( DEPRECATED )){
731 bStatusExport = false;
738 else if ( pElement->GetChildList() ){
739 for ( ULONG k = 0; k < pElement->GetChildList()->Count(); k++ )
740 CheckExportStatus( (XMLParentNode*) pElement->GetChildList()->GetObject( k ) );//, k);
743 break;
746 return bStatusExport;
749 /*****************************************************************************/
750 USHORT XMLElement::GetNodeType()
751 /*****************************************************************************/
753 return XML_NODE_TYPE_ELEMENT;
756 /*****************************************************************************/
757 XMLElement::XMLElement(const XMLElement& obj)
758 /*****************************************************************************/
760 XMLParentNode ( obj ),
761 sElementName ( obj.sElementName ),
762 pAttributes ( NULL ),
763 project ( obj.project ),
764 filename ( obj.filename ),
765 id ( obj.id ),
766 sOldRef ( obj.sOldRef ),
767 resourceType ( obj.resourceType ),
768 languageId ( obj.languageId ),
769 nPos ( obj.nPos )
772 if ( obj.pAttributes ){
773 pAttributes = new XMLAttributeList();
774 for ( ULONG i = 0; i < obj.pAttributes->Count(); i++ )
775 AddAttribute( *obj.pAttributes->GetObject( i ) , obj.pAttributes->GetObject( i )->GetValue() );
779 /*****************************************************************************/
780 XMLElement& XMLElement::operator=(const XMLElement& obj){
781 /*****************************************************************************/
782 if( this!=&obj ){
783 XMLParentNode::operator=(obj);
784 sElementName =obj.sElementName;
785 project =obj.project;
786 filename =obj.filename;
787 id =obj.id;
788 sOldRef =obj.sOldRef;
789 resourceType =obj.resourceType;
790 languageId =obj.languageId;
791 nPos =obj.nPos;
793 if ( pAttributes ){
794 for ( ULONG i = 0; i < pAttributes->Count(); i++ )
795 delete pAttributes->GetObject( i );
796 delete pAttributes;
798 if ( obj.pAttributes ){
799 pAttributes =new XMLAttributeList();
800 for ( ULONG i = 0; i < obj.pAttributes->Count(); i++ )
801 AddAttribute( *obj.pAttributes->GetObject( i ) , obj.pAttributes->GetObject( i )->GetValue() );
804 return *this;
807 /*****************************************************************************/
808 void XMLElement::AddAttribute( const String &rAttribute, const String &rValue )
809 /*****************************************************************************/
811 if ( !pAttributes )
812 pAttributes = new XMLAttributeList();
813 pAttributes->Insert( new XMLAttribute( rAttribute, rValue ), LIST_APPEND );
816 /*****************************************************************************/
817 void XMLElement::ChangeLanguageTag( const String &rValue ){
818 /*****************************************************************************/
819 static const String rName = String::CreateFromAscii("xml-lang");
820 SetLanguageId( ByteString(rValue,RTL_TEXTENCODING_UTF8) );
821 if ( pAttributes ){
822 for ( ULONG i = 0; i < pAttributes->Count(); i++ ){
823 if ( *pAttributes->GetObject( i ) == rName ){
824 pAttributes->GetObject( i )->setValue(rValue);
828 XMLChildNode* pNode = NULL;
829 XMLElement* pElem = NULL;
830 XMLChildNodeList* pCList = GetChildList();
832 if( pCList != NULL ){
833 for ( ULONG i = 0; i < pCList->Count(); i++ ){
834 pNode = pCList->GetObject( i );
835 if( pNode != NULL && pNode->GetNodeType() == XML_NODE_TYPE_ELEMENT ){
836 pElem = static_cast< XMLElement* >(pNode);
837 pElem->ChangeLanguageTag( rValue );
838 pElem->SetLanguageId( ByteString(rValue,RTL_TEXTENCODING_UTF8) );
839 pElem = NULL;
840 pNode = NULL;
843 pCList = NULL;
846 /*****************************************************************************/
847 XMLAttribute *XMLElement::GetAttribute( const String &rName )
848 /*****************************************************************************/
850 if ( pAttributes )
851 for ( ULONG i = 0; i < pAttributes->Count(); i++ )
852 if ( *pAttributes->GetObject( i ) == rName )
853 return pAttributes->GetObject( i );
855 return NULL;
858 /*****************************************************************************/
859 XMLElement::~XMLElement()
860 /*****************************************************************************/
862 if ( pAttributes ) {
863 for ( ULONG i = 0; i < pAttributes->Count(); i++ )
864 delete pAttributes->GetObject( i );
866 delete pAttributes;
867 pAttributes = NULL;
870 /*****************************************************************************/
871 bool XMLElement::Equals(OUString refStr){
872 /*****************************************************************************/
873 return refStr.equals( ToOUString() );
876 /*****************************************************************************/
877 OString XMLElement::ToOString(){
878 /*****************************************************************************/
879 OUString ouEmpty;
881 OUStringBuffer* buffer = new OUStringBuffer();
882 Print( this, *buffer , true );
884 OString result( (sal_Unicode* )buffer->getStr(), buffer->getLength() , RTL_TEXTENCODING_UTF8 );
885 delete buffer;
886 return result;
888 /*****************************************************************************/
889 OUString XMLElement::ToOUString(){
890 /*****************************************************************************/
891 OUStringBuffer* buffer = new OUStringBuffer();
892 Print(this,*buffer,true);
893 OUString result=buffer->makeStringAndClear();
894 String xy(result.getStr());
895 result=OUString(xy);
896 delete buffer;
897 return result;
899 /*****************************************************************************/
900 void XMLElement::Print(XMLNode *pCur, OUStringBuffer& buffer , bool rootelement ){
901 /*****************************************************************************/
902 //YD FIXME somewhere COMMENT is defined as 4!
903 static const String _COMMENT = String::CreateFromAscii("comment");
904 static const OUString XML_LANG ( OUString::createFromAscii("xml-lang") );
906 if(pCur!=NULL){
907 if(rootelement){
908 XMLElement *pElement = ( XMLElement * ) pCur;
909 if ( pElement->GetAttributeList()){
910 if ( pElement->GetChildList()){
911 XMLChildNode* tmp=NULL;
912 for ( ULONG k = 0; k < pElement->GetChildList()->Count(); k++ ){
913 tmp=pElement->GetChildList()->GetObject( k );
914 Print( tmp, buffer , false);
919 else{
921 switch( pCur->GetNodeType()) {
922 case XML_NODE_TYPE_ELEMENT: {
923 XMLElement *pElement = ( XMLElement * ) pCur;
925 if( !pElement->GetName().EqualsIgnoreCaseAscii( _COMMENT ) ){
926 buffer.append( OUString::createFromAscii("\\<") );
927 buffer.append( pElement->GetName() );
928 if ( pElement->GetAttributeList()){
929 for ( ULONG j = 0; j < pElement->GetAttributeList()->Count(); j++ ){
931 OUString aAttrName( *pElement->GetAttributeList()->GetObject( j ) );
932 if( !aAttrName.equalsIgnoreAsciiCase( XML_LANG ) ) {
933 buffer.append( OUString::createFromAscii(" ") );
934 buffer.append( aAttrName );
935 buffer.append( OUString::createFromAscii("=") );
936 buffer.append( OUString::createFromAscii("\\\"") );
937 buffer.append( pElement->GetAttributeList()->GetObject( j )->GetValue() );
938 buffer.append( OUString::createFromAscii("\\\"") );
942 if ( !pElement->GetChildList())
943 buffer.append( OUString::createFromAscii("/\\>") );
944 else {
945 buffer.append( OUString::createFromAscii("\\>") );
946 XMLChildNode* tmp=NULL;
947 for ( ULONG k = 0; k < pElement->GetChildList()->Count(); k++ ){
948 tmp=pElement->GetChildList()->GetObject( k );
949 Print( tmp, buffer , false);
951 buffer.append( OUString::createFromAscii("\\</") );
952 buffer.append( pElement->GetName() );
953 buffer.append( OUString::createFromAscii("\\>") );
957 break;
958 case XML_NODE_TYPE_DATA: {
959 XMLData *pData = ( XMLData * ) pCur;
960 String sData = pData->GetData();
961 buffer.append( sData );
963 break;
964 case XML_NODE_TYPE_COMMENT: {
965 XMLComment *pComment = ( XMLComment * ) pCur;
966 buffer.append( OUString::createFromAscii("<!--") );
967 buffer.append( pComment->GetComment() );
968 buffer.append( OUString::createFromAscii("-->") );
970 break;
971 case XML_NODE_TYPE_DEFAULT: {
972 XMLDefault *pDefault = ( XMLDefault * ) pCur;
973 buffer.append( pDefault->GetDefault() );
975 break;
978 }else {
979 fprintf(stdout,"\n#+------Error: NULL Pointer in XMLELement::Print------+#\n");
980 return;
986 // class XMLData
988 /*****************************************************************************/
989 XMLData::XMLData(const XMLData& obj)
990 /*****************************************************************************/
991 : XMLChildNode( obj ),
992 sData( obj.sData ) ,
993 isNewCreated ( obj.isNewCreated ){}
995 /*****************************************************************************/
996 XMLData& XMLData::operator=(const XMLData& obj){
997 /*****************************************************************************/
998 if( this!=&obj ){
999 XMLChildNode::operator=( obj );
1000 sData = obj.sData;
1001 isNewCreated = obj.isNewCreated;
1003 return *this;
1005 /*****************************************************************************/
1006 void XMLData::AddData( const String &rData) {
1007 /*****************************************************************************/
1008 sData += rData;
1011 /*****************************************************************************/
1012 USHORT XMLData::GetNodeType()
1013 /*****************************************************************************/
1015 return XML_NODE_TYPE_DATA;
1019 // class XMLComment
1022 /*****************************************************************************/
1023 USHORT XMLComment::GetNodeType()
1024 /*****************************************************************************/
1026 return XML_NODE_TYPE_COMMENT;
1028 /*****************************************************************************/
1029 XMLComment::XMLComment(const XMLComment& obj)
1030 /*****************************************************************************/
1031 : XMLChildNode( obj ),
1032 sComment( obj.sComment ){}
1034 /*****************************************************************************/
1035 XMLComment& XMLComment::operator=(const XMLComment& obj){
1036 /*****************************************************************************/
1037 if( this!=&obj ){
1038 XMLChildNode::operator=( obj );
1039 sComment = obj.sComment;
1041 return *this;
1045 // class XMLDefault
1048 /*****************************************************************************/
1049 USHORT XMLDefault::GetNodeType()
1050 /*****************************************************************************/
1052 return XML_NODE_TYPE_DEFAULT;
1054 /*****************************************************************************/
1055 XMLDefault::XMLDefault(const XMLDefault& obj)
1056 /*****************************************************************************/
1057 : XMLChildNode( obj ),
1058 sDefault( obj.sDefault){}
1060 /*****************************************************************************/
1061 XMLDefault& XMLDefault::operator=(const XMLDefault& obj){
1062 /*****************************************************************************/
1063 if( this!=&obj ){
1064 XMLChildNode::operator=( obj );
1065 sDefault = obj.sDefault;
1067 return *this;
1072 // class SimpleXMLParser
1075 #define XML_CHAR_TO_OUSTRING(x) OStringToOUString(OString(x), RTL_TEXTENCODING_UTF8)
1076 #define XML_CHAR_N_TO_OUSTRING(x,n) OStringToOUString(OString(x,n), RTL_TEXTENCODING_UTF8 )
1079 /*****************************************************************************/
1080 SimpleXMLParser::SimpleXMLParser()
1081 /*****************************************************************************/
1082 : pXMLFile( NULL )
1084 aParser = XML_ParserCreate( NULL );
1085 XML_SetUserData( aParser, this );
1086 XML_SetElementHandler( aParser, (XML_StartElementHandler) StartElementHandler, (XML_EndElementHandler) EndElementHandler );
1087 XML_SetCharacterDataHandler( aParser, (XML_CharacterDataHandler) CharacterDataHandler );
1088 XML_SetCommentHandler( aParser, (XML_CommentHandler) CommentHandler );
1089 XML_SetDefaultHandler( aParser, (XML_DefaultHandler) DefaultHandler );
1092 /*****************************************************************************/
1093 SimpleXMLParser::~SimpleXMLParser()
1094 /*****************************************************************************/
1096 XML_ParserFree( aParser );
1099 /*****************************************************************************/
1100 void SimpleXMLParser::StartElementHandler(
1101 void *userData, const XML_Char *name, const XML_Char **atts )
1102 /*****************************************************************************/
1104 (( SimpleXMLParser * ) userData )->StartElement( name, atts );
1108 /*****************************************************************************/
1109 void SimpleXMLParser::EndElementHandler(
1110 void *userData, const XML_Char *name )
1111 /*****************************************************************************/
1113 (( SimpleXMLParser * ) userData )->EndElement( name );
1116 /*****************************************************************************/
1117 void SimpleXMLParser::CharacterDataHandler(
1118 void *userData, const XML_Char *s, int len )
1119 /*****************************************************************************/
1121 (( SimpleXMLParser * ) userData )->CharacterData( s, len );
1124 /*****************************************************************************/
1125 void SimpleXMLParser::CommentHandler(
1126 void *userData, const XML_Char *data )
1127 /*****************************************************************************/
1129 (( SimpleXMLParser * ) userData )->Comment( data );
1132 /*****************************************************************************/
1133 void SimpleXMLParser::DefaultHandler(
1134 void *userData, const XML_Char *s, int len )
1135 /*****************************************************************************/
1137 (( SimpleXMLParser * ) userData )->Default( s, len );
1140 /*****************************************************************************/
1141 void SimpleXMLParser::StartElement(
1142 const XML_Char *name, const XML_Char **atts )
1143 /*****************************************************************************/
1145 String sElementName = String( XML_CHAR_TO_OUSTRING( name ));
1146 XMLElement *pElement = new XMLElement( sElementName, ( XMLParentNode * ) pCurNode );
1147 pCurNode = pElement;
1148 pCurData = NULL;
1150 int i = 0;
1151 while( atts[i] ) {
1152 pElement->AddAttribute(
1153 String( XML_CHAR_TO_OUSTRING( atts[ i ] )),
1154 String( XML_CHAR_TO_OUSTRING( atts[ i + 1 ] )));
1155 i += 2;
1159 /*****************************************************************************/
1160 void SimpleXMLParser::EndElement( const XML_Char *name )
1161 /*****************************************************************************/
1163 // This variable is not used at all, but the the sax C interface can't be changed
1164 // To prevent warnings this dummy assignment is used
1165 // +++
1166 (void) name;
1168 pCurNode = pCurNode->GetParent();
1169 pCurData = NULL;
1172 /*****************************************************************************/
1173 void SimpleXMLParser::CharacterData(
1174 const XML_Char *s, int len )
1175 /*****************************************************************************/
1177 if ( !pCurData ){
1178 String x=String( XML_CHAR_N_TO_OUSTRING( s, len ));
1179 XMLUtil::UnQuotHTML(x);
1180 pCurData = new XMLData( x , pCurNode );
1181 }else{
1182 String x=String( XML_CHAR_N_TO_OUSTRING( s, len ));
1183 XMLUtil::UnQuotHTML(x);
1184 pCurData->AddData( x );
1189 /*****************************************************************************/
1190 void SimpleXMLParser::Comment(
1191 const XML_Char *data )
1192 /*****************************************************************************/
1194 pCurData = NULL;
1195 new XMLComment( String( XML_CHAR_TO_OUSTRING( data )), pCurNode );
1198 /*****************************************************************************/
1199 void SimpleXMLParser::Default(
1200 const XML_Char *s, int len )
1201 /*****************************************************************************/
1203 pCurData = NULL;
1204 new XMLDefault(
1205 String( XML_CHAR_N_TO_OUSTRING( s, len )), pCurNode );
1208 /*****************************************************************************/
1209 XMLFile *SimpleXMLParser::Execute( const String &rFullFileName , const String &rFileName, XMLFile* pXMLFileIn )
1210 /*****************************************************************************/
1212 // printf("DBG: SimpleXMLParser::Execute( %s )", ByteString( rFileName , RTL_TEXTENCODING_ASCII_US ).GetBuffer() );
1213 aErrorInformation.eCode = XML_ERROR_NONE;
1214 aErrorInformation.nLine = 0;
1215 aErrorInformation.nColumn = 0;
1216 aErrorInformation.sMessage = String::CreateFromAscii( "ERROR: Unable to open file " );
1217 aErrorInformation.sMessage += rFileName;
1219 SvFileStream aStream( rFileName, STREAM_STD_READ );
1221 if ( !aStream.IsOpen())
1222 return NULL;
1224 SvMemoryStream aMemStream;
1225 aStream >> aMemStream;
1226 aMemStream.Seek( 0 );
1228 aStream.Close();
1230 pXMLFile = pXMLFileIn;
1231 pXMLFile->SetName( rFileName );
1232 pXMLFile->SetFullName( rFullFileName );
1234 return Execute( &aMemStream );
1237 /*****************************************************************************/
1238 XMLFile *SimpleXMLParser::Execute( SvMemoryStream *pStream )
1239 /*****************************************************************************/
1241 if ( !pXMLFile )
1242 pXMLFile = new XMLFile( String());
1244 pCurNode = pXMLFile;
1245 pCurData = NULL;
1247 ULONG nPos = pStream->Tell();
1248 pStream->Seek( STREAM_SEEK_TO_END );
1250 aErrorInformation.eCode = XML_ERROR_NONE;
1251 aErrorInformation.nLine = 0;
1252 aErrorInformation.nColumn = 0;
1253 if ( pXMLFile->GetName().Len()) {
1254 aErrorInformation.sMessage = String::CreateFromAscii( "File " );
1255 aErrorInformation.sMessage += pXMLFile->GetName();
1256 aErrorInformation.sMessage += String::CreateFromAscii( " parsed succesfully" );
1258 else
1259 aErrorInformation.sMessage = String::CreateFromAscii( "XML-File parsed successfully" );
1261 if ( !XML_Parse(
1262 aParser, ( char * ) pStream->GetData() + nPos, pStream->Tell() - nPos, TRUE ))
1264 aErrorInformation.eCode = XML_GetErrorCode( aParser );
1265 aErrorInformation.nLine = XML_GetErrorLineNumber( aParser );
1266 aErrorInformation.nColumn = XML_GetErrorColumnNumber( aParser );
1268 aErrorInformation.sMessage = String::CreateFromAscii( "ERROR: " );
1269 if ( pXMLFile->GetName().Len())
1270 aErrorInformation.sMessage += pXMLFile->GetName();
1271 else
1272 aErrorInformation.sMessage += String::CreateFromAscii( "XML-File" );
1273 aErrorInformation.sMessage += String::CreateFromAscii( " (" );
1274 aErrorInformation.sMessage += String::CreateFromInt64( aErrorInformation.nLine );
1275 aErrorInformation.sMessage += String::CreateFromAscii( "," );
1276 aErrorInformation.sMessage += String::CreateFromInt64( aErrorInformation.nColumn );
1277 aErrorInformation.sMessage += String::CreateFromAscii( "): " );
1279 switch( aErrorInformation.eCode ) {
1280 case XML_ERROR_NO_MEMORY: aErrorInformation.sMessage += String::CreateFromAscii( "No memory" ); break;
1281 case XML_ERROR_SYNTAX: aErrorInformation.sMessage += String::CreateFromAscii( "Syntax" ); break;
1282 case XML_ERROR_NO_ELEMENTS: aErrorInformation.sMessage += String::CreateFromAscii( "No elements" ); break;
1283 case XML_ERROR_INVALID_TOKEN: aErrorInformation.sMessage += String::CreateFromAscii( "Invalid token" ); break;
1284 case XML_ERROR_UNCLOSED_TOKEN: aErrorInformation.sMessage += String::CreateFromAscii( "Unclosed token" ); break;
1285 case XML_ERROR_PARTIAL_CHAR: aErrorInformation.sMessage += String::CreateFromAscii( "Partial char" ); break;
1286 case XML_ERROR_TAG_MISMATCH: aErrorInformation.sMessage += String::CreateFromAscii( "Tag mismatch" ); break;
1287 case XML_ERROR_DUPLICATE_ATTRIBUTE: aErrorInformation.sMessage += String::CreateFromAscii( "Dublicat attribute" ); break;
1288 case XML_ERROR_JUNK_AFTER_DOC_ELEMENT: aErrorInformation.sMessage += String::CreateFromAscii( "Junk after doc element" ); break;
1289 case XML_ERROR_PARAM_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Param entity ref" ); break;
1290 case XML_ERROR_UNDEFINED_ENTITY: aErrorInformation.sMessage += String::CreateFromAscii( "Undefined entity" ); break;
1291 case XML_ERROR_RECURSIVE_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Recursive entity ref" ); break;
1292 case XML_ERROR_ASYNC_ENTITY: aErrorInformation.sMessage += String::CreateFromAscii( "Async_entity" ); break;
1293 case XML_ERROR_BAD_CHAR_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Bad char ref" ); break;
1294 case XML_ERROR_BINARY_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Binary entity" ); break;
1295 case XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF: aErrorInformation.sMessage += String::CreateFromAscii( "Attribute external entity ref" ); break;
1296 case XML_ERROR_MISPLACED_XML_PI: aErrorInformation.sMessage += String::CreateFromAscii( "Misplaced xml pi" ); break;
1297 case XML_ERROR_UNKNOWN_ENCODING: aErrorInformation.sMessage += String::CreateFromAscii( "Unknown encoding" ); break;
1298 case XML_ERROR_INCORRECT_ENCODING: aErrorInformation.sMessage += String::CreateFromAscii( "Incorrect encoding" ); break;
1299 case XML_ERROR_UNCLOSED_CDATA_SECTION: aErrorInformation.sMessage += String::CreateFromAscii( "Unclosed cdata section" ); break;
1300 case XML_ERROR_EXTERNAL_ENTITY_HANDLING: aErrorInformation.sMessage += String::CreateFromAscii( "External entity handling" ); break;
1301 case XML_ERROR_NOT_STANDALONE: aErrorInformation.sMessage += String::CreateFromAscii( "Not standalone" ); break;
1302 case XML_ERROR_NONE: break;
1303 default:
1304 break;
1307 delete pXMLFile;
1308 pXMLFile = NULL;
1310 pStream->Seek( nPos );
1312 return pXMLFile;
1315 /*****************************************************************************/
1316 void XMLUtil::QuotHTML( String &rString )
1317 /*****************************************************************************/
1319 OUStringBuffer sReturn;
1320 static const String LT(String::CreateFromAscii("<"));
1321 static const String QLT(String::CreateFromAscii("&lt;"));
1322 static const String GT(String::CreateFromAscii(">"));
1323 static const String QGT(String::CreateFromAscii("&gt;"));
1324 static const String QUOT(String::CreateFromAscii("\\"));
1325 static const String QQUOT(String::CreateFromAscii("&quot;"));
1326 static const String APOS(String::CreateFromAscii("\""));
1327 static const String QAPOS(String::CreateFromAscii("&apos;"));
1328 static const String AMP(String::CreateFromAscii("&"));
1329 static const String QAMP(String::CreateFromAscii("&amp;"));
1330 static const String SLASH(String::CreateFromAscii("\\"));
1332 for ( USHORT i = 0; i < rString.Len(); i++) {
1333 if ( i < rString.Len()) {
1334 switch ( rString.GetChar( i )) {
1335 case '\\': if( i+1 <= rString.Len() ){
1336 switch( rString.GetChar( i+1 ) ){
1337 case '<': sReturn.append( LT );i++;break;
1338 case '>': sReturn.append( GT );i++;break;
1339 case '\\': sReturn.append( QUOT );i++;break;
1340 case '\"': sReturn.append( APOS );i++;break;
1341 //case '\'': sReturn += "\'";i++;break;
1342 //case '&' : sRetrun += "&";i++;break;
1343 default: sReturn.append( SLASH );break;
1347 break;
1349 case '<':
1350 sReturn.append( QLT );
1351 break;
1353 case '>':
1354 sReturn.append( QGT );
1355 break;
1357 case '\"':
1358 sReturn.append( QQUOT );
1359 break;
1361 /* case '\'':
1362 sReturn += "&apos;";
1363 break;
1365 case '&':
1366 if (
1367 ( ( i + 4 ) < rString.Len()) &&
1368 ( String( rString.Copy( i, 5 ) ).Equals( QAMP ) )
1370 sReturn.append( rString.GetChar( i ) );
1371 else
1372 sReturn.append( QAMP );
1373 break;
1375 default:
1376 sReturn.append( rString.GetChar( i ) );
1377 break;
1381 rString = String( sReturn.makeStringAndClear() );
1384 void XMLUtil::UnQuotHTML( String &rString ){
1385 UnQuotData( rString );
1388 void XMLUtil::UnQuotData( String &rString_in ){
1389 ByteString sReturn;
1390 ByteString sString( rString_in , RTL_TEXTENCODING_UTF8 );
1391 while ( sString.Len()) {
1392 if ( sString.Copy( 0, 1 ) == "\\" ) {
1393 sReturn += "\\\\";
1394 sString.Erase( 0, 1 );
1396 else if ( sString.Copy( 0, 5 ) == "&amp;" ) {
1397 sReturn += "&";
1398 sString.Erase( 0, 5 );
1400 else if ( sString.Copy( 0, 4 ) == "&lt;" ) {
1401 sReturn += "<";
1402 sString.Erase( 0, 4 );
1404 else if ( sString.Copy( 0, 4 ) == "&gt;" ) {
1405 sReturn += ">";
1406 sString.Erase( 0, 4 );
1408 else if ( sString.Copy( 0, 6 ) == "&quot;" ) {
1409 sReturn += "\"";
1410 sString.Erase( 0, 6 );
1412 else if ( sString.Copy( 0, 6 ) == "&apos;" ) {
1413 sReturn += "\'";
1414 sString.Erase( 0, 6 );
1416 else {
1417 sReturn += sString.GetChar( 0 );
1418 sString.Erase( 0, 1 );
1421 rString_in = String(sReturn , RTL_TEXTENCODING_UTF8 );
1426 XMLUtil::XMLUtil(){
1430 /*****************************************************************************/
1431 void XMLUtil::dump(){
1432 /*****************************************************************************/
1433 int cnt=1;
1434 printf("size=%lu\n",static_cast<unsigned long>(lMap.size()));
1435 for(HashMap::iterator pos = lMap.begin(); pos != lMap.end() ; ++pos){
1436 fprintf(stdout,"key=%s , value=%d , no=%d\n",pos->first.GetBuffer(),pos->second,cnt++);
1439 /*****************************************************************************/
1440 XMLUtil& XMLUtil::Instance(){
1441 /*****************************************************************************/
1442 static XMLUtil instance;
1443 return instance;
1445 /*****************************************************************************/
1446 XMLUtil::~XMLUtil(){}
1447 /*****************************************************************************/
1448 /*****************************************************************************/
1449 ByteString XMLUtil::GetIsoLangByIndex( USHORT nIndex )
1450 /*****************************************************************************/
1452 if(nIndex > 0 && MAX_LANGUAGES >= nIndex )
1453 return isoArray[nIndex];
1454 return "";