| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/libxml_utils.h" | 5 #include "chrome/common/libxml_utils.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/stringprintf.h" |
| 10 | 10 |
| 11 #include "libxml/xmlreader.h" | 11 #include "libxml/xmlreader.h" |
| 12 | 12 |
| 13 std::string XmlStringToStdString(const xmlChar* xmlstring) { | 13 std::string XmlStringToStdString(const xmlChar* xmlstring) { |
| 14 // xmlChar*s are UTF-8, so this cast is safe. | 14 // xmlChar*s are UTF-8, so this cast is safe. |
| 15 if (xmlstring) | 15 if (xmlstring) |
| 16 return std::string(reinterpret_cast<const char*>(xmlstring)); | 16 return std::string(reinterpret_cast<const char*>(xmlstring)); |
| 17 else | 17 else |
| 18 return ""; | 18 return ""; |
| 19 } | 19 } |
| 20 | 20 |
| 21 XmlReader::XmlReader() | 21 XmlReader::XmlReader() |
| 22 : reader_(NULL), | 22 : reader_(NULL), |
| 23 ALLOW_THIS_IN_INITIALIZER_LIST( | 23 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 24 error_func_(this, &XmlReader::GenericErrorCallback)) { | 24 error_func_(this, &XmlReader::GenericErrorCallback)) { |
| 25 } | 25 } |
| 26 | 26 |
| 27 XmlReader::~XmlReader() { | 27 XmlReader::~XmlReader() { |
| 28 if (reader_) | 28 if (reader_) |
| 29 xmlFreeTextReader(reader_); | 29 xmlFreeTextReader(reader_); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // static | 32 // static |
| 33 void XmlReader::GenericErrorCallback(void* context, const char* msg, ...) { | 33 void XmlReader::GenericErrorCallback(void* context, const char* msg, ...) { |
| 34 va_list args; | 34 va_list args; |
| 35 va_start(args, msg); | 35 va_start(args, msg); |
| 36 | 36 |
| 37 XmlReader* reader = static_cast<XmlReader*>(context); | 37 XmlReader* reader = static_cast<XmlReader*>(context); |
| 38 reader->errors_.append(StringPrintV(msg, args)); | 38 reader->errors_.append(base::StringPrintV(msg, args)); |
| 39 va_end(args); | 39 va_end(args); |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool XmlReader::Load(const std::string& input) { | 42 bool XmlReader::Load(const std::string& input) { |
| 43 const int kParseOptions = XML_PARSE_RECOVER | // recover on errors | 43 const int kParseOptions = XML_PARSE_RECOVER | // recover on errors |
| 44 XML_PARSE_NONET; // forbid network access | 44 XML_PARSE_NONET; // forbid network access |
| 45 // TODO(evanm): Verify it's OK to pass NULL for the URL and encoding. | 45 // TODO(evanm): Verify it's OK to pass NULL for the URL and encoding. |
| 46 // The libxml code allows for these, but it's unclear what effect is has. | 46 // The libxml code allows for these, but it's unclear what effect is has. |
| 47 reader_ = xmlReaderForMemory(input.data(), static_cast<int>(input.size()), | 47 reader_ = xmlReaderForMemory(input.data(), static_cast<int>(input.size()), |
| 48 NULL, NULL, kParseOptions); | 48 NULL, NULL, kParseOptions); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 writer_ = xmlNewTextWriterMemory(buffer_, 0); | 134 writer_ = xmlNewTextWriterMemory(buffer_, 0); |
| 135 xmlTextWriterSetIndent(writer_, 1); | 135 xmlTextWriterSetIndent(writer_, 1); |
| 136 xmlTextWriterStartDocument(writer_, NULL, NULL, NULL); | 136 xmlTextWriterStartDocument(writer_, NULL, NULL, NULL); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void XmlWriter::StopWriting() { | 139 void XmlWriter::StopWriting() { |
| 140 xmlTextWriterEndDocument(writer_); | 140 xmlTextWriterEndDocument(writer_); |
| 141 xmlFreeTextWriter(writer_); | 141 xmlFreeTextWriter(writer_); |
| 142 writer_ = NULL; | 142 writer_ = NULL; |
| 143 } | 143 } |
| OLD | NEW |