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/.
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 .
23 #include <HelpCompiler.hxx>
24 #include <BasCodeTagger.hxx>
28 #include <libxslt/xsltInternals.h>
29 #include <libxslt/transform.h>
30 #include <rtl/character.hxx>
31 #include <sal/log.hxx>
34 HelpCompiler::HelpCompiler(StreamTable
&in_streamTable
, fs::path in_inputFile
,
35 fs::path in_src
, fs::path in_zipdir
, fs::path in_resCompactStylesheet
,
36 fs::path in_resEmbStylesheet
, std::string in_module
, std::string in_lang
,
37 bool in_bExtensionMode
)
38 : streamTable(in_streamTable
), inputFile(std::move(in_inputFile
)),
39 src(std::move(in_src
)), zipdir(std::move(in_zipdir
)), module(std::move(in_module
)), lang(std::move(in_lang
)), resCompactStylesheet(std::move(in_resCompactStylesheet
)),
40 resEmbStylesheet(std::move(in_resEmbStylesheet
)), bExtensionMode( in_bExtensionMode
)
42 xmlKeepBlanksDefaultValue
= 0;
43 char* os
= getenv("OS");
46 gui
= (strcmp(os
, "WNT") ? "UNIX" : "WIN");
47 gui
= (strcmp(os
, "MACOSX") ? gui
: "MAC");
51 void HelpCompiler::tagBasicCodeExamples( xmlDocPtr doc
)
55 BasicCodeTagger
bct( doc
);
58 catch ( BasicCodeTagger::TaggerException
&ex
)
60 if ( ex
!= BasicCodeTagger::EMPTY_DOCUMENT
)
65 xmlDocPtr
HelpCompiler::compactXhpForJar( xmlDocPtr doc
)
67 static xsltStylesheetPtr compact
= nullptr;
68 static const char *params
[2 + 1];
74 compact
= xsltParseStylesheetFile(reinterpret_cast<const xmlChar
*>(resCompactStylesheet
.native_file_string().c_str()));
77 compacted
= xsltApplyStylesheet(compact
, doc
, params
);
81 void HelpCompiler::saveXhpForJar( xmlDocPtr doc
, const fs::path
&filePath
)
83 //save processed xhp document in ziptmp<module>_<lang>/text directory
85 std::string pathSep
= "\\";
87 std::string pathSep
= "/";
89 const std::string
& sourceXhpPath
= filePath
.native_file_string();
90 std::string zipdirPath
= zipdir
.native_file_string();
91 const std::string
srcdirPath( src
.native_file_string() );
92 // srcdirPath contains trailing /, but we want the file path with / at the beginning
93 std::string jarXhpPath
= sourceXhpPath
.substr( srcdirPath
.length() - 1 );
94 std::string xhpFileName
= jarXhpPath
.substr( jarXhpPath
.rfind( pathSep
) + 1 );
95 jarXhpPath
= jarXhpPath
.substr( 0, jarXhpPath
.rfind( pathSep
) );
96 if ( !jarXhpPath
.compare( 1, 11, "text" + pathSep
+ "sbasic" ) )
98 tagBasicCodeExamples( doc
);
100 if ( !jarXhpPath
.compare( 1, 11, "text" + pathSep
+ "shared" ) )
102 const size_t pos
= zipdirPath
.find( "ziptmp" );
103 if ( pos
!= std::string::npos
)
104 zipdirPath
.replace( pos
+ 6, module
.length(), "shared" );
106 xmlDocPtr compacted
= compactXhpForJar( doc
);
107 fs::create_directory( fs::path( zipdirPath
+ jarXhpPath
, fs::native
) );
108 if ( -1 == xmlSaveFormatFileEnc( (zipdirPath
+ jarXhpPath
+ pathSep
+ xhpFileName
).c_str(), compacted
, "utf-8", 0 ) )
109 std::cerr
<< "Error saving file to " << (zipdirPath
+ jarXhpPath
+ pathSep
+ xhpFileName
).c_str() << std::endl
;
110 xmlFreeDoc(compacted
);
113 xmlDocPtr
HelpCompiler::getSourceDocument(const fs::path
&filePath
)
118 // this is the mode when used within LibreOffice for importing help
119 // bundled with an extension
120 res
= xmlParseFile(filePath
.native_file_string().c_str());
124 // this is the mode when used at build time to generate LibreOffice
125 // help from its xhp source
126 static xsltStylesheetPtr cur
= nullptr;
127 static const char *params
[2 + 1];
130 static std::string
fsroot('\'' + src
.toUTF8() + '\'');
132 cur
= xsltParseStylesheetFile(reinterpret_cast<const xmlChar
*>(resEmbStylesheet
.native_file_string().c_str()));
135 params
[nbparams
++] = "fsroot";
136 params
[nbparams
++] = fsroot
.c_str();
137 params
[nbparams
] = nullptr;
139 xmlDocPtr doc
= xmlParseFile(filePath
.native_file_string().c_str());
141 saveXhpForJar( doc
, filePath
);
143 res
= xsltApplyStylesheet(cur
, doc
, params
);
149 // returns a node representing the whole stuff compiled for the current
151 xmlNodePtr
HelpCompiler::clone(xmlNodePtr node
, const std::string
& appl
)
153 xmlNodePtr root
= xmlCopyNode(node
, 2);
154 if (node
->xmlChildrenNode
)
156 xmlNodePtr list
= node
->xmlChildrenNode
;
159 if (strcmp(reinterpret_cast<const char*>(list
->name
), "switchinline") == 0 || strcmp(reinterpret_cast<const char*>(list
->name
), "switch") == 0)
162 xmlChar
* prop
= xmlGetProp(list
, reinterpret_cast<xmlChar
const *>("select"));
165 if (strcmp(reinterpret_cast<char *>(prop
), "sys") == 0)
169 else if (strcmp(reinterpret_cast<char *>(prop
), "appl") == 0)
175 if (tmp
.compare("") != 0)
178 xmlNodePtr caseList
=list
->xmlChildrenNode
;
181 xmlChar
*select
= xmlGetProp(caseList
, reinterpret_cast<xmlChar
const *>("select"));
184 if (!strcmp(reinterpret_cast<char*>(select
), tmp
.c_str()) && !isCase
)
187 xmlNodePtr clp
= caseList
->xmlChildrenNode
;
190 xmlAddChild(root
, clone(clp
, appl
));
198 if ((strcmp(reinterpret_cast<const char*>(caseList
->name
), "defaultinline") != 0) && (strcmp(reinterpret_cast<const char*>(caseList
->name
), "default") != 0))
200 xmlAddChild(root
, clone(caseList
, appl
));
206 xmlNodePtr clp
= caseList
->xmlChildrenNode
;
209 xmlAddChild(root
, clone(clp
, appl
));
215 caseList
= caseList
->next
;
221 xmlAddChild(root
, clone(list
, appl
));
234 std::string documentId
;
235 std::string fileName
;
237 std::unique_ptr
< std::vector
<std::string
> > hidlist
;
238 std::unique_ptr
<Hashtable
> keywords
;
239 std::unique_ptr
<Stringtable
> helptexts
;
241 std::vector
<std::string
> extendedHelpText
;
243 myparser(std::string indocumentId
, std::string infileName
,
244 std::string intitle
) : documentId(std::move(indocumentId
)), fileName(std::move(infileName
)),
245 title(std::move(intitle
))
247 hidlist
.reset(new std::vector
<std::string
>);
248 keywords
.reset(new Hashtable
);
249 helptexts
.reset(new Stringtable
);
251 void traverse( xmlNodePtr parentNode
);
253 std::string
dump(xmlNodePtr node
);
258 std::string
myparser::dump(xmlNodePtr node
)
261 if (node
->xmlChildrenNode
)
263 xmlNodePtr list
= node
->xmlChildrenNode
;
270 if (xmlNodeIsText(node
))
272 xmlChar
*pContent
= xmlNodeGetContent(node
);
273 app
+= std::string(reinterpret_cast<char*>(pContent
));
279 static void trim(std::string
& str
)
281 std::string::size_type pos
= str
.find_last_not_of(' ');
282 if(pos
!= std::string::npos
)
285 pos
= str
.find_first_not_of(' ');
286 if(pos
!= std::string::npos
)
293 void myparser::traverse( xmlNodePtr parentNode
)
295 // traverse all nodes that belong to the parent
297 for (test
= parentNode
->xmlChildrenNode
; test
; test
= test
->next
)
299 if (fileName
.empty() && !strcmp(reinterpret_cast<const char*>(test
->name
), "filename"))
301 xmlNodePtr node
= test
->xmlChildrenNode
;
302 if (xmlNodeIsText(node
))
304 xmlChar
*pContent
= xmlNodeGetContent(node
);
305 fileName
= std::string(reinterpret_cast<char*>(pContent
));
309 else if (title
.empty() && !strcmp(reinterpret_cast<const char*>(test
->name
), "title"))
315 else if (!strcmp(reinterpret_cast<const char*>(test
->name
), "bookmark"))
317 xmlChar
*branchxml
= xmlGetProp(test
, reinterpret_cast<const xmlChar
*>("branch"));
318 if (branchxml
== nullptr) {
319 throw HelpProcessingException(
320 HelpProcessingErrorClass::XmlParsing
, "bookmark lacks branch attribute");
322 std::string
branch(reinterpret_cast<char*>(branchxml
));
324 xmlChar
*idxml
= xmlGetProp(test
, reinterpret_cast<const xmlChar
*>("id"));
325 if (idxml
== nullptr) {
326 throw HelpProcessingException(
327 HelpProcessingErrorClass::XmlParsing
, "bookmark lacks id attribute");
329 std::string
anchor(reinterpret_cast<char*>(idxml
));
332 if (branch
.compare(0, 3, "hid") == 0)
334 size_t index
= branch
.find('/');
335 if (index
!= std::string::npos
)
337 auto hid
= branch
.substr(1 + index
);
338 // one shall serve as a documentId
339 if (documentId
.empty())
341 extendedHelpText
.push_back(hid
);
342 HCDBG(std::cerr
<< "hid pushback" << (anchor
.empty() ? hid
: hid
+ "#" + anchor
) << std::endl
);
343 hidlist
->push_back( anchor
.empty() ? hid
: hid
+ "#" + anchor
);
348 else if (branch
.compare("index") == 0)
352 for (xmlNodePtr nd
= test
->xmlChildrenNode
; nd
; nd
= nd
->next
)
354 if (strcmp(reinterpret_cast<const char*>(nd
->name
), "bookmark_value"))
357 std::string embedded
;
358 xmlChar
*embeddedxml
= xmlGetProp(nd
, reinterpret_cast<const xmlChar
*>("embedded"));
361 embedded
= std::string(reinterpret_cast<char*>(embeddedxml
));
362 xmlFree (embeddedxml
);
363 std::transform (embedded
.begin(), embedded
.end(),
364 embedded
.begin(), tocharlower
);
367 bool isEmbedded
= !embedded
.empty() && embedded
.compare("true") == 0;
371 std::string keyword
= dump(nd
);
372 size_t keywordSem
= keyword
.find(';');
373 if (keywordSem
!= std::string::npos
)
376 keyword
.substr(0,keywordSem
);
379 keyword
.substr(1+keywordSem
);
381 keyword
= tmppre
+ ";" + tmppos
;
383 ll
.push_back(keyword
);
386 (*keywords
)[anchor
] = ll
;
388 else if (branch
.compare("contents") == 0)
390 // currently not used
393 else if (!strcmp(reinterpret_cast<const char*>(test
->name
), "ahelp"))
396 std::string text
= dump(test
);
397 std::replace(text
.begin(), text
.end(), '\n', ' ');
401 std::string
hidstr("."); //. == previous seen hid bookmarks
402 xmlChar
*hid
= xmlGetProp(test
, reinterpret_cast<const xmlChar
*>("hid"));
405 hidstr
= std::string(reinterpret_cast<char*>(hid
));
409 if (hidstr
!= "." && !hidstr
.empty()) //simple case of explicitly named target
411 assert(!hidstr
.empty());
412 (*helptexts
)[hidstr
] = text
;
414 else //apply to list of "current" hids determined by recent bookmarks that have hid in their branch
416 //TODO: make these asserts and flush out all our broken help ids
417 SAL_WARN_IF(hidstr
.empty(), "helpcompiler", "hid='' for text:" << text
);
418 SAL_WARN_IF(!hidstr
.empty() && extendedHelpText
.empty(), "helpcompiler", "hid='.' with no hid bookmark branches in file: " << fileName
+ " for text: " << text
);
419 for (const std::string
& name
: extendedHelpText
)
421 (*helptexts
)[name
] = text
;
424 extendedHelpText
.clear();
431 void HelpCompiler::compile()
433 // we now have the jaroutputstream, which will contain the document.
434 // now determine the document as a dom tree in variable docResolved
436 xmlDocPtr docResolvedOrg
= getSourceDocument(inputFile
);
438 // now add path to the document
443 std::stringstream aStrStream
;
444 aStrStream
<< "ERROR: file not existing: " << inputFile
.native_file_string().c_str() << std::endl
;
445 throw HelpProcessingException( HelpProcessingErrorClass::General
, aStrStream
.str() );
448 std::string documentId
;
449 std::string fileName
;
451 // returns a clone of the document with switch-cases resolved
452 std::string appl
= module
.substr(1);
453 for (char & i
: appl
)
455 i
=rtl::toAsciiUpperCase(static_cast<unsigned char>(i
));
457 xmlNodePtr docResolved
= clone(xmlDocGetRootElement(docResolvedOrg
), appl
);
458 myparser
aparser(documentId
, fileName
, title
);
459 aparser
.traverse(docResolved
);
460 documentId
= aparser
.documentId
;
461 fileName
= aparser
.fileName
;
462 title
= aparser
.title
;
464 HCDBG(std::cerr
<< documentId
<< " : " << fileName
<< " : " << title
<< std::endl
);
466 xmlDocPtr docResolvedDoc
= xmlCopyDoc(docResolvedOrg
, false);
467 xmlDocSetRootElement(docResolvedDoc
, docResolved
);
469 streamTable
.dropappl();
470 streamTable
.appl_doc
= docResolvedDoc
;
471 streamTable
.appl_hidlist
= std::move(aparser
.hidlist
);
472 streamTable
.appl_helptexts
= std::move(aparser
.helptexts
);
473 streamTable
.appl_keywords
= std::move(aparser
.keywords
);
475 streamTable
.document_path
= fileName
;
476 streamTable
.document_title
= title
;
477 std::string actMod
= module
;
479 if ( !bExtensionMode
&& !fileName
.empty())
481 if (fileName
.compare(0, 6, "/text/") == 0)
483 actMod
= fileName
.substr(strlen("/text/"));
484 actMod
= actMod
.substr(0, actMod
.find('/'));
487 streamTable
.document_module
= actMod
;
488 xmlFreeDoc(docResolvedOrg
);
493 void create_directory(const fs::path
& indexDirName
)
496 std::cerr
<< "creating " <<
497 OUStringToOString(indexDirName
.data
, RTL_TEXTENCODING_UTF8
).getStr()
500 osl::Directory::createPath(indexDirName
.data
);
503 void copy(const fs::path
&src
, const fs::path
&dest
)
505 osl::File::copy(src
.data
, dest
.data
);
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */