Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <iostream>
11 : #include <fstream>
12 : #include <cassert>
13 : #include <cstring>
14 :
15 : #include <libxml/tree.h>
16 : #include <libxml/parser.h>
17 : #include <libxml/xmlmemory.h>
18 : #include <libxml/xmlstring.h>
19 :
20 : #include "export.hxx"
21 : #include "helper.hxx"
22 : #include "common.hxx"
23 : #include "po.hxx"
24 : #include "stringmerge.hxx"
25 :
26 : //Parse strings.xml file
27 0 : StringParser::StringParser(
28 : const OString& rInputFile, const OString& rLang )
29 : : m_pSource( 0 )
30 : , m_sLang( rLang )
31 0 : , m_bIsInitialized( false )
32 : {
33 0 : m_pSource = xmlParseFile( rInputFile.getStr() );
34 0 : if ( !m_pSource ) {
35 : std::cerr
36 0 : << "Stringx error: Cannot open source file: "
37 0 : << rInputFile.getStr() << std::endl;
38 0 : return;
39 : }
40 0 : if( !m_pSource->name )
41 : {
42 0 : m_pSource->name = new char[strlen(rInputFile.getStr())+1];
43 0 : strcpy( m_pSource->name, rInputFile.getStr() );
44 : }
45 0 : m_bIsInitialized = true;
46 : }
47 :
48 0 : StringParser::~StringParser()
49 : {
50 0 : }
51 :
52 : //Extract strings form source file
53 0 : void StringParser::Extract( const OString& rPOFile )
54 : {
55 : assert( m_bIsInitialized );
56 0 : PoOfstream aPOStream( rPOFile, PoOfstream::APP );
57 0 : if( !aPOStream.isOpen() )
58 : {
59 : std::cerr
60 0 : << "stringex error: Cannot open po file for extract: "
61 0 : << rPOFile.getStr() << std::endl;
62 0 : return;
63 : }
64 :
65 0 : xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); // <resource>
66 0 : for( xmlNodePtr pCurrent = pRootNode->children->next;
67 : pCurrent; pCurrent = pCurrent->next)
68 : {
69 0 : if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
70 : {
71 0 : xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
72 0 : xmlChar* pText = xmlNodeGetContent(pCurrent);
73 : const OString sTemp =
74 0 : 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 0 : sTemp);
79 :
80 0 : xmlFree( pID );
81 0 : xmlFree( pText );
82 : }
83 : }
84 :
85 0 : xmlFreeDoc( m_pSource );
86 0 : xmlCleanupParser();
87 0 : aPOStream.close();
88 0 : m_bIsInitialized = false;
89 : }
90 :
91 : //Merge strings to localized strings.xml file
92 0 : void StringParser::Merge(
93 : const OString &rMergeSrc, const OString &rDestinationFile )
94 : {
95 : assert( m_bIsInitialized );
96 :
97 0 : MergeDataFile* pMergeDataFile = 0;
98 0 : if( m_sLang != "qtz" )
99 : {
100 : pMergeDataFile = new MergeDataFile(
101 0 : rMergeSrc, static_cast<OString>( m_pSource->name ), false, false );
102 0 : const std::vector<OString> vLanguages = pMergeDataFile->GetLanguages();
103 0 : if( vLanguages.size()>=1 && vLanguages[0] != m_sLang )
104 : {
105 : std::cerr
106 0 : << "stringex error: given language conflicts with "
107 0 : << "language of Mergedata file: "
108 0 : << m_sLang.getStr() << " - "
109 0 : << vLanguages[0].getStr() << std::endl;
110 0 : delete pMergeDataFile;
111 0 : return;
112 0 : }
113 : }
114 :
115 0 : xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); //<resource>
116 :
117 0 : for( xmlNodePtr pCurrent = pRootNode->children;
118 : pCurrent; pCurrent = pCurrent->next)
119 : {
120 0 : if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
121 : {
122 0 : xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
123 : ResData aResData(
124 : helper::xmlStrToOString( pID ),
125 0 : static_cast<OString>(m_pSource->name) );
126 0 : xmlFree( pID );
127 0 : aResData.sResTyp = "string";
128 0 : OString sNewText;
129 0 : if( m_sLang == "qtz" )
130 : {
131 0 : xmlChar* pText = xmlNodeGetContent(pCurrent);
132 : const OString sOriginText =
133 0 : helper::unEscapeAll(helper::xmlStrToOString( pText ),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
134 0 : xmlFree( pText );
135 0 : sNewText = MergeEntrys::GetQTZText(aResData, sOriginText);
136 : }
137 0 : else if( pMergeDataFile )
138 : {
139 0 : MergeEntrys* pEntrys = pMergeDataFile->GetMergeEntrys( &aResData );
140 0 : if( pEntrys )
141 : {
142 0 : pEntrys->GetText( sNewText, STRING_TYP_TEXT, m_sLang );
143 0 : sNewText = helper::escapeAll(sNewText, "\n""\t""\'""\"","\\n""\\t""\\\'""\\\"");
144 : }
145 : }
146 0 : if( !sNewText.isEmpty() )
147 : {
148 : xmlNodeSetContent(
149 : pCurrent,
150 : xmlEncodeSpecialChars( NULL,
151 : reinterpret_cast<const xmlChar*>(
152 0 : sNewText.getStr() )));
153 0 : }
154 : }
155 : }
156 :
157 0 : delete pMergeDataFile;
158 0 : xmlSaveFile( rDestinationFile.getStr(), m_pSource );
159 0 : xmlFreeDoc( m_pSource );
160 0 : xmlCleanupParser();
161 0 : m_bIsInitialized = false;
162 0 : }
163 :
164 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|