1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QXmlStreamReader>
44 #include "qcoloringmessagehandler_p.h"
45 #include "qxmlpatternistcli_p.h"
49 using namespace QPatternist;
51 ColoringMessageHandler::ColoringMessageHandler(QObject *parent) : QAbstractMessageHandler(parent)
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);
61 /* If you're tuning the colors, take it easy laddie. Take into account:
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
67 insertMapping(Location, CyanForeground);
68 insertMapping(ErrorCode, RedForeground);
69 insertMapping(Keyword, BlueForeground);
70 insertMapping(Data, BlueForeground);
71 insertMapping(RunningText, DefaultColor);
74 void ColoringMessageHandler::handleMessage(QtMsgType type,
75 const QString &description,
76 const QUrl &identifier,
77 const QSourceLocation &sourceLocation)
79 const bool hasLine = sourceLocation.line() != -1;
87 writeUncolored(QXmlPatternistCLI::tr("Warning in %1, at line %2, column %3: %4").arg(sourceLocation.uri().toString(),
88 QString::number(sourceLocation.line()),
89 QString::number(sourceLocation.column()),
90 colorifyDescription(description)));
94 writeUncolored(QXmlPatternistCLI::tr("Warning in %1: %2").arg(sourceLocation.uri().toString(),
95 colorifyDescription(description)));
102 const QString errorCode(identifier.fragment());
103 Q_ASSERT(!errorCode.isEmpty());
104 QUrl uri(identifier);
105 uri.setFragment(QString());
109 if(sourceLocation.isNull())
110 location = QXmlPatternistCLI::tr("Unknown location");
112 location = sourceLocation.uri().toString();
115 /* If it's a standard error code, we don't want to output the
117 if(uri.toString() == QLatin1String("http://www.w3.org/2005/xqt-errors"))
120 errorId = identifier.toString();
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)));
132 writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2: %3").arg(colorify(errorId, ErrorCode),
133 colorify(location, Location),
134 colorifyDescription(description)));
142 Q_ASSERT_X(false, Q_FUNC_INFO,
143 "message() is not supposed to receive QtCriticalMsg or QtDebugMsg.");
149 QString ColoringMessageHandler::colorifyDescription(const QString &in) const
151 QXmlStreamReader reader(in);
153 result.reserve(in.size());
154 ColorType currentColor = RunningText;
156 while(!reader.atEnd())
160 switch(reader.tokenType())
162 case QXmlStreamReader::StartElement:
164 if(reader.name() == QLatin1String("span"))
166 Q_ASSERT(m_classToColor.contains(reader.attributes().value(QLatin1String("class")).toString()));
167 currentColor = m_classToColor.value(reader.attributes().value(QLatin1String("class")).toString());
172 case QXmlStreamReader::Characters:
174 result.append(colorify(reader.text().toString(), currentColor));
177 case QXmlStreamReader::EndElement:
179 currentColor = RunningText;
183 case QXmlStreamReader::StartDocument:
185 case QXmlStreamReader::EndDocument:
188 Q_ASSERT_X(false, Q_FUNC_INFO,
193 Q_ASSERT_X(!reader.hasError(), Q_FUNC_INFO,
194 "The output from Patternist must be well-formed.");