File indexing completed on 2025-02-09 04:28:38
0001 /* 0002 This file is part of the KTextTemplate library 0003 0004 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com> 0005 0006 SPDX-License-Identifier: LGPL-2.1-or-later 0007 0008 */ 0009 0010 #ifndef KTEXTTEMPLATE_OUTPUTSTREAM_H 0011 #define KTEXTTEMPLATE_OUTPUTSTREAM_H 0012 0013 #include "ktexttemplate_export.h" 0014 0015 #include <QSharedPointer> 0016 #include <QTextStream> 0017 0018 namespace KTextTemplate 0019 { 0020 0021 class SafeString; 0022 0023 /// @headerfile outputstream.h <KTextTemplate/OutputStream> 0024 0025 /** 0026 @brief The **%OutputStream** class is used to render templates to a 0027 QTextStream 0028 0029 A **%OutputStream** instance may be passed to the render method of a Template 0030 to render the template to a stream. 0031 0032 @code 0033 QFile outputFile("./output"); 0034 outputFile.open(QFile::WriteOnly); 0035 QTextStream tstream( &outputFile ); 0036 0037 OutputStream os(&tstream); 0038 t->render( &os, &context ); 0039 @endcode 0040 0041 The **%OutputStream** is used to escape the content streamed to it. By 0042 default, the escaping is html escaping, converting "&" to "&" for example. 0043 If generating non-html output, the @ref escape method may be overriden to 0044 perform a different escaping, or non at all. 0045 0046 If overriding the @ref escape method, the @ref clone method must also be 0047 overriden to return an **%OutputStream** with the same escaping behaviour. 0048 0049 @code 0050 class NoEscapeStream : public KTextTemplate::OutputStream 0051 { 0052 public: 0053 // ... 0054 0055 QString escape( const QString &input ) const 0056 { 0057 return input; 0058 } 0059 0060 QSharedPointer<OutputStream> clone( QTextStream *stream ) const 0061 { 0062 return QSharedPointer<NoEscapeStream>::create( stream ); 0063 } 0064 }; 0065 @endcode 0066 0067 @author Stephen Kelly <steveire@gmail.com> 0068 */ 0069 class KTEXTTEMPLATE_EXPORT OutputStream 0070 { 0071 public: 0072 /** 0073 Creates a null **%OutputStream**. Content streamed to 0074 this **%OutputStream** is sent to <tt>/dev/null</tt> 0075 */ 0076 OutputStream(); 0077 0078 /** 0079 Creates an **%OutputStream** which will stream content to @p stream 0080 with appropriate escaping. 0081 */ 0082 explicit OutputStream(QTextStream *stream); 0083 0084 /** 0085 Destructor 0086 */ 0087 virtual ~OutputStream(); 0088 0089 /** 0090 Returns an escaped version of @p input. Does not write anything to the 0091 stream. 0092 */ 0093 virtual QString escape(const QString &input) const; 0094 0095 /** 0096 Returns an escaped version of @p input. Does not write anything to the 0097 stream. 0098 */ 0099 QString escape(const SafeString &input) const; 0100 0101 /** 0102 Returns a cloned **%OutputStream** with the same filtering behaviour. 0103 */ 0104 virtual QSharedPointer<OutputStream> clone(QTextStream *stream) const; 0105 0106 /** 0107 Returns @p after escaping it, unless @p input is "safe", in which case, 0108 @p input is returned unmodified. 0109 */ 0110 QString conditionalEscape(const KTextTemplate::SafeString &input) const; 0111 0112 /** 0113 Writes @p input to the stream after escaping it. 0114 */ 0115 OutputStream &operator<<(const QString &input); 0116 0117 /** 0118 Writes @p input to the stream after escaping it if necessary. 0119 */ 0120 OutputStream &operator<<(const SafeString &input); 0121 0122 /** 0123 Reads the content of @p stream and writes it unmodified to the result 0124 stream. 0125 */ 0126 OutputStream &operator<<(QTextStream *stream); 0127 0128 private: 0129 QTextStream *m_stream; 0130 Q_DISABLE_COPY(OutputStream) 0131 }; 0132 } 0133 0134 #endif