xref: /trunk/main/l10ntools/layout/layoutparse.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include "layoutparse.hxx"
29 
30 #define STRING( str ) String( str, RTL_TEXTENCODING_UTF8 )
31 #define BSTRING( str ) ByteString( str, RTL_TEXTENCODING_UTF8 )
32 
33 LayoutXMLFile::LayoutXMLFile( bool mergeMode )
34     : XMLFile()
35     , mMergeMode( mergeMode )
36 {
37 }
38 
39 void
40 LayoutXMLFile::SearchL10NElements( XMLParentNode* pCur, int )
41 {
42     if ( !pCur )
43         pCur = this;
44 
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-- );
52 }
53 
54 std::vector<XMLAttribute*>
55 interestingAttributes( XMLAttributeList* lst )
56 {
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;
65 }
66 
67 void
68 LayoutXMLFile::HandleElement( XMLElement* element )
69 {
70     std::vector<XMLAttribute*> interesting = interestingAttributes( element->GetAttributeList() );
71 
72     if ( interesting.size() )
73     {
74         std::vector<XMLAttribute*>::iterator i = interesting.begin();
75 
76         ByteString id = BSTRING( (*i++)->GetValue() );
77 
78         if ( mMergeMode )
79             InsertL10NElement( id, element );
80         else
81             for ( ; i != interesting.end(); ++i )
82             {
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 );
92             }
93     }
94 
95     SearchL10NElements( (XMLParentNode*) element );
96 }
97 
98 void LayoutXMLFile::InsertL10NElement( ByteString const& id, XMLElement* element )
99 {
100     ByteString const language = "en-US";
101     LangHashMap* languageMap = 0;
102     XMLHashMap::iterator pos = XMLStrings->find( id );
103     if ( pos != XMLStrings->end() )
104     {
105         languageMap = pos->second;
106         fprintf( stderr, "error:%s:duplicate translation found, id=%s\n",
107                  id.GetBuffer(), BSTRING( sFileName ).GetBuffer() );
108         exit( 1 );
109     }
110     else
111     {
112         languageMap = new LangHashMap();
113         XMLStrings->insert( XMLHashMap::value_type( id , languageMap ) );
114         order.push_back( id );
115     }
116     (*languageMap)[ language ] = element;
117 }
118 
119 sal_Bool LayoutXMLFile::Write( ByteString &aFilename )
120 {
121 
122     if ( aFilename.Len() )
123     {
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
128         {
129             XMLFile::Write( aFStream );
130             aFStream.close();
131             return true;
132         }
133     }
134     return false;
135 }
136