libzypp  13.10.6
XmlEscape.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <string>
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  {
39  {
40  std::string escape(const std::string &istr) const
41  {
42  typedef unsigned char uchar;
43 
44  std::string str( istr );
45  for ( size_t i = 0; i < str.size(); ++i )
46  {
47  switch (str[i])
48  {
49  case '<': str.replace(i, 1, "&lt;"); i += 3; break;
50  case '>': str.replace(i, 1, "&gt;"); i += 3; break;
51  case '&': str.replace(i, 1, "&amp;"); i += 4; break;
52  case '"': str.replace(i, 1, "&quot;"); i += 5; break;
53  case '\'': str.replace(i, 1, "&apos;"); i += 5; break;
54 
55  // control chars we allow:
56  case '\n':
57  case '\r':
58  case '\t':
59  break;
60 
61  default:
62  if ( uchar(str[i]) < 32u )
63  str[i] = '?'; // filter problematic control chars (XML1.0)
64  break;
65  }
66  }
67  return str;
68  }
69 
70  std::string unescape(const std::string &istr) const
71  {
72  size_t i;
73  std::string str = istr;
74  i = str.find_first_of("&");
75  while (i != std::string::npos)
76  {
77  if (str[i] == '&')
78  {
79  if (!str.compare(i + 1, 3, "lt;"))
80  str.replace(i, 4, 1, '<');
81  else if (!str.compare(i + 1, 3, "gt;"))
82  str.replace(i, 4, 1, '>');
83  else if (!str.compare(i + 1, 4, "amp;"))
84  str.replace(i, 5, 1, '&');
85  else if (!str.compare(i + 1, 5, "apos;"))
86  str.replace(i, 6, 1, '\'');
87  else if (!str.compare(i + 1, 5, "quot;"))
88  str.replace(i, 6, 1, '"');
89  }
90  i = str.find_first_of("&", i + 1);
91  }
92  return str;
93  }
94  };
95  } // namespace parser
97 } // namespace iobind
99 
101 namespace zypp
102 {
104  namespace xml
105  {
106  std::string escape( const std::string & in_r )
107  { return iobind::parser::xml_escape_parser().escape( in_r ); }
108 
109  std::string unescape( const std::string & in_r )
110  { return iobind::parser::xml_escape_parser().unescape( in_r ); }
111 
112  } // namespace xml
114 } // namespace zypp
#define ZYPP_LOCAL
Definition: APIConfig.h:46
std::string unescape(const std::string &in_r)
Unescape xml special charaters (&amp; -&gt; &amp;; from IoBind library)
Definition: XmlEscape.cc:109
std::string escape(const std::string &istr) const
Definition: XmlEscape.cc:40
std::string escape(const std::string &in_r)
Escape xml special charaters (&amp; -&gt; &amp;; from IoBind library).
Definition: XmlEscape.cc:106
std::string unescape(const std::string &istr) const
Definition: XmlEscape.cc:70