tdf#131098 docx export: write fill property of graphic
[LibreOffice.git] / helpcompiler / source / HelpLinker.cxx
blob70b9af1fe4b42d117aa90feef929f649b8c791bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <HelpCompiler.hxx>
21 #include <HelpLinker.hxx>
23 #include <algorithm>
24 #include <fstream>
26 #include <string.h>
28 #include <libxslt/transform.h>
30 #include <sal/types.h>
31 #include <o3tl/char16_t2wchar_t.hxx>
32 #include <sal/log.hxx>
34 #include <expat.h>
35 #include <memory>
37 namespace {
38 FILE* fopen_impl(const fs::path& rPath, const char* szMode)
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
48 IndexerPreProcessor::IndexerPreProcessor
49 ( const fs::path& fsIndexBaseDir,
50 const fs::path& idxCaptionStylesheet, const fs::path& idxContentStylesheet )
52 m_fsCaptionFilesDirName = fsIndexBaseDir / "caption";
53 fs::create_directory( m_fsCaptionFilesDirName );
55 m_fsContentFilesDirName = fsIndexBaseDir / "content";
56 fs::create_directory( m_fsContentFilesDirName );
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()));
64 IndexerPreProcessor::~IndexerPreProcessor()
66 if( m_xsltStylesheetPtrCaption )
67 xsltFreeStylesheet( m_xsltStylesheetPtrCaption );
68 if( m_xsltStylesheetPtrContent )
69 xsltFreeStylesheet( m_xsltStylesheetPtrContent );
72 static std::string getEncodedPath( const std::string& Path )
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;
85 void IndexerPreProcessor::processDocument
86 ( xmlDocPtr doc, const std::string &EncodedDocPath )
88 std::string aStdStr_EncodedDocPathURL = getEncodedPath( EncodedDocPath );
90 if( m_xsltStylesheetPtrCaption )
92 xmlDocPtr resCaption = xsltApplyStylesheet( m_xsltStylesheetPtrCaption, doc, nullptr );
93 xmlNodePtr pResNodeCaption = resCaption->xmlChildrenNode;
94 if( pResNodeCaption )
96 fs::path fsCaptionPureTextFile_docURL = m_fsCaptionFilesDirName / aStdStr_EncodedDocPathURL;
97 FILE* pFile_docURL = fopen_impl( fsCaptionPureTextFile_docURL, "w" );
98 if( pFile_docURL )
100 fprintf( pFile_docURL, "%s\n", pResNodeCaption->content );
101 fclose( pFile_docURL );
104 xmlFreeDoc(resCaption);
107 if( !m_xsltStylesheetPtrContent )
108 return;
110 xmlDocPtr resContent = xsltApplyStylesheet( m_xsltStylesheetPtrContent, doc, nullptr );
111 xmlNodePtr pResNodeContent = resContent->xmlChildrenNode;
112 if( pResNodeContent )
114 fs::path fsContentPureTextFile_docURL = m_fsContentFilesDirName / aStdStr_EncodedDocPathURL;
115 FILE* pFile_docURL = fopen_impl( fsContentPureTextFile_docURL, "w" );
116 if( pFile_docURL )
118 fprintf( pFile_docURL, "%s\n", pResNodeContent->content );
119 fclose( pFile_docURL );
122 xmlFreeDoc(resContent);
125 namespace {
127 struct Data
129 std::vector<std::string> _idList;
131 void append(const std::string &id)
133 _idList.push_back(id);
136 std::string getString() const
138 std::string ret;
139 for (auto const& elem : _idList)
140 ret += elem + ";";
141 return ret;
147 static void writeKeyValue_DBHelp( FILE* pFile, const std::string& aKeyStr, const std::string& aValueStr )
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 )
157 if (fwrite( aKeyStr.c_str(), 1, nKeyLen, pFile ) != nKeyLen)
158 fprintf(stderr, "fwrite to db failed\n");
160 if (fprintf( pFile, " %x ", nValueLen ) < 0)
161 fprintf(stderr, "fwrite to db failed\n");
162 if( nValueLen > 0 )
164 if (fwrite( aValueStr.c_str(), 1, nValueLen, pFile ) != nValueLen)
165 fprintf(stderr, "fwrite to db failed\n");
167 if (fprintf( pFile, "%c", cLF ) < 0)
168 fprintf(stderr, "fwrite to db failed\n");
171 namespace {
173 class HelpKeyword
175 private:
176 typedef std::unordered_map<std::string, Data> DataHashtable;
177 DataHashtable _hash;
179 public:
180 void insert(const std::string &key, const std::string &id)
182 Data &data = _hash[key];
183 data.append(id);
186 void dump_DBHelp( const fs::path& rFileName )
188 FILE* pFile = fopen_impl( rFileName, "wb" );
189 if( pFile == nullptr )
190 return;
192 for (auto const& elem : _hash)
193 writeKeyValue_DBHelp( pFile, elem.first, elem.second.getString() );
195 fclose( pFile );
201 namespace URLEncoder
203 static std::string encode(const std::string &rIn)
205 const char * const good = "!$&'()*+,-.=@_";
206 static const char hex[17] = "0123456789ABCDEF";
208 std::string result;
209 for (char c : rIn)
211 if (rtl::isAsciiAlphanumeric (static_cast<unsigned char>(c))
212 || strchr (good, c))
214 result += c;
215 } else {
216 result += '%';
217 result += hex[static_cast<unsigned char>(c) >> 4];
218 result += hex[c & 0xf];
221 return result;
225 void 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)
229 HCDBG(std::cerr << "HelpLinker::addBookmark " << thishid << " " <<
230 fileB << " " << anchorB << " " << jarfileB << " " << titleB << std::endl);
232 thishid = URLEncoder::encode(thishid);
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();
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())
246 dataB[i++] = '#';
247 for (char j : anchorB)
248 dataB[i++] = j;
250 dataB[i++] = static_cast<unsigned char>(jarfileB.length());
251 for (char j : jarfileB)
252 dataB[i++] = j;
254 dataB[i++] = static_cast<unsigned char>(titleB.length());
255 for (char j : titleB)
256 dataB[i++] = j;
258 if( pFile_DBHelp != nullptr )
260 std::string aValueStr( dataB.begin(), dataB.end() );
261 writeKeyValue_DBHelp( pFile_DBHelp, thishid, aValueStr );
265 void HelpLinker::initIndexerPreProcessor()
267 m_pIndexerPreProcessor.reset( new IndexerPreProcessor( indexDirParentName,
268 idxCaptionStylesheet, idxContentStylesheet ) );
271 void HelpLinker::link()
274 if( bExtensionMode )
276 indexDirParentName = extensionDestination;
278 else
280 indexDirParentName = zipdir;
281 fs::create_directory(indexDirParentName);
284 std::string mod = module;
285 std::transform (mod.begin(), mod.end(), mod.begin(), tocharlower);
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;
294 fs::path helpTextFileName_DBHelp(indexDirParentName / (mod + (bUse_ ? ".ht_" : ".ht")));
295 FILE* pFileHelpText_DBHelp = fopen_impl( helpTextFileName_DBHelp, "wb" );
297 fs::path dbBaseFileName_DBHelp(indexDirParentName / (mod + (bUse_ ? ".db_" : ".db")));
298 FILE* pFileDbBase_DBHelp = fopen_impl( dbBaseFileName_DBHelp, "wb" );
300 fs::path keyWordFileName_DBHelp(indexDirParentName / (mod + (bUse_ ? ".key_" : ".key")));
302 HelpKeyword helpKeyword;
304 // catch HelpProcessingException to avoid locking data bases
307 bool bIndexForExtension = true;
308 // lastly, initialize the indexBuilder
309 if ( (!bExtensionMode || bIndexForExtension) && !helpFiles.empty())
310 initIndexerPreProcessor();
312 // here we start our loop over the hzip files.
313 for (auto const& helpFile : helpFiles)
315 // process one file
316 // streamTable contains the streams in the hzip file
317 StreamTable streamTable;
318 const std::string &xhpFileName = helpFile;
320 if (!bExtensionMode && xhpFileName.rfind(".xhp") != xhpFileName.length()-4)
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)");
328 continue;
331 fs::path langsourceRoot(sourceRoot);
332 fs::path xhpFile;
334 if( bExtensionMode )
336 // langsourceRoot == sourceRoot for extensions
337 std::string xhpFileNameComplete( extensionPath );
338 xhpFileNameComplete.append( '/' + xhpFileName );
339 xhpFile = fs::path( xhpFileNameComplete );
341 else
343 langsourceRoot.append( "/" );
344 if ( m_bUseLangRoot )
345 langsourceRoot.append( lang + '/' );
346 xhpFile = fs::path(xhpFileName, fs::native);
349 HelpCompiler hc( streamTable, std::move(xhpFile), std::move(langsourceRoot), zipdir,
350 compactStylesheet, embeddStylesheet, module, lang, bExtensionMode );
352 HCDBG(std::cerr << "before compile of " << xhpFileName << std::endl);
353 hc.compile();
354 HCDBG(std::cerr << "after compile of " << xhpFileName << std::endl);
356 if (!m_bCreateIndex)
357 continue;
359 std::string documentPath = streamTable.document_path;
360 if (documentPath.compare(0, 1, "/") == 0)
361 documentPath = documentPath.substr(1);
363 std::string documentJarfile = streamTable.document_module + ".jar";
365 std::string documentTitle = streamTable.document_title;
366 if (documentTitle.empty())
367 documentTitle = "<notitle>";
369 const std::string& fileB = documentPath;
370 const std::string& jarfileB = documentJarfile;
371 std::string& titleB = documentTitle;
373 // add once this as its own id.
374 addBookmark( pFileDbBase_DBHelp, documentPath, fileB, std::string(), jarfileB, titleB);
376 const std::vector<std::string> *hidlist = streamTable.appl_hidlist.get();
377 if (hidlist)
379 // now iterate over all elements of the hidlist
380 for (auto & elem : *hidlist)
382 std::string thishid = elem;
384 std::string anchorB;
385 size_t index = thishid.rfind('#');
386 if (index != std::string::npos)
388 anchorB = thishid.substr(1 + index);
389 thishid = thishid.substr(0, index);
391 addBookmark( pFileDbBase_DBHelp, thishid, fileB, anchorB, jarfileB, titleB);
395 // now the keywords
396 const Hashtable *anchorToLL = streamTable.appl_keywords.get();
397 if (anchorToLL && !anchorToLL->empty())
399 std::string fakedHid = URLEncoder::encode(documentPath);
400 for (auto const& elemAnchor : *anchorToLL)
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)
410 helpKeyword.insert(elem, totalId);
416 // and last the helptexts
417 const Stringtable *helpTextHash = streamTable.appl_helptexts.get();
418 if (helpTextHash)
420 for (auto const& elem : *helpTextHash)
422 std::string helpTextId = elem.first;
423 const std::string& helpTextText = elem.second;
425 helpTextId = URLEncoder::encode(helpTextId);
427 if( pFileHelpText_DBHelp != nullptr )
428 writeKeyValue_DBHelp( pFileHelpText_DBHelp, helpTextId, helpTextText );
432 //IndexerPreProcessor
433 if( !bExtensionMode || bIndexForExtension )
435 // now the indexing
436 xmlDocPtr document = streamTable.appl_doc;
437 if (document)
439 std::string temp = module;
440 std::transform (temp.begin(), temp.end(), temp.begin(), tocharlower);
441 m_pIndexerPreProcessor->processDocument(document, URLEncoder::encode(documentPath) );
448 catch( const HelpProcessingException& )
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;
458 if( pFileHelpText_DBHelp != nullptr )
459 fclose( pFileHelpText_DBHelp );
460 if( pFileDbBase_DBHelp != nullptr )
461 fclose( pFileDbBase_DBHelp );
463 helpKeyword.dump_DBHelp( keyWordFileName_DBHelp);
465 if( bExtensionMode )
466 return;
468 // New index
469 for (auto const& additionalFile : additionalFiles)
471 const std::string &additionalFileName = additionalFile.second;
472 const std::string &additionalFileKey = additionalFile.first;
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;
481 fs::path fsTargetName( indexDirParentName / additionalFileKey );
483 fs::copy( fsAdditionalFileName, fsTargetName );
488 void HelpLinker::main( std::vector<std::string> &args,
489 std::string const * pExtensionPath, std::string const * pDestination,
490 const OUString* pOfficeHelpPath )
492 bExtensionMode = false;
493 helpFiles.clear();
495 if ((!args.empty()) && args[0][0] == '@')
497 std::vector<std::string> stringList;
498 std::ifstream fileReader(args[0].substr(1).c_str());
500 while (fileReader)
502 std::string token;
503 fileReader >> token;
504 if (!token.empty())
505 stringList.push_back(token);
507 fileReader.close();
509 args = stringList;
512 size_t i = 0;
513 bool bSrcOption = false;
514 while (i < args.size())
516 if (args[i].compare("-extlangsrc") == 0)
518 ++i;
519 if (i >= args.size())
521 std::stringstream aStrStream;
522 aStrStream << "extension source missing" << std::endl;
523 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
525 extsource = args[i];
527 else if (args[i].compare("-extlangdest") == 0)
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())
534 std::stringstream aStrStream;
535 aStrStream << "extension destination missing" << std::endl;
536 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
538 extdestination = args[i];
540 else if (args[i].compare("-src") == 0)
542 ++i;
543 if (i >= args.size())
545 std::stringstream aStrStream;
546 aStrStream << "sourceroot missing" << std::endl;
547 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
549 bSrcOption = true;
550 sourceRoot = fs::path(args[i], fs::native);
552 else if (args[i].compare("-compact") == 0)
554 ++i;
555 if (i >= args.size())
557 std::stringstream aStrStream;
558 aStrStream << "compactStylesheet missing" << std::endl;
559 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
562 compactStylesheet = fs::path(args[i], fs::native);
564 else if (args[i].compare("-sty") == 0)
566 ++i;
567 if (i >= args.size())
569 std::stringstream aStrStream;
570 aStrStream << "embeddingStylesheet missing" << std::endl;
571 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
574 embeddStylesheet = fs::path(args[i], fs::native);
576 else if (args[i].compare("-zipdir") == 0)
578 ++i;
579 if (i >= args.size())
581 std::stringstream aStrStream;
582 aStrStream << "idxtemp missing" << std::endl;
583 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
586 zipdir = fs::path(args[i], fs::native);
588 else if (args[i].compare("-idxcaption") == 0)
590 ++i;
591 if (i >= args.size())
593 std::stringstream aStrStream;
594 aStrStream << "idxcaption stylesheet missing" << std::endl;
595 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
598 idxCaptionStylesheet = fs::path(args[i], fs::native);
600 else if (args[i].compare("-idxcontent") == 0)
602 ++i;
603 if (i >= args.size())
605 std::stringstream aStrStream;
606 aStrStream << "idxcontent stylesheet missing" << std::endl;
607 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
610 idxContentStylesheet = fs::path(args[i], fs::native);
612 else if (args[i].compare("-o") == 0)
614 ++i;
615 if (i >= args.size())
617 std::stringstream aStrStream;
618 aStrStream << "outputfilename missing" << std::endl;
619 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
622 outputFile = fs::path(args[i], fs::native);
624 else if (args[i].compare("-mod") == 0)
626 ++i;
627 if (i >= args.size())
629 std::stringstream aStrStream;
630 aStrStream << "module name missing" << std::endl;
631 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
634 module = args[i];
636 else if (args[i].compare("-lang") == 0)
638 ++i;
639 if (i >= args.size())
641 std::stringstream aStrStream;
642 aStrStream << "language name missing" << std::endl;
643 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
646 lang = args[i];
648 else if (args[i].compare("-hid") == 0)
650 ++i;
651 throw HelpProcessingException( HelpProcessingErrorClass::General, "obsolete -hid argument used" );
653 else if (args[i].compare("-add") == 0)
655 std::string addFile, addFileUnderPath;
656 ++i;
657 if (i >= args.size())
659 std::stringstream aStrStream;
660 aStrStream << "pathname missing" << std::endl;
661 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
664 addFileUnderPath = args[i];
665 ++i;
666 if (i >= args.size())
668 std::stringstream aStrStream;
669 aStrStream << "pathname missing" << std::endl;
670 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
672 addFile = args[i];
673 if (!addFileUnderPath.empty() && !addFile.empty())
674 additionalFiles[addFileUnderPath] = addFile;
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;
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())
690 bExtensionMode = true;
691 if (!extsource.empty())
693 //called from helplinker.exe, pExtensionPath and pOfficeHelpPath
694 //should be NULL
695 sourceRoot = fs::path(extsource, fs::native);
696 extensionPath = sourceRoot.toUTF8();
698 if (extdestination.empty())
700 std::stringstream aStrStream;
701 aStrStream << "-extlangdest is missing" << std::endl;
702 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
704 else
706 //Convert from system path to file URL!!!
707 fs::path p(extdestination, fs::native);
708 extensionDestination = p.toUTF8();
711 else
713 assert(pExtensionPath);
714 //called from extension manager
715 extensionPath = *pExtensionPath;
716 sourceRoot = fs::path(extensionPath);
717 extensionDestination = *pDestination;
719 //check if -src option was used. This option must not be used
720 //when extension help is compiled.
721 if (bSrcOption)
723 std::stringstream aStrStream;
724 aStrStream << "-src must not be used together with -extsource missing" << std::endl;
725 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
729 if (!bExtensionMode && zipdir.empty())
731 std::stringstream aStrStream;
732 aStrStream << "no index dir given" << std::endl;
733 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
736 if ( (!bExtensionMode && idxCaptionStylesheet.empty())
737 || (!extsource.empty() && idxCaptionStylesheet.empty()) )
739 //No extension mode and extension mode using commandline
740 //!extsource.empty indicates extension mode using commandline
741 // -idxcaption parameter is required
742 std::stringstream aStrStream;
743 aStrStream << "no index caption stylesheet given" << std::endl;
744 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
746 else if ( bExtensionMode && extsource.empty())
748 //This part is used when compileExtensionHelp is called from the extensions manager.
749 //If extension help is compiled using helplinker in the build process
750 OUString aIdxCaptionPathFileURL = *pOfficeHelpPath + "/idxcaption.xsl";
752 OString aOStr_IdxCaptionPathFileURL( OUStringToOString
753 ( aIdxCaptionPathFileURL, osl_getThreadTextEncoding() ) );
754 std::string aStdStr_IdxCaptionPathFileURL( aOStr_IdxCaptionPathFileURL );
756 idxCaptionStylesheet = fs::path( aStdStr_IdxCaptionPathFileURL );
759 if ( (!bExtensionMode && idxContentStylesheet.empty())
760 || (!extsource.empty() && idxContentStylesheet.empty()) )
762 //No extension mode and extension mode using commandline
763 //!extsource.empty indicates extension mode using commandline
764 // -idxcontent parameter is required
765 std::stringstream aStrStream;
766 aStrStream << "no index content stylesheet given" << std::endl;
767 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
769 else if ( bExtensionMode && extsource.empty())
771 //If extension help is compiled using helplinker in the build process
772 //then -idxcontent must be supplied
773 //This part is used when compileExtensionHelp is called from the extensions manager.
774 OUString aIdxContentPathFileURL = *pOfficeHelpPath + "/idxcontent.xsl";
776 OString aOStr_IdxContentPathFileURL( OUStringToOString
777 ( aIdxContentPathFileURL, osl_getThreadTextEncoding() ) );
778 std::string aStdStr_IdxContentPathFileURL( aOStr_IdxContentPathFileURL );
780 idxContentStylesheet = fs::path( aStdStr_IdxContentPathFileURL );
782 if (!bExtensionMode && embeddStylesheet.empty())
784 std::stringstream aStrStream;
785 aStrStream << "no embedding resolving file given" << std::endl;
786 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
788 if (sourceRoot.empty())
790 std::stringstream aStrStream;
791 aStrStream << "no sourceroot given" << std::endl;
792 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
794 if (!bExtensionMode && outputFile.empty())
796 std::stringstream aStrStream;
797 aStrStream << "no output file given" << std::endl;
798 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
800 if (module.empty())
802 std::stringstream aStrStream;
803 aStrStream << "module missing" << std::endl;
804 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
806 if (!bExtensionMode && lang.empty())
808 std::stringstream aStrStream;
809 aStrStream << "language missing" << std::endl;
810 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
812 link();
815 // Variable to set an exception in "C" StructuredXMLErrorFunction
816 static const HelpProcessingException* GpXMLParsingException = nullptr;
818 extern "C" {
820 #if LIBXML_VERSION >= 21200
821 static void StructuredXMLErrorFunction(SAL_UNUSED_PARAMETER void *, const xmlError* error)
822 #else
823 static void StructuredXMLErrorFunction(SAL_UNUSED_PARAMETER void *, xmlErrorPtr error)
824 #endif
826 std::string aXMLParsingFile;
827 if( error->file != nullptr )
828 aXMLParsingFile = error->file;
829 int nXMLParsingLine = error->line;
830 GpXMLParsingException = new HelpProcessingException(error->message, aXMLParsingFile, nXMLParsingLine);
832 // Reset error handler
833 xmlSetStructuredErrorFunc( nullptr, nullptr );
838 HelpProcessingErrorInfo& HelpProcessingErrorInfo::operator=( const struct HelpProcessingException& e )
840 m_eErrorClass = e.m_eErrorClass;
841 m_aErrorMsg = OStringToOUString( std::string_view(e.m_aErrorMsg), osl_getThreadTextEncoding() );
842 m_aXMLParsingFile = OStringToOUString( std::string_view(e.m_aXMLParsingFile), osl_getThreadTextEncoding() );
843 m_nXMLParsingLine = e.m_nXMLParsingLine;
844 return *this;
848 // Returns true in case of success, false in case of error
849 bool compileExtensionHelp
851 const OUString& aOfficeHelpPath,
852 std::u16string_view aExtensionName,
853 std::u16string_view aExtensionLanguageRoot,
854 sal_Int32 nXhpFileCount, const OUString* pXhpFiles,
855 std::u16string_view aDestination,
856 HelpProcessingErrorInfo& o_rHelpProcessingErrorInfo
859 bool bSuccess = true;
861 std::vector<std::string> args;
862 args.reserve(nXhpFileCount + 2);
863 args.push_back(std::string("-mod"));
864 OString aOExtensionName = OUStringToOString( aExtensionName, osl_getThreadTextEncoding() );
865 args.push_back(std::string(aOExtensionName));
867 for( sal_Int32 iXhp = 0 ; iXhp < nXhpFileCount ; ++iXhp )
869 OUString aXhpFile = pXhpFiles[iXhp];
871 OString aOXhpFile = OUStringToOString( aXhpFile, osl_getThreadTextEncoding() );
872 args.push_back(std::string(aOXhpFile));
875 OString aOExtensionLanguageRoot = OUStringToOString( aExtensionLanguageRoot, osl_getThreadTextEncoding() );
876 const char* pExtensionPath = aOExtensionLanguageRoot.getStr();
877 std::string aStdStrExtensionPath = pExtensionPath;
878 OString aODestination = OUStringToOString(aDestination, osl_getThreadTextEncoding());
879 const char* pDestination = aODestination.getStr();
880 std::string aStdStrDestination = pDestination;
882 // Set error handler
883 xmlSetStructuredErrorFunc( nullptr, StructuredXMLErrorFunction );
886 HelpLinker aHelpLinker;
887 aHelpLinker.main( args, &aStdStrExtensionPath, &aStdStrDestination, &aOfficeHelpPath );
889 catch( const HelpProcessingException& e )
891 if( GpXMLParsingException != nullptr )
893 o_rHelpProcessingErrorInfo = *GpXMLParsingException;
894 delete GpXMLParsingException;
895 GpXMLParsingException = nullptr;
897 else
899 o_rHelpProcessingErrorInfo = e;
901 bSuccess = false;
903 // Reset error handler
904 xmlSetStructuredErrorFunc( nullptr, nullptr );
906 // i83624: Tree files
907 // The following basically checks if the help.tree is well formed XML.
908 // Apparently there have been cases when translations contained
909 // non-well-formed XML in the past.
910 OUString aTreeFileURL = OUString::Concat(aExtensionLanguageRoot) + "/help.tree";
911 osl::DirectoryItem aTreeFileItem;
912 osl::FileBase::RC rcGet = osl::DirectoryItem::get( aTreeFileURL, aTreeFileItem );
913 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileSize );
914 if( rcGet == osl::FileBase::E_None &&
915 aTreeFileItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None &&
916 aFileStatus.isValid( osl_FileStatus_Mask_FileSize ) )
918 sal_uInt64 ret, len = aFileStatus.getFileSize();
919 std::unique_ptr<char[]> s(new char[ int(len) ]); // the buffer to hold the installed files
920 osl::File aFile( aTreeFileURL );
921 (void)aFile.open( osl_File_OpenFlag_Read );
922 aFile.read( s.get(), len, ret );
923 aFile.close();
925 XML_Parser parser = XML_ParserCreate( nullptr );
926 XML_Status parsed = XML_Parse( parser, s.get(), int( len ), true );
928 if (XML_STATUS_ERROR == parsed)
930 XML_Error nError = XML_GetErrorCode( parser );
931 o_rHelpProcessingErrorInfo.m_eErrorClass = HelpProcessingErrorClass::XmlParsing;
932 o_rHelpProcessingErrorInfo.m_aErrorMsg = OUString::createFromAscii( XML_ErrorString( nError ) );
933 o_rHelpProcessingErrorInfo.m_aXMLParsingFile = aTreeFileURL;
934 // CRASHES!!! o_rHelpProcessingErrorInfo.m_nXMLParsingLine = XML_GetCurrentLineNumber( parser );
935 bSuccess = false;
938 XML_ParserFree( parser );
941 return bSuccess;
944 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */