1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
25 #include <osl/file.hxx>
26 #include <sal/log.hxx>
30 #include "helpmerge.hxx"
32 #include <sys/types.h>
37 #include <rtl/strbuf.hxx>
47 #if OSL_DEBUG_LEVEL > 2
48 void HelpParser::Dump(XMLHashMap
* rElem_in
)
50 for(XMLHashMap::iterator pos
= rElem_in
->begin();pos
!= rElem_in
->end(); ++pos
)
52 Dump(pos
->second
,pos
->first
);
56 void HelpParser::Dump(LangHashMap
* rElem_in
,const rtl::OString
& sKey_in
)
60 fprintf(stdout
,"+------------%s-----------+\n",sKey_in
.getStr() );
61 for(LangHashMap::iterator posn
=rElem_in
->begin();posn
!=rElem_in
->end();++posn
)
64 y
=posn
->second
->ToOString();
65 fprintf(stdout
,"key=%s value=%s\n",x
.getStr(),y
.getStr());
67 fprintf(stdout
,"+--------------------------+\n");
71 HelpParser::HelpParser( const rtl::OString
&rHelpFile
)
72 : sHelpFile( rHelpFile
)
75 /*****************************************************************************/
76 bool HelpParser::CreateSDF(
77 /*****************************************************************************/
78 const rtl::OString
&rSDFFile_in
, const rtl::OString
&rPrj_in
,const rtl::OString
&rRoot_in
,
79 const rtl::OString
&sHelpFile
, XMLFile
*pXmlFile
, const rtl::OString
&rGsi1
){
80 SimpleXMLParser aParser
;
81 rtl::OUString
sXmlFile(
82 rtl::OStringToOUString(sHelpFile
, RTL_TEXTENCODING_ASCII_US
));
83 //TODO: explicit BOM handling?
85 std::auto_ptr
<XMLFile
> file ( aParser
.Execute( sXmlFile
, pXmlFile
) );
87 if(file
.get() == NULL
)
92 (rtl::OUStringToOString(
93 aParser
.GetError().sMessage
, RTL_TEXTENCODING_ASCII_US
).
98 if( !file
->CheckExportStatus() ){
101 std::ofstream
aSDFStream(
102 rSDFFile_in
.getStr(), std::ios_base::out
| std::ios_base::trunc
);
104 if (!aSDFStream
.is_open()) {
105 fprintf(stdout
,"Can't open file %s\n",rSDFFile_in
.getStr());
109 rtl::OString
sActFileName(
110 common::pathnameToken(sHelpFile
.getStr(), rRoot_in
.getStr()));
112 XMLHashMap
* aXMLStrHM
= file
->GetStrings();
114 XMLElement
* pXMLElement
= NULL
;
116 OUStringBuffer sBuffer
;
117 const OUString
sOUPrj( rPrj_in
.getStr() , rPrj_in
.getLength() , RTL_TEXTENCODING_ASCII_US
);
118 const OUString
sOUActFileName(sActFileName
.getStr() , sActFileName
.getLength() , RTL_TEXTENCODING_ASCII_US
);
119 const OUString
sOUGsi1( rGsi1
.getStr() , rGsi1
.getLength() , RTL_TEXTENCODING_ASCII_US
);
121 Export::InitLanguages( false );
122 std::vector
<rtl::OString
> aLanguages
= Export::GetLanguages();
124 std::vector
<rtl::OString
> order
= file
->getOrder();
125 std::vector
<rtl::OString
>::iterator pos
;
126 XMLHashMap::iterator posm
;
128 for( pos
= order
.begin(); pos
!= order
.end() ; ++pos
)
130 posm
= aXMLStrHM
->find( *pos
);
131 pElem
= posm
->second
;
134 for( unsigned int n
= 0; n
< aLanguages
.size(); n
++ )
136 sCur
= aLanguages
[ n
];
137 pXMLElement
= (*pElem
)[ sCur
];
139 if( pXMLElement
!= NULL
)
142 pXMLElement
->ToOUString().
144 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n")),
147 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t")),
148 rtl::OUString()).trim());
149 sBuffer
.append( sOUPrj
);
150 sBuffer
.append('\t');
151 if ( !rRoot_in
.isEmpty())
152 sBuffer
.append( sOUActFileName
);
153 sBuffer
.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t0\t"));
154 sBuffer
.append( sOUGsi1
); //"help";
155 sBuffer
.append('\t');
156 rtl::OString sID
= posm
->first
; // ID
157 sBuffer
.append( rtl::OStringToOUString( sID
, RTL_TEXTENCODING_UTF8
) );
158 sBuffer
.append('\t');
159 rtl::OString sOldRef
= pXMLElement
->GetOldref(); // oldref
160 sBuffer
.append( rtl::OStringToOUString(sOldRef
, RTL_TEXTENCODING_UTF8
) );
161 sBuffer
.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t\t\t0\t"));
162 sBuffer
.append( rtl::OStringToOUString( sCur
, RTL_TEXTENCODING_UTF8
) );
163 sBuffer
.append('\t');
164 sBuffer
.append( data
);
165 sBuffer
.appendAscii(RTL_CONSTASCII_STRINGPARAM("\t\t\t\t"));
166 rtl::OString
sOut(rtl::OUStringToOString(sBuffer
.makeStringAndClear().getStr() , RTL_TEXTENCODING_UTF8
));
167 if( !data
.isEmpty() )
168 aSDFStream
<< sOut
.getStr() << '\n';
173 fprintf(stdout
,"\nDBG: NullPointer in HelpParser::CreateSDF, Language %s, File %s\n", sCur
.getStr(), sHelpFile
.getStr());
182 bool HelpParser::Merge( const rtl::OString
&rSDFFile
, const rtl::OString
&rDestinationFile
,
183 const rtl::OString
& rLanguage
, MergeDataFile
& aMergeDataFile
)
187 bool hasNoError
= true;
189 SimpleXMLParser aParser
;
191 rtl::OUString
sXmlFile(
192 rtl::OStringToOUString(sHelpFile
, RTL_TEXTENCODING_ASCII_US
));
193 //TODO: explicit BOM handling?
195 XMLFile
* xmlfile
= ( aParser
.Execute( sXmlFile
, new XMLFile( rtl::OUString('0') ) ) );
196 hasNoError
= MergeSingleFile( xmlfile
, aMergeDataFile
, rLanguage
, rDestinationFile
);
201 bool HelpParser::MergeSingleFile( XMLFile
* file
, MergeDataFile
& aMergeDataFile
, const rtl::OString
& sLanguage
,
202 rtl::OString
const & sPath
)
206 XMLHashMap
* aXMLStrHM
= file
->GetStrings();
207 LangHashMap
* aLangHM
;
208 static ResData
pResData( "","","");
209 pResData
.sResTyp
= "help";
211 for(XMLHashMap::iterator pos
=aXMLStrHM
->begin();pos
!=aXMLStrHM
->end();++pos
) // Merge every l10n related string
214 aLangHM
= pos
->second
;
215 #if OSL_DEBUG_LEVEL > 2
216 printf("*********************DUMPING HASHMAP***************************************");
218 printf("DBG: sHelpFile = %s\n",sHelpFile
.getStr() );
221 pResData
.sGId
= pos
->first
;
222 pResData
.sFilename
= sHelpFile
;
224 ProcessHelp( aLangHM
, sLanguage
, &pResData
, aMergeDataFile
);
231 /* ProcessHelp Methode: search for en-US entry and replace it with the current language*/
232 void HelpParser::ProcessHelp( LangHashMap
* aLangHM
, const rtl::OString
& sCur
, ResData
*pResData
, MergeDataFile
& aMergeDataFile
){
234 XMLElement
* pXMLElement
= NULL
;
235 PFormEntrys
*pEntrys
= NULL
;
236 XMLData
*data
= NULL
;
243 if( !sCur
.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")) ){
244 pXMLElement
= (*aLangHM
)[ "en-US" ];
245 if( pXMLElement
== NULL
)
247 printf("Error: Can't find en-US entry\n");
249 if( pXMLElement
!= NULL
)
251 sLId
= pXMLElement
->GetOldref();
252 pResData
->sId
= sLId
;
254 pEntrys
= aMergeDataFile
.GetPFormEntrys( pResData
);
257 rtl::OString sNewText
;
258 rtl::OUString
sSourceText(
259 pXMLElement
->ToOUString().
261 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n")),
264 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t")),
266 // re-add spaces to the beginning of translated string,
267 // important for indentation of Basic code examples
268 sal_Int32 nPreSpaces
= 0;
269 sal_Int32 nLen
= sSourceText
.getLength();
270 while ( (nPreSpaces
< nLen
) && (*(sSourceText
.getStr()+nPreSpaces
) == ' ') )
272 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, sCur
, true );
274 if (helper::isWellFormedXML(helper::QuotHTML(sNewText
)))
276 sNewdata
= sSourceText
.copy(0,nPreSpaces
) +
277 rtl::OStringToOUString(sNewText
, RTL_TEXTENCODING_UTF8
);
281 sNewdata
= sSourceText
;
283 if (!sNewdata
.isEmpty())
285 if( pXMLElement
!= NULL
)
287 data
= new XMLData( sNewdata
, NULL
, true ); // Add new one
288 pXMLElement
->RemoveAndDeleteAllChildren();
289 pXMLElement
->AddChild( data
);
290 aLangHM
->erase( sCur
);
294 else if( pResData
== NULL
)
296 fprintf(stdout
,"Can't find GID=%s LID=%s TYP=%s\n",
297 pResData
->sGId
.getStr(), pResData
->sId
.getStr(),
298 pResData
->sResTyp
.getStr());
300 pXMLElement
->ChangeLanguageTag(
301 rtl::OStringToOUString(sCur
, RTL_TEXTENCODING_ASCII_US
));
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */