source: branches/vendor/trolltech/qt/current/tools/xmlpatterns/qcoloringmessagehandler.cpp @ 2

Last change on this file since 2 was 2, checked in by Dmitry A. Kuminov, 13 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file.  Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file.  Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QXmlStreamReader>
43
44#include "main.h"
45
46#include "qcoloringmessagehandler_p.h"
47
48QT_BEGIN_NAMESPACE
49
50using namespace QPatternist;
51
52ColoringMessageHandler::ColoringMessageHandler(QObject *parent) : QAbstractMessageHandler(parent)
53{
54    m_classToColor.insert(QLatin1String("XQuery-data"), Data);
55    m_classToColor.insert(QLatin1String("XQuery-expression"), Keyword);
56    m_classToColor.insert(QLatin1String("XQuery-function"), Keyword);
57    m_classToColor.insert(QLatin1String("XQuery-keyword"), Keyword);
58    m_classToColor.insert(QLatin1String("XQuery-type"), Keyword);
59    m_classToColor.insert(QLatin1String("XQuery-uri"), Data);
60    m_classToColor.insert(QLatin1String("XQuery-filepath"), Data);
61
62    /* If you're tuning the colors, take it easy laddie. Take into account:
63     *
64     * - Get over your own taste, there's others too on this planet
65     * - Make sure it works well on black & white
66     * - Make sure it works well on white & black
67     */
68    insertMapping(Location, CyanForeground);
69    insertMapping(ErrorCode, RedForeground);
70    insertMapping(Keyword, BlueForeground);
71    insertMapping(Data, BlueForeground);
72    insertMapping(RunningText, DefaultColor);
73}
74
75void ColoringMessageHandler::handleMessage(QtMsgType type,
76                                           const QString &description,
77                                           const QUrl &identifier,
78                                           const QSourceLocation &sourceLocation)
79{
80    const bool hasLine = sourceLocation.line() != -1;
81
82    switch(type)
83    {
84        case QtWarningMsg:
85        {
86            if(hasLine)
87            {
88                writeUncolored(QXmlPatternistCLI::tr("Warning in %1, at line %2, column %3: %4").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()),
89                                                                                                 QString::number(sourceLocation.line()),
90                                                                                                 QString::number(sourceLocation.column()),
91                                                                                                 colorifyDescription(description)));
92            }
93            else
94            {
95                writeUncolored(QXmlPatternistCLI::tr("Warning in %1: %2").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()),
96                                                                          colorifyDescription(description)));
97            }
98
99            break;
100        }
101        case QtFatalMsg:
102        {
103            Q_ASSERT(!sourceLocation.isNull());
104            const QString errorCode(identifier.fragment());
105            Q_ASSERT(!errorCode.isEmpty());
106            QUrl uri(identifier);
107            uri.setFragment(QString());
108
109            QString errorId;
110            /* If it's a standard error code, we don't want to output the
111             * whole URI. */
112            if(uri.toString() == QLatin1String("http://www.w3.org/2005/xqt-errors"))
113                errorId = errorCode;
114            else
115                errorId = QString::fromLatin1(identifier.toEncoded());
116
117            if(hasLine)
118            {
119                writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2, at line %3, column %4: %5").arg(colorify(errorId, ErrorCode),
120                                                                                                  colorify(QString::fromLatin1(sourceLocation.uri().toEncoded()), Location),
121                                                                                                  colorify(QString::number(sourceLocation.line()), Location),
122                                                                                                  colorify(QString::number(sourceLocation.column()), Location),
123                                                                                                  colorifyDescription(description)));
124            }
125            else
126            {
127                writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2: %3").arg(colorify(errorId, ErrorCode),
128                                                                           colorify(QString::fromLatin1(sourceLocation.uri().toEncoded()), Location),
129                                                                           colorifyDescription(description)));
130            }
131            break;
132        }
133        case QtCriticalMsg:
134        /* Fallthrough. */
135        case QtDebugMsg:
136        {
137            Q_ASSERT_X(false, Q_FUNC_INFO,
138                       "message() is not supposed to receive QtCriticalMsg or QtDebugMsg.");
139            return;
140        }
141    }
142}
143
144QString ColoringMessageHandler::colorifyDescription(const QString &in) const
145{
146    QXmlStreamReader reader(in);
147    QString result;
148    result.reserve(in.size());
149    ColorType currentColor = RunningText;
150
151    while(!reader.atEnd())
152    {
153        reader.readNext();
154
155        switch(reader.tokenType())
156        {
157            case QXmlStreamReader::StartElement:
158            {
159                if(reader.name() == QLatin1String("span"))
160                {
161                    Q_ASSERT(m_classToColor.contains(reader.attributes().value(QLatin1String("class")).toString()));
162                    currentColor = m_classToColor.value(reader.attributes().value(QLatin1String("class")).toString());
163                }
164
165                continue;
166            }
167            case QXmlStreamReader::Characters:
168            {
169                result.append(colorify(reader.text().toString(), currentColor));
170                continue;
171            }
172            case QXmlStreamReader::EndElement:
173            {
174                currentColor = RunningText;
175                continue;
176            }
177            /* Fallthrough, */
178            case QXmlStreamReader::StartDocument:
179            /* Fallthrough, */
180            case QXmlStreamReader::EndDocument:
181                continue;
182            default:
183                Q_ASSERT_X(false, Q_FUNC_INFO,
184                           "Unexpected node.");
185        }
186    }
187
188    Q_ASSERT_X(!reader.hasError(), Q_FUNC_INFO,
189               "The output from Patternist must be well-formed.");
190    return result;
191}
192
193QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.