xref: /trunk/main/l10ntools/source/help/HelpCompiler.cxx (revision ce48dd1f26396c7ab9fed48d2df6a6b2bfcf6e06)
13cd96b95SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
33cd96b95SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
43cd96b95SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
53cd96b95SAndrew Rist  * distributed with this work for additional information
63cd96b95SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
73cd96b95SAndrew Rist  * to you under the Apache License, Version 2.0 (the
83cd96b95SAndrew Rist  * "License"); you may not use this file except in compliance
93cd96b95SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
113cd96b95SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
133cd96b95SAndrew Rist  * Unless required by applicable law or agreed to in writing,
143cd96b95SAndrew Rist  * software distributed under the License is distributed on an
153cd96b95SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
163cd96b95SAndrew Rist  * KIND, either express or implied.  See the License for the
173cd96b95SAndrew Rist  * specific language governing permissions and limitations
183cd96b95SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
203cd96b95SAndrew Rist  *************************************************************/
213cd96b95SAndrew Rist 
223cd96b95SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "HelpCompiler.hxx"
25cdf0e10cSrcweir #include <limits.h>
26cdf0e10cSrcweir #include <stdlib.h>
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <libxslt/xslt.h>
29cdf0e10cSrcweir #include <libxslt/xsltInternals.h>
30cdf0e10cSrcweir #include <libxslt/transform.h>
31cdf0e10cSrcweir #include <libxslt/xsltutils.h>
32cdf0e10cSrcweir #ifdef __MINGW32__
33cdf0e10cSrcweir #include <tools/prewin.h>
34cdf0e10cSrcweir #include <tools/postwin.h>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #include <osl/thread.hxx>
37cdf0e10cSrcweir 
impl_sleep(sal_uInt32 nSec)38cdf0e10cSrcweir static void impl_sleep( sal_uInt32 nSec )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     TimeValue aTime;
41cdf0e10cSrcweir     aTime.Seconds = nSec;
42cdf0e10cSrcweir     aTime.Nanosec = 0;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     osl::Thread::wait( aTime );
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
HelpCompiler(StreamTable & in_streamTable,const fs::path & in_inputFile,const fs::path & in_src,const fs::path & in_resEmbStylesheet,const std::string & in_module,const std::string & in_lang,bool in_bExtensionMode)47cdf0e10cSrcweir HelpCompiler::HelpCompiler(StreamTable &in_streamTable, const fs::path &in_inputFile,
48cdf0e10cSrcweir     const fs::path &in_src, const fs::path &in_resEmbStylesheet,
49cdf0e10cSrcweir     const std::string &in_module, const std::string &in_lang, bool in_bExtensionMode)
50cdf0e10cSrcweir     : streamTable(in_streamTable), inputFile(in_inputFile),
51cdf0e10cSrcweir     src(in_src), module(in_module), lang(in_lang), resEmbStylesheet(in_resEmbStylesheet),
52cdf0e10cSrcweir     bExtensionMode( in_bExtensionMode )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     xmlKeepBlanksDefaultValue = 0;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
getSourceDocument(const fs::path & filePath)57cdf0e10cSrcweir xmlDocPtr HelpCompiler::getSourceDocument(const fs::path &filePath)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     static const char *params[4 + 1];
60cdf0e10cSrcweir     static xsltStylesheetPtr cur = NULL;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     xmlDocPtr res;
63cdf0e10cSrcweir     if( bExtensionMode )
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         res = xmlParseFile(filePath.native_file_string().c_str());
66cdf0e10cSrcweir         if( !res ){
67cdf0e10cSrcweir             impl_sleep( 3 );
68cdf0e10cSrcweir             res = xmlParseFile(filePath.native_file_string().c_str());
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir     else
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         if (!cur)
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir             static std::string fsroot('\'' + src.toUTF8() + '\'');
76cdf0e10cSrcweir             static std::string esclang('\'' + lang + '\'');
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             xmlSubstituteEntitiesDefault(1);
79cdf0e10cSrcweir             xmlLoadExtDtdDefaultValue = 1;
80cdf0e10cSrcweir             cur = xsltParseStylesheetFile((const xmlChar *)resEmbStylesheet.native_file_string().c_str());
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             int nbparams = 0;
83cdf0e10cSrcweir             params[nbparams++] = "Language";
84cdf0e10cSrcweir             params[nbparams++] = esclang.c_str();
85cdf0e10cSrcweir             params[nbparams++] = "fsroot";
86cdf0e10cSrcweir             params[nbparams++] = fsroot.c_str();
87cdf0e10cSrcweir             params[nbparams] = NULL;
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         xmlDocPtr doc = xmlParseFile(filePath.native_file_string().c_str());
90cdf0e10cSrcweir         if( !doc )
91cdf0e10cSrcweir         {
92cdf0e10cSrcweir             impl_sleep( 3 );
93cdf0e10cSrcweir             doc = xmlParseFile(filePath.native_file_string().c_str());
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         //???res = xmlParseFile(filePath.native_file_string().c_str());
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         res = xsltApplyStylesheet(cur, doc, params);
99cdf0e10cSrcweir         xmlFreeDoc(doc);
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir     return res;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
switchFind(xmlDocPtr doc)104cdf0e10cSrcweir HashSet HelpCompiler::switchFind(xmlDocPtr doc)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     HashSet hs;
107cdf0e10cSrcweir     xmlChar *xpath = (xmlChar*)"//switchinline";
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     xmlXPathContextPtr context = xmlXPathNewContext(doc);
110cdf0e10cSrcweir     xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context);
111cdf0e10cSrcweir     xmlXPathFreeContext(context);
112cdf0e10cSrcweir     if (result)
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         xmlNodeSetPtr nodeset = result->nodesetval;
115cdf0e10cSrcweir         for (int i = 0; i < nodeset->nodeNr; i++)
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             xmlNodePtr el = nodeset->nodeTab[i];
118cdf0e10cSrcweir             xmlChar *select = xmlGetProp(el, (xmlChar*)"select");
119cdf0e10cSrcweir             if (select)
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 if (!strcmp((const char*)select, "appl"))
122cdf0e10cSrcweir                 {
123cdf0e10cSrcweir                     xmlNodePtr n1 = el->xmlChildrenNode;
124cdf0e10cSrcweir                     while (n1)
125cdf0e10cSrcweir                     {
126cdf0e10cSrcweir                         if ((!xmlStrcmp(n1->name, (const xmlChar*)"caseinline")))
127cdf0e10cSrcweir                         {
128cdf0e10cSrcweir                             xmlChar *appl = xmlGetProp(n1, (xmlChar*)"select");
129cdf0e10cSrcweir                             hs.push_back(std::string((const char*)appl));
130cdf0e10cSrcweir                             xmlFree(appl);
131cdf0e10cSrcweir                         }
132cdf0e10cSrcweir                         else if ((!xmlStrcmp(n1->name, (const xmlChar*)"defaultinline")))
133cdf0e10cSrcweir                             hs.push_back(std::string("DEFAULT"));
134cdf0e10cSrcweir                         n1 = n1->next;
135cdf0e10cSrcweir                     }
136cdf0e10cSrcweir                 }
137cdf0e10cSrcweir                 xmlFree(select);
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir         xmlXPathFreeObject(result);
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir     hs.push_back(std::string("DEFAULT"));
143cdf0e10cSrcweir     return hs;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir // returns a node representing the whole stuff compiled for the current
147cdf0e10cSrcweir // application.
clone(xmlNodePtr node,const std::string & appl)148cdf0e10cSrcweir xmlNodePtr HelpCompiler::clone(xmlNodePtr node, const std::string& appl)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir     xmlNodePtr parent = xmlCopyNode(node, 2);
151cdf0e10cSrcweir     xmlNodePtr n = node->xmlChildrenNode;
152cdf0e10cSrcweir     while (n != NULL)
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         bool isappl = false;
155cdf0e10cSrcweir         if ( (!strcmp((const char*)n->name, "switchinline")) ||
156cdf0e10cSrcweir              (!strcmp((const char*)n->name, "switch")) )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             xmlChar *select = xmlGetProp(n, (xmlChar*)"select");
159cdf0e10cSrcweir             if (select)
160cdf0e10cSrcweir             {
161cdf0e10cSrcweir                 if (!strcmp((const char*)select, "appl"))
162cdf0e10cSrcweir                     isappl = true;
163cdf0e10cSrcweir                 xmlFree(select);
164cdf0e10cSrcweir             }
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         if (isappl)
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             xmlNodePtr caseNode = n->xmlChildrenNode;
169cdf0e10cSrcweir             if (appl == "DEFAULT")
170cdf0e10cSrcweir             {
171cdf0e10cSrcweir                 while (caseNode)
172cdf0e10cSrcweir                 {
173cdf0e10cSrcweir                     if (!strcmp((const char*)caseNode->name, "defaultinline"))
174cdf0e10cSrcweir                     {
175cdf0e10cSrcweir                         xmlNodePtr cnl = caseNode->xmlChildrenNode;
176cdf0e10cSrcweir                         while (cnl)
177cdf0e10cSrcweir                         {
178cdf0e10cSrcweir                             xmlAddChild(parent, clone(cnl, appl));
179cdf0e10cSrcweir                             cnl = cnl->next;
180cdf0e10cSrcweir                         }
181cdf0e10cSrcweir                         break;
182cdf0e10cSrcweir                     }
183cdf0e10cSrcweir                     caseNode = caseNode->next;
184cdf0e10cSrcweir                 }
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir             else
187cdf0e10cSrcweir             {
188cdf0e10cSrcweir                 while (caseNode)
189cdf0e10cSrcweir                 {
190cdf0e10cSrcweir                     isappl=false;
191cdf0e10cSrcweir                     if (!strcmp((const char*)caseNode->name, "caseinline"))
192cdf0e10cSrcweir                     {
193cdf0e10cSrcweir                         xmlChar *select = xmlGetProp(n, (xmlChar*)"select");
194cdf0e10cSrcweir                         if (select)
195cdf0e10cSrcweir                         {
196cdf0e10cSrcweir                             if (!strcmp((const char*)select, appl.c_str()))
197cdf0e10cSrcweir                                 isappl = true;
198cdf0e10cSrcweir                             xmlFree(select);
199cdf0e10cSrcweir                         }
200cdf0e10cSrcweir                         if (isappl)
201cdf0e10cSrcweir                         {
202cdf0e10cSrcweir                             xmlNodePtr cnl = caseNode->xmlChildrenNode;
203cdf0e10cSrcweir                             while (cnl)
204cdf0e10cSrcweir                             {
205cdf0e10cSrcweir                                 xmlAddChild(parent, clone(cnl, appl));
206cdf0e10cSrcweir                                 cnl = cnl->next;
207cdf0e10cSrcweir                             }
208cdf0e10cSrcweir                             break;
209cdf0e10cSrcweir                         }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir                     }
212cdf0e10cSrcweir                     caseNode = caseNode->next;
213cdf0e10cSrcweir                 }
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir         else
218cdf0e10cSrcweir             xmlAddChild(parent, clone(n, appl));
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         n = n->next;
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir     return parent;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir class myparser
226cdf0e10cSrcweir {
227cdf0e10cSrcweir public:
228cdf0e10cSrcweir     std::string documentId;
229cdf0e10cSrcweir     std::string fileName;
230cdf0e10cSrcweir     std::string title;
231cdf0e10cSrcweir     HashSet *hidlist;
232cdf0e10cSrcweir     Hashtable *keywords;
233cdf0e10cSrcweir     Stringtable *helptexts;
234cdf0e10cSrcweir private:
235cdf0e10cSrcweir     HashSet extendedHelpText;
236cdf0e10cSrcweir public:
myparser(const std::string & indocumentId,const std::string & infileName,const std::string & intitle)237cdf0e10cSrcweir     myparser(const std::string &indocumentId, const std::string &infileName,
238cdf0e10cSrcweir         const std::string &intitle) : documentId(indocumentId), fileName(infileName),
239cdf0e10cSrcweir         title(intitle)
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         hidlist = new HashSet;
242cdf0e10cSrcweir         keywords = new Hashtable;
243cdf0e10cSrcweir         helptexts = new Stringtable;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir     void traverse( xmlNodePtr parentNode );
246cdf0e10cSrcweir private:
247cdf0e10cSrcweir     std::string dump(xmlNodePtr node);
248cdf0e10cSrcweir };
249cdf0e10cSrcweir 
dump(xmlNodePtr node)250cdf0e10cSrcweir std::string myparser::dump(xmlNodePtr node)
251cdf0e10cSrcweir {
252cdf0e10cSrcweir     std::string app;
253cdf0e10cSrcweir     if (node->xmlChildrenNode)
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         xmlNodePtr list = node->xmlChildrenNode;
256cdf0e10cSrcweir         while (list)
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             app += dump(list);
259cdf0e10cSrcweir             list = list->next;
260cdf0e10cSrcweir         }
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir     if (xmlNodeIsText(node))
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         xmlChar *pContent = xmlNodeGetContent(node);
265cdf0e10cSrcweir         app += std::string((const char*)pContent);
266cdf0e10cSrcweir         xmlFree(pContent);
267cdf0e10cSrcweir         // std::cout << app << std::endl;
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir     return app;
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
trim(std::string & str)272cdf0e10cSrcweir void trim(std::string& str)
273cdf0e10cSrcweir {
274cdf0e10cSrcweir     std::string::size_type pos = str.find_last_not_of(' ');
275cdf0e10cSrcweir     if(pos != std::string::npos)
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         str.erase(pos + 1);
278cdf0e10cSrcweir         pos = str.find_first_not_of(' ');
279cdf0e10cSrcweir         if(pos != std::string::npos)
280cdf0e10cSrcweir             str.erase(0, pos);
281cdf0e10cSrcweir     }
282cdf0e10cSrcweir     else
283cdf0e10cSrcweir         str.erase(str.begin(), str.end());
284cdf0e10cSrcweir }
285cdf0e10cSrcweir 
traverse(xmlNodePtr parentNode)286cdf0e10cSrcweir void myparser::traverse( xmlNodePtr parentNode )
287cdf0e10cSrcweir {
288cdf0e10cSrcweir     // traverse all nodes that belong to the parent
289cdf0e10cSrcweir     xmlNodePtr test ;
290cdf0e10cSrcweir     for (test = parentNode->xmlChildrenNode; test; test = test->next)
291cdf0e10cSrcweir     {
292cdf0e10cSrcweir         if (fileName.empty() && !strcmp((const char*)test->name, "filename"))
293cdf0e10cSrcweir         {
294cdf0e10cSrcweir             xmlNodePtr node = test->xmlChildrenNode;
295cdf0e10cSrcweir             if (xmlNodeIsText(node))
296cdf0e10cSrcweir             {
297cdf0e10cSrcweir                 xmlChar *pContent = xmlNodeGetContent(node);
298cdf0e10cSrcweir                 fileName = std::string((const char*)pContent);
299cdf0e10cSrcweir                 xmlFree(pContent);
300cdf0e10cSrcweir             }
301cdf0e10cSrcweir         }
302cdf0e10cSrcweir         else if (title.empty() && !strcmp((const char*)test->name, "title"))
303cdf0e10cSrcweir         {
304cdf0e10cSrcweir             title = dump(test);
305cdf0e10cSrcweir             if (title.empty())
306cdf0e10cSrcweir                 title = "<notitle>";
307cdf0e10cSrcweir         }
308cdf0e10cSrcweir         else if (!strcmp((const char*)test->name, "bookmark"))
309cdf0e10cSrcweir         {
310cdf0e10cSrcweir             xmlChar *branchxml = xmlGetProp(test, (const xmlChar*)"branch");
311cdf0e10cSrcweir             xmlChar *idxml = xmlGetProp(test, (const xmlChar*)"id");
312cdf0e10cSrcweir             std::string branch((const char*)branchxml);
313cdf0e10cSrcweir             std::string anchor((const char*)idxml);
314cdf0e10cSrcweir             xmlFree (branchxml);
315cdf0e10cSrcweir             xmlFree (idxml);
316cdf0e10cSrcweir 
317cdf0e10cSrcweir             std::string hid;
318cdf0e10cSrcweir 
319cdf0e10cSrcweir             if (branch.find("hid") == 0)
320cdf0e10cSrcweir             {
321cdf0e10cSrcweir                 size_t index = branch.find('/');
322cdf0e10cSrcweir                 if (index != std::string::npos)
323cdf0e10cSrcweir                 {
324cdf0e10cSrcweir                     hid = branch.substr(1 + index);
325cdf0e10cSrcweir                     // one shall serve as a documentId
326cdf0e10cSrcweir                     if (documentId.empty())
327cdf0e10cSrcweir                         documentId = hid;
328cdf0e10cSrcweir                     extendedHelpText.push_back(hid);
329cdf0e10cSrcweir                     std::string foo = anchor.empty() ? hid : hid + "#" + anchor;
330cdf0e10cSrcweir                     HCDBG(std::cerr << "hid pushback" << foo << std::endl);
331cdf0e10cSrcweir                     hidlist->push_back( anchor.empty() ? hid : hid + "#" + anchor);
332cdf0e10cSrcweir                 }
333cdf0e10cSrcweir                 else
334cdf0e10cSrcweir                     continue;
335cdf0e10cSrcweir             }
336cdf0e10cSrcweir             else if (branch.compare("index") == 0)
337cdf0e10cSrcweir             {
338cdf0e10cSrcweir                 LinkedList ll;
339cdf0e10cSrcweir 
340cdf0e10cSrcweir                 for (xmlNodePtr nd = test->xmlChildrenNode; nd; nd = nd->next)
341cdf0e10cSrcweir                 {
342cdf0e10cSrcweir                     if (strcmp((const char*)nd->name, "bookmark_value"))
343cdf0e10cSrcweir                         continue;
344cdf0e10cSrcweir 
345cdf0e10cSrcweir                     std::string embedded;
346cdf0e10cSrcweir                     xmlChar *embeddedxml = xmlGetProp(nd, (const xmlChar*)"embedded");
347cdf0e10cSrcweir                     if (embeddedxml)
348cdf0e10cSrcweir                     {
349cdf0e10cSrcweir                         embedded = std::string((const char*)embeddedxml);
350cdf0e10cSrcweir                         xmlFree (embeddedxml);
351cdf0e10cSrcweir                         std::transform (embedded.begin(), embedded.end(),
352cdf0e10cSrcweir                             embedded.begin(), tolower);
353cdf0e10cSrcweir                     }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir                     bool isEmbedded = !embedded.empty() && embedded.compare("true") == 0;
356cdf0e10cSrcweir                     if (isEmbedded)
357cdf0e10cSrcweir                         continue;
358cdf0e10cSrcweir 
359cdf0e10cSrcweir                     std::string keyword = dump(nd);
360cdf0e10cSrcweir                     size_t keywordSem = keyword.find(';');
361cdf0e10cSrcweir                     if (keywordSem != std::string::npos)
362cdf0e10cSrcweir                     {
363cdf0e10cSrcweir                         std::string tmppre =
364cdf0e10cSrcweir                                     keyword.substr(0,keywordSem);
365cdf0e10cSrcweir                         trim(tmppre);
366cdf0e10cSrcweir                         std::string tmppos =
367cdf0e10cSrcweir                                     keyword.substr(1+keywordSem);
368cdf0e10cSrcweir                         trim(tmppos);
369cdf0e10cSrcweir                         keyword = tmppre + ";" + tmppos;
370cdf0e10cSrcweir                     }
371cdf0e10cSrcweir                     ll.push_back(keyword);
372cdf0e10cSrcweir                 }
373cdf0e10cSrcweir                 if (!ll.empty())
374cdf0e10cSrcweir                     (*keywords)[anchor] = ll;
375cdf0e10cSrcweir             }
376cdf0e10cSrcweir             else if (branch.compare("contents") == 0)
377cdf0e10cSrcweir             {
378cdf0e10cSrcweir                 // currently not used
379cdf0e10cSrcweir             }
380cdf0e10cSrcweir         }
381cdf0e10cSrcweir         else if (!strcmp((const char*)test->name, "ahelp"))
382cdf0e10cSrcweir         {
383cdf0e10cSrcweir             std::string text = dump(test);
384cdf0e10cSrcweir             trim(text);
385cdf0e10cSrcweir             std::string name;
386cdf0e10cSrcweir 
387cdf0e10cSrcweir             HashSet::const_iterator aEnd = extendedHelpText.end();
388cdf0e10cSrcweir             for (HashSet::const_iterator iter = extendedHelpText.begin(); iter != aEnd;
389cdf0e10cSrcweir                 ++iter)
390cdf0e10cSrcweir             {
391cdf0e10cSrcweir                 name = *iter;
392cdf0e10cSrcweir                 (*helptexts)[name] = text;
393cdf0e10cSrcweir             }
394cdf0e10cSrcweir             extendedHelpText.clear();
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir         // traverse children
398cdf0e10cSrcweir         traverse(test);
399cdf0e10cSrcweir     }
400cdf0e10cSrcweir }
401cdf0e10cSrcweir 
compile(void)402cdf0e10cSrcweir bool HelpCompiler::compile( void ) throw( HelpProcessingException )
403cdf0e10cSrcweir {
404cdf0e10cSrcweir     // we now have the jaroutputstream, which will contain the document.
405cdf0e10cSrcweir     // now determine the document as a dom tree in variable docResolved
406cdf0e10cSrcweir 
407cdf0e10cSrcweir     xmlDocPtr docResolvedOrg = getSourceDocument(inputFile);
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     // now add path to the document
410cdf0e10cSrcweir     // resolve the dom
411cdf0e10cSrcweir     if (!docResolvedOrg)
412cdf0e10cSrcweir     {
413cdf0e10cSrcweir         impl_sleep( 3 );
414cdf0e10cSrcweir         docResolvedOrg = getSourceDocument(inputFile);
415cdf0e10cSrcweir         if( !docResolvedOrg )
416cdf0e10cSrcweir         {
417cdf0e10cSrcweir             std::stringstream aStrStream;
418cdf0e10cSrcweir             aStrStream << "ERROR: file not existing: " << inputFile.native_file_string().c_str() << std::endl;
419cdf0e10cSrcweir             throw HelpProcessingException( HELPPROCESSING_GENERAL_ERROR, aStrStream.str() );
420cdf0e10cSrcweir         }
421cdf0e10cSrcweir     }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     // now find all applications for which one has to compile
424cdf0e10cSrcweir     std::string documentId;
425cdf0e10cSrcweir     std::string fileName;
426cdf0e10cSrcweir     std::string title;
427cdf0e10cSrcweir     // returns all applications for which one has to compile
428cdf0e10cSrcweir     HashSet applications = switchFind(docResolvedOrg);
429cdf0e10cSrcweir 
430cdf0e10cSrcweir     HashSet::const_iterator aEnd = applications.end();
431cdf0e10cSrcweir     for (HashSet::const_iterator aI = applications.begin(); aI != aEnd; ++aI)
432cdf0e10cSrcweir     {
433cdf0e10cSrcweir         std::string appl = *aI;
434cdf0e10cSrcweir         std::string modulename = appl;
435cdf0e10cSrcweir         if (modulename[0] == 'S')
436cdf0e10cSrcweir         {
437cdf0e10cSrcweir             modulename = modulename.substr(1);
438cdf0e10cSrcweir             std::transform(modulename.begin(), modulename.end(), modulename.begin(), tolower);
439cdf0e10cSrcweir         }
440cdf0e10cSrcweir         if (modulename != "DEFAULT" && modulename != module)
441cdf0e10cSrcweir             continue;
442cdf0e10cSrcweir 
443*ce48dd1fSmseidel         // returns a clone of the document with switch-cases resolved
444cdf0e10cSrcweir         xmlNodePtr docResolved = clone(xmlDocGetRootElement(docResolvedOrg), appl);
445cdf0e10cSrcweir         myparser aparser(documentId, fileName, title);
446cdf0e10cSrcweir         aparser.traverse(docResolved);
447cdf0e10cSrcweir 
448cdf0e10cSrcweir         documentId = aparser.documentId;
449cdf0e10cSrcweir         fileName = aparser.fileName;
450cdf0e10cSrcweir         title = aparser.title;
451cdf0e10cSrcweir 
452cdf0e10cSrcweir         HCDBG(std::cerr << documentId << " : " << fileName << " : " << title << std::endl);
453cdf0e10cSrcweir 
454cdf0e10cSrcweir         xmlDocPtr docResolvedDoc = xmlCopyDoc(docResolvedOrg, false);
455cdf0e10cSrcweir         xmlDocSetRootElement(docResolvedDoc, docResolved);
456cdf0e10cSrcweir 
457cdf0e10cSrcweir         if (modulename == "DEFAULT")
458cdf0e10cSrcweir         {
459cdf0e10cSrcweir             streamTable.dropdefault();
460cdf0e10cSrcweir             streamTable.default_doc = docResolvedDoc;
461cdf0e10cSrcweir             streamTable.default_hidlist = aparser.hidlist;
462cdf0e10cSrcweir             streamTable.default_helptexts = aparser.helptexts;
463cdf0e10cSrcweir             streamTable.default_keywords = aparser.keywords;
464cdf0e10cSrcweir         }
465cdf0e10cSrcweir         else if (modulename == module)
466cdf0e10cSrcweir         {
467cdf0e10cSrcweir             streamTable.dropappl();
468cdf0e10cSrcweir             streamTable.appl_doc = docResolvedDoc;
469cdf0e10cSrcweir             streamTable.appl_hidlist = aparser.hidlist;
470cdf0e10cSrcweir             streamTable.appl_helptexts = aparser.helptexts;
471cdf0e10cSrcweir             streamTable.appl_keywords = aparser.keywords;
472cdf0e10cSrcweir         }
473cdf0e10cSrcweir         else
474cdf0e10cSrcweir         {
475cdf0e10cSrcweir             std::stringstream aStrStream;
476cdf0e10cSrcweir             aStrStream << "ERROR: Found unexpected module name \"" << modulename
477cdf0e10cSrcweir                        << "\" in file" << src.native_file_string().c_str() << std::endl;
478cdf0e10cSrcweir             throw HelpProcessingException( HELPPROCESSING_GENERAL_ERROR, aStrStream.str() );
479cdf0e10cSrcweir         }
480cdf0e10cSrcweir 
481cdf0e10cSrcweir     } // end iteration over all applications
482cdf0e10cSrcweir 
483cdf0e10cSrcweir     streamTable.document_id = documentId;
484cdf0e10cSrcweir     streamTable.document_path = fileName;
485cdf0e10cSrcweir     streamTable.document_title = title;
486cdf0e10cSrcweir     std::string actMod = module;
487cdf0e10cSrcweir     if ( !bExtensionMode && !fileName.empty())
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         if (fileName.find("/text/") == 0)
490cdf0e10cSrcweir         {
491cdf0e10cSrcweir             int len = strlen("/text/");
492cdf0e10cSrcweir             actMod = fileName.substr(len);
493cdf0e10cSrcweir             actMod = actMod.substr(0, actMod.find('/'));
494cdf0e10cSrcweir         }
495cdf0e10cSrcweir     }
496cdf0e10cSrcweir     streamTable.document_module = actMod;
497cdf0e10cSrcweir 
498cdf0e10cSrcweir     xmlFreeDoc(docResolvedOrg);
499cdf0e10cSrcweir     return true;
500cdf0e10cSrcweir }
501cdf0e10cSrcweir 
502cdf0e10cSrcweir namespace fs
503cdf0e10cSrcweir {
getThreadTextEncoding(void)504cdf0e10cSrcweir     rtl_TextEncoding getThreadTextEncoding( void )
505cdf0e10cSrcweir     {
506cdf0e10cSrcweir         static bool bNeedsInit = true;
507cdf0e10cSrcweir         static rtl_TextEncoding nThreadTextEncoding;
508cdf0e10cSrcweir         if( bNeedsInit )
509cdf0e10cSrcweir         {
510cdf0e10cSrcweir             bNeedsInit = false;
511cdf0e10cSrcweir             nThreadTextEncoding = osl_getThreadTextEncoding();
512cdf0e10cSrcweir         }
513cdf0e10cSrcweir         return nThreadTextEncoding;
514cdf0e10cSrcweir     }
515cdf0e10cSrcweir 
create_directory(const fs::path indexDirName)516cdf0e10cSrcweir     void create_directory(const fs::path indexDirName)
517cdf0e10cSrcweir     {
518cdf0e10cSrcweir         HCDBG(
519cdf0e10cSrcweir             std::cerr << "creating " <<
520cdf0e10cSrcweir             rtl::OUStringToOString(indexDirName.data, RTL_TEXTENCODING_UTF8).getStr()
521cdf0e10cSrcweir             << std::endl
522cdf0e10cSrcweir              );
523cdf0e10cSrcweir         osl::Directory::createPath(indexDirName.data);
524cdf0e10cSrcweir     }
525cdf0e10cSrcweir 
rename(const fs::path & src,const fs::path & dest)526cdf0e10cSrcweir     void rename(const fs::path &src, const fs::path &dest)
527cdf0e10cSrcweir     {
528cdf0e10cSrcweir         osl::File::move(src.data, dest.data);
529cdf0e10cSrcweir     }
530cdf0e10cSrcweir 
copy(const fs::path & src,const fs::path & dest)531cdf0e10cSrcweir     void copy(const fs::path &src, const fs::path &dest)
532cdf0e10cSrcweir     {
533cdf0e10cSrcweir         osl::File::copy(src.data, dest.data);
534cdf0e10cSrcweir     }
535cdf0e10cSrcweir 
exists(const fs::path & in)536cdf0e10cSrcweir     bool exists(const fs::path &in)
537cdf0e10cSrcweir     {
538cdf0e10cSrcweir         osl::File tmp(in.data);
539cdf0e10cSrcweir         return (tmp.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None);
540cdf0e10cSrcweir     }
541cdf0e10cSrcweir 
remove(const fs::path & in)542cdf0e10cSrcweir     void remove(const fs::path &in)
543cdf0e10cSrcweir     {
544cdf0e10cSrcweir         osl::File::remove(in.data);
545cdf0e10cSrcweir     }
546cdf0e10cSrcweir 
removeRecursive(rtl::OUString const & _suDirURL)547cdf0e10cSrcweir     void removeRecursive(rtl::OUString const& _suDirURL)
548cdf0e10cSrcweir     {
549cdf0e10cSrcweir         {
550cdf0e10cSrcweir             osl::Directory aDir(_suDirURL);
551cdf0e10cSrcweir             aDir.open();
552cdf0e10cSrcweir             if (aDir.isOpen())
553cdf0e10cSrcweir             {
554cdf0e10cSrcweir                 osl::DirectoryItem aItem;
555cdf0e10cSrcweir                 osl::FileStatus aStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes);
556cdf0e10cSrcweir                 while (aDir.getNextItem(aItem) == ::osl::FileBase::E_None)
557cdf0e10cSrcweir                 {
558cdf0e10cSrcweir                     if (osl::FileBase::E_None == aItem.getFileStatus(aStatus) &&
559cdf0e10cSrcweir                         aStatus.isValid(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes))
560cdf0e10cSrcweir                     {
561cdf0e10cSrcweir                         rtl::OUString suFilename = aStatus.getFileName();
562cdf0e10cSrcweir                         rtl::OUString suFullFileURL;
563cdf0e10cSrcweir                         suFullFileURL += _suDirURL;
564cdf0e10cSrcweir                         suFullFileURL += rtl::OUString::createFromAscii("/");
565cdf0e10cSrcweir                         suFullFileURL += suFilename;
566cdf0e10cSrcweir 
567cdf0e10cSrcweir                         if (aStatus.getFileType() == osl::FileStatus::Directory)
568cdf0e10cSrcweir                             removeRecursive(suFullFileURL);
569cdf0e10cSrcweir                         else
570cdf0e10cSrcweir                             osl::File::remove(suFullFileURL);
571cdf0e10cSrcweir                     }
572cdf0e10cSrcweir                 }
573cdf0e10cSrcweir                 aDir.close();
574cdf0e10cSrcweir             }
575cdf0e10cSrcweir         }
576cdf0e10cSrcweir         osl::Directory::remove(_suDirURL);
577cdf0e10cSrcweir     }
578cdf0e10cSrcweir 
remove_all(const fs::path & in)579cdf0e10cSrcweir     void remove_all(const fs::path &in)
580cdf0e10cSrcweir     {
581cdf0e10cSrcweir         removeRecursive(in.data);
582cdf0e10cSrcweir     }
583cdf0e10cSrcweir }
584cdf0e10cSrcweir 
585*ce48dd1fSmseidel /* vim: set noet sw=4 ts=4: */
586