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