update credits
[LibreOffice.git] / l10ntools / source / stringmerge.cxx
blob9e3fa7a270d36d29e789c40a991bf6aa130ec644
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <iostream>
11 #include <fstream>
12 #include <cassert>
13 #include <cstring>
15 #include <libxml/tree.h>
16 #include <libxml/parser.h>
17 #include <libxml/xmlmemory.h>
18 #include <libxml/xmlstring.h>
20 #include "export.hxx"
21 #include "helper.hxx"
22 #include "common.hxx"
23 #include "po.hxx"
24 #include "stringmerge.hxx"
26 //Parse strings.xml file
27 StringParser::StringParser(
28 const OString& rInputFile, const OString& rLang )
29 : m_pSource( 0 )
30 , m_sLang( rLang )
31 , m_bIsInitialized( false )
33 m_pSource = xmlParseFile( rInputFile.getStr() );
34 if ( !m_pSource ) {
35 std::cerr
36 << "Stringx error: Cannot open source file: "
37 << rInputFile.getStr() << std::endl;
38 return;
40 if( !m_pSource->name )
42 m_pSource->name = new char[strlen(rInputFile.getStr())+1];
43 strcpy( m_pSource->name, rInputFile.getStr() );
45 m_bIsInitialized = true;
48 StringParser::~StringParser()
52 //Extract strings form source file
53 void StringParser::Extract( const OString& rPOFile )
55 assert( m_bIsInitialized );
56 PoOfstream aPOStream( rPOFile, PoOfstream::APP );
57 if( !aPOStream.isOpen() )
59 std::cerr
60 << "stringex error: Cannot open po file for extract: "
61 << rPOFile.getStr() << std::endl;
62 return;
65 xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); // <resource>
66 for( xmlNodePtr pCurrent = pRootNode->children->next;
67 pCurrent; pCurrent = pCurrent->next)
69 if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
71 xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
72 xmlChar* pText = xmlNodeGetContent(pCurrent);
73 const OString sTemp =
74 helper::unEscapeAll(helper::xmlStrToOString( pText ),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
75 common::writePoEntry(
76 "Stringex", aPOStream, m_pSource->name, "string",
77 helper::xmlStrToOString( pID ), OString(), OString(),
78 sTemp);
80 xmlFree( pID );
81 xmlFree( pText );
85 xmlFreeDoc( m_pSource );
86 xmlCleanupParser();
87 aPOStream.close();
88 m_bIsInitialized = false;
91 //Merge strings to localized strings.xml file
92 void StringParser::Merge(
93 const OString &rMergeSrc, const OString &rDestinationFile )
95 assert( m_bIsInitialized );
97 MergeDataFile* pMergeDataFile = 0;
98 if( m_sLang != "qtz" )
100 pMergeDataFile = new MergeDataFile(
101 rMergeSrc, static_cast<OString>( m_pSource->name ), false, false );
102 const std::vector<OString> vLanguages = pMergeDataFile->GetLanguages();
103 if( vLanguages.size()>=1 && vLanguages[0] != m_sLang )
105 std::cerr
106 << "stringex error: given language conflicts with "
107 << "language of Mergedata file: "
108 << m_sLang.getStr() << " - "
109 << vLanguages[0].getStr() << std::endl;
110 delete pMergeDataFile;
111 return;
115 xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); //<resource>
117 for( xmlNodePtr pCurrent = pRootNode->children;
118 pCurrent; pCurrent = pCurrent->next)
120 if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
122 xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
123 ResData aResData(
124 helper::xmlStrToOString( pID ),
125 static_cast<OString>(m_pSource->name) );
126 xmlFree( pID );
127 aResData.sResTyp = "string";
128 OString sNewText;
129 if( m_sLang == "qtz" )
131 xmlChar* pText = xmlNodeGetContent(pCurrent);
132 const OString sOriginText =
133 helper::unEscapeAll(helper::xmlStrToOString( pText ),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
134 xmlFree( pText );
135 sNewText = MergeEntrys::GetQTZText(aResData, sOriginText);
137 else if( pMergeDataFile )
139 MergeEntrys* pEntrys = pMergeDataFile->GetMergeEntrys( &aResData );
140 if( pEntrys )
142 pEntrys->GetText( sNewText, STRING_TYP_TEXT, m_sLang );
143 sNewText = helper::escapeAll(sNewText, "\n""\t""\'""\"","\\n""\\t""\\\'""\\\"");
146 if( !sNewText.isEmpty() )
148 xmlNodeSetContent(
149 pCurrent,
150 xmlEncodeSpecialChars( NULL,
151 reinterpret_cast<const xmlChar*>(
152 sNewText.getStr() )));
157 delete pMergeDataFile;
158 xmlSaveFile( rDestinationFile.getStr(), m_pSource );
159 xmlFreeDoc( m_pSource );
160 xmlCleanupParser();
161 m_bIsInitialized = false;
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */