libzypp 17.37.10

XmlEscape.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include <string>
14#include <zypp-core/parser/xml/XmlEscape.h>
15
16/*
17 IoBind Library License:
18--------------------------
19
20 The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux
21
22 This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
23
24 Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
25
26 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
27
28 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
29
30 3. This notice may not be removed or altered from any source distribution
31*/
33 namespace iobind
34{
36 namespace parser
37 {
38 struct ZYPP_LOCAL xml_escape_parser
39 {
40 std::string unescape(const std::string &istr) const
41 {
42 size_t i = 0;
43 std::string str = istr;
44 i = str.find_first_of('&');
45 while (i != std::string::npos)
46 {
47 if (str[i] == '&')
48 {
49 if (!str.compare(i + 1, 3, "lt;"))
50 str.replace(i, 4, 1, '<');
51 else if (!str.compare(i + 1, 3, "gt;"))
52 str.replace(i, 4, 1, '>');
53 else if (!str.compare(i + 1, 4, "amp;"))
54 str.replace(i, 5, 1, '&');
55 else if (!str.compare(i + 1, 5, "apos;"))
56 str.replace(i, 6, 1, '\'');
57 else if (!str.compare(i + 1, 5, "quot;"))
58 str.replace(i, 6, 1, '"');
59 }
60 i = str.find_first_of('&', i + 1);
61 }
62 return str;
63 }
64 };
65 } // namespace parser
66
67} // namespace iobind
68
69
71 namespace zypp
72{
74 namespace xml
75 {
77 namespace detail
78 {
79 std::ostream & EscapedString::dumpOn( std::ostream & str ) const
80 {
81 typedef unsigned char uchar;
82 for ( char ch : _in )
83 {
84 switch ( ch )
85 {
86 case '<': str << "&lt;"; break;
87 case '>': str << "&gt;"; break;
88 case '&': str << "&amp;"; break;
89 case '"': str << "&quot;"; break;
90 case '\'': str << "&apos;"; break;
91
92 // control chars we allow:
93 case '\n':
94 case '\r':
95 case '\t':
96 str << ch;
97 break;
98
99 default:
100 if ( uchar(ch) < 32u )
101 str << '?'; // filter problematic control chars (XML1.0)
102 else
103 str << ch;
104 break;
105 }
106 }
107 return str;
108 }
109
110 } // detail
111
112
113 std::string unescape( const std::string & in_r )
114 { return iobind::parser::xml_escape_parser().unescape( in_r ); }
115
116 } // namespace xml
118} // namespace zypp
iobind::parser
Definition XmlEscape.cc:37
iobind
Definition XmlEscape.cc:34
str
String related utilities and Regular expression matching.
zypp::xml::detail
Definition XmlEscape.cc:78
zypp::xml
Definition libxmlfwd.cc:22
zypp::xml::unescape
std::string unescape(const std::string &in_r)
Unescape xml special charaters (& -> &; from IoBind library)
Definition XmlEscape.cc:113
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
iobind::parser::xml_escape_parser
Definition XmlEscape.cc:39
iobind::parser::xml_escape_parser::unescape
std::string unescape(const std::string &istr) const
Definition XmlEscape.cc:40
zypp::xml::detail::EscapedString::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Definition XmlEscape.cc:79
zypp::xml::detail::EscapedString::_in
const std::string & _in
Definition XmlEscape.h:37
ZYPP_LOCAL
#define ZYPP_LOCAL
Definition Globals.h:71
XmlEscape.h