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>
33 HelpCompiler::HelpCompiler(StreamTable
&in_streamTable
, const fs::path
&in_inputFile
,
34 const fs::path
&in_src
, const fs::path
&in_zipdir
, const fs::path
&in_resCompactStylesheet
,
35 const fs::path
&in_resEmbStylesheet
, const std::string
&in_module
, const std::string
&in_lang
,
36 bool in_bExtensionMode
)
37 : streamTable(in_streamTable
), inputFile(in_inputFile
),
38 src(in_src
), zipdir(in_zipdir
), module(in_module
), lang(in_lang
), resCompactStylesheet(in_resCompactStylesheet
),
39 resEmbStylesheet(in_resEmbStylesheet
), bExtensionMode( in_bExtensionMode
)
41 xmlKeepBlanksDefaultValue
= 0;
42 char* os
= getenv("OS");
45 gui
= (strcmp(os
, "WNT") ? "UNIX" : "WIN");
46 gui
= (strcmp(os
, "MACOSX") ? gui
: "MAC");
50 void HelpCompiler::tagBasicCodeExamples( xmlDocPtr doc
)
54 BasicCodeTagger
bct( doc
);
57 catch ( BasicCodeTagger::TaggerException
&ex
)
59 if ( ex
!= BasicCodeTagger::EMPTY_DOCUMENT
)
64 xmlDocPtr
HelpCompiler::compactXhpForJar( xmlDocPtr doc
)
66 static xsltStylesheetPtr compact
= nullptr;
67 static const char *params
[2 + 1];
73 compact
= xsltParseStylesheetFile(reinterpret_cast<const xmlChar
*>(resCompactStylesheet
.native_file_string().c_str()));
76 compacted
= xsltApplyStylesheet(compact
, doc
, params
);
80 void HelpCompiler::saveXhpForJar( xmlDocPtr doc
, const fs::path
&filePath
)
82 //save processed xhp document in ziptmp<module>_<lang>/text directory
84 std::string pathSep
= "\\";
86 std::string pathSep
= "/";
88 const std::string
& sourceXhpPath
= filePath
.native_file_string();
89 std::string zipdirPath
= zipdir
.native_file_string();
90 const std::string
srcdirPath( src
.native_file_string() );
91 // srcdirPath contains trailing /, but we want the file path with / at the beginning
92 std::string jarXhpPath
= sourceXhpPath
.substr( srcdirPath
.length() - 1 );
93 std::string xhpFileName
= jarXhpPath
.substr( jarXhpPath
.rfind( pathSep
) + 1 );
94 jarXhpPath
= jarXhpPath
.substr( 0, jarXhpPath
.rfind( pathSep
) );
95 if ( !jarXhpPath
.compare( 1, 11, "text" + pathSep
+ "sbasic" ) )
97 tagBasicCodeExamples( doc
);
99 if ( !jarXhpPath
.compare( 1, 11, "text" + pathSep
+ "shared" ) )
101 const size_t pos
= zipdirPath
.find( "ziptmp" );
102 if ( pos
!= std::string::npos
)
103 zipdirPath
.replace( pos
+ 6, module
.length(), "shared" );
105 xmlDocPtr compacted
= compactXhpForJar( doc
);
106 fs::create_directory( fs::path( zipdirPath
+ jarXhpPath
, fs::native
) );
107 if ( -1 == xmlSaveFormatFileEnc( (zipdirPath
+ jarXhpPath
+ pathSep
+ xhpFileName
).c_str(), compacted
, "utf-8", 0 ) )
108 std::cerr
<< "Error saving file to " << (zipdirPath
+ jarXhpPath
+ pathSep
+ xhpFileName
).c_str() << std::endl
;
109 xmlFreeDoc(compacted
);
112 xmlDocPtr
HelpCompiler::getSourceDocument(const fs::path
&filePath
)
117 // this is the mode when used within LibreOffice for importing help
118 // bundled with an extension
119 res
= xmlParseFile(filePath
.native_file_string().c_str());
123 // this is the mode when used at build time to generate LibreOffice
124 // help from its xhp source
125 static xsltStylesheetPtr cur
= nullptr;
126 static const char *params
[2 + 1];
129 static std::string
fsroot('\'' + src
.toUTF8() + '\'');
131 cur
= xsltParseStylesheetFile(reinterpret_cast<const xmlChar
*>(resEmbStylesheet
.native_file_string().c_str()));
134 params
[nbparams
++] = "fsroot";
135 params
[nbparams
++] = fsroot
.c_str();
136 params
[nbparams
] = nullptr;
138 xmlDocPtr doc
= xmlParseFile(filePath
.native_file_string().c_str());
140 saveXhpForJar( doc
, filePath
);
142 res
= xsltApplyStylesheet(cur
, doc
, params
);
148 // returns a node representing the whole stuff compiled for the current
150 xmlNodePtr
HelpCompiler::clone(xmlNodePtr node
, const std::string
& appl
)
152 xmlNodePtr root
= xmlCopyNode(node
, 2);
153 if (node
->xmlChildrenNode
)
155 xmlNodePtr list
= node
->xmlChildrenNode
;
158 if (strcmp(reinterpret_cast<const char*>(list
->name
), "switchinline") == 0 || strcmp(reinterpret_cast<const char*>(list
->name
), "switch") == 0)
161 xmlChar
* prop
= xmlGetProp(list
, reinterpret_cast<xmlChar
const *>("select"));
164 if (strcmp(reinterpret_cast<char *>(prop
), "sys") == 0)
168 else if (strcmp(reinterpret_cast<char *>(prop
), "appl") == 0)
174 if (tmp
.compare("") != 0)
177 xmlNodePtr caseList
=list
->xmlChildrenNode
;
180 xmlChar
*select
= xmlGetProp(caseList
, reinterpret_cast<xmlChar
const *>("select"));
183 if (!strcmp(reinterpret_cast<char*>(select
), tmp
.c_str()) && !isCase
)
186 xmlNodePtr clp
= caseList
->xmlChildrenNode
;
189 xmlAddChild(root
, clone(clp
, appl
));
197 if ((strcmp(reinterpret_cast<const char*>(caseList
->name
), "defaultinline") != 0) && (strcmp(reinterpret_cast<const char*>(caseList
->name
), "default") != 0))
199 xmlAddChild(root
, clone(caseList
, appl
));
205 xmlNodePtr clp
= caseList
->xmlChildrenNode
;
208 xmlAddChild(root
, clone(clp
, appl
));
214 caseList
= caseList
->next
;
220 xmlAddChild(root
, clone(list
, appl
));
233 std::string documentId
;
234 std::string fileName
;
236 std::unique_ptr
< std::vector
<std::string
> > hidlist
;
237 std::unique_ptr
<Hashtable
> keywords
;
238 std::unique_ptr
<Stringtable
> helptexts
;
240 std::vector
<std::string
> extendedHelpText
;
242 myparser(const std::string
&indocumentId
, const std::string
&infileName
,
243 const std::string
&intitle
) : documentId(indocumentId
), fileName(infileName
),
246 hidlist
.reset(new std::vector
<std::string
>);
247 keywords
.reset(new Hashtable
);
248 helptexts
.reset(new Stringtable
);
250 void traverse( xmlNodePtr parentNode
);
252 std::string
dump(xmlNodePtr node
);
257 std::string
myparser::dump(xmlNodePtr node
)
260 if (node
->xmlChildrenNode
)
262 xmlNodePtr list
= node
->xmlChildrenNode
;
269 if (xmlNodeIsText(node
))
271 xmlChar
*pContent
= xmlNodeGetContent(node
);
272 app
+= std::string(reinterpret_cast<char*>(pContent
));
278 static void trim(std::string
& str
)
280 std::string::size_type pos
= str
.find_last_not_of(' ');
281 if(pos
!= std::string::npos
)
284 pos
= str
.find_first_not_of(' ');
285 if(pos
!= std::string::npos
)
292 void myparser::traverse( xmlNodePtr parentNode
)
294 // traverse all nodes that belong to the parent
296 for (test
= parentNode
->xmlChildrenNode
; test
; test
= test
->next
)
298 if (fileName
.empty() && !strcmp(reinterpret_cast<const char*>(test
->name
), "filename"))
300 xmlNodePtr node
= test
->xmlChildrenNode
;
301 if (xmlNodeIsText(node
))
303 xmlChar
*pContent
= xmlNodeGetContent(node
);
304 fileName
= std::string(reinterpret_cast<char*>(pContent
));
308 else if (title
.empty() && !strcmp(reinterpret_cast<const char*>(test
->name
), "title"))
314 else if (!strcmp(reinterpret_cast<const char*>(test
->name
), "bookmark"))
316 xmlChar
*branchxml
= xmlGetProp(test
, reinterpret_cast<const xmlChar
*>("branch"));
317 if (branchxml
== nullptr) {
318 throw HelpProcessingException(
319 HelpProcessingErrorClass::XmlParsing
, "bookmark lacks branch attribute");
321 std::string
branch(reinterpret_cast<char*>(branchxml
));
323 xmlChar
*idxml
= xmlGetProp(test
, reinterpret_cast<const xmlChar
*>("id"));
324 if (idxml
== nullptr) {
325 throw HelpProcessingException(
326 HelpProcessingErrorClass::XmlParsing
, "bookmark lacks id attribute");
328 std::string
anchor(reinterpret_cast<char*>(idxml
));
331 if (branch
.compare(0, 3, "hid") == 0)
333 size_t index
= branch
.find('/');
334 if (index
!= std::string::npos
)
336 auto hid
= branch
.substr(1 + index
);
337 // one shall serve as a documentId
338 if (documentId
.empty())
340 extendedHelpText
.push_back(hid
);
341 HCDBG(std::cerr
<< "hid pushback" << (anchor
.empty() ? hid
: hid
+ "#" + anchor
) << std::endl
);
342 hidlist
->push_back( anchor
.empty() ? hid
: hid
+ "#" + anchor
);
347 else if (branch
.compare("index") == 0)
351 for (xmlNodePtr nd
= test
->xmlChildrenNode
; nd
; nd
= nd
->next
)
353 if (strcmp(reinterpret_cast<const char*>(nd
->name
), "bookmark_value"))
356 std::string embedded
;
357 xmlChar
*embeddedxml
= xmlGetProp(nd
, reinterpret_cast<const xmlChar
*>("embedded"));
360 embedded
= std::string(reinterpret_cast<char*>(embeddedxml
));
361 xmlFree (embeddedxml
);
362 std::transform (embedded
.begin(), embedded
.end(),
363 embedded
.begin(), tocharlower
);
366 bool isEmbedded
= !embedded
.empty() && embedded
.compare("true") == 0;
370 std::string keyword
= dump(nd
);
371 size_t keywordSem
= keyword
.find(';');
372 if (keywordSem
!= std::string::npos
)
375 keyword
.substr(0,keywordSem
);
378 keyword
.substr(1+keywordSem
);
380 keyword
= tmppre
+ ";" + tmppos
;
382 ll
.push_back(keyword
);
385 (*keywords
)[anchor
] = ll
;
387 else if (branch
.compare("contents") == 0)
389 // currently not used
392 else if (!strcmp(reinterpret_cast<const char*>(test
->name
), "ahelp"))
395 std::string text
= dump(test
);
396 std::replace(text
.begin(), text
.end(), '\n', ' ');
400 std::string
hidstr("."); //. == previous seen hid bookmarks
401 xmlChar
*hid
= xmlGetProp(test
, reinterpret_cast<const xmlChar
*>("hid"));
404 hidstr
= std::string(reinterpret_cast<char*>(hid
));
408 if (hidstr
!= "." && !hidstr
.empty()) //simple case of explicitly named target
410 assert(!hidstr
.empty());
411 (*helptexts
)[hidstr
] = text
;
413 else //apply to list of "current" hids determined by recent bookmarks that have hid in their branch
415 //TODO: make these asserts and flush out all our broken help ids
416 SAL_WARN_IF(hidstr
.empty(), "helpcompiler", "hid='' for text:" << text
);
417 SAL_WARN_IF(!hidstr
.empty() && extendedHelpText
.empty(), "helpcompiler", "hid='.' with no hid bookmark branches in file: " << fileName
+ " for text: " << text
);
418 for (const std::string
& name
: extendedHelpText
)
420 (*helptexts
)[name
] = text
;
423 extendedHelpText
.clear();
430 void HelpCompiler::compile()
432 // we now have the jaroutputstream, which will contain the document.
433 // now determine the document as a dom tree in variable docResolved
435 xmlDocPtr docResolvedOrg
= getSourceDocument(inputFile
);
437 // now add path to the document
442 std::stringstream aStrStream
;
443 aStrStream
<< "ERROR: file not existing: " << inputFile
.native_file_string().c_str() << std::endl
;
444 throw HelpProcessingException( HelpProcessingErrorClass::General
, aStrStream
.str() );
447 std::string documentId
;
448 std::string fileName
;
450 // returns a clone of the document with switch-cases resolved
451 std::string appl
= module
.substr(1);
452 for (char & i
: appl
)
454 i
=rtl::toAsciiUpperCase(static_cast<unsigned char>(i
));
456 xmlNodePtr docResolved
= clone(xmlDocGetRootElement(docResolvedOrg
), appl
);
457 myparser
aparser(documentId
, fileName
, title
);
458 aparser
.traverse(docResolved
);
459 documentId
= aparser
.documentId
;
460 fileName
= aparser
.fileName
;
461 title
= aparser
.title
;
463 HCDBG(std::cerr
<< documentId
<< " : " << fileName
<< " : " << title
<< std::endl
);
465 xmlDocPtr docResolvedDoc
= xmlCopyDoc(docResolvedOrg
, false);
466 xmlDocSetRootElement(docResolvedDoc
, docResolved
);
468 streamTable
.dropappl();
469 streamTable
.appl_doc
= docResolvedDoc
;
470 streamTable
.appl_hidlist
= std::move(aparser
.hidlist
);
471 streamTable
.appl_helptexts
= std::move(aparser
.helptexts
);
472 streamTable
.appl_keywords
= std::move(aparser
.keywords
);
474 streamTable
.document_path
= fileName
;
475 streamTable
.document_title
= title
;
476 std::string actMod
= module
;
478 if ( !bExtensionMode
&& !fileName
.empty())
480 if (fileName
.compare(0, 6, "/text/") == 0)
482 actMod
= fileName
.substr(strlen("/text/"));
483 actMod
= actMod
.substr(0, actMod
.find('/'));
486 streamTable
.document_module
= actMod
;
487 xmlFreeDoc(docResolvedOrg
);
492 void create_directory(const fs::path
& indexDirName
)
495 std::cerr
<< "creating " <<
496 OUStringToOString(indexDirName
.data
, RTL_TEXTENCODING_UTF8
).getStr()
499 osl::Directory::createPath(indexDirName
.data
);
502 void copy(const fs::path
&src
, const fs::path
&dest
)
504 osl::File::copy(src
.data
, dest
.data
);
508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */