/src/wt/src/web/EscapeOStream.h
Line | Count | Source |
1 | | // This may look like C code, but it's really -*- C++ -*- |
2 | | /* |
3 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
4 | | * |
5 | | * See the LICENSE file for terms of use. |
6 | | */ |
7 | | #ifndef WT_ESCAPE_OSTREAM_H_ |
8 | | #define WT_ESCAPE_OSTREAM_H_ |
9 | | |
10 | | #include <Wt/WStringStream.h> |
11 | | |
12 | | #ifndef WT_DBO_ESCAPEOSTREAM |
13 | | #define WT_ESCAPEOSTREAM_API WT_API |
14 | | #else // WT_DBO_ESCAPEOSTREAM |
15 | | #define WT_ESCAPEOSTREAM_API WTDBO_API |
16 | | #endif // WT_DBO_ESCAPEOSTREAM |
17 | | |
18 | | namespace Wt { |
19 | | |
20 | | #ifdef WT_DBO_ESCAPEOSTREAM |
21 | | namespace Dbo { |
22 | | #endif |
23 | | |
24 | | class WT_ESCAPEOSTREAM_API EscapeOStream |
25 | | { |
26 | | public: |
27 | | enum RuleSet { Empty = 0, HtmlAttribute = 1, |
28 | | JsStringLiteralSQuote = 2, JsStringLiteralDQuote = 3, |
29 | | Plain = 4, PlainTextNewLines = 5 }; |
30 | | |
31 | | EscapeOStream(); |
32 | | EscapeOStream(std::ostream& sink); |
33 | | EscapeOStream(WStringStream& sink); |
34 | | EscapeOStream(EscapeOStream& other); |
35 | | |
36 | | void pushEscape(RuleSet rules); |
37 | | void popEscape(); |
38 | | |
39 | | #ifdef WT_TARGET_JAVA |
40 | | EscapeOStream& push(); |
41 | | #endif // WT_TARGET_JAVA |
42 | | |
43 | | void append(const std::string& s, const EscapeOStream& rules); |
44 | | void append(const char *s, std::size_t len); |
45 | | |
46 | | EscapeOStream& operator<< (char); |
47 | | EscapeOStream& operator<< (const char *s) |
48 | 0 | { |
49 | 0 | if (c_special_ == 0) |
50 | 0 | stream_ << s; |
51 | 0 | else |
52 | 0 | put(s, *this); |
53 | |
|
54 | 0 | return *this; |
55 | 0 | } |
56 | | |
57 | | EscapeOStream& operator<< (const std::string& s); |
58 | | EscapeOStream& operator<< (int); |
59 | | EscapeOStream& operator<< (unsigned int); |
60 | | EscapeOStream& operator<< (long long); |
61 | | EscapeOStream& operator<< (bool); |
62 | | EscapeOStream& operator<< (const EscapeOStream& other); |
63 | | |
64 | | const char *c_str(); // for default constructor, can return 0 |
65 | | std::string str() const; // for default constructor |
66 | | |
67 | | bool empty() const; |
68 | | void clear(); |
69 | | |
70 | | private: |
71 | | WStringStream own_stream_; |
72 | | WStringStream& stream_; |
73 | | |
74 | | struct Entry { |
75 | | char c; |
76 | | std::string s; |
77 | | }; |
78 | | std::vector<Entry> mixed_; |
79 | | std::string special_; |
80 | | const char *c_special_; |
81 | | |
82 | | void mixRules(); |
83 | | void put(const char *s, const EscapeOStream& rules); |
84 | | |
85 | | void sAppend(char c); |
86 | | void sAppend(const char *s, int length); |
87 | | void sAppend(const std::string& s); |
88 | | |
89 | | std::vector<RuleSet> ruleSets_; |
90 | | |
91 | | static const std::vector<Entry> standardSets_[6]; |
92 | | static const std::string standardSetsSpecial_[6]; |
93 | | |
94 | | static const Entry htmlAttributeEntries_[3]; |
95 | | static const Entry jsStringLiteralSQuoteEntries_[5]; |
96 | | static const Entry jsStringLiteralDQuoteEntries_[5]; |
97 | | static const Entry plainTextEntries_[3]; |
98 | | static const Entry plainTextNewLinesEntries_[4]; |
99 | | }; |
100 | | |
101 | | #ifdef WT_DBO_ESCAPEOSTREAM |
102 | | } // namespace Dbo |
103 | | #endif |
104 | | |
105 | | } // namespace Wt |
106 | | |
107 | | #endif // ESCAPE_OSTREAM_H_ |