File indexing completed on 2025-02-09 04:28:37

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 // krazy:excludeall=dpointer
0011 
0012 #ifndef KTEXTTEMPLATE_FILTER_H
0013 #define KTEXTTEMPLATE_FILTER_H
0014 
0015 #include "ktexttemplate_export.h"
0016 #include "outputstream.h"
0017 #include "safestring.h"
0018 
0019 #include <QSharedPointer>
0020 #include <QStringList>
0021 #include <QVariant>
0022 
0023 namespace KTextTemplate
0024 {
0025 
0026 /// @headerfile filter.h <KTextTemplate/Filter>
0027 
0028 /**
0029   @brief Base class for all filters.
0030 
0031   The **%Filter** class can be implemented in plugin libraries to make
0032   additional functionality available to templates.
0033 
0034   Developers are required only to implement the @ref doFilter method and
0035   integrate the filter as part of a custom plugin, but will never create or
0036   access filters directly in application code.
0037 
0038   The FilterExpression class is the access interface to a chain of **%Filter**
0039   objects.
0040 
0041   The @ref escape and @ref conditionalEscape methods are available for escaping
0042   data where needed.
0043 
0044   @see @ref filters
0045 
0046   @author Stephen Kelly <steveire@gmail.com>
0047 */
0048 class KTEXTTEMPLATE_EXPORT Filter
0049 {
0050 public:
0051     /**
0052       Destructor.
0053     */
0054     virtual ~Filter();
0055 
0056 #ifndef K_DOXYGEN
0057     /**
0058       FilterExpression makes it possible to access stream methods like escape
0059       while resolving.
0060     */
0061     void setStream(OutputStream *stream);
0062 #endif
0063 
0064     /**
0065       Escapes and returns @p input. The OutputStream::escape method is used to
0066       escape @p input.
0067     */
0068     SafeString escape(const QString &input) const;
0069 
0070     /**
0071       Escapes and returns @p input. The OutputStream::escape method is used to
0072       escape @p input.
0073     */
0074     SafeString escape(const SafeString &input) const;
0075 
0076     /**
0077       Escapes @p input if not already safe from further escaping and returns it.
0078       The OutputStream::escape method is used to escape @p input.
0079     */
0080     SafeString conditionalEscape(const SafeString &input) const;
0081 
0082     /**
0083       Reimplement to filter @p input given @p argument.
0084 
0085       @p autoescape determines whether the autoescape feature is currently on or
0086       off. Most filters will not use this.
0087 
0088       @see @ref autoescaping
0089     */
0090     virtual QVariant doFilter(const QVariant &input, const QVariant &argument = {}, bool autoescape = {}) const = 0;
0091 
0092     /**
0093       Reimplement to return whether this filter is safe.
0094     */
0095     virtual bool isSafe() const;
0096 
0097 private:
0098 #ifndef K_DOXYGEN
0099     OutputStream *m_stream;
0100 #endif
0101 };
0102 }
0103 
0104 #endif