1*3cd96b95SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*3cd96b95SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*3cd96b95SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*3cd96b95SAndrew Rist * distributed with this work for additional information 6*3cd96b95SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*3cd96b95SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*3cd96b95SAndrew Rist * "License"); you may not use this file except in compliance 9*3cd96b95SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*3cd96b95SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*3cd96b95SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*3cd96b95SAndrew Rist * software distributed under the License is distributed on an 15*3cd96b95SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*3cd96b95SAndrew Rist * KIND, either express or implied. See the License for the 17*3cd96b95SAndrew Rist * specific language governing permissions and limitations 18*3cd96b95SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*3cd96b95SAndrew Rist *************************************************************/ 21*3cd96b95SAndrew Rist 22*3cd96b95SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "layoutparse.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #define STRING( str ) String( str, RTL_TEXTENCODING_UTF8 ) 27cdf0e10cSrcweir #define BSTRING( str ) ByteString( str, RTL_TEXTENCODING_UTF8 ) 28cdf0e10cSrcweir 29cdf0e10cSrcweir LayoutXMLFile::LayoutXMLFile( bool mergeMode ) 30cdf0e10cSrcweir : XMLFile() 31cdf0e10cSrcweir , mMergeMode( mergeMode ) 32cdf0e10cSrcweir { 33cdf0e10cSrcweir } 34cdf0e10cSrcweir 35cdf0e10cSrcweir void 36cdf0e10cSrcweir LayoutXMLFile::SearchL10NElements( XMLParentNode* pCur, int ) 37cdf0e10cSrcweir { 38cdf0e10cSrcweir if ( !pCur ) 39cdf0e10cSrcweir pCur = this; 40cdf0e10cSrcweir 41cdf0e10cSrcweir /* Recurse int children, SearchL10NElements does not do that for us. */ 42cdf0e10cSrcweir if ( XMLChildNodeList* lst = pCur->GetChildList() ) 43cdf0e10cSrcweir for ( sal_uLong i = 0; i < lst->Count(); i++ ) 44cdf0e10cSrcweir if ( lst->GetObject( i )->GetNodeType() == XML_NODE_TYPE_ELEMENT ) 45cdf0e10cSrcweir HandleElement( ( XMLElement* )lst->GetObject( i ) ); 46cdf0e10cSrcweir else if ( lst->GetObject( i )->GetNodeType() == XML_NODE_TYPE_COMMENT ) 47cdf0e10cSrcweir lst->Remove( i-- ); 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir std::vector<XMLAttribute*> 51cdf0e10cSrcweir interestingAttributes( XMLAttributeList* lst ) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir std::vector<XMLAttribute*> interesting; 54cdf0e10cSrcweir if ( lst ) 55cdf0e10cSrcweir for ( sal_uLong i = 0; i < lst->Count(); i++ ) 56cdf0e10cSrcweir if ( lst->GetObject( i )->Equals( STRING( "id" ) ) ) 57cdf0e10cSrcweir interesting.insert( interesting.begin(), lst->GetObject( i ) ); 58cdf0e10cSrcweir else if ( ! BSTRING( *lst->GetObject( i ) ).CompareTo( "_", 1 ) ) 59cdf0e10cSrcweir interesting.push_back( lst->GetObject( i ) ); 60cdf0e10cSrcweir return interesting; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir void 64cdf0e10cSrcweir LayoutXMLFile::HandleElement( XMLElement* element ) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir std::vector<XMLAttribute*> interesting = interestingAttributes( element->GetAttributeList() ); 67cdf0e10cSrcweir 68cdf0e10cSrcweir if ( interesting.size() ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir std::vector<XMLAttribute*>::iterator i = interesting.begin(); 71cdf0e10cSrcweir 72cdf0e10cSrcweir ByteString id = BSTRING( (*i++)->GetValue() ); 73cdf0e10cSrcweir 74cdf0e10cSrcweir if ( mMergeMode ) 75cdf0e10cSrcweir InsertL10NElement( id, element ); 76cdf0e10cSrcweir else 77cdf0e10cSrcweir for ( ; i != interesting.end(); ++i ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir ByteString attributeId = id; 80cdf0e10cSrcweir ByteString value = BSTRING( ( *i )->GetValue() ); 81cdf0e10cSrcweir XMLElement *e = new XMLElement( *element ); 82cdf0e10cSrcweir e->RemoveAndDeleteAllChilds(); 83cdf0e10cSrcweir /* Copy translatable text to CONTENT. */ 84cdf0e10cSrcweir //new XMLData( STRING( ( *i )->GetValue() ), e, true ); 85cdf0e10cSrcweir new XMLData( STRING( value ), e, true ); 86cdf0e10cSrcweir attributeId += BSTRING ( **i ); 87cdf0e10cSrcweir InsertL10NElement( attributeId, e ); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir SearchL10NElements( (XMLParentNode*) element ); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir void LayoutXMLFile::InsertL10NElement( ByteString const& id, XMLElement* element ) 95cdf0e10cSrcweir { 96cdf0e10cSrcweir ByteString const language = "en-US"; 97cdf0e10cSrcweir LangHashMap* languageMap = 0; 98cdf0e10cSrcweir XMLHashMap::iterator pos = XMLStrings->find( id ); 99cdf0e10cSrcweir if ( pos != XMLStrings->end() ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir languageMap = pos->second; 102cdf0e10cSrcweir fprintf( stderr, "error:%s:duplicate translation found, id=%s\n", 103cdf0e10cSrcweir id.GetBuffer(), BSTRING( sFileName ).GetBuffer() ); 104cdf0e10cSrcweir exit( 1 ); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir else 107cdf0e10cSrcweir { 108cdf0e10cSrcweir languageMap = new LangHashMap(); 109cdf0e10cSrcweir XMLStrings->insert( XMLHashMap::value_type( id , languageMap ) ); 110cdf0e10cSrcweir order.push_back( id ); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir (*languageMap)[ language ] = element; 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir sal_Bool LayoutXMLFile::Write( ByteString &aFilename ) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir 118cdf0e10cSrcweir if ( aFilename.Len() ) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir ofstream aFStream( aFilename.GetBuffer() , ios::out | ios::trunc ); 121cdf0e10cSrcweir if ( !aFStream ) 122cdf0e10cSrcweir fprintf( stderr, "ERROR: cannot open file:%s\n", aFilename.GetBuffer() ); 123cdf0e10cSrcweir else 124cdf0e10cSrcweir { 125cdf0e10cSrcweir XMLFile::Write( aFStream ); 126cdf0e10cSrcweir aFStream.close(); 127cdf0e10cSrcweir return true; 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir return false; 131cdf0e10cSrcweir } 132