2012-11-27 07:12:22 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/xmlmemory.h>
|
|
|
|
#include <libxml/xmlstring.h>
|
|
|
|
|
|
|
|
#include "export.hxx"
|
2013-03-27 14:08:50 -05:00
|
|
|
#include "helper.hxx"
|
2012-11-27 07:12:22 -06:00
|
|
|
#include "common.hxx"
|
2013-03-15 09:42:05 -05:00
|
|
|
#include "po.hxx"
|
2012-11-27 07:12:22 -06:00
|
|
|
#include "stringmerge.hxx"
|
|
|
|
|
|
|
|
//Parse strings.xml file
|
|
|
|
StringParser::StringParser(
|
|
|
|
const OString& rInputFile, const OString& rLang )
|
|
|
|
: m_pSource( 0 )
|
|
|
|
, m_sLang( rLang )
|
|
|
|
, m_bIsInitialized( false )
|
|
|
|
{
|
|
|
|
m_pSource = xmlParseFile( rInputFile.getStr() );
|
|
|
|
if ( !m_pSource ) {
|
|
|
|
std::cerr
|
|
|
|
<< "Stringx error: Cannot open source file: "
|
|
|
|
<< rInputFile.getStr() << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( !m_pSource->name )
|
|
|
|
{
|
|
|
|
m_pSource->name = new char[strlen(rInputFile.getStr())+1];
|
|
|
|
strcpy( m_pSource->name, rInputFile.getStr() );
|
|
|
|
}
|
|
|
|
m_bIsInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringParser::~StringParser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//Extract strings form source file
|
2013-03-15 09:42:05 -05:00
|
|
|
void StringParser::Extract( const OString& rPOFile )
|
2012-11-27 07:12:22 -06:00
|
|
|
{
|
|
|
|
assert( m_bIsInitialized );
|
2013-03-15 09:42:05 -05:00
|
|
|
PoOfstream aPOStream( rPOFile, PoOfstream::APP );
|
|
|
|
if( !aPOStream.isOpen() )
|
2012-11-27 07:12:22 -06:00
|
|
|
{
|
|
|
|
std::cerr
|
2013-03-15 09:42:05 -05:00
|
|
|
<< "stringex error: Cannot open po file for extract: "
|
|
|
|
<< rPOFile.getStr() << std::endl;
|
2012-11-27 07:12:22 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); // <resource>
|
|
|
|
for( xmlNodePtr pCurrent = pRootNode->children->next;
|
|
|
|
pCurrent; pCurrent = pCurrent->next)
|
|
|
|
{
|
|
|
|
if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
|
|
|
|
{
|
|
|
|
xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
|
|
|
|
xmlChar* pText = xmlNodeGetContent(pCurrent);
|
2013-04-12 22:43:53 -05:00
|
|
|
const OString sTemp =
|
2013-05-26 01:30:22 -05:00
|
|
|
helper::unEscapeAll(helper::xmlStrToOString( pText ),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
|
2013-03-27 14:08:50 -05:00
|
|
|
common::writePoEntry(
|
2013-03-15 09:42:05 -05:00
|
|
|
"Stringex", aPOStream, m_pSource->name, "string",
|
2013-03-27 14:08:50 -05:00
|
|
|
helper::xmlStrToOString( pID ), OString(), OString(),
|
2013-04-12 22:43:53 -05:00
|
|
|
sTemp);
|
2012-11-27 07:12:22 -06:00
|
|
|
|
|
|
|
xmlFree( pID );
|
|
|
|
xmlFree( pText );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlFreeDoc( m_pSource );
|
|
|
|
xmlCleanupParser();
|
2013-03-15 09:42:05 -05:00
|
|
|
aPOStream.close();
|
2012-11-27 07:12:22 -06:00
|
|
|
m_bIsInitialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Merge strings to localized strings.xml file
|
|
|
|
void StringParser::Merge(
|
|
|
|
const OString &rMergeSrc, const OString &rDestinationFile )
|
|
|
|
{
|
|
|
|
assert( m_bIsInitialized );
|
|
|
|
|
2013-04-20 02:20:31 -05:00
|
|
|
MergeDataFile* pMergeDataFile = 0;
|
|
|
|
if( m_sLang != "qtz" )
|
2012-11-27 07:12:22 -06:00
|
|
|
{
|
2013-04-20 02:20:31 -05:00
|
|
|
pMergeDataFile = new MergeDataFile(
|
|
|
|
rMergeSrc, static_cast<OString>( m_pSource->name ), false, false );
|
|
|
|
const std::vector<OString> vLanguages = pMergeDataFile->GetLanguages();
|
|
|
|
if( vLanguages.size()>=1 && vLanguages[0] != m_sLang )
|
|
|
|
{
|
|
|
|
std::cerr
|
|
|
|
<< "stringex error: given language conflicts with "
|
|
|
|
<< "language of Mergedata file: "
|
|
|
|
<< m_sLang.getStr() << " - "
|
|
|
|
<< vLanguages[0].getStr() << std::endl;
|
2013-05-08 13:53:42 -05:00
|
|
|
delete pMergeDataFile;
|
2013-04-20 02:20:31 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-11-27 07:12:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); //<resource>
|
|
|
|
|
|
|
|
for( xmlNodePtr pCurrent = pRootNode->children;
|
|
|
|
pCurrent; pCurrent = pCurrent->next)
|
|
|
|
{
|
|
|
|
if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
|
|
|
|
{
|
|
|
|
xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
|
|
|
|
ResData aResData(
|
2013-04-12 23:02:11 -05:00
|
|
|
helper::xmlStrToOString( pID ),
|
2012-11-27 07:12:22 -06:00
|
|
|
static_cast<OString>(m_pSource->name) );
|
|
|
|
xmlFree( pID );
|
|
|
|
aResData.sResTyp = "string";
|
2013-04-20 02:20:31 -05:00
|
|
|
OString sNewText;
|
|
|
|
if( m_sLang == "qtz" )
|
|
|
|
{
|
|
|
|
xmlChar* pText = xmlNodeGetContent(pCurrent);
|
|
|
|
const OString sOriginText =
|
2013-05-26 01:30:22 -05:00
|
|
|
helper::unEscapeAll(helper::xmlStrToOString( pText ),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
|
2013-04-20 02:20:31 -05:00
|
|
|
xmlFree( pText );
|
|
|
|
sNewText = MergeEntrys::GetQTZText(aResData, sOriginText);
|
|
|
|
}
|
|
|
|
else if( pMergeDataFile )
|
|
|
|
{
|
|
|
|
MergeEntrys* pEntrys = pMergeDataFile->GetMergeEntrys( &aResData );
|
|
|
|
if( pEntrys )
|
|
|
|
{
|
|
|
|
pEntrys->GetText( sNewText, STRING_TYP_TEXT, m_sLang );
|
|
|
|
sNewText = helper::escapeAll(sNewText, "\n""\t""\'""\"","\\n""\\t""\\\'""\\\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !sNewText.isEmpty() )
|
2012-11-27 07:12:22 -06:00
|
|
|
{
|
|
|
|
xmlNodeSetContent(
|
|
|
|
pCurrent,
|
|
|
|
xmlEncodeSpecialChars( NULL,
|
|
|
|
reinterpret_cast<const xmlChar*>(
|
|
|
|
sNewText.getStr() )));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 02:20:31 -05:00
|
|
|
delete pMergeDataFile;
|
2012-11-27 07:12:22 -06:00
|
|
|
xmlSaveFile( rDestinationFile.getStr(), m_pSource );
|
|
|
|
xmlFreeDoc( m_pSource );
|
|
|
|
xmlCleanupParser();
|
|
|
|
m_bIsInitialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|