LCOV - code coverage report
Current view: top level - templates/scriptabletags - scriptabletags.cpp (source / functions) Hit Total Coverage
Test: test_all.info Lines: 79 89 88.8 %
Date: 2011-06-24 Functions: 10 13 76.9 %
Branches: 176 400 44.0 %

           Branch data     Line data    Source code
       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.1 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                 :            :   Lesser 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 "scriptabletags.h"
      22                 :            : 
      23                 :            : #include <QtPlugin>
      24                 :            : #include <QtCore/QFile>
      25                 :            : 
      26                 :            : #include <QtScript/QScriptEngine>
      27                 :            : 
      28                 :            : #include "nodebuiltins_p.h"
      29                 :            : 
      30                 :            : #include "scriptablenode.h"
      31                 :            : #include "scriptablevariable.h"
      32                 :            : #include "scriptablefilterexpression.h"
      33                 :            : #include "scriptablefilter.h"
      34                 :            : #include "scriptabletemplate.h"
      35                 :            : #include "scriptablesafestring.h"
      36                 :            : #include "exception.h"
      37                 :            : 
      38                 :            : #include "token.h"
      39                 :            : 
      40         [ +  - ]:          2 : Q_DECLARE_METATYPE( Token )
      41                 :            : 
      42                 :            : using namespace Grantlee;
      43                 :            : 
      44                 :          5 : QScriptValue tokenToScriptValue( QScriptEngine *engine, const Token &t )
      45                 :            : {
      46                 :          5 :   QScriptValue obj = engine->newObject();
      47 [ +  - ][ +  - ]:          5 :   obj.setProperty( QLatin1String( "tokenType" ), t.tokenType );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      48 [ +  - ][ +  - ]:          5 :   obj.setProperty( QLatin1String( "content" ), t.content );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      49                 :            :   return obj;
      50                 :            : }
      51                 :            : 
      52                 :          0 : void tokenFromScriptValue( const QScriptValue &obj, Token &t )
      53                 :            : {
      54 [ #  # ][ #  # ]:          0 :   t.tokenType = obj.property( QLatin1String( "tokenType" ) ).toInt32();
                 [ #  # ]
      55 [ #  # ][ #  # ]:          0 :   t.content = obj.property( QLatin1String( "content" ) ).toString();
         [ #  # ][ #  # ]
                 [ #  # ]
      56                 :          0 : }
      57                 :            : 
      58                 :          2 : ScriptableTagLibrary::ScriptableTagLibrary( QObject *parent )
      59 [ +  - ][ +  - ]:          2 :     : QObject( parent ), m_scriptEngine( 0 )
         [ +  - ][ +  - ]
      60                 :            : {
      61 [ +  - ][ +  - ]:          2 :   m_scriptEngine = new QScriptEngine( this );
      62                 :            : 
      63 [ +  - ][ +  - ]:          2 :   qScriptRegisterMetaType( m_scriptEngine, tokenToScriptValue, tokenFromScriptValue );
                 [ +  - ]
      64 [ +  - ][ +  - ]:          2 :   qScriptRegisterMetaType( m_scriptEngine, nodeToScriptValue, nodeFromScriptValue );
                 [ +  - ]
      65                 :            : //   qScriptRegisterMetaType(m_scriptEngine.data(), tokenToScriptValue, tokenFromScriptValue);
      66                 :            : //   qScriptRegisterMetaType(m_scriptEngine.data(), nodeToScriptValue, nodeFromScriptValue);
      67                 :            : 
      68                 :            :   // Make Node new-able
      69 [ +  - ][ +  - ]:          4 :   QScriptValue nodeCtor = m_scriptEngine->newFunction( ScriptableNodeConstructor );
      70 [ +  - ][ +  - ]:          4 :   QScriptValue nodeMetaObject = m_scriptEngine->newQMetaObject( &ScriptableNode::staticMetaObject, nodeCtor );
      71 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "Node" ), nodeMetaObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      72                 :            : 
      73                 :            :   // Make Variable new-able
      74 [ +  - ][ +  - ]:          4 :   QScriptValue variableCtor = m_scriptEngine->newFunction( ScriptableVariableConstructor );
      75 [ +  - ][ +  - ]:          4 :   QScriptValue variableMetaObject = m_scriptEngine->newQMetaObject( &VariableNode::staticMetaObject, variableCtor );
      76 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "Variable" ), variableMetaObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      77                 :            : 
      78                 :            :   // Make FilterExpression new-able
      79 [ +  - ][ +  - ]:          4 :   QScriptValue filterExpressionCtor = m_scriptEngine->newFunction( ScriptableFilterExpressionConstructor );
      80 [ +  - ][ +  - ]:          4 :   QScriptValue filterExpressionMetaObject = m_scriptEngine->newQMetaObject( &ScriptableFilterExpression::staticMetaObject, filterExpressionCtor );
      81 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "FilterExpression" ), filterExpressionMetaObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      82                 :            : 
      83                 :            :   // Make Template new-able
      84 [ +  - ][ +  - ]:          4 :   QScriptValue templateCtor = m_scriptEngine->newFunction( ScriptableTemplateConstructor );
      85 [ +  - ][ +  - ]:          4 :   QScriptValue templateMetaObject = m_scriptEngine->newQMetaObject( &ScriptableTemplate::staticMetaObject, templateCtor );
      86 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "Template" ), templateMetaObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      87                 :            : 
      88                 :            :   // Create a global Library object
      89 [ +  - ][ +  - ]:          4 :   QScriptValue libraryObject = m_scriptEngine->newQObject( this );
                 [ +  - ]
      90 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "Library" ), libraryObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      91                 :            : 
      92                 :            :   // Create a global AbstractNodeFactory object to make smartSplit available.
      93 [ +  - ][ +  - ]:          2 :   ScriptableNodeFactory *nodeFactory = new ScriptableNodeFactory( this );
      94 [ +  - ][ +  - ]:          4 :   QScriptValue nodeFactoryObject = m_scriptEngine->newQObject( nodeFactory );
                 [ +  - ]
      95 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "AbstractNodeFactory" ), nodeFactoryObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      96                 :            : 
      97                 :            :   // Make mark_safe a globally available object.
      98 [ +  - ][ +  - ]:          4 :   QScriptValue markSafeFunctionObject = m_scriptEngine->newFunction( markSafeFunction );
      99 [ +  - ][ +  - ]:          2 :   m_scriptEngine->globalObject().setProperty( QLatin1String( "mark_safe" ), markSafeFunctionObject );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     100                 :            : 
     101                 :          2 : }
     102                 :            : 
     103                 :         28 : bool ScriptableTagLibrary::evaluateScript( const QString &name )
     104                 :            : {
     105                 :         56 :   QFile scriptFile( name );
     106                 :            : 
     107 [ +  - ][ +  - ]:         28 :   if ( !scriptFile.exists() || !scriptFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
         [ +  - ][ +  - ]
         [ -  + ][ -  + ]
     108                 :          0 :     return false;
     109                 :            :   }
     110                 :            : 
     111 [ +  - ][ +  - ]:         56 :   QTextStream fstream( &scriptFile );
     112         [ +  - ]:         28 :   fstream.setCodec( "UTF-8" );
     113 [ +  - ][ +  - ]:         56 :   const QString fileContent = fstream.readAll();
     114                 :            : 
     115         [ +  - ]:         28 :   scriptFile.close();
     116                 :            : 
     117 [ +  - ][ +  - ]:         28 :   m_scriptEngine->evaluate( fileContent );
         [ +  - ][ +  - ]
     118                 :            : 
     119 [ +  - ][ -  + ]:         28 :   if ( m_scriptEngine->hasUncaughtException() ) {
     120 [ #  # ][ #  # ]:          0 :     throw Grantlee::Exception( TagSyntaxError, m_scriptEngine->uncaughtExceptionBacktrace().join( QChar::fromLatin1( ' ' ) ) );
         [ #  # ][ #  # ]
                 [ #  # ]
     121                 :            :   }
     122                 :         28 :   return true;
     123                 :            : }
     124                 :            : 
     125                 :         14 : QHash<QString, AbstractNodeFactory*> ScriptableTagLibrary::nodeFactories( const QString &name )
     126                 :            : {
     127                 :         14 :   m_factoryNames.clear();
     128                 :         14 :   m_nodeFactories.clear();
     129                 :         28 :   QHash<QString, AbstractNodeFactory*> h;
     130                 :            : 
     131 [ +  - ][ -  + ]:         14 :   if ( !evaluateScript( name ) ) {
     132         [ #  # ]:          0 :     return h;
     133                 :            :   }
     134                 :            : 
     135         [ +  - ]:         14 :   return getFactories();
     136                 :            : }
     137                 :            : 
     138                 :         14 : QHash< QString, Filter* > ScriptableTagLibrary::filters( const QString& name )
     139                 :            : {
     140                 :         14 :   m_filterNames.clear();
     141                 :         14 :   m_filters.clear();
     142                 :         28 :   QHash<QString, Filter*> filters;
     143                 :            : 
     144 [ +  - ][ -  + ]:         14 :   if ( !evaluateScript( name ) ) {
     145         [ #  # ]:          0 :     return filters;
     146                 :            :   }
     147                 :            : 
     148         [ +  - ]:         14 :   return getFilters();
     149                 :            : }
     150                 :            : 
     151                 :         14 : QHash<QString, AbstractNodeFactory*> ScriptableTagLibrary::getFactories()
     152                 :            : {
     153                 :         14 :   QHash<QString, AbstractNodeFactory*> factories;
     154 [ +  - ][ +  - ]:         28 :   QHashIterator<QString, QString> it( m_factoryNames );
     155 [ +  - ][ +  + ]:         39 :   while ( it.hasNext() ) {
     156         [ +  - ]:         25 :     it.next();
     157 [ +  - ][ +  - ]:         50 :     QString factoryName = it.value();
                 [ +  - ]
     158 [ +  - ][ +  - ]:         50 :     QString tagName = it.key();
                 [ +  - ]
     159                 :            : 
     160 [ +  - ][ +  - ]:         50 :     QScriptValue factoryObject = m_scriptEngine->globalObject().property( factoryName );
         [ +  - ][ +  - ]
                 [ +  - ]
     161                 :            : 
     162 [ +  - ][ +  - ]:         25 :     ScriptableNodeFactory *snf = new ScriptableNodeFactory();
     163         [ +  - ]:         25 :     snf->setScriptEngine( m_scriptEngine );
     164 [ +  - ][ +  - ]:         25 :     snf->setFactory( factoryObject );
                 [ +  - ]
     165                 :            : 
     166         [ +  - ]:         25 :     factories.insert( tagName, snf );
     167                 :            :   }
     168                 :            : 
     169                 :            :   return factories;
     170                 :            : }
     171                 :            : 
     172                 :         14 : QHash<QString, Filter*> ScriptableTagLibrary::getFilters()
     173                 :            : {
     174                 :         14 :   QHash<QString, Filter*> filters;
     175                 :            : 
     176 [ +  - ][ +  - ]:         28 :   QListIterator<QString> it( m_filterNames );
     177 [ +  - ][ +  + ]:         47 :   while ( it.hasNext() ) {
     178 [ +  - ][ +  - ]:         66 :     QScriptValue filterObject = m_scriptEngine->globalObject().property( it.next() );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     179 [ +  - ][ +  - ]:         66 :     QString filterName = filterObject.property( QLatin1String( "filterName" ) ).toString();
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     180 [ +  - ][ +  - ]:         33 :     ScriptableFilter *filter = new ScriptableFilter( filterObject, m_scriptEngine );
         [ +  - ][ +  - ]
     181         [ +  - ]:         33 :     filters.insert( filterName, filter );
     182                 :            :   }
     183 [ +  - ][ -  + ]:         14 :   if ( m_scriptEngine->hasUncaughtException() ) {
     184 [ #  # ][ #  # ]:          0 :     throw Grantlee::Exception( TagSyntaxError, m_scriptEngine->uncaughtExceptionBacktrace().join( QChar::fromLatin1( ' ' ) ) );
         [ #  # ][ #  # ]
                 [ #  # ]
     185                 :            :   }
     186                 :            : 
     187                 :            :   return filters;
     188                 :            : }
     189                 :            : 
     190                 :         50 : void ScriptableTagLibrary::addFactory( const QString &factoryName, const QString &tagName )
     191                 :            : {
     192                 :         50 :   m_factoryNames.insert( tagName, factoryName );
     193                 :         50 : }
     194                 :            : 
     195                 :         66 : void ScriptableTagLibrary::addFilter( const QString &filterName )
     196                 :            : {
     197                 :         66 :   m_filterNames << filterName;
     198                 :         66 : }
     199                 :            : 
     200 [ #  # ][ #  # ]:          0 : Q_EXPORT_PLUGIN2( grantlee_scriptabletags_library, ScriptableTagLibrary )
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     201                 :            : 
     202                 :            : 
     203                 :            : #include "scriptabletags.moc"

Generated by: LCOV version 1.9