Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / api / qcoloringmessagehandler.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
44 #include "qcoloringmessagehandler_p.h"
45 #include "qxmlpatternistcli_p.h"
46
47 QT_BEGIN_NAMESPACE
48
49 using namespace QPatternist;
50
51 ColoringMessageHandler::ColoringMessageHandler(QObject *parent) : QAbstractMessageHandler(parent)
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      */
67     insertMapping(Location, CyanForeground);
68     insertMapping(ErrorCode, RedForeground);
69     insertMapping(Keyword, BlueForeground);
70     insertMapping(Data, BlueForeground);
71     insertMapping(RunningText, DefaultColor);
72 }
73
74 void ColoringMessageHandler::handleMessage(QtMsgType type,
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(sourceLocation.uri().toString(),
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(sourceLocation.uri().toString(),
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
107             QString location;
108
109             if(sourceLocation.isNull())
110                 location = QXmlPatternistCLI::tr("Unknown location");
111             else
112                 location = sourceLocation.uri().toString();
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 = identifier.toString();
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
149 QString ColoringMessageHandler::colorifyDescription(const QString &in) const
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         {
162             case QXmlStreamReader::StartElement:
163             {
164                 if(reader.name() == QLatin1String("span"))
165                 {
166                     Q_ASSERT(m_classToColor.contains(reader.attributes().value(QLatin1String("class")).toString()));
167                     currentColor = m_classToColor.value(reader.attributes().value(QLatin1String("class")).toString());
168                 }
169
170                 continue;
171             }
172             case QXmlStreamReader::Characters:
173             {
174                 result.append(colorify(reader.text().toString(), currentColor));
175                 continue;
176             }
177             case QXmlStreamReader::EndElement:
178             {
179                 currentColor = RunningText;
180                 continue;
181             }
182             /* Fallthrough, */
183             case QXmlStreamReader::StartDocument:
184             /* Fallthrough, */
185             case QXmlStreamReader::EndDocument:
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
198 QT_END_NAMESPACE