xml_parser_utils.hpp 4.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
// ----------------------------------------------------------------------------
// Copyright (C) 2002-2006 Marcin Kalicinski
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org
// ----------------------------------------------------------------------------
#ifndef BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
#define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED

#include <boost/property_tree/detail/ptree_utils.hpp>
#include <boost/property_tree/detail/xml_parser_error.hpp>
#include <boost/property_tree/detail/xml_parser_writer_settings.hpp>
#include <string>
#include <algorithm>
#include <locale>

namespace boost { namespace property_tree { namespace xml_parser
{

    template<class Ch>
    std::basic_string<Ch> condense(const std::basic_string<Ch> &s)
    {
        std::basic_string<Ch> r;
        std::locale loc;
        bool space = false;
        typename std::basic_string<Ch>::const_iterator end = s.end();
        for (typename std::basic_string<Ch>::const_iterator it = s.begin();
             it != end; ++it)
        {
            if (isspace(*it, loc) || *it == Ch('\n'))
            {
                if (!space)
                    r += Ch(' '), space = true;
            }
            else
                r += *it, space = false;
        }
        return r;
    }

    template<class Ch>
    std::basic_string<Ch> encode_char_entities(const std::basic_string<Ch> &s)
    {
        // Don't do anything for empty strings.
        if(s.empty()) return s;

        typedef typename std::basic_string<Ch> Str;
        Str r;
        // To properly round-trip spaces and not uglify the XML beyond
        // recognition, we have to encode them IF the text contains only spaces.
        Str sp(1, Ch(' '));
        if(s.find_first_not_of(sp) == Str::npos) {
            // The first will suffice.
            r = detail::widen<Ch>("&#32;");
            r += Str(s.size() - 1, Ch(' '));
        } else {
            typename Str::const_iterator end = s.end();
            for (typename Str::const_iterator it = s.begin(); it != end; ++it)
            {
                switch (*it)
                {
                    case Ch('<'): r += detail::widen<Ch>("&lt;"); break;
                    case Ch('>'): r += detail::widen<Ch>("&gt;"); break;
                    case Ch('&'): r += detail::widen<Ch>("&amp;"); break;
                    case Ch('"'): r += detail::widen<Ch>("&quot;"); break;
                    case Ch('\''): r += detail::widen<Ch>("&apos;"); break;
                    default: r += *it; break;
                }
            }
        }
        return r;
    }
    
    template<class Ch>
    std::basic_string<Ch> decode_char_entities(const std::basic_string<Ch> &s)
    {
        typedef typename std::basic_string<Ch> Str;
        Str r;
        typename Str::const_iterator end = s.end();
        for (typename Str::const_iterator it = s.begin(); it != end; ++it)
        {
            if (*it == Ch('&'))
            {
                typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';'));
                if (semicolon == end)
                    BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
                Str ent(it + 1, semicolon);
                if (ent == detail::widen<Ch>("lt")) r += Ch('<');
                else if (ent == detail::widen<Ch>("gt")) r += Ch('>');
                else if (ent == detail::widen<Ch>("amp")) r += Ch('&');
                else if (ent == detail::widen<Ch>("quot")) r += Ch('"');
                else if (ent == detail::widen<Ch>("apos")) r += Ch('\'');
                else
                    BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
                it = semicolon;
            }
            else
                r += *it;
        }
        return r;
    }
    
    template<class Ch>
    const std::basic_string<Ch> &xmldecl()
    {
        static std::basic_string<Ch> s = detail::widen<Ch>("<?xml>");
        return s;
    }

    template<class Ch>
    const std::basic_string<Ch> &xmlattr()
    {
        static std::basic_string<Ch> s = detail::widen<Ch>("<xmlattr>");
        return s;
    }

    template<class Ch>
    const std::basic_string<Ch> &xmlcomment()
    {
        static std::basic_string<Ch> s = detail::widen<Ch>("<xmlcomment>");
        return s;
    }

    template<class Ch>
    const std::basic_string<Ch> &xmltext()
    {
        static std::basic_string<Ch> s = detail::widen<Ch>("<xmltext>");
        return s;
    }

} } }

#endif