gSAFE  1.3.8
docgen.cpp
1 /* gSAFE - LIB
2  general Sql dAtabase FrontEnd
3  http://hyperprog.com/gsafe/
4 
5  (C) 2005-2013 Peter Deak (hyper80@gmail.com)
6 
7  License: GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
8 
9  docgen.cpp
10 */
11 
12 #include <QtCore>
13 #include <QtXml>
14 
15 #include "datalib.h"
16 #include "dconsole.h"
17 #include "docgen.h"
18 
20 {
21  sdebug("*** HyperDoc::HyperDoc ***");
22  name = n;
23  ignore = false;
24  itermode = false;
25  resourceProviders = new QList<HResourceProvider *>();
26  dtsp = new QList<HTable *>();
27  dtsn = new QList<QString>();
28  fts = new QList<HFloatTables *>();
29  insideIter = NULL;
30  iter_hft_name = "";
31  iter_notfound = "";
32  skip_cond_deep = 0;
33  clear();
35  sdebug("*** HyperDoc::HyperDoc ***END");
36 }
37 
39 {
40  sdebug("*** HyperDoc::~HyperDoc ***");
41  clear();
43  delete resourceProviders;
44  delete dtsp;
45  delete dtsn;
46  delete fts;
47  sdebug("*** HyperDoc::~HyperDoc ***END");
48 }
49 
51 {
52  body = "";
53 }
54 
55 void HyperDoc::set(QString docstr)
56 {
57  body = docstr;
58 }
59 
60 void HyperDoc::add(QString docstr)
61 {
62  body.append(docstr);
63 }
64 
66 {
67  resourceProviders->clear();
68  dtsp->clear();
69  dtsn->clear();
70  fts->clear();
71 }
72 
73 void HyperDoc::addDataSource(HTable *ht,QString redefinedName)
74 {
75  if(ht == NULL)
76  return;
77  dtsp->push_back(ht);
78  if(redefinedName.isEmpty())
79  dtsn->push_back(ht->sqlTableName());
80  else
81  dtsn->push_back(redefinedName);
82 }
83 
85 {
86  resourceProviders->push_back(rp);
87 }
88 
90 {
91  fts->push_back(ft);
92 }
93 
95 {
96  return body;
97 }
98 
99 QString HyperDoc::getData(QString data)
100 {
101  if(data.isEmpty())
102  return QString("");
103 
104  HTable *req_t;
105  HDataField *req_f;
106  QString tname,fname;
107  QList<QString>::iterator namei;
108  QList<HTable *>::iterator datai;
109 
110  tname = data.section(':',0,0);
111  fname = data.section(':',1,1);
112 
113 
114  if(tname == "*")
115  {
116  sdebug(QString("Data requested from table \"%1\" with name \"%2\"").arg(tname).arg(fname));
117  if(insideIter != NULL)
118  {
119  req_f = insideIter->fieldBySqlName(fname,false);
120  if(req_f != NULL)
121  return req_f->dbValueToDispValue(req_f->getValue()).toString();
122  }
123  }
124  else
125  {
126  QString mainname,subname;
127 
128  mainname = tname.section('.',0,0);
129  subname = tname.section('.',1,1);
130 
131  if(subname.isEmpty())
132  subname = mainname;
133 
134  sdebug(QString("Data requested from table \"%1\" (subnamed:\"%2\") with name \"%3\"")
135  .arg(mainname).arg(subname).arg(fname));
136  req_t = getDataTable(mainname);
137  if(req_t != NULL)
138  {
139  req_f = req_t->fieldBySqlName(fname,true,subname);
140  if(req_f != NULL)
141  return req_f->dbValueToDispValue(req_f->getValue()).toString();
142  }
143  }
144  return QString("");
145 }
146 
147 HTable* HyperDoc::getDataTable(QString data)
148 {
149  QList<QString>::iterator namei;
150  QList<HTable *>::iterator datai;
151 
152  namei = dtsn->begin();
153  datai = dtsp->begin();
154  while(namei != dtsn->end())
155  {
156  if(*namei == data)
157  {
158  return *datai;
159  }
160  ++namei;
161  ++datai;
162  }
163  return NULL;
164 }
165 
166 QString HyperDoc::getRes(QString res)
167 {
168  if(res.isEmpty())
169  return QString("");
170 
171  QList<HResourceProvider *>::iterator i;
172 
173  i = resourceProviders->begin();
174  while(i != resourceProviders->end())
175  {
176  if((*i)->hasResource(res))
177  return (*i)->getResource(res);
178  ++i;
179  }
180  return QString("");
181 }
182 
183 HFloatTables* HyperDoc::getHFT(QString res)
184 {
185  if(res.isEmpty())
186  return NULL;
187 
188  QList<HFloatTables *>::iterator i;
189 
190  i = fts->begin();
191  while(i != fts->end())
192  {
193  if((*i)->sqlTableName() == res)
194  return (*i);
195  ++i;
196  }
197  return NULL;
198 }
199 
201 {
202  QString parserdata;
203 
204  sdebug("*** HyperDoc::generateDoc ***");
205 
206  parserdata = body;
207  parserdata.replace("[","<");
208  parserdata.replace("]",">");
209  parserdata.replace("|","\"");
210  QXmlInputSource *source =new QXmlInputSource();
211  source->setData(parserdata);
212 
213  errstr="";
214  QXmlSimpleReader reader;
215  reader.setContentHandler( this );
216  reader.setErrorHandler( this );
217  preprocessed = "";
218  ignore = false;
219  itermode = false;
220  iterbuffer = "";
221  iter_hft_name = "";
222  iter_notfound = "";
223  if(!reader.parse( *source ))
224  {
225  emit errorSignal("Error during the XML parsing! \n: " + errorString());
226  delete source;
227  sdebug("*** HyperDoc::generateDoc ***ERROR");
228  sdebug(errstr);
229  return "";
230  }
231  delete source;
232  source = NULL;
233  sdebug("*** HyperDoc::generateDoc ***");
234  return preprocessed;
235 }
236 
238 void HyperDoc::out(QString o)
239 {
240  if(!itermode)
241  preprocessed.append(o);
242  else
243  iterbuffer.append(o);
244 }
245 
246 bool HyperDoc::startDocument(void)
247 {
248  out("");
249  return true;
250 }
251 
252 bool HyperDoc::endDocument(void)
253 {
254  out("");
255  return true;
256 }
257 
258 bool HyperDoc::startElement( const QString& ns, const QString& ln, const QString& name,const QXmlAttributes& atts)
259 {
260  Q_UNUSED(ns);
261  Q_UNUSED(ln);
262 
263  int i;
264 
265  if(!buffer.isEmpty())
266  {
267  out(buffer);
268  clearBuffer();
269  }
270 
271  if(ignore && name.toLower() == "cond") //we already in an ignored part
272  {
273  ++skip_cond_deep;
274  sdebug("------------- Increment skip_cond_deep");
275  return true;
276  }
277 
278  if(ignore)
279  return true;
280 
281  if(!itermode)
282  if(name.toLower() == "data" || name.toLower() == "res")
283  {
284  att_begin = atts.value("begin");
285  att_end = atts.value("end");
286  att_notfound= atts.value("notfound");
287  return true;
288  }
289 
290  if(name.toLower() == "iterinside")
291  return true;
292 
293  if(name.toLower() == "iter")
294  {
295  if(itermode)
296  emit errorSignal("ERROR:Cannot paste an ITER tag into an another ITER!");
297 
298  iter_hft_name = atts.value("data");
299  iter_notfound = atts.value("notfound");
300  iterbuffer = "<iterinside>";
301  itermode=true;
302  return true;
303  }
304 
305  if(!itermode)
306  if(name.toLower() == "cond")
307  {
308  QString a,b;
309 
310  if(!atts.value("res").isEmpty())
311  a = getRes(atts.value("res"));
312  if(!atts.value("data").isEmpty())
313  a = getData(atts.value("data"));
314 
315  b = atts.value("to");
316 
317  sdebug(QString("HyperDoc-COND \"%1\" with \"%2\" ;").arg(a).arg(b));
318 
319  if(atts.value("is") == "eq")
320  {
321  if(a == b)
322  {
323  sdebug(" - (eq) result:true ");
324  ignore = false;
325  }
326  else
327  {
328  sdebug(" - (eq) result:false ");
329  ignore = true;
330  }
331  }
332  else if(atts.value("is") == "ne")
333  {
334  if(a != b)
335  {
336  sdebug(" - (ne) result:true ");
337  ignore = false;
338  }
339  else
340  {
341  sdebug(" - (ne) result:false ");
342  ignore = true;
343  }
344  }
345  else
346  ignore = true;
347  return true;
348  }
349 
351  buffer = "";
352  out("<"+name);
353  for(i=0;i < atts.count();++i)
354  {
355  out(QString(" %1=\"%2\"").arg(atts.qName(i)).arg(atts.value(i)));
356  }
357 
358  out(">");
359  return true;
360 }
361 
362 bool HyperDoc::endElement( const QString& ns, const QString& ln, const QString& name)
363 {
364  Q_UNUSED(ns);
365  Q_UNUSED(ln);
366  //The data and res tags have to arrive before the emptiyng of the buffer because
367  //they works from buffer. The other not.
368  if(!ignore && !itermode)
369  if(name.toLower() == "data" || name.toLower() == "res")
370  {
371  QString data;
372 
373  if(name.toLower() == "data") data=getData(buffer);
374  if(name.toLower() == "res") data=getRes (buffer);
375 
376  if(data.isEmpty())
377  out(att_notfound);
378  else
379  out(att_begin+data+att_end);
380  clearBuffer();
381  return true;
382  }
383 
384  if(!buffer.isEmpty())
385  {
386  out(buffer);
387  clearBuffer();
388  }
389 
390  if(!itermode)
391  if(name.toLower() == "cond")
392  {
393  if(ignore && skip_cond_deep > 0)
394  {
395  --skip_cond_deep;
396  sdebug("------------- Decrement skip_cond_deep");
397  return true;
398  }
399  sdebug("Reached end of cond: set ingnore to false");
400  ignore = false;
401  return true;
402  }
403 
404  if(name.toLower() == "iterinside")
405  return true;
406 
407  if(name.toLower() == "iter")
408  {
409  int i;
410  HFloatTables *flott;
411  HyperDoc *subdoc;
412 
413  iterbuffer.append("</iterinside>");
414  itermode=false;
415  subdoc =new HyperDoc(name);
416  //copying resources:
417  *(subdoc->resourceProviders) = *resourceProviders;
418  *(subdoc->dtsp) = *dtsp;
419  *(subdoc->dtsn) = *dtsn;
420  *(subdoc->fts) = *fts;
421 
422  subdoc->set(iterbuffer);
423 
424  flott = getHFT(iter_hft_name);
425  if(flott == NULL || flott->count() == 0)
426  out(iter_notfound);
427  else
428  {
429  for(i=0;i<flott->count();++i)
430  {
431  subdoc->insideIter = flott->getTByNum(i);
432  out(subdoc->generateDoc());
433  }
434  }
435  iter_hft_name = "";
436  iter_notfound = "";
437  iterbuffer = "";
438  delete subdoc;
439  }
440 
441  if(ignore)
442  return true;
443 
445  buffer = "";
446  out(QString("</%1>").arg(name));
447  return true;
448 }
449 
450 bool HyperDoc::characters(const QString& ch)
451 {
452  if(!ignore)
453  buffer.append(ch);
454  return true;
455 }
456 
457 bool HyperDoc::processingInstruction(const QString &target,const QString &data)
458 {
459  sdebug("OOOOOOOOOOOOOOOOOOOOOOOOOO"+target+","+data);
460  return true;
461 }
462 
463 bool HyperDoc::skippedEntity(const QString &name)
464 {
465  sdebug("IIIIIIIIIIII Skipped entry:"+name);
466  return true;
467 }
468 
469 void HyperDoc::clearBuffer()
470 {
471  buffer = "";
472  att_begin = "";
473  att_end = "";
474  att_notfound = "";
475 }
476 
477 bool HyperDoc::warning(const QXmlParseException& exception)
478 {
479  errstr = QString("WARNING!\nLIN:%1 COL:%2\npubID:%3 sysID:%4\nMessage:%5\nSorry :-(")
480  .arg(exception.lineNumber())
481  .arg(exception.columnNumber())
482  .arg(exception.publicId())
483  .arg(exception.systemId())
484  .arg(exception.message());
485  return false;
486 }
487 
488 bool HyperDoc::error(const QXmlParseException& exception)
489 {
490  errstr = QString("WARNING!\nLIN:%1 COL:%2\npubID:%3 sysID:%4\nMessage:%5\nSorry :-(")
491  .arg(exception.lineNumber())
492  .arg(exception.columnNumber())
493  .arg(exception.publicId())
494  .arg(exception.systemId())
495  .arg(exception.message());
496  return false;
497 }
498 
499 bool HyperDoc::fatalError(const QXmlParseException& exception)
500 {
501  errstr = QString("WARNING!\nLIN:%1 COL:%2\npubID:%3 sysID:%4\nMessage:%5\nSorry :-(")
502  .arg(exception.lineNumber())
503  .arg(exception.columnNumber())
504  .arg(exception.publicId())
505  .arg(exception.systemId())
506  .arg(exception.message());
507  return false;
508 }
509 //end code.
void clear()
Definition: docgen.cpp:50
~HyperDoc()
Definition: docgen.cpp:38
HDataField * fieldBySqlName(QString sqln, bool all=false, QString containerSqlTable="")
Definition: datalib.cpp:2342
QString sqlTableName(void)
Definition: datalib.cpp:2574
void add(QString docstr)
Definition: docgen.cpp:60
void addDataSource(HTable *ht, QString redefinedName="")
Definition: docgen.cpp:73
void addIterDataTables(HFloatTables *ft)
Definition: docgen.cpp:89
void addResourceProvider(HResourceProvider *rp)
Definition: docgen.cpp:84
int count(void)
Definition: datalib.cpp:4073
void clearResources()
Definition: docgen.cpp:65
HyperDoc(QString n)
Definition: docgen.cpp:19
void set(QString docstr)
Definition: docgen.cpp:55
HTable * getTByNum(int idx)
Definition: datalib.cpp:4126
virtual QVariant dbValueToDispValue(QVariant v)
Definition: datalib.cpp:5309
void sdebug(QString s)
Definition: dconsole.cpp:48
void errorSignal(QString err)
QString getRawDoc()
Definition: docgen.cpp:94
virtual QVariant getValue(void)
Definition: datalib.cpp:5418
QString generateDoc()
Definition: docgen.cpp:200