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