Qt 4.8
Public Types | Public Functions | Static Public Functions | Protected Functions | Static Private Functions | Friends | List of all members
QPatternist::HexBinary Class Reference

Implements the value instance of the xs:hexBinary type. More...

#include <qhexbinary_p.h>

Inheritance diagram for QPatternist::HexBinary:
QPatternist::Base64Binary QPatternist::AtomicValue QSharedData QPatternist::CppCastingHelper< AtomicValue >

Public Types

typedef AtomicValue::Ptr Ptr
 
- Public Types inherited from QPatternist::Base64Binary
typedef AtomicValue::Ptr Ptr
 
- Public Types inherited from QPatternist::AtomicValue
typedef QList< AtomicValue::PtrList
 
typedef QExplicitlySharedDataPointer< AtomicValuePtr
 

Public Functions

virtual QString stringValue () const
 
virtual ItemType::Ptr type () const
 
- Public Functions inherited from QPatternist::Base64Binary
const QByteArrayasByteArray () const
 
- Public Functions inherited from QPatternist::AtomicValue
virtual bool evaluateEBV (const QExplicitlySharedDataPointer< DynamicContext > &context) const
 
virtual bool hasError () const
 
virtual ~AtomicValue ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
const TCastTarget * as () const
 
TCastTarget * as ()
 

Static Public Functions

static AtomicValue::Ptr fromLexical (const NamePool::Ptr &np, const QString &value)
 
static HexBinary::Ptr fromValue (const QByteArray &data)
 
- Static Public Functions inherited from QPatternist::Base64Binary
static AtomicValue::Ptr fromLexical (const QString &value)
 
static Base64Binary::Ptr fromValue (const QByteArray &data)
 
- Static Public Functions inherited from QPatternist::AtomicValue
static ItemType::Ptr qtToXDMType (const QXmlItem &item)
 
static QVariant toQt (const AtomicValue *const value)
 
static QVariant toQt (const AtomicValue::Ptr &value)
 
static Item toXDM (const QVariant &value)
 

Protected Functions

 HexBinary (const QByteArray &val)
 
- Protected Functions inherited from QPatternist::Base64Binary
 Base64Binary (const QByteArray &val)
 
- Protected Functions inherited from QPatternist::AtomicValue
 AtomicValue ()
 
- Protected Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
 CppCastingHelper ()
 

Static Private Functions

static qint8 fromHex (const QChar &c)
 Returns -1 on invalid input. More...
 

Friends

class CommonValues
 

Additional Inherited Members

- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Protected Variables inherited from QPatternist::Base64Binary
const QByteArray m_value
 

Detailed Description

Implements the value instance of the xs:hexBinary type.

HexBinary inherits from Base64Binary for implementation reasons. The two classes are similar, and inheritance therefore save code.

Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 74 of file qhexbinary_p.h.

Typedefs

◆ Ptr

Definition at line 79 of file qhexbinary_p.h.

Constructors and Destructors

◆ HexBinary()

HexBinary::HexBinary ( const QByteArray val)
protected

Definition at line 55 of file qhexbinary.cpp.

Referenced by fromLexical(), and fromValue().

55  : Base64Binary(val)
56 {
57 }
Base64Binary(const QByteArray &val)

Functions

◆ fromHex()

qint8 HexBinary::fromHex ( const QChar c)
inlinestaticprivate

Returns -1 on invalid input.

Definition at line 59 of file qhexbinary.cpp.

Referenced by fromLexical().

60 {
61  if(c.unicode() > 'f')
62  return -1;
63 
64  const char *const range = "0123456789ABCDEFabcdef";
65 
66  const char *const in = strchr(range, c.unicode());
67 
68  if(!in)
69  return -1;
70 
71  /* Pointer arithmetic. */
72  int digit = in - range;
73 
74  if(digit > 15)
75  digit -= 6;
76 
77  return digit;
78 }
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251

◆ fromLexical()

AtomicValue::Ptr HexBinary::fromLexical ( const NamePool::Ptr np,
const QString value 
)
static

Creates a xs:hexBinary from the lexical representation value.

Definition at line 80 of file qhexbinary.cpp.

Referenced by QPatternist::StringToHexBinaryCaster::castFrom().

81 {
82  const QString lexical(str.trimmed());
83  const int len = lexical.length();
84 
85  if(len == 0)
86  return AtomicValue::Ptr(new HexBinary(QByteArray()));
87 
88  if((len & 1) != 0)
89  {
90  /* Catch a common case. */
91  return ValidationError::createError(QtXmlPatterns::tr(
92  "A value of type %1 must contain an even number of "
93  "digits. The value %2 does not.")
96  }
97 
98  QByteArray val;
99  val.resize(len / 2);
100 
101  for(int i = 0; i < len / 2; ++i)
102  {
103  qint8 p1 = fromHex(lexical[i * 2]);
104  qint8 p2 = fromHex(lexical[i * 2 + 1]);
105 
106  if(p1 == -1 || p2 == -1)
107  {
108  const QString hex(QString::fromLatin1("%1%2").arg(lexical[i * 2], lexical[i * 2 + 1]));
109 
110  return ValidationError::createError(QtXmlPatterns::tr(
111  "%1 is not valid as a value of type %2.")
112  .arg(formatData(hex),
114  }
115 
116  val[i] = static_cast<char>(p1 * 16 + p2);
117  }
118  Q_ASSERT(!val.isEmpty());
119 
120  return AtomicValue::Ptr(new HexBinary(val));
121 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static AtomicValue::Ptr createError(const QString &description=QString(), const ReportContext::ErrorCode=ReportContext::FORG0001)
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Q_CORE_EXPORT QTextStream & hex(QTextStream &s)
signed char qint8
Definition: qglobal.h:933
static qint8 fromHex(const QChar &c)
Returns -1 on invalid input.
Definition: qhexbinary.cpp:59
HexBinary(const QByteArray &val)
Definition: qhexbinary.cpp:55
static QString formatData(const QString &data)
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
void resize(int size)
Sets the size of the byte array to size bytes.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static const AtomicType::Ptr xsHexBinary

◆ fromValue()

HexBinary::Ptr HexBinary::fromValue ( const QByteArray data)
static

Creates an instance representing value.

Definition at line 123 of file qhexbinary.cpp.

Referenced by QPatternist::Base64BinaryToHexBinaryCaster::castFrom(), and QPatternist::AtomicValue::toXDM().

124 {
125  return HexBinary::Ptr(new HexBinary(data));
126 }
AtomicValue::Ptr Ptr
Definition: qhexbinary_p.h:79
HexBinary(const QByteArray &val)
Definition: qhexbinary.cpp:55

◆ stringValue()

QString HexBinary::stringValue ( ) const
virtual

Reimplemented from QPatternist::Base64Binary.

Definition at line 128 of file qhexbinary.cpp.

129 {
130  static const char s_toHex[] = "0123456789ABCDEF";
131  const int len = m_value.count();
132  QString result;
133  result.reserve(len * 2);
134 
135  for(int i = 0; i < len; ++i)
136  {
137  // This cast is significant.
138  const unsigned char val = static_cast<unsigned char>(m_value.at(i));
139  result += QLatin1Char(s_toHex[val >> 4]);
140  result += QLatin1Char(s_toHex[val & 0x0F]);
141  }
142 
143  return result;
144 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QByteArray m_value

◆ type()

ItemType::Ptr HexBinary::type ( ) const
virtual

Reimplemented from QPatternist::Base64Binary.

Definition at line 146 of file qhexbinary.cpp.

147 {
149 }
static const AtomicType::Ptr xsHexBinary

Friends and Related Functions

◆ CommonValues

friend class CommonValues
friend

Definition at line 77 of file qhexbinary_p.h.


The documentation for this class was generated from the following files: