LibreOffice Module helpcompiler (master) 1
HelpLinker.cxx
Go to the documentation of this file.
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <HelpCompiler.hxx>
21#include <HelpLinker.hxx>
22
23#include <algorithm>
24#include <fstream>
25
26#include <string.h>
27
28#include <libxslt/transform.h>
29
30#include <sal/types.h>
32#include <sal/log.hxx>
33
34#include <expat.h>
35#include <memory>
36
37namespace {
38FILE* fopen_impl(const fs::path& rPath, const char* szMode)
39{
40#ifdef _WIN32 //We need _wfopen to support long file paths on Windows XP
41 return _wfopen(rPath.native_file_string_w().c_str(), o3tl::toW(OUString::createFromAscii(szMode).getStr()));
42#else
43 return fopen(rPath.native_file_string().c_str(), szMode);
44#endif
45}
46}
47
49 ( const fs::path& fsIndexBaseDir,
50 const fs::path& idxCaptionStylesheet, const fs::path& idxContentStylesheet )
51{
52 m_fsCaptionFilesDirName = fsIndexBaseDir / "caption";
54
55 m_fsContentFilesDirName = fsIndexBaseDir / "content";
57
58 m_xsltStylesheetPtrCaption = xsltParseStylesheetFile
59 (reinterpret_cast<const xmlChar *>(idxCaptionStylesheet.native_file_string().c_str()));
60 m_xsltStylesheetPtrContent = xsltParseStylesheetFile
61 (reinterpret_cast<const xmlChar *>(idxContentStylesheet.native_file_string().c_str()));
62}
63
65{
67 xsltFreeStylesheet( m_xsltStylesheetPtrCaption );
69 xsltFreeStylesheet( m_xsltStylesheetPtrContent );
70}
71
72static std::string getEncodedPath( const std::string& Path )
73{
74 std::string_view aOStr_Path( Path );
75 OUString aOUStr_Path( OStringToOUString
76 ( aOStr_Path, osl_getThreadTextEncoding() ) );
77 OUString aPathURL;
78 osl::File::getFileURLFromSystemPath( aOUStr_Path, aPathURL );
79 OString aOStr_PathURL( OUStringToOString
80 ( aPathURL, osl_getThreadTextEncoding() ) );
81 std::string aStdStr_PathURL( aOStr_PathURL );
82 return aStdStr_PathURL;
83}
84
86 ( xmlDocPtr doc, const std::string &EncodedDocPath )
87{
88 std::string aStdStr_EncodedDocPathURL = getEncodedPath( EncodedDocPath );
89
91 {
92 xmlDocPtr resCaption = xsltApplyStylesheet( m_xsltStylesheetPtrCaption, doc, nullptr );
93 xmlNodePtr pResNodeCaption = resCaption->xmlChildrenNode;
94 if( pResNodeCaption )
95 {
96 fs::path fsCaptionPureTextFile_docURL = m_fsCaptionFilesDirName / aStdStr_EncodedDocPathURL;
97 FILE* pFile_docURL = fopen_impl( fsCaptionPureTextFile_docURL, "w" );
98 if( pFile_docURL )
99 {
100 fprintf( pFile_docURL, "%s\n", pResNodeCaption->content );
101 fclose( pFile_docURL );
102 }
103 }
104 xmlFreeDoc(resCaption);
105 }
106
108 return;
109
110 xmlDocPtr resContent = xsltApplyStylesheet( m_xsltStylesheetPtrContent, doc, nullptr );
111 xmlNodePtr pResNodeContent = resContent->xmlChildrenNode;
112 if( pResNodeContent )
113 {
114 fs::path fsContentPureTextFile_docURL = m_fsContentFilesDirName / aStdStr_EncodedDocPathURL;
115 FILE* pFile_docURL = fopen_impl( fsContentPureTextFile_docURL, "w" );
116 if( pFile_docURL )
117 {
118 fprintf( pFile_docURL, "%s\n", pResNodeContent->content );
119 fclose( pFile_docURL );
120 }
121 }
122 xmlFreeDoc(resContent);
123}
124
125namespace {
126
127struct Data
128{
129 std::vector<std::string> _idList;
130
131 void append(const std::string &id)
132 {
133 _idList.push_back(id);
134 }
135
136 std::string getString() const
137 {
138 std::string ret;
139 for (auto const& elem : _idList)
140 ret += elem + ";";
141 return ret;
142 }
143};
144
145}
146
147static void writeKeyValue_DBHelp( FILE* pFile, const std::string& aKeyStr, const std::string& aValueStr )
148{
149 if( pFile == nullptr )
150 return;
151 char const cLF = 10;
152 unsigned int nKeyLen = aKeyStr.length();
153 unsigned int nValueLen = aValueStr.length();
154 fprintf( pFile, "%x ", nKeyLen );
155 if( nKeyLen > 0 )
156 {
157 if (fwrite( aKeyStr.c_str(), 1, nKeyLen, pFile ) != nKeyLen)
158 fprintf(stderr, "fwrite to db failed\n");
159 }
160 if (fprintf( pFile, " %x ", nValueLen ) < 0)
161 fprintf(stderr, "fwrite to db failed\n");
162 if( nValueLen > 0 )
163 {
164 if (fwrite( aValueStr.c_str(), 1, nValueLen, pFile ) != nValueLen)
165 fprintf(stderr, "fwrite to db failed\n");
166 }
167 if (fprintf( pFile, "%c", cLF ) < 0)
168 fprintf(stderr, "fwrite to db failed\n");
169}
170
171namespace {
172
173class HelpKeyword
174{
175private:
176 typedef std::unordered_map<std::string, Data> DataHashtable;
177 DataHashtable _hash;
178
179public:
180 void insert(const std::string &key, const std::string &id)
181 {
182 Data &data = _hash[key];
183 data.append(id);
184 }
185
186 void dump_DBHelp( const fs::path& rFileName )
187 {
188 FILE* pFile = fopen_impl( rFileName, "wb" );
189 if( pFile == nullptr )
190 return;
191
192 for (auto const& elem : _hash)
193 writeKeyValue_DBHelp( pFile, elem.first, elem.second.getString() );
194
195 fclose( pFile );
196 }
197};
198
199}
200
201namespace URLEncoder
202{
203 static std::string encode(const std::string &rIn)
204 {
205 const char * const good = "!$&'()*+,-.=@_";
206 static const char hex[17] = "0123456789ABCDEF";
207
208 std::string result;
209 for (char c : rIn)
210 {
211 if (rtl::isAsciiAlphanumeric (static_cast<unsigned char>(c))
212 || strchr (good, c))
213 {
214 result += c;
215 } else {
216 result += '%';
217 result += hex[static_cast<unsigned char>(c) >> 4];
218 result += hex[c & 0xf];
219 }
220 }
221 return result;
222 }
223}
224
225void HelpLinker::addBookmark( FILE* pFile_DBHelp, std::string thishid,
226 const std::string& fileB, const std::string& anchorB,
227 const std::string& jarfileB, const std::string& titleB)
228{
229 HCDBG(std::cerr << "HelpLinker::addBookmark " << thishid << " " <<
230 fileB << " " << anchorB << " " << jarfileB << " " << titleB << std::endl);
231
232 thishid = URLEncoder::encode(thishid);
233
234 int fileLen = fileB.length();
235 if (!anchorB.empty())
236 fileLen += (1 + anchorB.length());
237 int dataLen = 1 + fileLen + 1 + jarfileB.length() + 1 + titleB.length();
238
239 std::vector<unsigned char> dataB(dataLen);
240 size_t i = 0;
241 dataB[i++] = static_cast<unsigned char>(fileLen);
242 for (char j : fileB)
243 dataB[i++] = static_cast<unsigned char>(j);
244 if (!anchorB.empty())
245 {
246 dataB[i++] = '#';
247 for (char j : anchorB)
248 dataB[i++] = j;
249 }
250 dataB[i++] = static_cast<unsigned char>(jarfileB.length());
251 for (char j : jarfileB)
252 dataB[i++] = j;
253
254 dataB[i++] = static_cast<unsigned char>(titleB.length());
255 for (char j : titleB)
256 dataB[i++] = j;
257
258 if( pFile_DBHelp != nullptr )
259 {
260 std::string aValueStr( dataB.begin(), dataB.end() );
261 writeKeyValue_DBHelp( pFile_DBHelp, thishid, aValueStr );
262 }
263}
264
266{
269}
270
272{
273
274 if( bExtensionMode )
275 {
277 }
278 else
279 {
282 }
283
284 std::string mod = module;
285 std::transform (mod.begin(), mod.end(), mod.begin(), tocharlower);
286
287 // do the work here
288 // continue with introduction of the overall process thing into the
289 // here all hzip files will be worked on
290 bool bUse_ = true;
291 if( !bExtensionMode )
292 bUse_ = false;
293
294 fs::path helpTextFileName_DBHelp(indexDirParentName / (mod + (bUse_ ? ".ht_" : ".ht")));
295 FILE* pFileHelpText_DBHelp = fopen_impl( helpTextFileName_DBHelp, "wb" );
296
297 fs::path dbBaseFileName_DBHelp(indexDirParentName / (mod + (bUse_ ? ".db_" : ".db")));
298 FILE* pFileDbBase_DBHelp = fopen_impl( dbBaseFileName_DBHelp, "wb" );
299
300 fs::path keyWordFileName_DBHelp(indexDirParentName / (mod + (bUse_ ? ".key_" : ".key")));
301
302 HelpKeyword helpKeyword;
303
304 // catch HelpProcessingException to avoid locking data bases
305 try
306 {
307 bool bIndexForExtension = true;
308 // lastly, initialize the indexBuilder
309 if ( (!bExtensionMode || bIndexForExtension) && !helpFiles.empty())
311
312 // here we start our loop over the hzip files.
313 for (auto const& helpFile : helpFiles)
314 {
315 // process one file
316 // streamTable contains the streams in the hzip file
317 StreamTable streamTable;
318 const std::string &xhpFileName = helpFile;
319
320 if (!bExtensionMode && xhpFileName.rfind(".xhp") != xhpFileName.length()-4)
321 {
322 // only work on .xhp - files
323 SAL_WARN("helpcompiler",
324 "ERROR: input list entry '"
325 << xhpFileName
326 << "' has the wrong extension (only files with extension .xhp are accepted)");
327
328 continue;
329 }
330
331 fs::path langsourceRoot(sourceRoot);
332 fs::path xhpFile;
333
334 if( bExtensionMode )
335 {
336 // langsourceRoot == sourceRoot for extensions
337 std::string xhpFileNameComplete( extensionPath );
338 xhpFileNameComplete.append( '/' + xhpFileName );
339 xhpFile = fs::path( xhpFileNameComplete );
340 }
341 else
342 {
343 langsourceRoot.append( "/" );
344 if ( m_bUseLangRoot )
345 langsourceRoot.append( lang + '/' );
346 xhpFile = fs::path(xhpFileName, fs::native);
347 }
348
349 HelpCompiler hc( streamTable, std::move(xhpFile), std::move(langsourceRoot), zipdir,
351
352 HCDBG(std::cerr << "before compile of " << xhpFileName << std::endl);
353 hc.compile();
354 HCDBG(std::cerr << "after compile of " << xhpFileName << std::endl);
355
356 if (!m_bCreateIndex)
357 continue;
358
359 std::string documentPath = streamTable.document_path;
360 if (documentPath.compare(0, 1, "/") == 0)
361 documentPath = documentPath.substr(1);
362
363 std::string documentJarfile = streamTable.document_module + ".jar";
364
365 std::string documentTitle = streamTable.document_title;
366 if (documentTitle.empty())
367 documentTitle = "<notitle>";
368
369 const std::string& fileB = documentPath;
370 const std::string& jarfileB = documentJarfile;
371 std::string& titleB = documentTitle;
372
373 // add once this as its own id.
374 addBookmark( pFileDbBase_DBHelp, documentPath, fileB, std::string(), jarfileB, titleB);
375
376 const std::vector<std::string> *hidlist = streamTable.appl_hidlist.get();
377 if (hidlist)
378 {
379 // now iterate over all elements of the hidlist
380 for (auto & elem : *hidlist)
381 {
382 std::string thishid = elem;
383
384 std::string anchorB;
385 size_t index = thishid.rfind('#');
386 if (index != std::string::npos)
387 {
388 anchorB = thishid.substr(1 + index);
389 thishid = thishid.substr(0, index);
390 }
391 addBookmark( pFileDbBase_DBHelp, thishid, fileB, anchorB, jarfileB, titleB);
392 }
393 }
394
395 // now the keywords
396 const Hashtable *anchorToLL = streamTable.appl_keywords.get();
397 if (anchorToLL && !anchorToLL->empty())
398 {
399 std::string fakedHid = URLEncoder::encode(documentPath);
400 for (auto const& elemAnchor : *anchorToLL)
401 {
402 const std::string &anchor = elemAnchor.first;
403 addBookmark(pFileDbBase_DBHelp, documentPath, fileB,
404 anchor, jarfileB, titleB);
405 std::string totalId = fakedHid + "#" + anchor;
406 // std::cerr << hzipFileName << std::endl;
407 const LinkedList& ll = elemAnchor.second;
408 for (auto const& elem : ll)
409 {
410 helpKeyword.insert(elem, totalId);
411 }
412 }
413
414 }
415
416 // and last the helptexts
417 const Stringtable *helpTextHash = streamTable.appl_helptexts.get();
418 if (helpTextHash)
419 {
420 for (auto const& elem : *helpTextHash)
421 {
422 std::string helpTextId = elem.first;
423 const std::string& helpTextText = elem.second;
424
425 helpTextId = URLEncoder::encode(helpTextId);
426
427 if( pFileHelpText_DBHelp != nullptr )
428 writeKeyValue_DBHelp( pFileHelpText_DBHelp, helpTextId, helpTextText );
429 }
430 }
431
432 //IndexerPreProcessor
433 if( !bExtensionMode || bIndexForExtension )
434 {
435 // now the indexing
436 xmlDocPtr document = streamTable.appl_doc;
437 if (document)
438 {
439 std::string temp = module;
440 std::transform (temp.begin(), temp.end(), temp.begin(), tocharlower);
441 m_pIndexerPreProcessor->processDocument(document, URLEncoder::encode(documentPath) );
442 }
443 }
444
445 }
446
447 }
448 catch( const HelpProcessingException& )
449 {
450 // catch HelpProcessingException to avoid locking data bases
451 if( pFileHelpText_DBHelp != nullptr )
452 fclose( pFileHelpText_DBHelp );
453 if( pFileDbBase_DBHelp != nullptr )
454 fclose( pFileDbBase_DBHelp );
455 throw;
456 }
457
458 if( pFileHelpText_DBHelp != nullptr )
459 fclose( pFileHelpText_DBHelp );
460 if( pFileDbBase_DBHelp != nullptr )
461 fclose( pFileDbBase_DBHelp );
462
463 helpKeyword.dump_DBHelp( keyWordFileName_DBHelp);
464
465 if( bExtensionMode )
466 return;
467
468 // New index
469 for (auto const& additionalFile : additionalFiles)
470 {
471 const std::string &additionalFileName = additionalFile.second;
472 const std::string &additionalFileKey = additionalFile.first;
473
474 fs::path fsAdditionalFileName( additionalFileName, fs::native );
475 HCDBG({
476 std::string aNativeStr = fsAdditionalFileName.native_file_string();
477 const char* pStr = aNativeStr.c_str();
478 std::cerr << pStr << std::endl;
479 });
480
481 fs::path fsTargetName( indexDirParentName / additionalFileKey );
482
483 fs::copy( fsAdditionalFileName, fsTargetName );
484 }
485}
486
487
488void HelpLinker::main( std::vector<std::string> &args,
489 std::string const * pExtensionPath, std::string const * pDestination,
490 const OUString* pOfficeHelpPath )
491{
492 bExtensionMode = false;
493 helpFiles.clear();
494
495 if ((!args.empty()) && args[0][0] == '@')
496 {
497 std::vector<std::string> stringList;
498 std::ifstream fileReader(args[0].substr(1).c_str());
499
500 while (fileReader)
501 {
502 std::string token;
503 fileReader >> token;
504 if (!token.empty())
505 stringList.push_back(token);
506 }
507 fileReader.close();
508
509 args = stringList;
510 }
511
512 size_t i = 0;
513 bool bSrcOption = false;
514 while (i < args.size())
515 {
516 if (args[i].compare("-extlangsrc") == 0)
517 {
518 ++i;
519 if (i >= args.size())
520 {
521 std::stringstream aStrStream;
522 aStrStream << "extension source missing" << std::endl;
524 }
525 extsource = args[i];
526 }
527 else if (args[i].compare("-extlangdest") == 0)
528 {
529 //If this argument is not provided then the location provided in -extsource will
530 //also be the destination
531 ++i;
532 if (i >= args.size())
533 {
534 std::stringstream aStrStream;
535 aStrStream << "extension destination missing" << std::endl;
537 }
539 }
540 else if (args[i].compare("-src") == 0)
541 {
542 ++i;
543 if (i >= args.size())
544 {
545 std::stringstream aStrStream;
546 aStrStream << "sourceroot missing" << std::endl;
548 }
549 bSrcOption = true;
551 }
552 else if (args[i].compare("-compact") == 0)
553 {
554 ++i;
555 if (i >= args.size())
556 {
557 std::stringstream aStrStream;
558 aStrStream << "compactStylesheet missing" << std::endl;
560 }
561
563 }
564 else if (args[i].compare("-sty") == 0)
565 {
566 ++i;
567 if (i >= args.size())
568 {
569 std::stringstream aStrStream;
570 aStrStream << "embeddingStylesheet missing" << std::endl;
572 }
573
575 }
576 else if (args[i].compare("-zipdir") == 0)
577 {
578 ++i;
579 if (i >= args.size())
580 {
581 std::stringstream aStrStream;
582 aStrStream << "idxtemp missing" << std::endl;
584 }
585
587 }
588 else if (args[i].compare("-idxcaption") == 0)
589 {
590 ++i;
591 if (i >= args.size())
592 {
593 std::stringstream aStrStream;
594 aStrStream << "idxcaption stylesheet missing" << std::endl;
596 }
597
599 }
600 else if (args[i].compare("-idxcontent") == 0)
601 {
602 ++i;
603 if (i >= args.size())
604 {
605 std::stringstream aStrStream;
606 aStrStream << "idxcontent stylesheet missing" << std::endl;
608 }
609
611 }
612 else if (args[i].compare("-o") == 0)
613 {
614 ++i;
615 if (i >= args.size())
616 {
617 std::stringstream aStrStream;
618 aStrStream << "outputfilename missing" << std::endl;
620 }
621
623 }
624 else if (args[i].compare("-mod") == 0)
625 {
626 ++i;
627 if (i >= args.size())
628 {
629 std::stringstream aStrStream;
630 aStrStream << "module name missing" << std::endl;
632 }
633
634 module = args[i];
635 }
636 else if (args[i].compare("-lang") == 0)
637 {
638 ++i;
639 if (i >= args.size())
640 {
641 std::stringstream aStrStream;
642 aStrStream << "language name missing" << std::endl;
644 }
645
646 lang = args[i];
647 }
648 else if (args[i].compare("-hid") == 0)
649 {
650 ++i;
651 throw HelpProcessingException( HelpProcessingErrorClass::General, "obsolete -hid argument used" );
652 }
653 else if (args[i].compare("-add") == 0)
654 {
655 std::string addFile, addFileUnderPath;
656 ++i;
657 if (i >= args.size())
658 {
659 std::stringstream aStrStream;
660 aStrStream << "pathname missing" << std::endl;
662 }
663
664 addFileUnderPath = args[i];
665 ++i;
666 if (i >= args.size())
667 {
668 std::stringstream aStrStream;
669 aStrStream << "pathname missing" << std::endl;
671 }
672 addFile = args[i];
673 if (!addFileUnderPath.empty() && !addFile.empty())
674 additionalFiles[addFileUnderPath] = addFile;
675 }
676 else if (args[i].compare("-nolangroot") == 0)
677 m_bUseLangRoot = false;
678 else if (args[i].compare("-noindex") == 0)
679 m_bCreateIndex = false;
680 else
681 helpFiles.push_back(args[i]);
682 ++i;
683 }
684
685 //We can be called from the helplinker executable or the extension manager
686 //In the latter case extsource is not used.
687 if( (pExtensionPath && pExtensionPath->length() > 0 && pOfficeHelpPath)
688 || !extsource.empty())
689 {
690 bExtensionMode = true;
691 if (!extsource.empty())
692 {
693 //called from helplinker.exe, pExtensionPath and pOfficeHelpPath
694 //should be NULL
697
698 if (extdestination.empty())
699 {
700 std::stringstream aStrStream;
701 aStrStream << "-extlangdest is missing" << std::endl;
703 }
704 else
705 {
706 //Convert from system path to file URL!!!
708 extensionDestination = p.toUTF8();
709 }
710 }
711 else
712 { //called from extension manager
713 extensionPath = *pExtensionPath;
715 extensionDestination = *pDestination;
716 }
717 //check if -src option was used. This option must not be used
718 //when extension help is compiled.
719 if (bSrcOption)
720 {
721 std::stringstream aStrStream;
722 aStrStream << "-src must not be used together with -extsource missing" << std::endl;
724 }
725 }
726
727 if (!bExtensionMode && zipdir.empty())
728 {
729 std::stringstream aStrStream;
730 aStrStream << "no index dir given" << std::endl;
732 }
733
735 || (!extsource.empty() && idxCaptionStylesheet.empty()) )
736 {
737 //No extension mode and extension mode using commandline
739 // -idxcaption parameter is required
740 std::stringstream aStrStream;
741 aStrStream << "no index caption stylesheet given" << std::endl;
743 }
744 else if ( bExtensionMode && extsource.empty())
745 {
746 //This part is used when compileExtensionHelp is called from the extensions manager.
747 //If extension help is compiled using helplinker in the build process
748 OUString aIdxCaptionPathFileURL = *pOfficeHelpPath + "/idxcaption.xsl";
749
750 OString aOStr_IdxCaptionPathFileURL( OUStringToOString
751 ( aIdxCaptionPathFileURL, osl_getThreadTextEncoding() ) );
752 std::string aStdStr_IdxCaptionPathFileURL( aOStr_IdxCaptionPathFileURL );
753
754 idxCaptionStylesheet = fs::path( aStdStr_IdxCaptionPathFileURL );
755 }
756
758 || (!extsource.empty() && idxContentStylesheet.empty()) )
759 {
760 //No extension mode and extension mode using commandline
762 // -idxcontent parameter is required
763 std::stringstream aStrStream;
764 aStrStream << "no index content stylesheet given" << std::endl;
766 }
767 else if ( bExtensionMode && extsource.empty())
768 {
769 //If extension help is compiled using helplinker in the build process
770 //then -idxcontent must be supplied
771 //This part is used when compileExtensionHelp is called from the extensions manager.
772 OUString aIdxContentPathFileURL = *pOfficeHelpPath + "/idxcontent.xsl";
773
774 OString aOStr_IdxContentPathFileURL( OUStringToOString
775 ( aIdxContentPathFileURL, osl_getThreadTextEncoding() ) );
776 std::string aStdStr_IdxContentPathFileURL( aOStr_IdxContentPathFileURL );
777
778 idxContentStylesheet = fs::path( aStdStr_IdxContentPathFileURL );
779 }
781 {
782 std::stringstream aStrStream;
783 aStrStream << "no embedding resolving file given" << std::endl;
785 }
786 if (sourceRoot.empty())
787 {
788 std::stringstream aStrStream;
789 aStrStream << "no sourceroot given" << std::endl;
791 }
793 {
794 std::stringstream aStrStream;
795 aStrStream << "no output file given" << std::endl;
797 }
798 if (module.empty())
799 {
800 std::stringstream aStrStream;
801 aStrStream << "module missing" << std::endl;
803 }
804 if (!bExtensionMode && lang.empty())
805 {
806 std::stringstream aStrStream;
807 aStrStream << "language missing" << std::endl;
809 }
810 link();
811}
812
813// Variable to set an exception in "C" StructuredXMLErrorFunction
815
816extern "C" {
817
818static void StructuredXMLErrorFunction(SAL_UNUSED_PARAMETER void *, xmlErrorPtr error)
819{
820 std::string aErrorMsg = error->message;
821 std::string aXMLParsingFile;
822 if( error->file != nullptr )
823 aXMLParsingFile = error->file;
824 int nXMLParsingLine = error->line;
825 HelpProcessingException* pException = new HelpProcessingException( aErrorMsg, aXMLParsingFile, nXMLParsingLine );
826 GpXMLParsingException = pException;
827
828 // Reset error handler
829 xmlSetStructuredErrorFunc( nullptr, nullptr );
830}
831
832}
833
835{
837 m_aErrorMsg = OStringToOUString( std::string_view(e.m_aErrorMsg), osl_getThreadTextEncoding() );
838 m_aXMLParsingFile = OStringToOUString( std::string_view(e.m_aXMLParsingFile), osl_getThreadTextEncoding() );
840 return *this;
841}
842
843
844// Returns true in case of success, false in case of error
846(
847 const OUString& aOfficeHelpPath,
848 std::u16string_view aExtensionName,
849 std::u16string_view aExtensionLanguageRoot,
850 sal_Int32 nXhpFileCount, const OUString* pXhpFiles,
851 std::u16string_view aDestination,
852 HelpProcessingErrorInfo& o_rHelpProcessingErrorInfo
853)
854{
855 bool bSuccess = true;
856
857 std::vector<std::string> args;
858 args.reserve(nXhpFileCount + 2);
859 args.push_back(std::string("-mod"));
860 OString aOExtensionName = OUStringToOString( aExtensionName, osl_getThreadTextEncoding() );
861 args.push_back(std::string(aOExtensionName));
862
863 for( sal_Int32 iXhp = 0 ; iXhp < nXhpFileCount ; ++iXhp )
864 {
865 OUString aXhpFile = pXhpFiles[iXhp];
866
867 OString aOXhpFile = OUStringToOString( aXhpFile, osl_getThreadTextEncoding() );
868 args.push_back(std::string(aOXhpFile));
869 }
870
871 OString aOExtensionLanguageRoot = OUStringToOString( aExtensionLanguageRoot, osl_getThreadTextEncoding() );
872 const char* pExtensionPath = aOExtensionLanguageRoot.getStr();
873 std::string aStdStrExtensionPath = pExtensionPath;
874 OString aODestination = OUStringToOString(aDestination, osl_getThreadTextEncoding());
875 const char* pDestination = aODestination.getStr();
876 std::string aStdStrDestination = pDestination;
877
878 // Set error handler
879 xmlSetStructuredErrorFunc( nullptr, StructuredXMLErrorFunction );
880 try
881 {
882 HelpLinker aHelpLinker;
883 aHelpLinker.main( args, &aStdStrExtensionPath, &aStdStrDestination, &aOfficeHelpPath );
884 }
885 catch( const HelpProcessingException& e )
886 {
887 if( GpXMLParsingException != nullptr )
888 {
889 o_rHelpProcessingErrorInfo = *GpXMLParsingException;
891 GpXMLParsingException = nullptr;
892 }
893 else
894 {
895 o_rHelpProcessingErrorInfo = e;
896 }
897 bSuccess = false;
898 }
899 // Reset error handler
900 xmlSetStructuredErrorFunc( nullptr, nullptr );
901
902 // i83624: Tree files
903 // The following basically checks if the help.tree is well formed XML.
904 // Apparently there have been cases when translations contained
905 // non-well-formed XML in the past.
906 OUString aTreeFileURL = OUString::Concat(aExtensionLanguageRoot) + "/help.tree";
907 osl::DirectoryItem aTreeFileItem;
908 osl::FileBase::RC rcGet = osl::DirectoryItem::get( aTreeFileURL, aTreeFileItem );
909 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileSize );
910 if( rcGet == osl::FileBase::E_None &&
911 aTreeFileItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None &&
912 aFileStatus.isValid( osl_FileStatus_Mask_FileSize ) )
913 {
914 sal_uInt64 ret, len = aFileStatus.getFileSize();
915 std::unique_ptr<char[]> s(new char[ int(len) ]); // the buffer to hold the installed files
916 osl::File aFile( aTreeFileURL );
917 (void)aFile.open( osl_File_OpenFlag_Read );
918 aFile.read( s.get(), len, ret );
919 aFile.close();
920
921 XML_Parser parser = XML_ParserCreate( nullptr );
922 XML_Status parsed = XML_Parse( parser, s.get(), int( len ), true );
923
924 if (XML_STATUS_ERROR == parsed)
925 {
926 XML_Error nError = XML_GetErrorCode( parser );
927 o_rHelpProcessingErrorInfo.m_eErrorClass = HelpProcessingErrorClass::XmlParsing;
928 o_rHelpProcessingErrorInfo.m_aErrorMsg = OUString::createFromAscii( XML_ErrorString( nError ) );
929 o_rHelpProcessingErrorInfo.m_aXMLParsingFile = aTreeFileURL;
930 // CRASHES!!! o_rHelpProcessingErrorInfo.m_nXMLParsingLine = XML_GetCurrentLineNumber( parser );
931 bSuccess = false;
932 }
933
934 XML_ParserFree( parser );
935 }
936
937 return bSuccess;
938}
939
940/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< std::string, LinkedList > Hashtable
std::unordered_map< std::string, std::string > Stringtable
std::deque< std::string > LinkedList
char tocharlower(char c)
#define HCDBG(foo)
static void writeKeyValue_DBHelp(FILE *pFile, const std::string &aKeyStr, const std::string &aValueStr)
Definition: HelpLinker.cxx:147
static void StructuredXMLErrorFunction(SAL_UNUSED_PARAMETER void *, xmlErrorPtr error)
Definition: HelpLinker.cxx:818
bool compileExtensionHelp(const OUString &aOfficeHelpPath, std::u16string_view aExtensionName, std::u16string_view aExtensionLanguageRoot, sal_Int32 nXhpFileCount, const OUString *pXhpFiles, std::u16string_view aDestination, HelpProcessingErrorInfo &o_rHelpProcessingErrorInfo)
Definition: HelpLinker.cxx:846
static const HelpProcessingException * GpXMLParsingException
Definition: HelpLinker.cxx:814
static std::string getEncodedPath(const std::string &Path)
Definition: HelpLinker.cxx:72
std::string lang
Definition: HelpLinker.hxx:72
std::unique_ptr< IndexerPreProcessor > m_pIndexerPreProcessor
Definition: HelpLinker.hxx:77
fs::path zipdir
Definition: HelpLinker.hxx:67
fs::path embeddStylesheet
Definition: HelpLinker.hxx:64
fs::path sourceRoot
Definition: HelpLinker.hxx:62
Stringtable additionalFiles
Definition: HelpLinker.hxx:60
std::string extensionDestination
Definition: HelpLinker.hxx:74
fs::path compactStylesheet
Definition: HelpLinker.hxx:63
std::string module
Definition: HelpLinker.hxx:71
bool m_bUseLangRoot
Definition: HelpLinker.hxx:78
void initIndexerPreProcessor()
Definition: HelpLinker.cxx:265
bool bExtensionMode
Definition: HelpLinker.hxx:75
fs::path indexDirParentName
Definition: HelpLinker.hxx:76
fs::path idxContentStylesheet
Definition: HelpLinker.hxx:66
fs::path outputFile
Definition: HelpLinker.hxx:68
std::string extensionPath
Definition: HelpLinker.hxx:73
void link()
Definition: HelpLinker.cxx:271
std::string extdestination
Definition: HelpLinker.hxx:70
bool m_bCreateIndex
Definition: HelpLinker.hxx:79
std::vector< std::string > helpFiles
Definition: HelpLinker.hxx:61
L10N_DLLPUBLIC void main(std::vector< std::string > &args, std::string const *pExtensionPath=nullptr, std::string const *pDestination=nullptr, const OUString *pOfficeHelpPath=nullptr)
Definition: HelpLinker.cxx:488
std::string extsource
Definition: HelpLinker.hxx:69
static void addBookmark(FILE *pFile_DBHelp, std::string thishid, const std::string &fileB, const std::string &anchorB, const std::string &jarfileB, const std::string &titleB)
Definition: HelpLinker.cxx:225
fs::path idxCaptionStylesheet
Definition: HelpLinker.hxx:65
xsltStylesheetPtr m_xsltStylesheetPtrCaption
Definition: HelpLinker.hxx:33
fs::path m_fsContentFilesDirName
Definition: HelpLinker.hxx:31
xsltStylesheetPtr m_xsltStylesheetPtrContent
Definition: HelpLinker.hxx:34
IndexerPreProcessor(const fs::path &fsIndexBaseDir, const fs::path &idxCaptionStylesheet, const fs::path &idxContentStylesheet)
Definition: HelpLinker.cxx:49
fs::path m_fsCaptionFilesDirName
Definition: HelpLinker.hxx:30
void processDocument(xmlDocPtr doc, const std::string &EncodedDocPath)
Definition: HelpLinker.cxx:86
std::string document_module
std::unique_ptr< Hashtable > appl_keywords
std::string document_path
std::unique_ptr< Stringtable > appl_helptexts
std::unique_ptr< std::vector< std::string > > appl_hidlist
std::string document_title
xmlDocPtr appl_doc
void append(const char *in)
std::string native_file_string() const
std::string toUTF8() const
bool empty() const
anchor
void * p
#define SAL_WARN(area, stream)
static std::string encode(const std::string &rIn)
Definition: HelpLinker.cxx:203
OUString getString(const Any &_rAny)
@ native
void copy(const fs::path &src, const fs::path &dest)
void create_directory(const fs::path &indexDirName)
int i
index
args
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
parser
SbxDecimal::CmpResult compare(SAL_UNUSED_PARAMETER const SbxDecimal &, SAL_UNUSED_PARAMETER const SbxDecimal &)
HelpProcessingErrorClass m_eErrorClass
Definition: compilehelp.hxx:47
HelpProcessingErrorInfo & operator=(const struct HelpProcessingException &e)
Definition: HelpLinker.cxx:834
HelpProcessingErrorClass m_eErrorClass
Any result