Qt 4.8
qderivedstring_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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 //
43 // W A R N I N G
44 // -------------
45 //
46 // This file is not part of the Qt API. It exists purely as an
47 // implementation detail. This header file may change from version to
48 // version without notice, or even be removed.
49 //
50 // We mean it.
51 
52 #ifndef Patternist_DerivedString_H
53 #define Patternist_DerivedString_H
54 
55 #include <QRegExp>
56 
57 #include "private/qxmlutils_p.h"
58 #include "qbuiltintypes_p.h"
59 #include "qpatternistlocale_p.h"
60 #include "qvalidationerror_p.h"
61 
63 
65 
66 namespace QPatternist
67 {
94  template<TypeOfDerivedString DerivedType>
95  class DerivedString : public AtomicValue
96  {
97  private:
98  static inline ItemType::Ptr itemType()
99  {
100  switch(DerivedType)
101  {
103  case TypeToken: return BuiltinTypes::xsToken;
106  case TypeName: return BuiltinTypes::xsName;
107  case TypeNCName: return BuiltinTypes::xsNCName;
108  case TypeID: return BuiltinTypes::xsID;
109  case TypeIDREF: return BuiltinTypes::xsIDREF;
110  case TypeENTITY: return BuiltinTypes::xsENTITY;
111  case TypeString: return BuiltinTypes::xsString;
112  }
113 
114  Q_ASSERT_X(false, Q_FUNC_INFO, "This line is not supposed to be reached.");
115  return ItemType::Ptr();
116  }
117 
119 
120  inline DerivedString(const QString &value) : m_value(value)
121  {
122  }
123 
128  static inline bool isNameChar(const QChar &ch)
129  {
130  return ch.isLetter() ||
131  ch.isDigit() ||
132  ch == QLatin1Char('.') ||
133  ch == QLatin1Char('-') ||
134  ch == QLatin1Char('_') ||
135  ch == QLatin1Char(':');
136  }
137 
143  static inline bool isValidName(const QString &input)
144  {
145  if(input.isEmpty())
146  return false;
147 
148  const QChar first(input.at(0));
149 
150  if(first.isLetter() ||
151  first == QLatin1Char('_') ||
152  first == QLatin1Char(':'))
153  {
154  const int len = input.length();
155 
156  if(len == 1)
157  return true;
158 
159  /* Since we've checked the first character above, we start at
160  * position 1. */
161  for(int i = 1; i < len; ++i)
162  {
163  if(!isNameChar(input.at(i)))
164  return false;
165  }
166 
167  return true;
168  }
169  else
170  return false;
171  }
172 
180  static inline bool isValidNMTOKEN(const QString &input)
181  {
182  const int len = input.length();
183 
184  if(len == 0)
185  return false;
186 
187  for(int i = 0; i < len; ++i)
188  {
189  if(!isNameChar(input.at(i)))
190  return false;
191  }
192 
193  return true;
194  }
195 
209  static QString attributeNormalize(const QString &input)
210  {
211  QString retval(input);
212  const int len = retval.length();
213  const QLatin1Char space(' ');
214 
215  for(int i = 0; i < len; ++i)
216  {
217  const QChar ati(retval.at(i));
218 
219  if(ati.isSpace() && ati != space)
220  retval[i] = space;
221  }
222 
223  return retval;
224  }
225 
226  static AtomicValue::Ptr error(const NamePool::Ptr &np, const QString &invalidValue)
227  {
228  return ValidationError::createError(QString::fromLatin1("%1 is not a valid value for "
229  "type %2.").arg(formatData(invalidValue))
230  .arg(formatType(np, itemType())));
231  }
232 
233  public:
234 
242  static AtomicValue::Ptr fromValue(const QString &value)
243  {
244  return AtomicValue::Ptr(new DerivedString(value));
245  }
246 
251  static AtomicValue::Ptr fromLexical(const NamePool::Ptr &np, const QString &lexical)
252  {
253  switch(DerivedType)
254  {
255  case TypeString:
256  return AtomicValue::Ptr(new DerivedString(lexical));
258  return AtomicValue::Ptr(new DerivedString(attributeNormalize(lexical)));
259  case TypeToken:
260  return AtomicValue::Ptr(new DerivedString(lexical.simplified()));
261  case TypeLanguage:
262  {
263  const QString simplified(lexical.trimmed());
264 
265  const QRegExp validate(QLatin1String("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
266  Q_ASSERT(validate.isValid());
267 
268  if(validate.exactMatch(simplified))
269  return AtomicValue::Ptr(new DerivedString(lexical.simplified()));
270  else
271  return error(np, simplified);
272  }
273  case TypeNMTOKEN:
274  {
275  const QString trimmed(lexical.trimmed());
276 
279  else
280  return error(np, trimmed);
281  }
282  case TypeName:
283  {
284  const QString simplified(lexical.simplified());
285 
286  if(isValidName(simplified))
287  return AtomicValue::Ptr(new DerivedString(simplified));
288  else
289  return error(np, simplified);
290  }
291  case TypeID:
292  /* Fallthrough. */
293  case TypeIDREF:
294  /* Fallthrough. */
295  case TypeENTITY:
296  /* Fallthrough. */
297  case TypeNCName:
298  {
299  /* We treat xs:ID, xs:ENTITY, xs:IDREF and xs:NCName in the exact same
300  * way, except for the type annotation.
301  *
302  * We use trimmed() instead of simplified() because it's
303  * faster and whitespace isn't allowed between
304  * non-whitespace characters anyway, for these types. */
305  const QString trimmed(lexical.trimmed());
306 
309  else
310  return error(np, trimmed);
311  }
312  default:
313  {
314  Q_ASSERT_X(false, Q_FUNC_INFO, "This line is not supposed to be reached.");
315  return AtomicValue::Ptr();
316  }
317  }
318  }
319 
320  virtual QString stringValue() const
321  {
322  return m_value;
323  }
324 
326  {
327  return m_value.length() > 0;
328  }
329 
330  virtual ItemType::Ptr type() const
331  {
332  return itemType();
333  }
334  };
335 }
336 
338 
340 
341 #endif
static AtomicValue::Ptr fromValue(const QString &value)
static const AtomicType::Ptr xsID
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
bool isLetter() const
Returns true if the character is a letter (Letter_* categories); otherwise returns false...
Definition: qchar.cpp:653
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
static ItemType::Ptr itemType()
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static bool isValidName(const QString &input)
static AtomicValue::Ptr createError(const QString &description=QString(), const ReportContext::ErrorCode=ReportContext::FORG0001)
static const AtomicType::Ptr xsNormalizedString
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static QString attributeNormalize(const QString &input)
Performs attribute value normalization as if input was not from a CDATA section.
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static const AtomicType::Ptr xsName
static const AtomicType::Ptr xsIDREF
static const AtomicType::Ptr xsString
static const AtomicType::Ptr xsNMTOKEN
DerivedString(const QString &value)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Base class for all classes representing atomic values.
Definition: qitem_p.h:118
QString trimmed() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end.
Definition: qstring.cpp:4506
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The namespace for the internal API of QtXmlPatterns.
virtual ItemType::Ptr type() const
static const AtomicType::Ptr xsENTITY
static QString formatData(const QString &data)
Represents instances of derived xs:string types, such as xs:normalizedString.
QExplicitlySharedDataPointer< ItemType > Ptr
Definition: qitemtype_p.h:88
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
QString trimmed(QString source)
Definition: generator.cpp:233
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static bool isValidNMTOKEN(const QString &input)
virtual QString stringValue() const
static const AtomicType::Ptr xsNCName
static bool isNameChar(const QChar &ch)
This is an incomplete test for whether ch conforms to the XML 1.0 NameChar production.
static AtomicValue::Ptr error(const NamePool::Ptr &np, const QString &invalidValue)
static const AtomicType::Ptr xsLanguage
#define QT_END_HEADER
Definition: qglobal.h:137
virtual bool evaluateEBV(const QExplicitlySharedDataPointer< DynamicContext > &) const
static bool isNCName(const QStringRef &ncName)
Determines whether c is a valid instance of production [4]NCName in the XML 1.0 Namespaces specificat...
Definition: qxmlutils.cpp:377
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool isDigit() const
Returns true if the character is a decimal digit (Number_DecimalDigit); otherwise returns false...
Definition: qchar.cpp:699
static const AtomicType::Ptr xsToken
static AtomicValue::Ptr fromLexical(const NamePool::Ptr &np, const QString &lexical)
#define Q_FUNC_INFO
Definition: qglobal.h:1871