From cba4d931bec2cb910e4dc149e0bc5d441cd64511 Mon Sep 17 00:00:00 2001 From: Carlos Manuel Duclos Vergara Date: Mon, 21 Dec 2009 12:03:44 +0100 Subject: Fixing a problem with xmlpatterns, where code from tools/xmlpatterns was being included by src/xmlpatterns. Reviewed-by: Peter Hartmann --- src/xmlpatterns/api/api.pri | 116 ++++---- src/xmlpatterns/api/qcoloringmessagehandler.cpp | 198 ++++++++++++++ src/xmlpatterns/api/qcoloringmessagehandler_p.h | 98 +++++++ src/xmlpatterns/api/qcoloroutput.cpp | 348 ++++++++++++++++++++++++ src/xmlpatterns/api/qcoloroutput_p.h | 133 +++++++++ src/xmlpatterns/xmlpatterns.pro | 34 ++- tools/xmlpatterns/main.cpp | 2 +- tools/xmlpatterns/main.h | 10 +- tools/xmlpatterns/qcoloringmessagehandler.cpp | 199 -------------- tools/xmlpatterns/qcoloringmessagehandler_p.h | 98 ------- tools/xmlpatterns/qcoloroutput.cpp | 348 ------------------------ tools/xmlpatterns/qcoloroutput_p.h | 133 --------- tools/xmlpatterns/xmlpatterns.pro | 8 +- 13 files changed, 854 insertions(+), 871 deletions(-) create mode 100644 src/xmlpatterns/api/qcoloringmessagehandler.cpp create mode 100644 src/xmlpatterns/api/qcoloringmessagehandler_p.h create mode 100644 src/xmlpatterns/api/qcoloroutput.cpp create mode 100644 src/xmlpatterns/api/qcoloroutput_p.h delete mode 100644 tools/xmlpatterns/qcoloringmessagehandler.cpp delete mode 100644 tools/xmlpatterns/qcoloringmessagehandler_p.h delete mode 100644 tools/xmlpatterns/qcoloroutput.cpp delete mode 100644 tools/xmlpatterns/qcoloroutput_p.h diff --git a/src/xmlpatterns/api/api.pri b/src/xmlpatterns/api/api.pri index 9fcc2f5..a0adf75 100644 --- a/src/xmlpatterns/api/api.pri +++ b/src/xmlpatterns/api/api.pri @@ -1,59 +1,57 @@ -HEADERS += $$PWD/qabstractxmlforwarditerator_p.h \ - $$PWD/qabstractmessagehandler.h \ - $$PWD/qabstracturiresolver.h \ - $$PWD/qabstractxmlnodemodel.h \ - $$PWD/qabstractxmlnodemodel_p.h \ - $$PWD/qabstractxmlpullprovider_p.h \ - $$PWD/qabstractxmlreceiver.h \ - $$PWD/qabstractxmlreceiver_p.h \ - $$PWD/qdeviceresourceloader_p.h \ - $$PWD/qiodevicedelegate_p.h \ - $$PWD/qnetworkaccessdelegator_p.h \ - $$PWD/qpullbridge_p.h \ - $$PWD/qresourcedelegator_p.h \ - $$PWD/qsimplexmlnodemodel.h \ - $$PWD/qsourcelocation.h \ - $$PWD/quriloader_p.h \ - $$PWD/qvariableloader_p.h \ - $$PWD/qxmlformatter.h \ - $$PWD/qxmlname.h \ - $$PWD/qxmlnamepool.h \ - $$PWD/qxmlquery.h \ - $$PWD/qxmlquery_p.h \ - $$PWD/qxmlresultitems.h \ - $$PWD/qxmlresultitems_p.h \ - $$PWD/qxmlschema.h \ - $$PWD/qxmlschema_p.h \ - $$PWD/qxmlschemavalidator.h \ - $$PWD/qxmlschemavalidator_p.h \ - $$PWD/qxmlserializer.h \ - $$PWD/qxmlserializer_p.h \ - $$PWD/../../../tools/xmlpatterns/qcoloringmessagehandler_p.h \ - $$PWD/../../../tools/xmlpatterns/qcoloroutput_p.h - -SOURCES += $$PWD/qvariableloader.cpp \ - $$PWD/qabstractmessagehandler.cpp \ - $$PWD/qabstracturiresolver.cpp \ - $$PWD/qabstractxmlnodemodel.cpp \ - $$PWD/qabstractxmlpullprovider.cpp \ - $$PWD/qabstractxmlreceiver.cpp \ - $$PWD/qiodevicedelegate.cpp \ - $$PWD/qnetworkaccessdelegator.cpp \ - $$PWD/qpullbridge.cpp \ - $$PWD/qresourcedelegator.cpp \ - $$PWD/qsimplexmlnodemodel.cpp \ - $$PWD/qsourcelocation.cpp \ - $$PWD/quriloader.cpp \ - $$PWD/qxmlformatter.cpp \ - $$PWD/qxmlname.cpp \ - $$PWD/qxmlnamepool.cpp \ - $$PWD/qxmlquery.cpp \ - $$PWD/qxmlresultitems.cpp \ - $$PWD/qxmlschema.cpp \ - $$PWD/qxmlschema_p.cpp \ - $$PWD/qxmlschemavalidator.cpp \ - $$PWD/qxmlserializer.cpp \ - $$PWD/../../../tools/xmlpatterns/qcoloringmessagehandler.cpp \ - $$PWD/../../../tools/xmlpatterns/qcoloroutput.cpp - -INCLUDEPATH += $$PWD/../../../tools/xmlpatterns/ +HEADERS += $$PWD/qabstractxmlforwarditerator_p.h \ + $$PWD/qabstractmessagehandler.h \ + $$PWD/qabstracturiresolver.h \ + $$PWD/qabstractxmlnodemodel.h \ + $$PWD/qabstractxmlnodemodel_p.h \ + $$PWD/qabstractxmlpullprovider_p.h \ + $$PWD/qabstractxmlreceiver.h \ + $$PWD/qabstractxmlreceiver_p.h \ + $$PWD/qdeviceresourceloader_p.h \ + $$PWD/qiodevicedelegate_p.h \ + $$PWD/qnetworkaccessdelegator_p.h \ + $$PWD/qpullbridge_p.h \ + $$PWD/qresourcedelegator_p.h \ + $$PWD/qsimplexmlnodemodel.h \ + $$PWD/qsourcelocation.h \ + $$PWD/quriloader_p.h \ + $$PWD/qvariableloader_p.h \ + $$PWD/qxmlformatter.h \ + $$PWD/qxmlname.h \ + $$PWD/qxmlnamepool.h \ + $$PWD/qxmlquery.h \ + $$PWD/qxmlquery_p.h \ + $$PWD/qxmlresultitems.h \ + $$PWD/qxmlresultitems_p.h \ + $$PWD/qxmlschema.h \ + $$PWD/qxmlschema_p.h \ + $$PWD/qxmlschemavalidator.h \ + $$PWD/qxmlschemavalidator_p.h \ + $$PWD/qxmlserializer.h \ + $$PWD/qxmlserializer_p.h \ + $$PWD/qcoloringmessagehandler_p.h \ + $$PWD/qcoloroutput_p.h \ + $$PWD/qxmlpatternistcli_p.h +SOURCES += $$PWD/qvariableloader.cpp \ + $$PWD/qabstractmessagehandler.cpp \ + $$PWD/qabstracturiresolver.cpp \ + $$PWD/qabstractxmlnodemodel.cpp \ + $$PWD/qabstractxmlpullprovider.cpp \ + $$PWD/qabstractxmlreceiver.cpp \ + $$PWD/qiodevicedelegate.cpp \ + $$PWD/qnetworkaccessdelegator.cpp \ + $$PWD/qpullbridge.cpp \ + $$PWD/qresourcedelegator.cpp \ + $$PWD/qsimplexmlnodemodel.cpp \ + $$PWD/qsourcelocation.cpp \ + $$PWD/quriloader.cpp \ + $$PWD/qxmlformatter.cpp \ + $$PWD/qxmlname.cpp \ + $$PWD/qxmlnamepool.cpp \ + $$PWD/qxmlquery.cpp \ + $$PWD/qxmlresultitems.cpp \ + $$PWD/qxmlschema.cpp \ + $$PWD/qxmlschema_p.cpp \ + $$PWD/qxmlschemavalidator.cpp \ + $$PWD/qxmlserializer.cpp \ + $$PWD/qcoloringmessagehandler.cpp \ + $$PWD/qcoloroutput.cpp diff --git a/src/xmlpatterns/api/qcoloringmessagehandler.cpp b/src/xmlpatterns/api/qcoloringmessagehandler.cpp new file mode 100644 index 0000000..7d3eb6f --- /dev/null +++ b/src/xmlpatterns/api/qcoloringmessagehandler.cpp @@ -0,0 +1,198 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "qcoloringmessagehandler_p.h" +#include "qxmlpatternistcli_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +ColoringMessageHandler::ColoringMessageHandler(QObject *parent) : QAbstractMessageHandler(parent) +{ + m_classToColor.insert(QLatin1String("XQuery-data"), Data); + m_classToColor.insert(QLatin1String("XQuery-expression"), Keyword); + m_classToColor.insert(QLatin1String("XQuery-function"), Keyword); + m_classToColor.insert(QLatin1String("XQuery-keyword"), Keyword); + m_classToColor.insert(QLatin1String("XQuery-type"), Keyword); + m_classToColor.insert(QLatin1String("XQuery-uri"), Data); + m_classToColor.insert(QLatin1String("XQuery-filepath"), Data); + + /* If you're tuning the colors, take it easy laddie. Take into account: + * + * - Get over your own taste, there's others too on this planet + * - Make sure it works well on black & white + * - Make sure it works well on white & black + */ + insertMapping(Location, CyanForeground); + insertMapping(ErrorCode, RedForeground); + insertMapping(Keyword, BlueForeground); + insertMapping(Data, BlueForeground); + insertMapping(RunningText, DefaultColor); +} + +void ColoringMessageHandler::handleMessage(QtMsgType type, + const QString &description, + const QUrl &identifier, + const QSourceLocation &sourceLocation) +{ + const bool hasLine = sourceLocation.line() != -1; + + switch(type) + { + case QtWarningMsg: + { + if(hasLine) + { + writeUncolored(QXmlPatternistCLI::tr("Warning in %1, at line %2, column %3: %4").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()), + QString::number(sourceLocation.line()), + QString::number(sourceLocation.column()), + colorifyDescription(description))); + } + else + { + writeUncolored(QXmlPatternistCLI::tr("Warning in %1: %2").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()), + colorifyDescription(description))); + } + + break; + } + case QtFatalMsg: + { + const QString errorCode(identifier.fragment()); + Q_ASSERT(!errorCode.isEmpty()); + QUrl uri(identifier); + uri.setFragment(QString()); + + QString location; + + if(sourceLocation.isNull()) + location = QXmlPatternistCLI::tr("Unknown location"); + else + location = QString::fromLatin1(sourceLocation.uri().toEncoded()); + + QString errorId; + /* If it's a standard error code, we don't want to output the + * whole URI. */ + if(uri.toString() == QLatin1String("http://www.w3.org/2005/xqt-errors")) + errorId = errorCode; + else + errorId = QString::fromLatin1(identifier.toEncoded()); + + if(hasLine) + { + writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2, at line %3, column %4: %5").arg(colorify(errorId, ErrorCode), + colorify(location, Location), + colorify(QString::number(sourceLocation.line()), Location), + colorify(QString::number(sourceLocation.column()), Location), + colorifyDescription(description))); + } + else + { + writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2: %3").arg(colorify(errorId, ErrorCode), + colorify(location, Location), + colorifyDescription(description))); + } + break; + } + case QtCriticalMsg: + /* Fallthrough. */ + case QtDebugMsg: + { + Q_ASSERT_X(false, Q_FUNC_INFO, + "message() is not supposed to receive QtCriticalMsg or QtDebugMsg."); + return; + } + } +} + +QString ColoringMessageHandler::colorifyDescription(const QString &in) const +{ + QXmlStreamReader reader(in); + QString result; + result.reserve(in.size()); + ColorType currentColor = RunningText; + + while(!reader.atEnd()) + { + reader.readNext(); + + switch(reader.tokenType()) + { + case QXmlStreamReader::StartElement: + { + if(reader.name() == QLatin1String("span")) + { + Q_ASSERT(m_classToColor.contains(reader.attributes().value(QLatin1String("class")).toString())); + currentColor = m_classToColor.value(reader.attributes().value(QLatin1String("class")).toString()); + } + + continue; + } + case QXmlStreamReader::Characters: + { + result.append(colorify(reader.text().toString(), currentColor)); + continue; + } + case QXmlStreamReader::EndElement: + { + currentColor = RunningText; + continue; + } + /* Fallthrough, */ + case QXmlStreamReader::StartDocument: + /* Fallthrough, */ + case QXmlStreamReader::EndDocument: + continue; + default: + Q_ASSERT_X(false, Q_FUNC_INFO, + "Unexpected node."); + } + } + + Q_ASSERT_X(!reader.hasError(), Q_FUNC_INFO, + "The output from Patternist must be well-formed."); + return result; +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/api/qcoloringmessagehandler_p.h b/src/xmlpatterns/api/qcoloringmessagehandler_p.h new file mode 100644 index 0000000..3e8d18b --- /dev/null +++ b/src/xmlpatterns/api/qcoloringmessagehandler_p.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the XMLPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_ColoringMessageHandler_h +#define Patternist_ColoringMessageHandler_h + +#include + +#include "qcoloroutput_p.h" +#include "qabstractmessagehandler.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + class ColoringMessageHandler : public QAbstractMessageHandler + , private ColorOutput + { + public: + ColoringMessageHandler(QObject *parent = 0); + + protected: + virtual void handleMessage(QtMsgType type, + const QString &description, + const QUrl &identifier, + const QSourceLocation &sourceLocation); + + private: + QString colorifyDescription(const QString &in) const; + + enum ColorType + { + RunningText, + Location, + ErrorCode, + Keyword, + Data + }; + + QHash m_classToColor; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/api/qcoloroutput.cpp b/src/xmlpatterns/api/qcoloroutput.cpp new file mode 100644 index 0000000..4f27fd5 --- /dev/null +++ b/src/xmlpatterns/api/qcoloroutput.cpp @@ -0,0 +1,348 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +#include "qcoloroutput_p.h" + +// TODO: rename insertMapping() to insertColorMapping() +// TODO: Use a smart pointer for managing ColorOutputPrivate *d; +// TODO: break out the C++ example into a snippet file + +/* This include must appear here, because if it appears at the beginning of the file for + * instance, it breaks build -- "qglobal.h:628: error: template with + * C linkage" -- on Mac OS X 10.4. */ +#ifndef Q_OS_WIN +#include +#endif + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +namespace QPatternist +{ + class ColorOutputPrivate + { + public: + ColorOutputPrivate() : currentColorID(-1) + + { + /* - QIODevice::Unbuffered because we want it to appear when the user actually calls, performance + * is considered of lower priority. + */ + m_out.open(stderr, QIODevice::WriteOnly | QIODevice::Unbuffered); + + coloringEnabled = isColoringPossible(); + } + + ColorOutput::ColorMapping colorMapping; + int currentColorID; + bool coloringEnabled; + + static const char *const foregrounds[]; + static const char *const backgrounds[]; + + inline void write(const QString &msg) + { + m_out.write(msg.toLocal8Bit()); + } + + static QString escapeCode(const QString &in) + { + QString result; + result.append(QChar(0x1B)); + result.append(QLatin1Char('[')); + result.append(in); + result.append(QLatin1Char('m')); + return result; + } + + private: + QFile m_out; + + /*! + Returns true if it's suitable to send colored output to \c stderr. + */ + inline bool isColoringPossible() const + { +# if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) + /* Windows doesn't at all support ANSI escape codes, unless + * the user install a "device driver". See the Wikipedia links in the + * class documentation for details. */ + return false; +# else + /* We use QFile::handle() to get the file descriptor. It's a bit unsure + * whether it's 2 on all platforms and in all cases, so hopefully this layer + * of abstraction helps handle such cases. */ + return isatty(m_out.handle()); +# endif + } + }; +} + +const char *const ColorOutputPrivate::foregrounds[] = +{ + "0;30", + "0;34", + "0;32", + "0;36", + "0;31", + "0;35", + "0;33", + "0;37", + "1;30", + "1;34", + "1;32", + "1;36", + "1;31", + "1;35", + "1;33", + "1;37" +}; + +const char *const ColorOutputPrivate::backgrounds[] = +{ + "0;40", + "0;44", + "0;42", + "0;46", + "0;41", + "0;45", + "0;43" +}; + +/*! + \since 4.4 + \nonreentrant + \brief Outputs colored messages to \c stderr. + \internal + + ColorOutput is a convenience class for outputting messages to \c stderr + using color escape codes, as mandated in ECMA-48. ColorOutput will only + color output when it is detected to be suitable. For instance, if \c stderr is + detected to be attached to a file instead of a TTY, no coloring will be done. + + ColorOutput does its best attempt. but it is generally undefined what coloring + or effect the various coloring flags has. It depends strongly on what terminal + software that is being used. + + When using `echo -e 'my escape sequence'`, \033 works as an initiator but not + when printing from a C++ program, despite having escaped the backslash. + That's why we below use characters with value 0x1B. + + It can be convenient to subclass ColorOutput with a private scope, such that the + functions are directly available in the class using it. + + \section1 Usage + + To output messages, call write() or writeUncolored(). write() takes as second + argument an integer, which ColorOutput uses as a lookup key to find the color + it should color the text in. The mapping from keys to colors is done using + insertMapping(). Typically this is used by having enums for the various kinds + of messages, which subsequently are registered. + + \code + enum MyMessage + { + Error, + Important + }; + + ColorOutput output; + output.insertMapping(Error, ColorOutput::RedForeground); + output.insertMapping(Import, ColorOutput::BlueForeground); + + output.write("This is important", Important); + output.write("Jack, I'm only the selected official!", Error); + \endcode + + \sa {http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html} {Bash Prompt HOWTO, 6.1. Colours} + {http://linuxgazette.net/issue51/livingston-blade.html} {Linux Gazette, Tweaking Eterm, Edward Livingston-Blade} + {http://www.ecma-international.org/publications/standards/Ecma-048.htm} {Standard ECMA-48, Control Functions for Coded Character Sets, ECMA International}, + {http://en.wikipedia.org/wiki/ANSI_escape_code} {Wikipedia, ANSI escape code} + {http://linuxgazette.net/issue65/padala.html} {Linux Gazette, So You Like Color!, Pradeep Padala} + */ + +/*! + \enum ColorOutput::ColorCode + + \value DefaultColor ColorOutput performs no coloring. This typically means black on white + or white on black, depending on the settings of the user's terminal. + */ + +/*! + Sets the color mapping to be \a cMapping. + + Negative values are disallowed. + + \sa colorMapping(), insertMapping() + */ +void ColorOutput::setColorMapping(const ColorMapping &cMapping) +{ + d->colorMapping = cMapping; +} + +/*! + Returns the color mappings in use. + + \sa setColorMapping(), insertMapping() + */ +ColorOutput::ColorMapping ColorOutput::colorMapping() const +{ + return d->colorMapping; +} + +/*! + Constructs a ColorOutput instance, ready for use. + */ +ColorOutput::ColorOutput() : d(new ColorOutputPrivate()) +{ +} + +/*! + Destructs this ColorOutput instance. + */ +ColorOutput::~ColorOutput() +{ + delete d; +} + +/*! + Sends \a message to \c stderr, using the color looked up in colorMapping() using \a colorID. + + If \a color isn't available in colorMapping(), result and behavior is undefined. + + If \a colorID is 0, which is the default value, the previously used coloring is used. ColorOutput + is initialized to not color at all. + + If \a message is empty, effects are undefined. + + \a message will be printed as is. For instance, no line endings will be inserted. + */ +void ColorOutput::write(const QString &message, int colorID) +{ + d->write(colorify(message, colorID)); +} + +/*! + Writes \a message to \c stderr as if for instance + QTextStream would have been used, and adds a line ending at the end. + + This function can be practical to use such that one can use ColorOutput for all forms of writing. + */ +void ColorOutput::writeUncolored(const QString &message) +{ + d->write(message + QLatin1Char('\n')); +} + +/*! + Treats \a message and \a colorID identically to write(), but instead of writing + \a message to \c stderr, it is prepared for being written to \c stderr, but is then + returned. + + This is useful when the colored string is inserted into a translated string(dividing + the string into several small strings prevents proper translation). + */ +QString ColorOutput::colorify(const QString &message, int colorID) const +{ + Q_ASSERT_X(colorID == -1 || d->colorMapping.contains(colorID), Q_FUNC_INFO, + qPrintable(QString::fromLatin1("There is no color registered by id %1").arg(colorID))); + Q_ASSERT_X(!message.isEmpty(), Q_FUNC_INFO, "It makes no sense to attempt to print an empty string."); + + if(colorID != -1) + d->currentColorID = colorID; + + if(d->coloringEnabled && colorID != -1) + { + const int color(d->colorMapping.value(colorID)); + + /* If DefaultColor is set, we don't want to color it. */ + if(color & DefaultColor) + return message; + + const int foregroundCode = (int(color) & ForegroundMask) >> ForegroundShift; + const int backgroundCode = (int(color) & BackgroundMask) >> BackgroundShift; + QString finalMessage; + bool closureNeeded = false; + + if(foregroundCode) + { + finalMessage.append(ColorOutputPrivate::escapeCode(QLatin1String(ColorOutputPrivate::foregrounds[foregroundCode - 1]))); + closureNeeded = true; + } + + if(backgroundCode) + { + finalMessage.append(ColorOutputPrivate::escapeCode(QLatin1String(ColorOutputPrivate::backgrounds[backgroundCode - 1]))); + closureNeeded = true; + } + + finalMessage.append(message); + + if(closureNeeded) + { + finalMessage.append(QChar(0x1B)); + finalMessage.append(QLatin1String("[0m")); + } + + return finalMessage; + } + else + return message; +} + +/*! + Adds a color mapping from \a colorID to \a colorCode, for this ColorOutput instance. + + This is a convenience function for creating a ColorOutput::ColorMapping instance and + calling setColorMapping(). + + \sa colorMapping(), setColorMapping() + */ +void ColorOutput::insertMapping(int colorID, const ColorCode colorCode) +{ + d->colorMapping.insert(colorID, colorCode); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/api/qcoloroutput_p.h b/src/xmlpatterns/api/qcoloroutput_p.h new file mode 100644 index 0000000..1917ec7 --- /dev/null +++ b/src/xmlpatterns/api/qcoloroutput_p.h @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the XMLPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_ColorOutput_h +#define Patternist_ColorOutput_h + +#include +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + class ColorOutputPrivate; + + class ColorOutput + { + enum + { + ForegroundShift = 10, + BackgroundShift = 20, + SpecialShift = 20, + ForegroundMask = ((1 << ForegroundShift) - 1) << ForegroundShift, + BackgroundMask = ((1 << BackgroundShift) - 1) << BackgroundShift + }; + + public: + enum ColorCodeComponent + { + BlackForeground = 1 << ForegroundShift, + BlueForeground = 2 << ForegroundShift, + GreenForeground = 3 << ForegroundShift, + CyanForeground = 4 << ForegroundShift, + RedForeground = 5 << ForegroundShift, + PurpleForeground = 6 << ForegroundShift, + BrownForeground = 7 << ForegroundShift, + LightGrayForeground = 8 << ForegroundShift, + DarkGrayForeground = 9 << ForegroundShift, + LightBlueForeground = 10 << ForegroundShift, + LightGreenForeground = 11 << ForegroundShift, + LightCyanForeground = 12 << ForegroundShift, + LightRedForeground = 13 << ForegroundShift, + LightPurpleForeground = 14 << ForegroundShift, + YellowForeground = 15 << ForegroundShift, + WhiteForeground = 16 << ForegroundShift, + + BlackBackground = 1 << BackgroundShift, + BlueBackground = 2 << BackgroundShift, + GreenBackground = 3 << BackgroundShift, + CyanBackground = 4 << BackgroundShift, + RedBackground = 5 << BackgroundShift, + PurpleBackground = 6 << BackgroundShift, + BrownBackground = 7 << BackgroundShift, + DefaultColor = 1 << SpecialShift + }; + + typedef QFlags ColorCode; + typedef QHash ColorMapping; + + ColorOutput(); + ~ColorOutput(); + + void setColorMapping(const ColorMapping &cMapping); + ColorMapping colorMapping() const; + void insertMapping(int colorID, const ColorCode colorCode); + + void writeUncolored(const QString &message); + void write(const QString &message, int color = -1); + QString colorify(const QString &message, int color = -1) const; + + private: + ColorOutputPrivate *d; + Q_DISABLE_COPY(ColorOutput) + }; +} + +Q_DECLARE_OPERATORS_FOR_FLAGS(QPatternist::ColorOutput::ColorCode) + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/xmlpatterns.pro b/src/xmlpatterns/xmlpatterns.pro index 1df497d..a224762 100644 --- a/src/xmlpatterns/xmlpatterns.pro +++ b/src/xmlpatterns/xmlpatterns.pro @@ -1,15 +1,14 @@ -TARGET = QtXmlPatterns -QPRO_PWD = $$PWD -QT = core network -DEFINES += QT_BUILD_XMLPATTERNS_LIB QT_NO_USING_NAMESPACE +TARGET = QtXmlPatterns +QPRO_PWD = $$PWD +QT = core \ + network +DEFINES += QT_BUILD_XMLPATTERNS_LIB \ + QT_NO_USING_NAMESPACE win32-msvc*|win32-icc:QMAKE_LFLAGS += /BASE:0x61000000 - -unix:QMAKE_PKGCONFIG_REQUIRES = QtCore QtNetwork - +unix:QMAKE_PKGCONFIG_REQUIRES = QtCore \ + QtNetwork include(../qbase.pri) - PRECOMPILED_HEADER = ../corelib/global/qt_pch.h - include($$PWD/common.pri) include($$PWD/acceltree/acceltree.pri) include($$PWD/api/api.pri) @@ -25,14 +24,13 @@ include($$PWD/schema/schema.pri) include($$PWD/type/type.pri) include($$PWD/utils/utils.pri) include($$PWD/qobjectmodel/qobjectmodel.pri, "", true) +wince*:# The Microsoft MIPS compiler crashes if /Og is specified +: -wince*: { - # The Microsoft MIPS compiler crashes if /Og is specified - # -O2/1 expands to /Og plus additional arguments. - contains(DEFINES, MIPS): { - QMAKE_CXXFLAGS_RELEASE ~= s/-O2/-Oi -Ot -Oy -Ob2/ - QMAKE_CXXFLAGS_RELEASE ~= s/-O1/-Os -Oy -Ob2/ - } +# -O2/1 expands to /Og plus additional arguments. +contains(DEFINES, MIPS): { + QMAKE_CXXFLAGS_RELEASE ~= s/-O2/-Oi -Ot -Oy -Ob2/ + QMAKE_CXXFLAGS_RELEASE ~= s/-O1/-Os -Oy -Ob2/ } - -symbian:TARGET.UID3=0x2001E62B +symbian:TARGET.UID3 = 0x2001E62B +HEADERS += diff --git a/tools/xmlpatterns/main.cpp b/tools/xmlpatterns/main.cpp index 604523b..76853b5 100644 --- a/tools/xmlpatterns/main.cpp +++ b/tools/xmlpatterns/main.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -58,7 +59,6 @@ #include "private/qautoptr_p.h" #include "qapplicationargument_p.h" #include "qapplicationargumentparser_p.h" -#include "qcoloringmessagehandler_p.h" #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) /* Needed for opening stdout with _fdopen & friends. io.h seems to not be diff --git a/tools/xmlpatterns/main.h b/tools/xmlpatterns/main.h index cdef999..76b7097 100644 --- a/tools/xmlpatterns/main.h +++ b/tools/xmlpatterns/main.h @@ -54,21 +54,13 @@ #include -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - class QXmlPatternistCLI { public: - Q_DECLARE_TR_FUNCTIONS(QXmlPatternistCLI) + Q_DECLARE_TR_FUNCTIONS(QXmlPatternistCLI) private: inline QXmlPatternistCLI(); Q_DISABLE_COPY(QXmlPatternistCLI) }; -QT_END_NAMESPACE - -QT_END_HEADER - #endif diff --git a/tools/xmlpatterns/qcoloringmessagehandler.cpp b/tools/xmlpatterns/qcoloringmessagehandler.cpp deleted file mode 100644 index a639ddd..0000000 --- a/tools/xmlpatterns/qcoloringmessagehandler.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include - -#include "main.h" - -#include "qcoloringmessagehandler_p.h" - -QT_BEGIN_NAMESPACE - -using namespace QPatternist; - -ColoringMessageHandler::ColoringMessageHandler(QObject *parent) : QAbstractMessageHandler(parent) -{ - m_classToColor.insert(QLatin1String("XQuery-data"), Data); - m_classToColor.insert(QLatin1String("XQuery-expression"), Keyword); - m_classToColor.insert(QLatin1String("XQuery-function"), Keyword); - m_classToColor.insert(QLatin1String("XQuery-keyword"), Keyword); - m_classToColor.insert(QLatin1String("XQuery-type"), Keyword); - m_classToColor.insert(QLatin1String("XQuery-uri"), Data); - m_classToColor.insert(QLatin1String("XQuery-filepath"), Data); - - /* If you're tuning the colors, take it easy laddie. Take into account: - * - * - Get over your own taste, there's others too on this planet - * - Make sure it works well on black & white - * - Make sure it works well on white & black - */ - insertMapping(Location, CyanForeground); - insertMapping(ErrorCode, RedForeground); - insertMapping(Keyword, BlueForeground); - insertMapping(Data, BlueForeground); - insertMapping(RunningText, DefaultColor); -} - -void ColoringMessageHandler::handleMessage(QtMsgType type, - const QString &description, - const QUrl &identifier, - const QSourceLocation &sourceLocation) -{ - const bool hasLine = sourceLocation.line() != -1; - - switch(type) - { - case QtWarningMsg: - { - if(hasLine) - { - writeUncolored(QXmlPatternistCLI::tr("Warning in %1, at line %2, column %3: %4").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()), - QString::number(sourceLocation.line()), - QString::number(sourceLocation.column()), - colorifyDescription(description))); - } - else - { - writeUncolored(QXmlPatternistCLI::tr("Warning in %1: %2").arg(QString::fromLatin1(sourceLocation.uri().toEncoded()), - colorifyDescription(description))); - } - - break; - } - case QtFatalMsg: - { - const QString errorCode(identifier.fragment()); - Q_ASSERT(!errorCode.isEmpty()); - QUrl uri(identifier); - uri.setFragment(QString()); - - QString location; - - if(sourceLocation.isNull()) - location = QXmlPatternistCLI::tr("Unknown location"); - else - location = QString::fromLatin1(sourceLocation.uri().toEncoded()); - - QString errorId; - /* If it's a standard error code, we don't want to output the - * whole URI. */ - if(uri.toString() == QLatin1String("http://www.w3.org/2005/xqt-errors")) - errorId = errorCode; - else - errorId = QString::fromLatin1(identifier.toEncoded()); - - if(hasLine) - { - writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2, at line %3, column %4: %5").arg(colorify(errorId, ErrorCode), - colorify(location, Location), - colorify(QString::number(sourceLocation.line()), Location), - colorify(QString::number(sourceLocation.column()), Location), - colorifyDescription(description))); - } - else - { - writeUncolored(QXmlPatternistCLI::tr("Error %1 in %2: %3").arg(colorify(errorId, ErrorCode), - colorify(location, Location), - colorifyDescription(description))); - } - break; - } - case QtCriticalMsg: - /* Fallthrough. */ - case QtDebugMsg: - { - Q_ASSERT_X(false, Q_FUNC_INFO, - "message() is not supposed to receive QtCriticalMsg or QtDebugMsg."); - return; - } - } -} - -QString ColoringMessageHandler::colorifyDescription(const QString &in) const -{ - QXmlStreamReader reader(in); - QString result; - result.reserve(in.size()); - ColorType currentColor = RunningText; - - while(!reader.atEnd()) - { - reader.readNext(); - - switch(reader.tokenType()) - { - case QXmlStreamReader::StartElement: - { - if(reader.name() == QLatin1String("span")) - { - Q_ASSERT(m_classToColor.contains(reader.attributes().value(QLatin1String("class")).toString())); - currentColor = m_classToColor.value(reader.attributes().value(QLatin1String("class")).toString()); - } - - continue; - } - case QXmlStreamReader::Characters: - { - result.append(colorify(reader.text().toString(), currentColor)); - continue; - } - case QXmlStreamReader::EndElement: - { - currentColor = RunningText; - continue; - } - /* Fallthrough, */ - case QXmlStreamReader::StartDocument: - /* Fallthrough, */ - case QXmlStreamReader::EndDocument: - continue; - default: - Q_ASSERT_X(false, Q_FUNC_INFO, - "Unexpected node."); - } - } - - Q_ASSERT_X(!reader.hasError(), Q_FUNC_INFO, - "The output from Patternist must be well-formed."); - return result; -} - -QT_END_NAMESPACE diff --git a/tools/xmlpatterns/qcoloringmessagehandler_p.h b/tools/xmlpatterns/qcoloringmessagehandler_p.h deleted file mode 100644 index 3e8d18b..0000000 --- a/tools/xmlpatterns/qcoloringmessagehandler_p.h +++ /dev/null @@ -1,98 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the XMLPatterns module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. - -#ifndef Patternist_ColoringMessageHandler_h -#define Patternist_ColoringMessageHandler_h - -#include - -#include "qcoloroutput_p.h" -#include "qabstractmessagehandler.h" - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -namespace QPatternist -{ - class ColoringMessageHandler : public QAbstractMessageHandler - , private ColorOutput - { - public: - ColoringMessageHandler(QObject *parent = 0); - - protected: - virtual void handleMessage(QtMsgType type, - const QString &description, - const QUrl &identifier, - const QSourceLocation &sourceLocation); - - private: - QString colorifyDescription(const QString &in) const; - - enum ColorType - { - RunningText, - Location, - ErrorCode, - Keyword, - Data - }; - - QHash m_classToColor; - }; -} - -QT_END_NAMESPACE - -QT_END_HEADER - -#endif diff --git a/tools/xmlpatterns/qcoloroutput.cpp b/tools/xmlpatterns/qcoloroutput.cpp deleted file mode 100644 index 4f27fd5..0000000 --- a/tools/xmlpatterns/qcoloroutput.cpp +++ /dev/null @@ -1,348 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtXmlPatterns module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include - -#include "qcoloroutput_p.h" - -// TODO: rename insertMapping() to insertColorMapping() -// TODO: Use a smart pointer for managing ColorOutputPrivate *d; -// TODO: break out the C++ example into a snippet file - -/* This include must appear here, because if it appears at the beginning of the file for - * instance, it breaks build -- "qglobal.h:628: error: template with - * C linkage" -- on Mac OS X 10.4. */ -#ifndef Q_OS_WIN -#include -#endif - -QT_BEGIN_NAMESPACE - -using namespace QPatternist; - -namespace QPatternist -{ - class ColorOutputPrivate - { - public: - ColorOutputPrivate() : currentColorID(-1) - - { - /* - QIODevice::Unbuffered because we want it to appear when the user actually calls, performance - * is considered of lower priority. - */ - m_out.open(stderr, QIODevice::WriteOnly | QIODevice::Unbuffered); - - coloringEnabled = isColoringPossible(); - } - - ColorOutput::ColorMapping colorMapping; - int currentColorID; - bool coloringEnabled; - - static const char *const foregrounds[]; - static const char *const backgrounds[]; - - inline void write(const QString &msg) - { - m_out.write(msg.toLocal8Bit()); - } - - static QString escapeCode(const QString &in) - { - QString result; - result.append(QChar(0x1B)); - result.append(QLatin1Char('[')); - result.append(in); - result.append(QLatin1Char('m')); - return result; - } - - private: - QFile m_out; - - /*! - Returns true if it's suitable to send colored output to \c stderr. - */ - inline bool isColoringPossible() const - { -# if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) - /* Windows doesn't at all support ANSI escape codes, unless - * the user install a "device driver". See the Wikipedia links in the - * class documentation for details. */ - return false; -# else - /* We use QFile::handle() to get the file descriptor. It's a bit unsure - * whether it's 2 on all platforms and in all cases, so hopefully this layer - * of abstraction helps handle such cases. */ - return isatty(m_out.handle()); -# endif - } - }; -} - -const char *const ColorOutputPrivate::foregrounds[] = -{ - "0;30", - "0;34", - "0;32", - "0;36", - "0;31", - "0;35", - "0;33", - "0;37", - "1;30", - "1;34", - "1;32", - "1;36", - "1;31", - "1;35", - "1;33", - "1;37" -}; - -const char *const ColorOutputPrivate::backgrounds[] = -{ - "0;40", - "0;44", - "0;42", - "0;46", - "0;41", - "0;45", - "0;43" -}; - -/*! - \since 4.4 - \nonreentrant - \brief Outputs colored messages to \c stderr. - \internal - - ColorOutput is a convenience class for outputting messages to \c stderr - using color escape codes, as mandated in ECMA-48. ColorOutput will only - color output when it is detected to be suitable. For instance, if \c stderr is - detected to be attached to a file instead of a TTY, no coloring will be done. - - ColorOutput does its best attempt. but it is generally undefined what coloring - or effect the various coloring flags has. It depends strongly on what terminal - software that is being used. - - When using `echo -e 'my escape sequence'`, \033 works as an initiator but not - when printing from a C++ program, despite having escaped the backslash. - That's why we below use characters with value 0x1B. - - It can be convenient to subclass ColorOutput with a private scope, such that the - functions are directly available in the class using it. - - \section1 Usage - - To output messages, call write() or writeUncolored(). write() takes as second - argument an integer, which ColorOutput uses as a lookup key to find the color - it should color the text in. The mapping from keys to colors is done using - insertMapping(). Typically this is used by having enums for the various kinds - of messages, which subsequently are registered. - - \code - enum MyMessage - { - Error, - Important - }; - - ColorOutput output; - output.insertMapping(Error, ColorOutput::RedForeground); - output.insertMapping(Import, ColorOutput::BlueForeground); - - output.write("This is important", Important); - output.write("Jack, I'm only the selected official!", Error); - \endcode - - \sa {http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html} {Bash Prompt HOWTO, 6.1. Colours} - {http://linuxgazette.net/issue51/livingston-blade.html} {Linux Gazette, Tweaking Eterm, Edward Livingston-Blade} - {http://www.ecma-international.org/publications/standards/Ecma-048.htm} {Standard ECMA-48, Control Functions for Coded Character Sets, ECMA International}, - {http://en.wikipedia.org/wiki/ANSI_escape_code} {Wikipedia, ANSI escape code} - {http://linuxgazette.net/issue65/padala.html} {Linux Gazette, So You Like Color!, Pradeep Padala} - */ - -/*! - \enum ColorOutput::ColorCode - - \value DefaultColor ColorOutput performs no coloring. This typically means black on white - or white on black, depending on the settings of the user's terminal. - */ - -/*! - Sets the color mapping to be \a cMapping. - - Negative values are disallowed. - - \sa colorMapping(), insertMapping() - */ -void ColorOutput::setColorMapping(const ColorMapping &cMapping) -{ - d->colorMapping = cMapping; -} - -/*! - Returns the color mappings in use. - - \sa setColorMapping(), insertMapping() - */ -ColorOutput::ColorMapping ColorOutput::colorMapping() const -{ - return d->colorMapping; -} - -/*! - Constructs a ColorOutput instance, ready for use. - */ -ColorOutput::ColorOutput() : d(new ColorOutputPrivate()) -{ -} - -/*! - Destructs this ColorOutput instance. - */ -ColorOutput::~ColorOutput() -{ - delete d; -} - -/*! - Sends \a message to \c stderr, using the color looked up in colorMapping() using \a colorID. - - If \a color isn't available in colorMapping(), result and behavior is undefined. - - If \a colorID is 0, which is the default value, the previously used coloring is used. ColorOutput - is initialized to not color at all. - - If \a message is empty, effects are undefined. - - \a message will be printed as is. For instance, no line endings will be inserted. - */ -void ColorOutput::write(const QString &message, int colorID) -{ - d->write(colorify(message, colorID)); -} - -/*! - Writes \a message to \c stderr as if for instance - QTextStream would have been used, and adds a line ending at the end. - - This function can be practical to use such that one can use ColorOutput for all forms of writing. - */ -void ColorOutput::writeUncolored(const QString &message) -{ - d->write(message + QLatin1Char('\n')); -} - -/*! - Treats \a message and \a colorID identically to write(), but instead of writing - \a message to \c stderr, it is prepared for being written to \c stderr, but is then - returned. - - This is useful when the colored string is inserted into a translated string(dividing - the string into several small strings prevents proper translation). - */ -QString ColorOutput::colorify(const QString &message, int colorID) const -{ - Q_ASSERT_X(colorID == -1 || d->colorMapping.contains(colorID), Q_FUNC_INFO, - qPrintable(QString::fromLatin1("There is no color registered by id %1").arg(colorID))); - Q_ASSERT_X(!message.isEmpty(), Q_FUNC_INFO, "It makes no sense to attempt to print an empty string."); - - if(colorID != -1) - d->currentColorID = colorID; - - if(d->coloringEnabled && colorID != -1) - { - const int color(d->colorMapping.value(colorID)); - - /* If DefaultColor is set, we don't want to color it. */ - if(color & DefaultColor) - return message; - - const int foregroundCode = (int(color) & ForegroundMask) >> ForegroundShift; - const int backgroundCode = (int(color) & BackgroundMask) >> BackgroundShift; - QString finalMessage; - bool closureNeeded = false; - - if(foregroundCode) - { - finalMessage.append(ColorOutputPrivate::escapeCode(QLatin1String(ColorOutputPrivate::foregrounds[foregroundCode - 1]))); - closureNeeded = true; - } - - if(backgroundCode) - { - finalMessage.append(ColorOutputPrivate::escapeCode(QLatin1String(ColorOutputPrivate::backgrounds[backgroundCode - 1]))); - closureNeeded = true; - } - - finalMessage.append(message); - - if(closureNeeded) - { - finalMessage.append(QChar(0x1B)); - finalMessage.append(QLatin1String("[0m")); - } - - return finalMessage; - } - else - return message; -} - -/*! - Adds a color mapping from \a colorID to \a colorCode, for this ColorOutput instance. - - This is a convenience function for creating a ColorOutput::ColorMapping instance and - calling setColorMapping(). - - \sa colorMapping(), setColorMapping() - */ -void ColorOutput::insertMapping(int colorID, const ColorCode colorCode) -{ - d->colorMapping.insert(colorID, colorCode); -} - -QT_END_NAMESPACE diff --git a/tools/xmlpatterns/qcoloroutput_p.h b/tools/xmlpatterns/qcoloroutput_p.h deleted file mode 100644 index 1917ec7..0000000 --- a/tools/xmlpatterns/qcoloroutput_p.h +++ /dev/null @@ -1,133 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the XMLPatterns module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. - -#ifndef Patternist_ColorOutput_h -#define Patternist_ColorOutput_h - -#include -#include - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -namespace QPatternist -{ - class ColorOutputPrivate; - - class ColorOutput - { - enum - { - ForegroundShift = 10, - BackgroundShift = 20, - SpecialShift = 20, - ForegroundMask = ((1 << ForegroundShift) - 1) << ForegroundShift, - BackgroundMask = ((1 << BackgroundShift) - 1) << BackgroundShift - }; - - public: - enum ColorCodeComponent - { - BlackForeground = 1 << ForegroundShift, - BlueForeground = 2 << ForegroundShift, - GreenForeground = 3 << ForegroundShift, - CyanForeground = 4 << ForegroundShift, - RedForeground = 5 << ForegroundShift, - PurpleForeground = 6 << ForegroundShift, - BrownForeground = 7 << ForegroundShift, - LightGrayForeground = 8 << ForegroundShift, - DarkGrayForeground = 9 << ForegroundShift, - LightBlueForeground = 10 << ForegroundShift, - LightGreenForeground = 11 << ForegroundShift, - LightCyanForeground = 12 << ForegroundShift, - LightRedForeground = 13 << ForegroundShift, - LightPurpleForeground = 14 << ForegroundShift, - YellowForeground = 15 << ForegroundShift, - WhiteForeground = 16 << ForegroundShift, - - BlackBackground = 1 << BackgroundShift, - BlueBackground = 2 << BackgroundShift, - GreenBackground = 3 << BackgroundShift, - CyanBackground = 4 << BackgroundShift, - RedBackground = 5 << BackgroundShift, - PurpleBackground = 6 << BackgroundShift, - BrownBackground = 7 << BackgroundShift, - DefaultColor = 1 << SpecialShift - }; - - typedef QFlags ColorCode; - typedef QHash ColorMapping; - - ColorOutput(); - ~ColorOutput(); - - void setColorMapping(const ColorMapping &cMapping); - ColorMapping colorMapping() const; - void insertMapping(int colorID, const ColorCode colorCode); - - void writeUncolored(const QString &message); - void write(const QString &message, int color = -1); - QString colorify(const QString &message, int color = -1) const; - - private: - ColorOutputPrivate *d; - Q_DISABLE_COPY(ColorOutput) - }; -} - -Q_DECLARE_OPERATORS_FOR_FLAGS(QPatternist::ColorOutput::ColorCode) - -QT_END_NAMESPACE - -QT_END_HEADER - -#endif diff --git a/tools/xmlpatterns/xmlpatterns.pro b/tools/xmlpatterns/xmlpatterns.pro index 47f5a48..8cd321c 100644 --- a/tools/xmlpatterns/xmlpatterns.pro +++ b/tools/xmlpatterns/xmlpatterns.pro @@ -17,16 +17,12 @@ CONFIG -= app_bundle # in libQtXmlPatterns. See src/xmlpatterns/api/api.pri. SOURCES = main.cpp \ qapplicationargument.cpp \ - qapplicationargumentparser.cpp \ - qcoloringmessagehandler.cpp \ - qcoloroutput.cpp + qapplicationargumentparser.cpp HEADERS = main.h \ qapplicationargument.cpp \ - qapplicationargumentparser.cpp \ - qcoloringmessagehandler_p.h \ - qcoloroutput_p.h + qapplicationargumentparser.cpp symbian: TARGET.UID3 = 0xA000D7C9 -- cgit v0.12