WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZxQuery.cpp
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2013, 2014 Bob Dinitto
4  *
5  * ZxQuery.cpp
6  *
7  * Created on: 29-Sep, 2013
8  * Author: Bob Dinitto bob@ninzo.com
9  *
10  * Zuble is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 #include "ZxQuery.h"
27 #include <QtQml>
28 #include <QFile>
29 #include "ZblThreadErr.h"
30 #include "zglobal.h"
31 //#include "ZmlEngine.h"
32 //#include "XmlDocument.h"
33 //#include "XmlFactory.h"
34 #include "ZxItem.h"
35 #include "ZApplication.h"
36 
37 
38 namespace Zbl
39 {
40 
42 
43 
44 ZxQuery::ZxQuery(QObject *parent) :
45  QObject(parent), m_xmlItem(0)
46 {
47 }
48 
49 ZxQuery::ZxQuery(const ZxQuery& source, QObject *parent) :
50  QObject(parent),
51  m_query(source.m_query),
52  m_queryText(source.m_queryText),
53  m_focusText(source.m_focusText),
54  m_xmlItem(source.m_xmlItem),
55  m_enableSymbols(false)
56 {
57  m_queryUrl = source.m_queryUrl;
58  //m_focusUrl = source.m_focusUrl;
60 }
61 
62 ZxQuery::ZxQuery(const QXmlNamePool & np, QObject *parent) :
63  QObject(parent), m_query(np), m_xmlItem(0),
64  m_enableSymbols(false)
65 {
66 }
67 
68 void ZxQuery::setQueryUrl(const QString& url)
69 {
70  zDebug() << "ZxQuery::setQueryUrl: %s" << cStr(url);
71 
72  m_queryFile.setUrl(url);
73 
74  m_queryText.clear();
75 
76  QString scheme(ZApplication::getPathScheme(url));
77 
78  bool queryOK = false;
79 
81  {
82  zDebug() << "ZxQuery::setQueryUrl: opening resource file" << cStr(url);
83 
84  if(!m_queryFile.open())
85  {
86  zWarning() << "The XQuery failed. " << "Can't open query resource URL: "
87  << url << " File error: " << m_queryFile.errorString();
88 
89  return;
90  }
91 
92  m_query.setQuery(m_queryFile.m_f);
93 
94  queryOK = m_query.isValid();
95 
96  if(!queryOK)
97  {
98  zDebug() << "The XQuery failed for query URL: " << url;
99  }
100  }
101  else
102  {
103  m_query.setQuery(m_queryFile.m_url);
104 
105  queryOK = m_query.isValid();
106 
107  if(!queryOK)
108  {
109  zDebug() << "The XQuery failed for query URL: " << url;
110  }
111  }
112 
113  emit queryUrlChanged(queryOK);
114 }
115 
116 const QString ZxQuery::getQueryUrl()
117 {
118  return m_queryUrl.url();
119 }
120 
121 void ZxQuery::setFocusUrl(const QString& url)
122 {
123  zDebug() << "ZxQuery::setFocusUrl: %s" << cStr(url);
124 
125  m_focusFile.setUrl(url);
126 
127  QString scheme(ZApplication::getPathScheme(url));
128 
129  bool focusOK = true;
130 
132  {
133  zDebug() << "ZxQuery::setFocusUrl: opening resource file" << cStr(url);
134 
135  if(!m_focusFile.open())
136  {
137  zDebug() << "XQuery can't open resource for focus URL: " << url << " File error: " << m_focusFile.errorString();
138 
139  return;
140  }
141 
142  focusOK = m_query.setFocus(m_focusFile.m_f);
143 
144  if(!focusOK)
145  {
146  zDebug() << "Invalid XQuery focus for resource URL: " << url;
147  }
148  }
149  else
150  {
151  focusOK = m_query.setFocus(m_focusFile.m_url);
152 
153  if(!focusOK)
154  {
155  zDebug() << "Invalid XQuery focus for URL: " << url;
156  }
157  }
158 
159  emit focusUrlChanged(focusOK);
160 }
161 
162 const QString ZxQuery::getFocusUrl()
163 {
164  return m_focusFile.getUrl();
165 }
166 
167 void ZxQuery::setFocusText(const QString& text)
168 {
169  zDebug() << "ZxQuery::setFocusText";
170 
171  zDebug() << "TEXT=" << text;
172 
174 
175  m_focusText = text;
176 
177  m_query.setFocus(text);
178 }
179 
180 const QString ZxQuery::getFocusText()
181 {
182  return m_focusText;
183 }
184 
185 void ZxQuery::setFocusItem(QObject* item)
186 {
187  zDebug() << "ZxQuery::setFocusItem";
188 
189  Zbl::ZxItem* zItem = qobject_cast<Zbl::ZxItem*>(item);
190 
191  if(!zItem)
192  {
193  const char* className = item->metaObject()->className();
194 
195  zDebug() << "The XQuery failed for focus Item: " << className << ", Item object should be of class Zbl::ZxItem";
196 
197  return;
198  }
199 
200  //m_queryText.clear( ); TBD: wtf?
201 
202  //m_focusUrl.clear();
203 
204  m_query.setFocus(zItem->m_item);
205 
206 }
207 
209 {
210  //ZxItem* newItem = new ZxItem();
211  ZxItem* newItem = NULL;
212 
213  if(!m_xmlItem.isNull())
214  newItem->setValue(m_xmlItem, this);
215 
216  return newItem;
217 }
218 
219 void ZxQuery::setQueryText(const QString& text)
220 {
221  zDebug() << "ZxQuery::setQueryText";
222 
223  zDebug() << "QUERY=" << text;
224 
225  m_queryText = text;
226 
227  if(!text.isEmpty())
228  {
229  m_queryUrl.clear( );
230 
231  m_query.setQuery(m_queryText);
232 
233  if(!m_query.isValid())
234  {
235  zDebug() << "Invalid XQuery: " << m_queryText.left(100) << "...";
236  }
237  }
238 }
239 
240 const QString ZxQuery::getQueryText()
241 {
242  return m_queryText;
243 }
244 
245 #if 0
246 void ZxQuery::clearQueryText()
247 {
248  m_queryText.clear();
249 }
250 #endif
251 
253 {
255 
256  qmlRegisterType<ZxQuery>("org.zuble.qml", 1, 0, "ZxQuery");
257 }
258 
260 {
261  zDebug() << "ZxQuery::evaluateToItems";
262 
263  ZxResultItems* items = new ZxResultItems(this);
264 
265  QXmlResultItems result;
266 
267  if (m_query.isValid())
268  {
269  m_query.evaluateTo(&result);
270 
271  /*
272  if(!result.hasError())
273  {
274  zDebug() << "XQuery evaluation failed for url: %s", cStr(m_srcUrl.url()));
275 
276  QString msg("The XQuery evaluation failed for query URL: %1");
277 
278  zDebug() << "ZxQuery", "Evaluation Failed", msg.arg(m_srcUrl.url()));
279  }
280  else
281  {
282  */
283  for(QXmlItem* item = new QXmlItem(result.next()); !item->isNull();
284  item = new QXmlItem(result.next()))
285  {
286  if (result.hasError())
287  {
288  zWarning() << "The XQuery evaluation failed for query URL: " << m_queryUrl.url();
289 
290  break;
291  }
292 
293  items->m_items.append(item);
294  }
295  //}
296  }
297  else
298  {
299  zWarning() << "The XQuery was invalid for query URL: " << m_queryUrl.url();
300  }
301 
302  zDebug() << "ZxQuery::evaluateToItems found " << items->m_items.size() << " items";
303 
304  return items;
305 }
306 
308 {
309  zDebug() << "ZxQuery::evaluateToStringList";
310 
311  QStringList result;
312 
313  if(m_query.isValid())
314  {
315  if(!m_query.evaluateTo(&result))
316  {
317  QString msg("The XQuery evaluation failed for query URL: %1");
318 
319  zDebug() << "Evaluation Failed" << m_queryUrl.url();
320  }
321  }
322 
323  return result;
324 }
325 
327 {
328  zDebug() << "ZxQuery::evaluateToXml";
329 
330  QString result;
331 
332  if(m_query.isValid())
333  {
334  if(!m_query.evaluateTo(&result))
335  {
336  zDebug() << "The XQuery evaluation failed for query URL: " << m_queryUrl.url();
337  }
338  }
339 
340  return result;
341 }
342 
343 QXmlNamePool ZxQuery::getNamePool() const
344 {
345  return m_query.namePool();
346 }
347 
349 {
350  return new ZxQuery(*this, parent());
351 }
352 
354 {
355  return new ZxQuery(m_query.namePool(), parent());
356 }
357 
359 {
360  if(m_f)
361  m_f->deleteLater();
362 }
363 
365 {
366  resetFile();
367 
368  QString resourcePath = m_url.url();
369 
370  resourcePath = ZApplication::resolvePath(resourcePath, isResourceScheme(resourcePath));
371 
372  m_f->setFileName(resourcePath);
373 
374  zDebug() << "ZxQuery::ResourceUrlFile::open setting resource: " << resourcePath;
375 
376  if(!m_f->open(QIODevice::ReadOnly))
377  {
378  zWarning() << "Resource failed to open: " << resourcePath;
379 
380  return false;
381  }
382 
383  return true;
384 }
385 
387 {
388  if(!m_f)
389  m_f = new QFile();
390  else
391  closeFile();
392 }
393 
395 {
396  if(m_f && m_f->isOpen())
397  m_f->close();
398 }
399 
401 {
402  if(m_f)
403  {
404  delete m_f;
405  m_f = NULL;
406  }
407 }
408 
409 void ZxQuery::ResourceUrlFile::setUrl(const QString& url)
410 {
411  if(url != m_url.url())
412  {
413  closeFile();
414  m_url = url;
415  }
416 }
417 
418 
419 void ZxQuery::bindVariable(const QString &localName, QObject* zXmlItem)
420 {
421 
422  ZxItem* item = qobject_cast<ZxItem*>(zXmlItem);
423 
424  if(!item)
425  {
426  zWarning() << "ZxQuery::bindVariable FAILED -- invalid object type: "
427  << "xZmlItem parameter must be a ZxItem object.";
428 
429  return;
430  }
431 
432  m_query.bindVariable(localName, item->m_item);
433 }
434 
435 
436 
438 {
439  m_enableSymbols = enabled;
440 }
441 
443 {
444  return m_enableSymbols;
445 }
446 
447 void ZxQuery::loadQueryText(const QString& fileUri)
448 {
449  ResourceUrlFile resFile;
450 
451  resFile.setUrl(fileUri);
452 
453  bool queryOK = false;
454 
455  zDebug() << "ZxQuery::loadQueryText: opening resource file " << fileUri;
456 
457  if(!resFile.open())
458  {
459  zWarning() << "The XQuery failed. " << "Can't open query resource URL: "
460  << fileUri << " File error: " << resFile.errorString();
461 
462  return;
463  }
464 
465  QFile* f = resFile.m_f;
466 
467  while(!f->atEnd() )
468  {
469  QString b(f->readLine(65535));
470 
471  if(f->error() != QFileDevice::NoError)
472  {
473  zWarning() << "ZxQuery::loadQueryText error while reading file: " << fileUri;
474  zWarning() << "ZxQuery::loadQueryText ABORTING, file error = " << f->errorString();
475  break;
476  }
477 
478  m_queryText += b;
479  }
480 
481  //zDebug() << "Query text buffer after load = " << m_queryText;
482 }
483 
484 void ZxQuery::concatQueryText(const QString& text)
485 {
486  zDebug() << "ZxQuery::concatQueryText";
487 
488  m_queryText += text;
489 
490  //zDebug() << "Query text buffer after concat = " << m_queryText;
491 
492 }
493 
495 {
496  zDebug() << "ZxQuery::commitQueryText";
497 
498  m_query.setQuery(m_queryText);
499 
500  if(!m_query.isValid())
501  {
502  zWarning() << "The XQuery failed for query text: " << m_queryText.left(100) << "...";
503  }
504 }
505 
506 
507 void ZxQuery::bindSymbol(const QString& symbolName, const QString& symbolText)
508 {
509  m_symbols.insert(symbolName, symbolText);
510 }
511 
512 QString ZxQuery::getSymbolText(const QString& symbolName)
513 {
514  return m_symbols.value(symbolName);
515 }
516 
517 void ZxQuery::removeSymbol(const QString& symbolName)
518 {
519  m_symbols.remove(symbolName);
520 }
521 
522 
523 
524 
525 } // Zbl
This class allows Javascript programs to access QXmlItem objects.
Definition: ZxItem.h:43
void setFocusItem(QObject *item)
sets the XQuery focus item
Definition: ZxQuery.cpp:185
bool m_enableSymbols
Enables query text buffer symbol substitution when true, disables it when false.
Definition: ZxQuery.h:492
static QString getPathScheme(const QString &path)
Obtains the scheme part of a URL path.
void setQueryUrl(const QString &url)
sets the XQuery query url and calls setQuery on the encapsulated QXmlQuery object.
Definition: ZxQuery.cpp:68
QObject * getFocusItem()
returns the focus URL of the XQuery object
Definition: ZxQuery.cpp:208
void setFocusUrl(const QString &url)
sets the XQuery focus url and calls setQuery on the encapsulated QXmlQuery object.
Definition: ZxQuery.cpp:121
Q_INVOKABLE void removeSymbol(const QString &symbolName)
Removes the specified symbol from the symbol substitution map.
Definition: ZxQuery.cpp:517
ResourceUrlFile m_focusFile
Pointer to a QFile object that will be used as the XQuery focus document.
Definition: ZxQuery.h:476
QString m_focusText
The focus URL.
Definition: ZxQuery.h:458
This class provides access to the QXmlResultItems class from Javascript programs. It accumulates the ...
Definition: ZxResultItems.h:45
#define ZBL_REGISTER_LOGGED_OBJECT
Definition: zglobal.h:104
const QString getFocusUrl()
returns the focus item the XQuery object
Definition: ZxQuery.cpp:162
qItemList m_items
A list of QXmlItem objects resulting from an XQuery evaluation.
void setUrl(const QString &url)
Definition: ZxQuery.cpp:409
Q_INVOKABLE void bindSymbol(const QString &symbolName, const QString &symbolText)
Binds the specified symbol name to the specified text.
Definition: ZxQuery.cpp:507
void queryUrlChanged(bool queryOK)
Q_INVOKABLE QObject * evaluateToItems()
Evaluates the current XQuery to a ZxResultItems object and returns that object.
Definition: ZxQuery.cpp:259
QString m_queryText
The source text of the query.
Definition: ZxQuery.h:446
void focusUrlChanged(bool focusOK)
void setSymbolSubstitution(bool enabled)
enables symbol substitution mechanism for query text buffer.
Definition: ZxQuery.cpp:437
void setFocusText(const QString &text)
sets the XQuery focus url and calls setQuery on the encapsulated QXmlQuery object.
Definition: ZxQuery.cpp:167
void freeFile()
Frees the QFile if onw exists.
Definition: ZxQuery.cpp:400
Q_INVOKABLE QString getSymbolText(const QString &symbolName)
obtain the text that will be substituted for the specified symbol when loading text into the query te...
Definition: ZxQuery.cpp:512
Definition: ZAndGate.cpp:6
void setQueryText(const QString &text)
Copies the specified text to the query text buffer and calls setQuery on the encapsulated QXmlQuery o...
Definition: ZxQuery.cpp:219
const bool getSymbolSubstitution()
returns the symbol substituion mode
Definition: ZxQuery.cpp:442
#define ZBL_DEFINE_LOGGED_OBJECT(class_name)
Definition: zglobal.h:99
Q_INVOKABLE void loadQueryText(const QString &fileUri)
Appends the contents of the specified file to the current contents of the query text buffer...
Definition: ZxQuery.cpp:447
ZxQuery(QObject *parent=0)
Definition: ZxQuery.cpp:44
Q_INVOKABLE QObject * getCopy()
Returns a ZxQuery object that is a copy of this object.
Definition: ZxQuery.cpp:348
QXmlNamePool getNamePool() const
Obtains the XML name pool for the encapsulated QXmlQuery object.
Definition: ZxQuery.cpp:343
#define zWarning()
Definition: zglobal.h:111
QXmlItem m_item
Encapsulated QmlItem object.
Definition: ZxItem.h:163
Q_INVOKABLE QObject * getNamePoolCopy()
Returns a ZxQuery object that uses the same QXmlNamePool as this object.
Definition: ZxQuery.cpp:353
QXmlItem m_xmlItem
The focus item.
Definition: ZxQuery.h:465
Q_INVOKABLE void concatQueryText(const QString &text)
Appends the specified text to the current contents of the query text buffer.
Definition: ZxQuery.cpp:484
QMap< QString, QString > m_symbols
Query text symbol substitution map.
Definition: ZxQuery.h:497
#define zDebug()
Definition: zglobal.h:113
Q_INVOKABLE QStringList evaluateToStringList()
Evaluates the current XQuery to a QStringList object and returns that object.
Definition: ZxQuery.cpp:307
static bool isResourceScheme(const QString &scheme)
Definition: ZxQuery.h:389
static QString resolvePath(const QString &path, bool includeUrlScheme=true)
Converts relative file paths into canonical file paths. Paths prefixed with prefix are mapped relativ...
#define cStr(qStr)
Definition: zglobal.h:49
static void registerType()
Registers ZxQuery as a QML type.
Definition: ZxQuery.cpp:252
const QString getQueryUrl()
returns the query URL of the XQuery object
Definition: ZxQuery.cpp:116
QXmlQuery m_query
The encapsulated QXmlQuery object.
Definition: ZxQuery.h:434
void closeFile()
Closes QFile if one exists.
Definition: ZxQuery.cpp:394
const QString getFocusText()
returns the focus text of the XQuery object
Definition: ZxQuery.cpp:180
This class provides access to the QXMLQuery class from Javascript.
Definition: ZxQuery.h:48
ResourceUrlFile m_queryFile
Pointer to a QFile object that will be used as the XQuery query text.
Definition: ZxQuery.h:487
A class for opening XQuery resource files.
Definition: ZxQuery.h:379
void resetFile()
Closes QFile if one exists, otherwise creates a new QFile.
Definition: ZxQuery.cpp:386
Q_INVOKABLE void commitQueryText()
Sends the current query text buffer to the XQuery processor.
Definition: ZxQuery.cpp:494
void setValue(const QXmlItem &item, ZxQuery *zQuery)
Definition: ZxItem.cpp:45
const QString getQueryText()
returns the text of the query
Definition: ZxQuery.cpp:240
Q_INVOKABLE QString evaluateToXml()
Evaluates the current XQuery to an XML string.
Definition: ZxQuery.cpp:326
Q_INVOKABLE void bindVariable(const QString &localName, QObject *zXmlItem)
Binds an XQuery variable with a value.
Definition: ZxQuery.cpp:419
QUrl m_queryUrl
The URL of the source query file;.
Definition: ZxQuery.h:440