1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: layoutparse.cxx,v $
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
)
39 , mMergeMode( mergeMode
)
44 LayoutXMLFile::SearchL10NElements( XMLParentNode
* pCur
, int )
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
)
58 std::vector
<XMLAttribute
*>
59 interestingAttributes( XMLAttributeList
* lst
)
61 std::vector
<XMLAttribute
*> interesting
;
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
) );
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() );
83 InsertL10NElement( id
, element
);
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() );
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
);
130 fprintf( stderr
, "ERROR: cannot open file:%s\n", aFilename
.GetBuffer() );
133 XMLFile::Write( aFStream
);