update dev300-m58
[ooovba.git] / transex3 / layout / layoutparse.cxx
blob35fa355fbd6102ad13a53e640f4734dd1facea52
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: layoutparse.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "layoutparse.hxx"
34 #define STRING( str ) String( str, RTL_TEXTENCODING_UTF8 )
35 #define BSTRING( str ) ByteString( str, RTL_TEXTENCODING_UTF8 )
37 LayoutXMLFile::LayoutXMLFile( bool mergeMode )
38 : XMLFile()
39 , mMergeMode( mergeMode )
43 void
44 LayoutXMLFile::SearchL10NElements( XMLParentNode* pCur, int )
46 if ( !pCur )
47 pCur = this;
49 /* Recurse int children, SearchL10NElements does not do that for us. */
50 if ( XMLChildNodeList* lst = pCur->GetChildList() )
51 for ( ULONG i = 0; i < lst->Count(); i++ )
52 if ( lst->GetObject( i )->GetNodeType() == XML_NODE_TYPE_ELEMENT )
53 HandleElement( ( XMLElement* )lst->GetObject( i ) );
54 else if ( lst->GetObject( i )->GetNodeType() == XML_NODE_TYPE_COMMENT )
55 lst->Remove( i-- );
58 std::vector<XMLAttribute*>
59 interestingAttributes( XMLAttributeList* lst )
61 std::vector<XMLAttribute*> interesting;
62 if ( lst )
63 for ( ULONG i = 0; i < lst->Count(); i++ )
64 if ( lst->GetObject( i )->Equals( STRING( "id" ) ) )
65 interesting.insert( interesting.begin(), lst->GetObject( i ) );
66 else if ( ! BSTRING( *lst->GetObject( i ) ).CompareTo( "_", 1 ) )
67 interesting.push_back( lst->GetObject( i ) );
68 return interesting;
71 void
72 LayoutXMLFile::HandleElement( XMLElement* element )
74 std::vector<XMLAttribute*> interesting = interestingAttributes( element->GetAttributeList() );
76 if ( interesting.size() )
78 std::vector<XMLAttribute*>::iterator i = interesting.begin();
80 ByteString id = BSTRING( (*i++)->GetValue() );
82 if ( mMergeMode )
83 InsertL10NElement( id, element );
84 else
85 for ( ; i != interesting.end(); ++i )
87 ByteString attributeId = id;
88 ByteString value = BSTRING( ( *i )->GetValue() );
89 XMLElement *e = new XMLElement( *element );
90 e->RemoveAndDeleteAllChilds();
91 /* Copy translatable text to CONTENT. */
92 //new XMLData( STRING( ( *i )->GetValue() ), e, true );
93 new XMLData( STRING( value ), e, true );
94 attributeId += BSTRING ( **i );
95 InsertL10NElement( attributeId, e );
99 SearchL10NElements( (XMLParentNode*) element );
102 void LayoutXMLFile::InsertL10NElement( ByteString const& id, XMLElement* element )
104 ByteString const language = "en-US";
105 LangHashMap* languageMap = 0;
106 XMLHashMap::iterator pos = XMLStrings->find( id );
107 if ( pos != XMLStrings->end() )
109 languageMap = pos->second;
110 fprintf( stderr, "error:%s:duplicate translation found, id=%s\n",
111 id.GetBuffer(), BSTRING( sFileName ).GetBuffer() );
112 exit( 1 );
114 else
116 languageMap = new LangHashMap();
117 XMLStrings->insert( XMLHashMap::value_type( id , languageMap ) );
118 order.push_back( id );
120 (*languageMap)[ language ] = element;
123 BOOL LayoutXMLFile::Write( ByteString &aFilename )
126 if ( aFilename.Len() )
128 ofstream aFStream( aFilename.GetBuffer() , ios::out | ios::trunc );
129 if ( !aFStream )
130 fprintf( stderr, "ERROR: cannot open file:%s\n", aFilename.GetBuffer() );
131 else
133 XMLFile::Write( aFStream );
134 aFStream.close();
135 return true;
138 return false;