Ejemplo n.º 1
0
void xml_compiler::read_xml_(ptree_ptr& out,
                             const std::string& file_path,
                             const pqrs::string::replacement& replacement) const {
  try {
    out.reset(new boost::property_tree::ptree());

    std::string xml;
    if (replacement.empty()) {
      pqrs::string::string_from_file(xml, file_path.c_str());
    } else {
      pqrs::string::string_by_replacing_double_curly_braces_from_file(xml,
                                                                      replacement_warnings_,
                                                                      file_path.c_str(),
                                                                      replacement);
    }
    if (xml.empty()) {
      // Show warning message when we failed to read file.
      //
      // If private.xml includes files in network file system, it might fail to read.
      // For that case, we continue reading with ignoring missing files.

      xml += "<?xml version=\"1.0\"?>\n"
             "<root>"
             "  <item>"
             "    <name style=\"caution\">Caution:</name>"
             "    <appendix><![CDATA[";
      xml += file_path + " is not found.";
      xml += "    ]]></appendix>"
             "  </item>"
             "</root>";
    }

    std::stringstream istream(xml, std::stringstream::in);

    int flags = boost::property_tree::xml_parser::no_comments;

    boost::property_tree::read_xml(istream, *out, flags);

  } catch (std::exception& e) {
    std::string what = e.what();

    // Hack:
    // boost::property_tree::read_xml throw exception with filename.
    // But, when we call read_xml with stream, the filename becomes "unspecified file" as follow.
    //
    // <unspecified file>(4): expected element name
    //
    // So, we change "unspecified file" to file name by ourself.
    boost::replace_first(what, "<unspecified file>", std::string("<") + file_path + ">");

    error_information_.set(what);
  }
}
Ejemplo n.º 2
0
  void
  xml_compiler::read_xml_(ptree_ptr& out,
                          const std::string& file_path,
                          const pqrs::string::replacement& replacement) const
  {
    try {
      out.reset(new boost::property_tree::ptree());

      std::string xml;
      if (replacement.empty()) {
        pqrs::string::string_from_file(xml, file_path.c_str());
      } else {
        pqrs::string::string_by_replacing_double_curly_braces_from_file(xml,
                                                                        replacement_warnings_,
                                                                        file_path.c_str(),
                                                                        replacement);
      }
      if (xml.empty()) {
        error_information_.set(file_path + " is not found.");
        return;
      }

      std::stringstream istream(xml, std::stringstream::in);

      int flags = boost::property_tree::xml_parser::no_comments;

      boost::property_tree::read_xml(istream, *out, flags);

    } catch (std::exception& e) {
      std::string what = e.what();

      // Hack:
      // boost::property_tree::read_xml throw exception with filename.
      // But, when we call read_xml with stream, the filename becomes "unspecified file" as follow.
      //
      // <unspecified file>(4): expected element name
      //
      // So, we change "unspecified file" to file name by ourself.
      boost::replace_first(what, "<unspecified file>", std::string("<") + file_path + ">");

      error_information_.set(what);
    }
  }