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 "common.hxx"
22 : #include "stringmerge.hxx"
23 :
24 :
25 : namespace
26 : {
27 : //Write out an sdf line
28 0 : static void lcl_WriteSDF(
29 : std::ofstream &aSDFStream, const OString& rText, const OString& rPrj,
30 : const OString& rActFileName, const OString& rID, const OString& rType )
31 : {
32 0 : OString sOutput( rPrj ); sOutput += "\t";
33 0 : sOutput += rActFileName;
34 0 : sOutput += "\t0\t";
35 0 : sOutput += rType; sOutput += "\t";
36 0 : sOutput += rID; sOutput += "\t\t\t\t0\ten-US\t";
37 0 : sOutput += rText; sOutput += "\t\t\t\t";
38 0 : aSDFStream << sOutput.getStr() << std::endl;
39 0 : }
40 :
41 : //Convert xmlChar* to OString
42 0 : static OString lcl_xmlStrToOString( const xmlChar* pString )
43 : {
44 0 : xmlChar* pTemp = xmlStrdup( pString );
45 : OString sResult =
46 0 : static_cast<OString>(reinterpret_cast<sal_Char*>( pTemp ));
47 0 : xmlFree( pTemp );
48 0 : return sResult;
49 : }
50 : }
51 :
52 : //Parse strings.xml file
53 0 : StringParser::StringParser(
54 : const OString& rInputFile, const OString& rLang )
55 : : m_pSource( 0 )
56 : , m_sLang( rLang )
57 0 : , m_bIsInitialized( false )
58 : {
59 0 : m_pSource = xmlParseFile( rInputFile.getStr() );
60 0 : if ( !m_pSource ) {
61 : std::cerr
62 0 : << "Stringx error: Cannot open source file: "
63 0 : << rInputFile.getStr() << std::endl;
64 0 : return;
65 : }
66 0 : if( !m_pSource->name )
67 : {
68 0 : m_pSource->name = new char[strlen(rInputFile.getStr())+1];
69 0 : strcpy( m_pSource->name, rInputFile.getStr() );
70 : }
71 0 : m_bIsInitialized = true;
72 : }
73 :
74 0 : StringParser::~StringParser()
75 : {
76 0 : }
77 :
78 : //Extract strings form source file
79 0 : void StringParser::Extract(
80 : const OString& rSDFFile, const OString& rPrj, const OString& rRoot )
81 : {
82 : assert( m_bIsInitialized );
83 : std::ofstream aSDFStream(
84 0 : rSDFFile.getStr(), std::ios_base::out | std::ios_base::trunc );
85 0 : if( !aSDFStream.is_open() )
86 : {
87 : std::cerr
88 0 : << "stringex error: Cannot open sdffile for extract: "
89 0 : << rSDFFile.getStr() << std::endl;
90 0 : return;
91 : }
92 :
93 0 : xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); // <resource>
94 0 : for( xmlNodePtr pCurrent = pRootNode->children->next;
95 : pCurrent; pCurrent = pCurrent->next)
96 : {
97 0 : if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
98 : {
99 0 : xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
100 0 : xmlChar* pText = xmlNodeGetContent(pCurrent);
101 : lcl_WriteSDF(
102 : aSDFStream,
103 : lcl_xmlStrToOString( pText ),
104 : rPrj,
105 : common::pathnameToken(
106 : m_pSource->name, rRoot.getStr()),
107 : lcl_xmlStrToOString( pID ),
108 0 : OString( "string" ));
109 :
110 0 : xmlFree( pID );
111 0 : xmlFree( pText );
112 : }
113 : }
114 :
115 0 : xmlFreeDoc( m_pSource );
116 0 : xmlCleanupParser();
117 0 : aSDFStream.close();
118 0 : m_bIsInitialized = false;
119 : }
120 :
121 : //Merge strings to localized strings.xml file
122 0 : void StringParser::Merge(
123 : const OString &rMergeSrc, const OString &rDestinationFile )
124 : {
125 : assert( m_bIsInitialized );
126 :
127 0 : if( (m_sLang == "en-US") || (m_sLang == "qtz") )
128 : {
129 : return;
130 : }
131 :
132 : MergeDataFile aMergeDataFile(
133 0 : rMergeSrc, static_cast<OString>( m_pSource->name ), false );
134 0 : const std::vector<OString> vLanguages = aMergeDataFile.GetLanguages();
135 0 : if( vLanguages.size()>=2 && vLanguages[0] != m_sLang )
136 : {
137 : std::cerr
138 0 : << "stringex error: given language conflicts with "
139 0 : << "language of Mergedata file: "
140 0 : << m_sLang.getStr() << " - "
141 0 : << vLanguages[vLanguages[0]=="qtz" ? 0 : 1].getStr() << std::endl;
142 : return;
143 : }
144 :
145 0 : xmlNodePtr pRootNode = xmlDocGetRootElement( m_pSource ); //<resource>
146 :
147 0 : for( xmlNodePtr pCurrent = pRootNode->children;
148 : pCurrent; pCurrent = pCurrent->next)
149 : {
150 0 : if (!xmlStrcmp(pCurrent->name, (const xmlChar*)("string")))
151 : {
152 0 : xmlChar* pID = xmlGetProp(pCurrent, (const xmlChar*)("name"));
153 : ResData aResData(
154 : "", lcl_xmlStrToOString( pID ),
155 0 : static_cast<OString>(m_pSource->name) );
156 0 : xmlFree( pID );
157 0 : aResData.sResTyp = "string";
158 : PFormEntrys* pEntrys =
159 0 : (&aMergeDataFile)->GetPFormEntrys( &aResData );
160 0 : if( pEntrys )
161 : {
162 0 : OString sNewText;
163 0 : pEntrys->GetText( sNewText, STRING_TYP_TEXT, m_sLang );
164 : xmlNodeSetContent(
165 : pCurrent,
166 : xmlEncodeSpecialChars( NULL,
167 : reinterpret_cast<const xmlChar*>(
168 0 : sNewText.getStr() )));
169 0 : }
170 : }
171 : }
172 :
173 0 : xmlSaveFile( rDestinationFile.getStr(), m_pSource );
174 0 : xmlFreeDoc( m_pSource );
175 0 : xmlCleanupParser();
176 0 : m_bIsInitialized = false;
177 0 : }
178 :
179 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|