1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "layoutparse.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #define STRING( str ) String( str, RTL_TEXTENCODING_UTF8 ) 31*cdf0e10cSrcweir #define BSTRING( str ) ByteString( str, RTL_TEXTENCODING_UTF8 ) 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir LayoutXMLFile::LayoutXMLFile( bool mergeMode ) 34*cdf0e10cSrcweir : XMLFile() 35*cdf0e10cSrcweir , mMergeMode( mergeMode ) 36*cdf0e10cSrcweir { 37*cdf0e10cSrcweir } 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir void 40*cdf0e10cSrcweir LayoutXMLFile::SearchL10NElements( XMLParentNode* pCur, int ) 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir if ( !pCur ) 43*cdf0e10cSrcweir pCur = this; 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir /* Recurse int children, SearchL10NElements does not do that for us. */ 46*cdf0e10cSrcweir if ( XMLChildNodeList* lst = pCur->GetChildList() ) 47*cdf0e10cSrcweir for ( sal_uLong i = 0; i < lst->Count(); i++ ) 48*cdf0e10cSrcweir if ( lst->GetObject( i )->GetNodeType() == XML_NODE_TYPE_ELEMENT ) 49*cdf0e10cSrcweir HandleElement( ( XMLElement* )lst->GetObject( i ) ); 50*cdf0e10cSrcweir else if ( lst->GetObject( i )->GetNodeType() == XML_NODE_TYPE_COMMENT ) 51*cdf0e10cSrcweir lst->Remove( i-- ); 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir std::vector<XMLAttribute*> 55*cdf0e10cSrcweir interestingAttributes( XMLAttributeList* lst ) 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir std::vector<XMLAttribute*> interesting; 58*cdf0e10cSrcweir if ( lst ) 59*cdf0e10cSrcweir for ( sal_uLong i = 0; i < lst->Count(); i++ ) 60*cdf0e10cSrcweir if ( lst->GetObject( i )->Equals( STRING( "id" ) ) ) 61*cdf0e10cSrcweir interesting.insert( interesting.begin(), lst->GetObject( i ) ); 62*cdf0e10cSrcweir else if ( ! BSTRING( *lst->GetObject( i ) ).CompareTo( "_", 1 ) ) 63*cdf0e10cSrcweir interesting.push_back( lst->GetObject( i ) ); 64*cdf0e10cSrcweir return interesting; 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir void 68*cdf0e10cSrcweir LayoutXMLFile::HandleElement( XMLElement* element ) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir std::vector<XMLAttribute*> interesting = interestingAttributes( element->GetAttributeList() ); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir if ( interesting.size() ) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir std::vector<XMLAttribute*>::iterator i = interesting.begin(); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir ByteString id = BSTRING( (*i++)->GetValue() ); 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir if ( mMergeMode ) 79*cdf0e10cSrcweir InsertL10NElement( id, element ); 80*cdf0e10cSrcweir else 81*cdf0e10cSrcweir for ( ; i != interesting.end(); ++i ) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir ByteString attributeId = id; 84*cdf0e10cSrcweir ByteString value = BSTRING( ( *i )->GetValue() ); 85*cdf0e10cSrcweir XMLElement *e = new XMLElement( *element ); 86*cdf0e10cSrcweir e->RemoveAndDeleteAllChilds(); 87*cdf0e10cSrcweir /* Copy translatable text to CONTENT. */ 88*cdf0e10cSrcweir //new XMLData( STRING( ( *i )->GetValue() ), e, true ); 89*cdf0e10cSrcweir new XMLData( STRING( value ), e, true ); 90*cdf0e10cSrcweir attributeId += BSTRING ( **i ); 91*cdf0e10cSrcweir InsertL10NElement( attributeId, e ); 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir SearchL10NElements( (XMLParentNode*) element ); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir void LayoutXMLFile::InsertL10NElement( ByteString const& id, XMLElement* element ) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir ByteString const language = "en-US"; 101*cdf0e10cSrcweir LangHashMap* languageMap = 0; 102*cdf0e10cSrcweir XMLHashMap::iterator pos = XMLStrings->find( id ); 103*cdf0e10cSrcweir if ( pos != XMLStrings->end() ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir languageMap = pos->second; 106*cdf0e10cSrcweir fprintf( stderr, "error:%s:duplicate translation found, id=%s\n", 107*cdf0e10cSrcweir id.GetBuffer(), BSTRING( sFileName ).GetBuffer() ); 108*cdf0e10cSrcweir exit( 1 ); 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir else 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir languageMap = new LangHashMap(); 113*cdf0e10cSrcweir XMLStrings->insert( XMLHashMap::value_type( id , languageMap ) ); 114*cdf0e10cSrcweir order.push_back( id ); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir (*languageMap)[ language ] = element; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir sal_Bool LayoutXMLFile::Write( ByteString &aFilename ) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir if ( aFilename.Len() ) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir ofstream aFStream( aFilename.GetBuffer() , ios::out | ios::trunc ); 125*cdf0e10cSrcweir if ( !aFStream ) 126*cdf0e10cSrcweir fprintf( stderr, "ERROR: cannot open file:%s\n", aFilename.GetBuffer() ); 127*cdf0e10cSrcweir else 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir XMLFile::Write( aFStream ); 130*cdf0e10cSrcweir aFStream.close(); 131*cdf0e10cSrcweir return true; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir return false; 135*cdf0e10cSrcweir } 136