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/.
15 #include <libxml/tree.h>
16 #include <libxml/parser.h>
17 #include <libxml/xmlmemory.h>
18 #include <libxml/xmlstring.h>
24 #include "stringmerge.hxx"
26 //Parse strings.xml file
27 StringParser::StringParser(
28 const OString
& rInputFile
, const OString
& rLang
)
31 , m_bIsInitialized( false )
33 m_pSource
= xmlParseFile( rInputFile
.getStr() );
36 << "Stringx error: Cannot open source file: "
37 << rInputFile
.getStr() << std::endl
;
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() )
60 << "stringex error: Cannot open po file for extract: "
61 << rPOFile
.getStr() << std::endl
;
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
);
74 helper::unEscapeAll(helper::xmlStrToOString( pText
),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
76 "Stringex", aPOStream
, m_pSource
->name
, "string",
77 helper::xmlStrToOString( pID
), OString(), OString(),
85 xmlFreeDoc( m_pSource
);
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
)
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
;
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"));
124 helper::xmlStrToOString( pID
),
125 static_cast<OString
>(m_pSource
->name
) );
127 aResData
.sResTyp
= "string";
129 if( m_sLang
== "qtz" )
131 xmlChar
* pText
= xmlNodeGetContent(pCurrent
);
132 const OString sOriginText
=
133 helper::unEscapeAll(helper::xmlStrToOString( pText
),"\\n""\\t""\\\"""\\\'","\n""\t""\"""\'");
135 sNewText
= MergeEntrys::GetQTZText(aResData
, sOriginText
);
137 else if( pMergeDataFile
)
139 MergeEntrys
* pEntrys
= pMergeDataFile
->GetMergeEntrys( &aResData
);
142 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, m_sLang
);
143 sNewText
= helper::escapeAll(sNewText
, "\n""\t""\'""\"","\\n""\\t""\\\'""\\\"");
146 if( !sNewText
.isEmpty() )
150 xmlEncodeSpecialChars( NULL
,
151 reinterpret_cast<const xmlChar
*>(
152 sNewText
.getStr() )));
157 delete pMergeDataFile
;
158 xmlSaveFile( rDestinationFile
.getStr(), m_pSource
);
159 xmlFreeDoc( m_pSource
);
161 m_bIsInitialized
= false;
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */