Qt 4.8
qcoloringmessagehandler.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QXmlStreamReader>
43 
45 #include "qxmlpatternistcli_p.h"
46 
48 
49 using namespace QPatternist;
50 
52 {
53  m_classToColor.insert(QLatin1String("XQuery-data"), Data);
54  m_classToColor.insert(QLatin1String("XQuery-expression"), Keyword);
55  m_classToColor.insert(QLatin1String("XQuery-function"), Keyword);
56  m_classToColor.insert(QLatin1String("XQuery-keyword"), Keyword);
57  m_classToColor.insert(QLatin1String("XQuery-type"), Keyword);
58  m_classToColor.insert(QLatin1String("XQuery-uri"), Data);
59  m_classToColor.insert(QLatin1String("XQuery-filepath"), Data);
60 
61  /* If you're tuning the colors, take it easy laddie. Take into account:
62  *
63  * - Get over your own taste, there's others too on this planet
64  * - Make sure it works well on black & white
65  * - Make sure it works well on white & black
66  */
72 }
73 
75  const QString &description,
76  const QUrl &identifier,
77  const QSourceLocation &sourceLocation)
78 {
79  const bool hasLine = sourceLocation.line() != -1;
80 
81  switch(type)
82  {
83  case QtWarningMsg:
84  {
85  if(hasLine)
86  {
87  writeUncolored(QXmlPatternistCLI::tr("Warning in %1, at line %2, column %3: %4").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()),
88  QString::number(sourceLocation.line()),
89  QString::number(sourceLocation.column()),
90  colorifyDescription(description)));
91  }
92  else
93  {
94  writeUncolored(QXmlPatternistCLI::tr("Warning in %1: %2").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()),
95  colorifyDescription(description)));
96  }
97 
98  break;
99  }
100  case QtFatalMsg:
101  {
102  const QString errorCode(identifier.fragment());
103  Q_ASSERT(!errorCode.isEmpty());
104  QUrl uri(identifier);
105  uri.setFragment(QString());
106 
108 
109  if(sourceLocation.isNull())
110  location = QXmlPatternistCLI::tr("Unknown location");
111  else
112  location = QString::fromLatin1(sourceLocation.uri().toEncoded());
113 
114  QString errorId;
115  /* If it's a standard error code, we don't want to output the
116  * whole URI. */
117  if(uri.toString() == QLatin1String("http://www.w3.org/2005/xqt-errors"))
118  errorId = errorCode;
119  else
120  errorId = QString::fromLatin1(identifier.toEncoded());
121 
122  if(hasLine)
123  {
124  writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2, at line %3, column %4: %5").arg(colorify(errorId, ErrorCode),
125  colorify(location, Location),
126  colorify(QString::number(sourceLocation.line()), Location),
127  colorify(QString::number(sourceLocation.column()), Location),
128  colorifyDescription(description)));
129  }
130  else
131  {
132  writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2: %3").arg(colorify(errorId, ErrorCode),
133  colorify(location, Location),
134  colorifyDescription(description)));
135  }
136  break;
137  }
138  case QtCriticalMsg:
139  /* Fallthrough. */
140  case QtDebugMsg:
141  {
142  Q_ASSERT_X(false, Q_FUNC_INFO,
143  "message() is not supposed to receive QtCriticalMsg or QtDebugMsg.");
144  return;
145  }
146  }
147 }
148 
150 {
151  QXmlStreamReader reader(in);
152  QString result;
153  result.reserve(in.size());
154  ColorType currentColor = RunningText;
155 
156  while(!reader.atEnd())
157  {
158  reader.readNext();
159 
160  switch(reader.tokenType())
161  {
163  {
164  if(reader.name() == QLatin1String("span"))
165  {
167  currentColor = m_classToColor.value(reader.attributes().value(QLatin1String("class")).toString());
168  }
169 
170  continue;
171  }
173  {
174  result.append(colorify(reader.text().toString(), currentColor));
175  continue;
176  }
178  {
179  currentColor = RunningText;
180  continue;
181  }
182  /* Fallthrough, */
184  /* Fallthrough, */
186  continue;
187  default:
188  Q_ASSERT_X(false, Q_FUNC_INFO,
189  "Unexpected node.");
190  }
191  }
192 
193  Q_ASSERT_X(!reader.hasError(), Q_FUNC_INFO,
194  "The output from Patternist must be well-formed.");
195  return result;
196 }
197 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
qint64 line() const
Returns the current line number.
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation)
This function must be implemented by the sub-class.
void writeUncolored(const QString &message)
Writes message to stderr as if for instance QTextStream would have been used, and adds a line ending ...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QStringRef name() const
Returns the local name of a StartElement, EndElement, or an EntityReference.
bool atEnd() const
Returns true if the reader has read until the end of the XML document, or if an error() has occurred ...
Definition: qxmlstream.cpp:590
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QString colorifyDescription(const QString &in) const
QtMsgType
This enum describes the messages that can be sent to a message handler (QtMsgHandler).
Definition: qglobal.h:1881
QString fragment() const
Returns the fragment of the URL.
Definition: qurl.cpp:5687
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
QStringRef value(const QString &namespaceUri, const QString &name) const
Returns the value of the attribute name in the namespace described with namespaceUri, or an empty string reference if the attribute is not defined.
QString colorify(const QString &message, int color=-1) const
Treats message and colorID identically to write(), but instead of writing message to stderr...
QUrl uri() const
Returns the resource that this QSourceLocation refers to.
bool isNull() const
Returns true if this QSourceLocation doesn&#39;t identify anything.
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
TokenType tokenType() const
Returns the type of the current token.
Definition: qxmlstream.cpp:656
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool hasError() const
Returns true if an error has occurred, otherwise false.
Definition: qxmlstream.h:396
The namespace for the internal API of QtXmlPatterns.
The QSourceLocation class identifies a location in a resource by URI, line, and column.
TokenType readNext()
Reads the next token and returns its type.
Definition: qxmlstream.cpp:623
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
void insertMapping(int colorID, const ColorCode colorCode)
Adds a color mapping from colorID to colorCode, for this ColorOutput instance.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QHash< QString, ColorType > m_classToColor
QString & append(QChar c)
Definition: qstring.cpp:1777
The QAbstractMessageHandler class provides a callback interface for handling messages.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
qint64 column() const
Returns the current column number.
The QXmlStreamReader class provides a fast parser for reading well-formed XML via a simple streaming ...
Definition: qxmlstream.h:290
QStringRef text() const
Returns the text of Characters , Comment , DTD , or EntityReference.
QXmlStreamAttributes attributes() const
Returns the attributes of a StartElement.
#define Q_FUNC_INFO
Definition: qglobal.h:1871