Avoid potential negative array index access to cached text.
[LibreOffice.git] / helpcompiler / inc / HelpCompiler.hxx
blob985d207529d0e39b5b8c6eeace444ff94412bd03
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 .
19 #pragma once
21 #include <sal/config.h>
23 #include <deque>
24 #include <memory>
25 #include <string>
26 #include <unordered_map>
27 #include <utility>
28 #include <vector>
30 #include <libxml/parser.h>
32 #include <rtl/ustring.hxx>
33 #include <rtl/character.hxx>
34 #include <osl/process.h>
35 #include <osl/file.hxx>
36 #include <osl/thread.h>
37 #include <o3tl/char16_t2wchar_t.hxx>
39 #include <helpcompiler/compilehelp.hxx>
41 #if OSL_DEBUG_LEVEL > 2
42 #include <iostream>
43 #define HCDBG(foo) do { if (true) foo; } while(false)
44 #else
45 #define HCDBG(foo) do { } while(false)
46 #endif
48 namespace fs
50 enum convert { native };
51 class path
53 public:
54 OUString data;
55 public:
56 path() {}
57 path(const std::string &in, convert)
59 OUString sWorkingDir;
60 osl_getProcessWorkingDir(&sWorkingDir.pData);
61 OUString ustrSystemPath(OStringToOUString(in, FileNameEnc()));
62 osl::File::getFileURLFromSystemPath(ustrSystemPath, data);
63 (void)osl::File::getAbsoluteFileURL(sWorkingDir, data, data);
65 path(const std::string &FileURL)
67 data = OStringToOUString(FileURL, FileNameEnc());
69 std::string native_file_string() const
71 OUString ustrSystemPath;
72 osl::File::getSystemPathFromFileURL(data, ustrSystemPath);
73 OString tmp(OUStringToOString(ustrSystemPath, FileNameEnc()));
74 HCDBG(std::cerr << "native_file_string is " << tmp.getStr() << std::endl);
75 return std::string(tmp);
77 #ifdef _WIN32
78 std::wstring native_file_string_w() const
80 OUString ustrSystemPath;
81 osl::File::getSystemPathFromFileURL(data, ustrSystemPath);
82 return std::wstring(o3tl::toW(ustrSystemPath.getStr()));
84 #endif
85 std::string toUTF8() const
87 OString tmp(OUStringToOString(data, RTL_TEXTENCODING_UTF8));
88 return std::string(tmp);
90 bool empty() const { return data.isEmpty(); }
91 path operator/(const std::string &in) const
93 path ret(*this);
94 HCDBG(std::cerr << "orig was " <<
95 OUStringToOString(ret.data, RTL_TEXTENCODING_UTF8).getStr() << std::endl);
96 OUString ustrSystemPath(OStringToOUString(in, FileNameEnc()));
97 ret.data += "/" + ustrSystemPath;
98 HCDBG(std::cerr << "final is " <<
99 OUStringToOString(ret.data, RTL_TEXTENCODING_UTF8).getStr() << std::endl);
100 return ret;
102 void append(const char *in)
104 OUString ustrSystemPath(OStringToOUString(in, FileNameEnc()));
105 data += ustrSystemPath;
107 void append(const std::string &in) { append(in.c_str()); }
109 private:
110 #ifdef _WIN32
111 // On Windows, libxslt and libxml use UTF-8 path strings
112 static constexpr rtl_TextEncoding FileNameEnc() { return RTL_TEXTENCODING_UTF8; }
113 #else
114 static rtl_TextEncoding FileNameEnc() { return osl_getThreadTextEncoding(); }
115 #endif
119 void create_directory(const fs::path& indexDirName);
120 void copy(const fs::path &src, const fs::path &dest);
124 typedef std::unordered_map<std::string, std::string> Stringtable;
125 typedef std::deque<std::string> LinkedList;
127 typedef std::unordered_map<std::string, LinkedList> Hashtable;
129 class StreamTable
131 public:
132 std::string document_path;
133 std::string document_module;
134 std::string document_title;
136 std::unique_ptr< std::vector<std::string> > appl_hidlist;
137 std::unique_ptr<Hashtable> appl_keywords;
138 std::unique_ptr<Stringtable> appl_helptexts;
139 xmlDocPtr appl_doc;
141 StreamTable() :
142 appl_doc(nullptr)
144 void dropappl()
146 appl_hidlist.reset();
147 appl_keywords.reset();
148 appl_helptexts.reset();
149 if (appl_doc) xmlFreeDoc(appl_doc);
151 ~StreamTable()
153 dropappl();
157 struct HelpProcessingException
159 HelpProcessingErrorClass m_eErrorClass;
160 std::string m_aErrorMsg;
161 std::string m_aXMLParsingFile;
162 int m_nXMLParsingLine;
164 HelpProcessingException( HelpProcessingErrorClass eErrorClass, std::string aErrorMsg )
165 : m_eErrorClass( eErrorClass )
166 , m_aErrorMsg(std::move( aErrorMsg ))
167 , m_nXMLParsingLine( 0 )
169 HelpProcessingException( std::string aErrorMsg, std::string aXMLParsingFile, int nXMLParsingLine )
170 : m_eErrorClass( HelpProcessingErrorClass::XmlParsing )
171 , m_aErrorMsg(std::move( aErrorMsg ))
172 , m_aXMLParsingFile(std::move( aXMLParsingFile ))
173 , m_nXMLParsingLine( nXMLParsingLine )
177 class HelpCompiler
179 public:
180 HelpCompiler(StreamTable &streamTable,
181 fs::path in_inputFile,
182 fs::path in_src,
183 fs::path in_zipdir,
184 fs::path in_resCompactStylesheet,
185 fs::path in_resEmbStylesheet,
186 std::string in_module,
187 std::string in_lang,
188 bool in_bExtensionMode);
189 /// @throws HelpProcessingException
190 /// @throws BasicCodeTagger::TaggerException
191 void compile();
192 private:
193 xmlDocPtr getSourceDocument(const fs::path &filePath);
194 static void tagBasicCodeExamples(xmlDocPtr doc);
195 xmlDocPtr compactXhpForJar(xmlDocPtr doc);
196 void saveXhpForJar(xmlDocPtr doc, const fs::path &filePath);
197 xmlNodePtr clone(xmlNodePtr node, const std::string& appl);
198 StreamTable &streamTable;
199 const fs::path inputFile, src, zipdir;
200 const std::string module, lang;
201 const fs::path resCompactStylesheet;
202 const fs::path resEmbStylesheet;
203 bool bExtensionMode;
204 std::string gui;
207 inline char tocharlower(char c)
209 return static_cast<char>(
210 rtl::toAsciiLowerCase(static_cast<unsigned char>(c)));
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */