Qt 4.8
Public Functions | Private Functions | Static Private Functions | Properties | List of all members
QPatternist::ReplaceFN Class Reference

Implements the function fn:replace(). More...

#include <qpatternmatchingfns_p.h>

Inheritance diagram for QPatternist::ReplaceFN:
QPatternist::PatternPlatform QPatternist::FunctionCall QPatternist::UnlimitedContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Public Functions

virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &context) const
 
 ReplaceFN ()
 
- Public Functions inherited from QPatternist::PatternPlatform
int captureCount () const
 
const QRegExp pattern (const DynamicContext::Ptr &context) const
 
- Public Functions inherited from QPatternist::FunctionCall
virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual SequenceType::List expectedOperandTypes () const
 
virtual ID id () const
 
virtual Expression::Properties properties () const
 
virtual void setSignature (const FunctionSignature::Ptr &sign)
 
virtual FunctionSignature::Ptr signature () const
 
virtual SequenceType::Ptr staticType () const
 
virtual Expression::Ptr typeCheck (const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
 
- Public Functions inherited from QPatternist::UnlimitedContainer
virtual bool compressOperands (const StaticContext::Ptr &)
 
virtual Expression::List operands () const
 
virtual void setOperands (const Expression::List &list)
 
 UnlimitedContainer (const Expression::List &operands=Expression::List())
 
- Public Functions inherited from QPatternist::Expression
virtual const SourceLocationReflectionactualReflection () const
 Returns this. More...
 
virtual void announceFocusType (const ItemType::Ptr &itemType)
 
Properties deepProperties () const
 Computes the union of properties for this Expression and all its descending children. More...
 
virtual Properties dependencies () const
 
virtual QString description () const
 
virtual bool evaluateEBV (const DynamicContext::Ptr &context) const
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual void evaluateToSequenceReceiver (const DynamicContext::Ptr &context) const
 
virtual ItemType::Ptr expectedContextItemType () const
 
 Expression ()
 
bool has (const Property prop) const
 
bool hasDependency (const Property prop) const
 
bool is (const ID id) const
 
bool isEvaluated () const
 
virtual ItemType::Ptr newFocusType () const
 
virtual QList< QExplicitlySharedDataPointer< OptimizationPass > > optimizationPasses () const
 
virtual PatternPriority patternPriority () const
 
const Expression::Ptrrewrite (const Expression::Ptr &to, const StaticContext::Ptr &context) const
 Rewrites this Expression to to, and return to. More...
 
virtual ~Expression ()
 
- 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< Expression >
const TCastTarget * as () const
 
TCastTarget * as ()
 
- Public Functions inherited from QPatternist::SourceLocationReflection
virtual QSourceLocation sourceLocation () const
 
 SourceLocationReflection ()
 
virtual ~SourceLocationReflection ()
 

Private Functions

QString parseReplacement (const int captureCount, const DynamicContext::Ptr &context) const
 

Static Private Functions

static QString errorAtEnd (const char ch)
 Centralizes the translation string. More...
 

Properties

QString m_replacementString
 

Additional Inherited Members

- Public Types inherited from QPatternist::PatternPlatform
enum  Flag {
  NoFlags = 0, DotAllMode = 1, MultiLineMode = 2, CaseInsensitive = 4,
  SimplifyWhitespace = 8
}
 
typedef QFlags< FlagFlags
 
- Public Types inherited from QPatternist::FunctionCall
typedef QExplicitlySharedDataPointer< FunctionCallPtr
 
- Public Types inherited from QPatternist::UnlimitedContainer
enum  CardinalityComputation { ProductOfCardinality, UnionOfCardinality }
 
- Public Types inherited from QPatternist::Expression
typedef QExplicitlySharedDataPointer< const ExpressionConstPtr
 A smart pointer wrapping const Expression instances. More...
 
enum  ID {
  IDBooleanValue = 1, IDCountFN, IDEmptyFN, IDExistsFN,
  IDExpressionSequence, IDGeneralComparison, IDIfThenClause, IDIgnorableExpression,
  IDIntegerValue, IDPositionFN, IDStringValue, IDValueComparison,
  IDRangeVariableReference, IDContextItem, IDUserFunctionCallsite, IDExpressionVariableReference,
  IDAttributeConstructor, IDUpperCaseFN, IDLowerCaseFN, IDFirstItemPredicate,
  IDEmptySequence, IDReturnOrderBy, IDLetClause, IDForClause,
  IDPath, IDNamespaceConstructor, IDArgumentReference, IDGenericPredicate,
  IDAxisStep, IDFloat, IDCombineNodes, IDUnresolvedVariableReference,
  IDCardinalityVerifier
}
 
typedef QList< Expression::PtrList
 
typedef QFlags< PropertyProperties
 
enum  Property {
  UseContextItem = 1, DisableElimination = 1 << 1, IsEvaluated = 1 << 2, DisableTypingDeduction = 1 << 3,
  EmptynessFollowsChild = 1 << 4, RewriteToEmptyOnEmpty = 1 << 5, RequiresFocus = 1 << 6, AffectsOrderOnly = 1 << 7,
  RequiresContextItem = (1 << 8) | RequiresFocus, CreatesFocusForLast = 1 << 9, LastOperandIsCollation = 1 << 10, DependsOnLocalVariable = (1 << 11) | DisableElimination,
  EvaluationCacheRedundant = (1 << 12), IsNodeConstructor = 1 << 13, RequiresCurrentItem = 1 << 14
}
 
typedef QExplicitlySharedDataPointer< ExpressionPtr
 A smart pointer wrapping mutable Expression instances. More...
 
typedef ::QAbstractXmlForwardIterator< Expression::PtrQAbstractXmlForwardIterator
 
typedef QVector< Expression::PtrVector
 
- Static Public Functions inherited from QPatternist::PatternPlatform
static QRegExp parsePattern (const QString &pattern, const ReportContext::Ptr &context, const SourceLocationReflection *const location)
 Parses pattern. More...
 
- Static Public Functions inherited from QPatternist::Expression
static void rewrite (Expression::Ptr &old, const Expression::Ptr &New, const StaticContext::Ptr &context)
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Protected Functions inherited from QPatternist::PatternPlatform
 PatternPlatform (const qint8 flagsPosition)
 This constructor is protected, because this class is supposed to be sub-classed. More...
 
- Protected Functions inherited from QPatternist::UnlimitedContainer
template<CardinalityComputation suppliedCard>
SequenceType::Ptr operandsUnionType () const
 
- Protected Functions inherited from QPatternist::Expression
void typeCheckOperands (const StaticContext::Ptr &context)
 
- Protected Functions inherited from QPatternist::CppCastingHelper< Expression >
 CppCastingHelper ()
 
- Protected Variables inherited from QPatternist::UnlimitedContainer
Expression::List m_operands
 

Detailed Description

Implements the function fn:replace().

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

Definition at line 91 of file qpatternmatchingfns_p.h.

Constructors and Destructors

◆ ReplaceFN()

ReplaceFN::ReplaceFN ( )

Definition at line 72 of file qpatternmatchingfns.cpp.

72  : PatternPlatform(3)
73 {
74 }
PatternPlatform(const qint8 flagsPosition)
This constructor is protected, because this class is supposed to be sub-classed.

Functions

◆ compress()

Expression::Ptr ReplaceFN::compress ( const StaticContext::Ptr context)
virtual

Overridden to attempt to pre-compile the replacement string.

Reimplemented from QPatternist::PatternPlatform.

Definition at line 176 of file qpatternmatchingfns.cpp.

177 {
178  const Expression::Ptr me(PatternPlatform::compress(context));
179 
180  if(me != this)
181  return me;
182 
183  if(m_operands.at(2)->is(IDStringValue))
184  {
185  const int capt = captureCount();
186  if(capt == -1)
187  return me;
188  else
190  }
191 
192  return me;
193 }
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual QExplicitlySharedDataPointer< DynamicContext > dynamicContext() const =0
QString parseReplacement(const int captureCount, const DynamicContext::Ptr &context) const
bool is(const ID id) const

◆ errorAtEnd()

QString ReplaceFN::errorAtEnd ( const char  ch)
inlinestaticprivate

Centralizes the translation string.

Definition at line 92 of file qpatternmatchingfns.cpp.

Referenced by parseReplacement().

93 {
94  return QtXmlPatterns::tr("%1 must be followed by %2 or %3, not at "
95  "the end of the replacement string.")
96  .arg(formatKeyword(QLatin1Char(ch)))
97  .arg(formatKeyword(QLatin1Char('\\')))
98  .arg(formatKeyword(QLatin1Char('$')));
99 }
QString formatKeyword(const QString &keyword)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ evaluateSingleton()

Item ReplaceFN::evaluateSingleton ( const DynamicContext::Ptr context) const
virtual

Reimplemented from QPatternist::Expression.

Definition at line 76 of file qpatternmatchingfns.cpp.

77 {
78  const QRegExp regexp(pattern(context));
79  QString input;
80 
81  const Item arg(m_operands.first()->evaluateSingleton(context));
82  if(arg)
83  input = arg.stringValue();
84 
85  const QString replacement(m_replacementString.isNull() ? parseReplacement(regexp.captureCount(), context)
87 
88 
89  return AtomicString::fromValue(input.replace(regexp, replacement));
90 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
The QString class provides a Unicode character string.
Definition: qstring.h:83
static AtomicString::Ptr fromValue(const QString &value)
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
QString parseReplacement(const int captureCount, const DynamicContext::Ptr &context) const
const QRegExp pattern(const DynamicContext::Ptr &context) const
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const

◆ parseReplacement()

QString ReplaceFN::parseReplacement ( const int  captureCount,
const DynamicContext::Ptr context 
) const
private

Reads the string in the third argument and converts it to a a QRegExp compatible replacement string, containing sub-group references and so forth.

Definition at line 101 of file qpatternmatchingfns.cpp.

Referenced by compress(), and evaluateSingleton().

103 {
104  // TODO what if there is no groups, can one rewrite to the replacement then?
105  const QString input(m_operands.at(2)->evaluateSingleton(context).stringValue());
106 
107  QString retval;
108  retval.reserve(input.size());
109  const int len = input.length();
110 
111  for(int i = 0; i < len; ++i)
112  {
113  const QChar ch(input.at(i));
114  switch(ch.toAscii())
115  {
116  case '$':
117  {
118  /* QRegExp uses '\' as opposed to '$' for marking sub groups. */
119  retval.append(QLatin1Char('\\'));
120 
121  ++i;
122  if(i == len)
123  {
124  context->error(errorAtEnd('$'), ReportContext::FORX0004, this);
125  return QString();
126  }
127 
128  const QChar nextCh(input.at(i));
129  if(nextCh.isDigit())
130  retval.append(nextCh);
131  else
132  {
133  context->error(QtXmlPatterns::tr("In the replacement string, %1 must be "
134  "followed by at least one digit when not escaped.")
135  .arg(formatKeyword(QLatin1Char('$'))),
137  return QString();
138  }
139 
140  break;
141  }
142  case '\\':
143  {
144  ++i;
145  if(i == len)
146  {
147  /* error, we've reached the end. */;
148  context->error(errorAtEnd('\\'), ReportContext::FORX0004, this);
149  }
150 
151  const QChar nextCh(input.at(i));
152  if(nextCh == QLatin1Char('\\') || nextCh == QLatin1Char('$'))
153  {
154  retval.append(ch);
155  break;
156  }
157  else
158  {
159  context->error(QtXmlPatterns::tr("In the replacement string, %1 can only be used to "
160  "escape itself or %2, not %3")
161  .arg(formatKeyword(QLatin1Char('\\')))
162  .arg(formatKeyword(QLatin1Char('$')))
163  .arg(formatKeyword(nextCh)),
165  return QString();
166  }
167  }
168  default:
169  retval.append(ch);
170  }
171  }
172 
173  return retval;
174 }
QString formatKeyword(const QString &keyword)
static QString errorAtEnd(const char ch)
Centralizes the translation string.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const

Properties

◆ m_replacementString

QString QPatternist::ReplaceFN::m_replacementString
private

Definition at line 114 of file qpatternmatchingfns_p.h.

Referenced by compress(), and evaluateSingleton().


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