~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to defaulttags/range.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
 
5
 
 
6
  This library is free software; you can redistribute it and/or
 
7
  modify it under the terms of the GNU Lesser General Public
 
8
  License as published by the Free Software Foundation; either version
 
9
  2 of the Licence, or (at your option) any later version.
 
10
 
 
11
  This library is distributed in the hope that it will be useful,
 
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
  Library General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public
 
17
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
*/
 
20
 
 
21
#include "range.h"
 
22
 
 
23
#include "parser.h"
 
24
 
 
25
#include "exception.h"
 
26
 
 
27
#include "util.h"
 
28
#include "engine.h"
 
29
 
 
30
 
 
31
RangeNodeFactory::RangeNodeFactory()
 
32
{
 
33
 
 
34
}
 
35
 
 
36
Grantlee::Node* RangeNodeFactory::getNode( const QString& tagContent, Parser* p ) const
 
37
{
 
38
  QStringList expr = smartSplit( tagContent );
 
39
 
 
40
  expr.takeAt( 0 );
 
41
  int numArgs = expr.size();
 
42
  if ( numArgs != 1 )
 
43
  {
 
44
    if ( numArgs <= 2 ) {
 
45
      throw Grantlee::Exception( TagSyntaxError, "'range' tag requires at least three arguments" );
 
46
    }
 
47
 
 
48
    if ( expr.at( numArgs - 2 ) != "as"  ) {
 
49
      throw Grantlee::Exception( TagSyntaxError, "Invalid arguments to 'range' tag" );
 
50
    }
 
51
  }
 
52
 
 
53
  const QString name = ( numArgs > 2 ) ? expr.at( numArgs - 1 ) : QString();
 
54
  if ( numArgs > 2 )
 
55
    numArgs -= 2;
 
56
 
 
57
  RangeNode *n = 0;
 
58
 
 
59
  switch ( numArgs ) {
 
60
  case 1:
 
61
    n = new RangeNode( name, FilterExpression( "0", p ), FilterExpression( expr.at( 0 ), p ), p );
 
62
    break;
 
63
  case 2:
 
64
    n = new RangeNode( name, FilterExpression( expr.at( 0 ), p ), FilterExpression( expr.at( 1 ), p ), p );
 
65
    break;
 
66
  case 3:
 
67
    n = new RangeNode( name, FilterExpression( expr.at( 0 ), p ), FilterExpression( expr.at( 1 ), p ), FilterExpression( expr.at( 2 ), p ), p );
 
68
    break;
 
69
  default:
 
70
    return 0;
 
71
  }
 
72
 
 
73
  NodeList list = p->parse( n, "endrange" );
 
74
  p->removeNextToken();
 
75
 
 
76
  n->setNodeList( list );
 
77
  return n;
 
78
}
 
79
 
 
80
RangeNode::RangeNode( const QString &name, const FilterExpression &startExpression, const FilterExpression &stopExpression, QObject* parent )
 
81
  : Node( parent ),
 
82
    m_name( name ),
 
83
    m_startExpression( startExpression ),
 
84
    m_stopExpression( stopExpression )
 
85
{
 
86
}
 
87
 
 
88
RangeNode::RangeNode( const QString &name, const FilterExpression &startExpression, const FilterExpression &stopExpression, const FilterExpression &stepExpression, QObject* parent )
 
89
  : Node( parent ),
 
90
    m_name( name ),
 
91
    m_startExpression( startExpression ),
 
92
    m_stopExpression( stopExpression ),
 
93
    m_stepExpression( stepExpression )
 
94
{
 
95
}
 
96
 
 
97
void RangeNode::setNodeList( NodeList list )
 
98
{
 
99
  m_list = list;
 
100
}
 
101
 
 
102
void RangeNode::render( OutputStream *stream, Context* c )
 
103
{
 
104
  int start;
 
105
  int stop;
 
106
  int step;
 
107
 
 
108
  start = m_startExpression.resolve( c ).toInt();
 
109
  stop = m_stopExpression.resolve( c ).toInt();
 
110
 
 
111
  if ( m_stepExpression.isValid() ) {
 
112
    step = m_stepExpression.resolve( c ).toInt();
 
113
  } else {
 
114
    step = 1;
 
115
  }
 
116
 
 
117
  const bool insertContext = !m_name.isEmpty();
 
118
 
 
119
  Q_ASSERT( start < stop );
 
120
 
 
121
  QString ret;
 
122
  for ( int i = start; i < stop; i += step ) {
 
123
    if ( insertContext ) {
 
124
      c->push();
 
125
      c->insert( m_name, i );
 
126
    }
 
127
    m_list.render( stream, c );
 
128
    if ( insertContext )
 
129
      c->pop();
 
130
  }
 
131
}
 
132