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 "treemerge.hxx"
29 // Extract strings from nodes on all level recursively
30 static void lcl_ExtractLevel(
31 const xmlDocPtr pSource
, const xmlNodePtr pRoot
,
32 const xmlChar
* pNodeName
, PoOfstream
& rPOStream
)
34 if( !pRoot
->children
)
38 for( xmlNodePtr pCurrent
= pRoot
->children
->next
;
39 pCurrent
; pCurrent
= pCurrent
->next
)
41 if (!xmlStrcmp(pCurrent
->name
, pNodeName
))
43 xmlChar
* pID
= xmlGetProp(pCurrent
, reinterpret_cast<const xmlChar
*>("id"));
45 xmlGetProp(pCurrent
, reinterpret_cast<const xmlChar
*>("title"));
48 "Treex", rPOStream
, pSource
->name
, helper::xmlStrToOString( pNodeName
),
49 helper::xmlStrToOString( pID
), OString(), OString(), helper::xmlStrToOString( pText
));
55 pSource
, pCurrent
, reinterpret_cast<const xmlChar
*>("node"),
61 // Update id and content of the topic
62 static xmlNodePtr
lcl_UpdateTopic(
63 const xmlNodePtr pCurrent
, const OString
& rXhpRoot
)
65 xmlNodePtr pReturn
= pCurrent
;
66 xmlChar
* pID
= xmlGetProp(pReturn
, reinterpret_cast<const xmlChar
*>("id"));
68 helper::xmlStrToOString( pID
);
71 const sal_Int32 nFirstSlash
= sID
.indexOf('/');
72 // Update id attribute of topic
75 sID
.copy( 0, nFirstSlash
+ 1 ) +
76 rXhpRoot
.copy( rXhpRoot
.lastIndexOf('/') + 1 ) +
77 sID
.copy( sID
.indexOf( '/', nFirstSlash
+ 1 ) );
79 pReturn
, reinterpret_cast<const xmlChar
*>("id"),
80 reinterpret_cast<const xmlChar
*>(sNewID
.getStr()));
83 const OString sXhpPath
=
85 sID
.copy(sID
.indexOf('/', nFirstSlash
+ 1));
86 xmlDocPtr pXhpFile
= xmlParseFile( sXhpPath
.getStr() );
87 // if xhpfile is missing than put this topic into comment
90 xmlNodePtr pTemp
= pReturn
;
92 xmlGetProp(pReturn
, reinterpret_cast<const xmlChar
*>("id"));
94 xmlStrcat( xmlCharStrdup("removed "), sNewID
);
95 pReturn
= xmlNewComment( sComment
);
96 xmlReplaceNode( pTemp
, pReturn
);
101 // update topic's content on the basis of xhpfile's title
104 xmlNodePtr pXhpNode
= xmlDocGetRootElement( pXhpFile
);
105 for( pXhpNode
= pXhpNode
->children
;
106 pXhpNode
; pXhpNode
= pXhpNode
->children
)
108 while( pXhpNode
->type
!= XML_ELEMENT_NODE
)
110 pXhpNode
= pXhpNode
->next
;
112 if(!xmlStrcmp(pXhpNode
->name
, reinterpret_cast<const xmlChar
*>("title")))
115 xmlNodeListGetString(pXhpFile
, pXhpNode
->children
, 1);
117 helper::xmlStrToOString( sTitle
).
118 replaceAll("$[officename]","%PRODUCTNAME").
119 replaceAll("$[officeversion]","%PRODUCTVERSION");
122 xmlEncodeSpecialChars( NULL
,
123 reinterpret_cast<const xmlChar
*>(
124 sNewTitle
.getStr() )));
132 << "Treex error: Cannot find title in "
133 << sXhpPath
.getStr() << std::endl
;
141 // Localize title attribute of help_section and node tags
142 static void lcl_MergeLevel(
143 xmlDocPtr io_pSource
, const xmlNodePtr pRoot
,
144 const xmlChar
* pNodeName
, MergeDataFile
* pMergeDataFile
,
145 const OString
& rLang
, const OString
& rXhpRoot
)
147 if( !pRoot
->children
)
151 for( xmlNodePtr pCurrent
= pRoot
->children
;
152 pCurrent
; pCurrent
= pCurrent
->next
)
154 if( !xmlStrcmp(pCurrent
->name
, pNodeName
) )
156 if( rLang
!= "en-US" )
159 xmlChar
* pID
= xmlGetProp(pCurrent
, reinterpret_cast<const xmlChar
*>("id"));
161 helper::xmlStrToOString( pID
),
162 static_cast<OString
>(io_pSource
->name
) );
164 aResData
.sResTyp
= helper::xmlStrToOString( pNodeName
);
167 MergeEntrys
* pEntrys
=
168 pMergeDataFile
->GetMergeEntrys( &aResData
);
171 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, rLang
);
174 else if( rLang
== "qtz" )
176 xmlChar
* pText
= xmlGetProp(pCurrent
, reinterpret_cast<const xmlChar
*>("title"));
177 const OString sOriginText
= helper::xmlStrToOString(pText
);
179 sNewText
= MergeEntrys::GetQTZText(aResData
, sOriginText
);
181 if( !sNewText
.isEmpty() )
184 pCurrent
, reinterpret_cast<const xmlChar
*>("title"),
185 reinterpret_cast<const xmlChar
*>(sNewText
.getStr()));
190 io_pSource
, pCurrent
, reinterpret_cast<const xmlChar
*>("node"),
191 pMergeDataFile
, rLang
, rXhpRoot
);
193 else if( !xmlStrcmp(pCurrent
->name
, reinterpret_cast<const xmlChar
*>("topic")) )
195 pCurrent
= lcl_UpdateTopic( pCurrent
, rXhpRoot
);
201 TreeParser::TreeParser(
202 const OString
& rInputFile
, const OString
& rLang
)
205 , m_bIsInitialized( false )
207 m_pSource
= xmlParseFile( rInputFile
.getStr() );
210 << "Treex error: Cannot open source file: "
211 << rInputFile
.getStr() << std::endl
;
214 if( !m_pSource
->name
)
216 m_pSource
->name
= static_cast<char *>(xmlMalloc(strlen(rInputFile
.getStr())+1));
217 strcpy( m_pSource
->name
, rInputFile
.getStr() );
219 m_bIsInitialized
= true;
222 TreeParser::~TreeParser()
226 void TreeParser::Extract( const OString
& rPOFile
)
228 assert( m_bIsInitialized
);
229 PoOfstream
aPOStream( rPOFile
, PoOfstream::APP
);
230 if( !aPOStream
.isOpen() )
233 << "Treex error: Cannot open po file for extract: "
234 << rPOFile
.getStr() << std::endl
;
238 xmlNodePtr pRootNode
= xmlDocGetRootElement( m_pSource
);
240 m_pSource
, pRootNode
, reinterpret_cast<const xmlChar
*>("help_section"),
243 xmlFreeDoc( m_pSource
);
246 m_bIsInitialized
= false;
249 void TreeParser::Merge(
250 const OString
&rMergeSrc
, const OString
&rDestinationFile
,
251 const OString
&rXhpRoot
)
253 assert( m_bIsInitialized
);
255 const xmlNodePtr pRootNode
= xmlDocGetRootElement( m_pSource
);
256 MergeDataFile
* pMergeDataFile
= 0;
257 if( m_sLang
!= "qtz" && m_sLang
!= "en-US" )
259 pMergeDataFile
= new MergeDataFile(
260 rMergeSrc
, static_cast<OString
>( m_pSource
->name
), false, false );
261 const std::vector
<OString
> vLanguages
= pMergeDataFile
->GetLanguages();
262 if( vLanguages
.size()>=1 && vLanguages
[0] != m_sLang
)
265 << ("Treex error: given language conflicts with language of"
267 << m_sLang
.getStr() << " - "
268 << vLanguages
[0].getStr() << std::endl
;
269 delete pMergeDataFile
;
274 m_pSource
, pRootNode
, reinterpret_cast<const xmlChar
*>("help_section"),
275 pMergeDataFile
, m_sLang
, rXhpRoot
);
277 delete pMergeDataFile
;
278 xmlSaveFile( rDestinationFile
.getStr(), m_pSource
);
279 xmlFreeDoc( m_pSource
);
281 m_bIsInitialized
= false;
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */