| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtXmlPatterns 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_Locale_H |
| 51 | #define Patternist_Locale_H |
| 52 | |
| 53 | #include <QCoreApplication> |
| 54 | #include <QString> |
| 55 | #include <QUrl> |
| 56 | |
| 57 | #include <private/qcardinality_p.h> |
| 58 | #include <private/qnamepool_p.h> |
| 59 | #include <private/qprimitives_p.h> |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | /** |
| 64 | * @file |
| 65 | * @short Contains functions used for formatting arguments, such as keywords and paths, |
| 66 | * in translated strings. |
| 67 | */ |
| 68 | |
| 69 | namespace QPatternist |
| 70 | { |
| 71 | /** |
| 72 | * @short Provides a translation context & functions for the Qt XML Patterns |
| 73 | * module. |
| 74 | * |
| 75 | * This class is not supposed to be instantiated. |
| 76 | */ |
| 77 | class QtXmlPatterns |
| 78 | { |
| 79 | public: |
| 80 | Q_DECLARE_TR_FUNCTIONS(QtXmlPatterns) |
| 81 | |
| 82 | private: |
| 83 | /** |
| 84 | * No implementation is provided, this class is not supposed to be |
| 85 | * instantiated. |
| 86 | */ |
| 87 | inline QtXmlPatterns(); |
| 88 | Q_DISABLE_COPY(QtXmlPatterns) |
| 89 | }; |
| 90 | |
| 91 | // don't make this function static, otherwise xlC 7 cannot find it |
| 92 | inline QString formatKeyword(const QString &keyword) |
| 93 | { |
| 94 | return QLatin1String("<span class='XQuery-keyword'>" ) + |
| 95 | escape(input: keyword) + |
| 96 | QLatin1String("</span>" ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @overload |
| 101 | */ |
| 102 | static inline QString formatKeyword(const QStringRef &keyword) |
| 103 | { |
| 104 | return formatKeyword(keyword: keyword.toString()); |
| 105 | } |
| 106 | |
| 107 | static inline QString formatKeyword(const char *const keyword) |
| 108 | { |
| 109 | return formatKeyword(keyword: QLatin1String(keyword)); |
| 110 | } |
| 111 | |
| 112 | static inline QString formatKeyword(const QChar keyword) |
| 113 | { |
| 114 | return formatKeyword(keyword: QString(keyword)); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @short Formats element name. |
| 119 | */ |
| 120 | static inline QString formatElement(const QString &element) |
| 121 | { |
| 122 | // for the moment we forward to formatKeyword, that will change later |
| 123 | return formatKeyword(keyword: element); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @overload |
| 128 | */ |
| 129 | static inline QString formatElement(const char *const element) |
| 130 | { |
| 131 | return formatElement(element: QLatin1String(element)); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @short Formats attribute name. |
| 136 | */ |
| 137 | static inline QString formatAttribute(const QString &attribute) |
| 138 | { |
| 139 | // for the moment we forward to formatKeyword, that will change later |
| 140 | return formatKeyword(keyword: attribute); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @overload |
| 145 | */ |
| 146 | static inline QString formatAttribute(const char *const attribute) |
| 147 | { |
| 148 | return formatAttribute(attribute: QLatin1String(attribute)); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @short Formats ItemType and SequenceType. |
| 153 | * |
| 154 | * This function is not declared static, because the compiler on target |
| 155 | * aix-xlc-64 won't accept it. |
| 156 | */ |
| 157 | template<typename T> |
| 158 | inline QString formatType(const NamePool::Ptr &np, const T &type) |
| 159 | { |
| 160 | Q_ASSERT(type); |
| 161 | return QLatin1String("<span class='XQuery-type'>" ) + |
| 162 | escape(type->displayName(np)) + |
| 163 | QLatin1String("</span>" ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @short Formats name of any type. |
| 168 | */ |
| 169 | static inline QString formatType(const NamePool::Ptr &np, const QXmlName &name) |
| 170 | { |
| 171 | return QLatin1String("<span class='XQuery-type'>" ) + |
| 172 | escape(input: np->displayName(qName: name)) + |
| 173 | QLatin1String("</span>" ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @short Formats Cardinality. |
| 178 | */ |
| 179 | static inline QString formatType(const Cardinality &type) |
| 180 | { |
| 181 | return QLatin1String("<span class='XQuery-type'>" ) + |
| 182 | escape(input: type.displayName(explanation: Cardinality::IncludeExplanation)) + |
| 183 | QLatin1String("</span>" ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @short Formats @p uri as a path to a resource, typically it's a filename |
| 188 | * or a URI. |
| 189 | */ |
| 190 | static inline QString formatResourcePath(const QUrl &uri) |
| 191 | { |
| 192 | const QString normalizedURI(escape(input: uri.toString(options: QUrl::RemovePassword))); |
| 193 | |
| 194 | return QLatin1String("<span class='XQuery-filepath'><a href='" ) + |
| 195 | normalizedURI + |
| 196 | QLatin1String("'>" ) + |
| 197 | normalizedURI + |
| 198 | QLatin1String("</a></span>" ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @short Formats @p uri for display. |
| 203 | * |
| 204 | * @note It's not guaranteed that URIs being formatted are valid. That can |
| 205 | * be an arbitrary string. |
| 206 | */ |
| 207 | static inline QString formatURI(const QUrl &uri) |
| 208 | { |
| 209 | return QLatin1String("<span class='XQuery-uri'>" ) + |
| 210 | escape(input: uri.toString(options: QUrl::RemovePassword)) + |
| 211 | QLatin1String("</span>" ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @short Formats @p uri, that's considered to be a URI, for display. |
| 216 | * |
| 217 | * @p uri does not have to be a valid QUrl or valid instance of @c |
| 218 | * xs:anyURI. |
| 219 | */ |
| 220 | static inline QString formatURI(const QString &uri) |
| 221 | { |
| 222 | const QUrl realURI(uri); |
| 223 | return formatURI(uri: realURI); |
| 224 | } |
| 225 | |
| 226 | static inline QString formatData(const QString &data) |
| 227 | { |
| 228 | return QLatin1String("<span class='XQuery-data'>" ) + |
| 229 | escape(input: data) + |
| 230 | QLatin1String("</span>" ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * This is an overload, provided for convenience. |
| 235 | */ |
| 236 | static inline QString formatData(const xsInteger data) |
| 237 | { |
| 238 | return formatData(data: QString::number(data)); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * This is an overload, provided for convenience. |
| 243 | */ |
| 244 | static inline QString formatData(const char *const data) |
| 245 | { |
| 246 | return formatData(data: QLatin1String(data)); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * This is an overload, provided for convenience. |
| 251 | */ |
| 252 | static inline QString formatData(const QLatin1Char &data) |
| 253 | { |
| 254 | return formatData(data: QString(data)); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Formats an arbitrary expression, such as a regular expression |
| 259 | * or XQuery query. |
| 260 | */ |
| 261 | static inline QString formatExpression(const QString &expr) |
| 262 | { |
| 263 | return QLatin1String("<span class='XQuery-expression'>" ) + |
| 264 | escape(input: expr) + |
| 265 | QLatin1String("</span>" ); |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
| 270 | #ifdef Q_NO_TYPESAFE_FLAGS |
| 271 | #error "Patternist does not compile with Q_NO_TYPESAFE_FLAGS set, because the code uses negative enum values. qglobal.h has typedef uint Flags." |
| 272 | #endif |
| 273 | |
| 274 | #ifdef QT_NO_EXCEPTIONS |
| 275 | #error "Patternist uses exceptions and cannot be built without." |
| 276 | #endif |
| 277 | |
| 278 | QT_END_NAMESPACE |
| 279 | #endif |
| 280 | |