update dev300-m58
[ooovba.git] / xmlhelp / source / com / sun / star / help / HelpCompiler.hxx
blob61233d0299fdadfca6dd85af13a9751bde9c6e38
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HelpCompiler.hxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef HELPCOMPILER_HXX
32 #define HELPCOMPILER_HXX
34 #include <string>
35 #include <hash_map>
36 #include <vector>
37 #include <list>
38 #include <fstream>
39 #include <iostream>
40 #include <sstream>
41 #include <algorithm>
42 #include <ctype.h>
43 #ifdef SYSTEM_DB
44 #include <db.h>
45 #else
46 #include <berkeleydb/db.h>
47 #endif
49 #include <boost/shared_ptr.hpp>
51 #include <libxml/xmlmemory.h>
52 #include <libxml/debugXML.h>
53 #include <libxml/HTMLtree.h>
54 #include <libxml/xmlIO.h>
55 #include <libxml/xinclude.h>
56 #include <libxml/catalog.h>
58 #include <rtl/ustring.hxx>
59 #include <osl/thread.h>
60 #include <osl/process.h>
61 #include <osl/file.hxx>
63 #include <xmlhelp/compilehelp.hxx>
65 #define EMULATEORIGINAL 1
67 #ifdef CMCDEBUG
68 #define HCDBG(foo) do { if (1) foo; } while(0)
69 #else
70 #define HCDBG(foo) do { if (0) foo; } while(0)
71 #endif
73 namespace fs
75 rtl_TextEncoding getThreadTextEncoding( void );
77 enum convert { native };
78 class path
80 public:
81 ::rtl::OUString data;
82 public:
83 path() {}
84 path(const path &rOther) : data(rOther.data) {}
85 path(const std::string &in, convert)
87 rtl::OUString sWorkingDir;
88 osl_getProcessWorkingDir(&sWorkingDir.pData);
90 rtl::OString tmp(in.c_str());
91 rtl::OUString ustrSystemPath(rtl::OStringToOUString(tmp, getThreadTextEncoding()));
92 osl::File::getFileURLFromSystemPath(ustrSystemPath, data);
93 osl::File::getAbsoluteFileURL(sWorkingDir, data, data);
95 path(const std::string &FileURL)
97 rtl::OString tmp(FileURL.c_str());
98 data = rtl::OStringToOUString(tmp, getThreadTextEncoding());
100 std::string native_file_string() const
102 ::rtl::OUString ustrSystemPath;
103 osl::File::getSystemPathFromFileURL(data, ustrSystemPath);
104 rtl::OString tmp(rtl::OUStringToOString(ustrSystemPath, getThreadTextEncoding()));
105 HCDBG(std::cerr << "native_file_string is " << tmp.getStr() << std::endl);
106 return std::string(tmp.getStr());
108 std::string native_directory_string() const { return native_file_string(); }
109 std::string toUTF8() const
111 rtl::OString tmp(rtl::OUStringToOString(data, RTL_TEXTENCODING_UTF8));
112 return std::string(tmp.getStr());
114 bool empty() const { return data.getLength() == 0; }
115 path operator/(const std::string &in) const
117 path ret(*this);
118 HCDBG(std::cerr << "orig was " <<
119 rtl::OUStringToOString(ret.data, RTL_TEXTENCODING_UTF8).getStr() << std::endl);
120 rtl::OString tmp(in.c_str());
121 rtl::OUString ustrSystemPath(rtl::OStringToOUString(tmp, getThreadTextEncoding()));
122 ret.data += rtl::OUString(sal_Unicode('/'));
123 ret.data += ustrSystemPath;
124 HCDBG(std::cerr << "final is " <<
125 rtl::OUStringToOString(ret.data, RTL_TEXTENCODING_UTF8).getStr() << std::endl);
126 return ret;
128 void append(const char *in)
130 rtl::OString tmp(in);
131 rtl::OUString ustrSystemPath(rtl::OStringToOUString(tmp, getThreadTextEncoding()));
132 data = data + ustrSystemPath;
134 void append(const std::string &in) { append(in.c_str()); }
137 void create_directory(const fs::path indexDirName);
138 void rename(const fs::path &src, const fs::path &dest);
139 void copy(const fs::path &src, const fs::path &dest);
140 bool exists(const fs::path &in);
141 void remove_all(const fs::path &in);
142 void remove(const fs::path &in);
145 struct joaat_hash
147 size_t operator()(const std::string &str) const
149 size_t hash = 0;
150 const char *key = str.data();
151 for (size_t i = 0; i < str.size(); i++)
153 hash += key[i];
154 hash += (hash << 10);
155 hash ^= (hash >> 6);
157 hash += (hash << 3);
158 hash ^= (hash >> 11);
159 hash += (hash << 15);
160 return hash;
164 #define get16bits(d) ((((sal_uInt32)(((const sal_uInt8 *)(d))[1])) << 8)\
165 +(sal_uInt32)(((const sal_uInt8 *)(d))[0]) )
167 struct SuperFastHash
169 size_t operator()(const std::string &str) const
171 const char * data = str.data();
172 int len = str.size();
173 size_t hash = len, tmp;
174 if (len <= 0 || data == NULL) return 0;
176 int rem = len & 3;
177 len >>= 2;
179 /* Main loop */
180 for (;len > 0; len--)
182 hash += get16bits (data);
183 tmp = (get16bits (data+2) << 11) ^ hash;
184 hash = (hash << 16) ^ tmp;
185 data += 2*sizeof (sal_uInt16);
186 hash += hash >> 11;
189 /* Handle end cases */
190 switch (rem)
192 case 3: hash += get16bits (data);
193 hash ^= hash << 16;
194 hash ^= data[sizeof (sal_uInt16)] << 18;
195 hash += hash >> 11;
196 break;
197 case 2: hash += get16bits (data);
198 hash ^= hash << 11;
199 hash += hash >> 17;
200 break;
201 case 1: hash += *data;
202 hash ^= hash << 10;
203 hash += hash >> 1;
206 /* Force "avalanching" of final 127 bits */
207 hash ^= hash << 3;
208 hash += hash >> 5;
209 hash ^= hash << 4;
210 hash += hash >> 17;
211 hash ^= hash << 25;
212 hash += hash >> 6;
214 return hash;
218 #define pref_hash joaat_hash
220 typedef std::hash_map<std::string, std::string, pref_hash> Stringtable;
221 typedef std::list<std::string> LinkedList;
222 typedef std::vector<std::string> HashSet;
224 typedef std::hash_map<std::string, LinkedList, pref_hash> Hashtable;
226 class StreamTable
228 public:
229 std::string document_id;
230 std::string document_path;
231 std::string document_module;
232 std::string document_title;
234 HashSet *appl_hidlist;
235 Hashtable *appl_keywords;
236 Stringtable *appl_helptexts;
237 xmlDocPtr appl_doc;
239 HashSet *default_hidlist;
240 Hashtable *default_keywords;
241 Stringtable *default_helptexts;
242 xmlDocPtr default_doc;
244 StreamTable() :
245 appl_hidlist(NULL), appl_keywords(NULL), appl_helptexts(NULL), appl_doc(NULL),
246 default_hidlist(NULL), default_keywords(NULL), default_helptexts(NULL), default_doc(NULL)
248 void dropdefault()
250 delete default_hidlist;
251 delete default_keywords;
252 delete default_helptexts;
253 if (default_doc) xmlFreeDoc(default_doc);
255 void dropappl()
257 delete appl_hidlist;
258 delete appl_keywords;
259 delete appl_helptexts;
260 if (appl_doc) xmlFreeDoc(appl_doc);
262 ~StreamTable()
264 dropappl();
265 dropdefault();
269 struct HelpProcessingException
271 HelpProcessingErrorClass m_eErrorClass;
272 std::string m_aErrorMsg;
273 std::string m_aXMLParsingFile;
274 int m_nXMLParsingLine;
276 HelpProcessingException( HelpProcessingErrorClass eErrorClass, const std::string& aErrorMsg )
277 : m_eErrorClass( eErrorClass )
278 , m_aErrorMsg( aErrorMsg )
280 HelpProcessingException( const std::string& aErrorMsg, const std::string& aXMLParsingFile, int nXMLParsingLine )
281 : m_eErrorClass( HELPPROCESSING_XMLPARSING_ERROR )
282 , m_aErrorMsg( aErrorMsg )
283 , m_aXMLParsingFile( aXMLParsingFile )
284 , m_nXMLParsingLine( nXMLParsingLine )
288 class HelpCompiler
290 public:
291 HelpCompiler(StreamTable &streamTable,
292 const fs::path &in_inputFile,
293 const fs::path &in_src,
294 const fs::path &in_resEmbStylesheet,
295 const std::string &in_module,
296 const std::string &in_lang,
297 bool in_bExtensionMode);
298 bool compile( void ) throw (HelpProcessingException);
299 void addEntryToJarFile(const std::string &prefix,
300 const std::string &entryName, const std::string &bytesToAdd);
301 void addEntryToJarFile(const std::string &prefix,
302 const std::string &entryName, const HashSet &bytesToAdd);
303 void addEntryToJarFile(const std::string &prefix,
304 const std::string &entryName, const Stringtable &bytesToAdd);
305 void addEntryToJarFile(const std::string &prefix,
306 const std::string &entryName, const Hashtable &bytesToAdd);
307 private:
308 xmlDocPtr getSourceDocument(const fs::path &filePath);
309 HashSet switchFind(xmlDocPtr doc);
310 xmlNodePtr clone(xmlNodePtr node, const std::string& appl);
311 StreamTable &streamTable;
312 const fs::path inputFile, src;
313 const std::string module, lang;
314 const fs::path resEmbStylesheet;
315 bool bExtensionMode;
318 #endif
320 /* vi:set tabstop=4 shiftwidth=4 expandtab: */