OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_COMMON_LIBXML_UTILS_H__ | 5 #ifndef THIRD_PARTY_LIBXML_CHROMIUM_LIBXML_UTILS_H_ |
6 #define CHROME_COMMON_LIBXML_UTILS_H__ | 6 #define THIRD_PARTY_LIBXML_CHROMIUM_LIBXML_UTILS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "libxml/xmlreader.h" | 11 #include "libxml/xmlreader.h" |
12 #include "libxml/xmlwriter.h" | 12 #include "libxml/xmlwriter.h" |
13 | 13 |
14 class FilePath; | |
15 | |
16 // Converts a libxml xmlChar* into a UTF-8 std::string. | 14 // Converts a libxml xmlChar* into a UTF-8 std::string. |
17 // NULL inputs produce an empty string. | 15 // NULL inputs produce an empty string. |
18 std::string XmlStringToStdString(const xmlChar* xmlstring); | 16 std::string XmlStringToStdString(const xmlChar* xmlstring); |
19 | 17 |
20 // libxml uses a global error function pointer for reporting errors. | 18 // libxml uses a global error function pointer for reporting errors. |
21 // A ScopedXmlErrorFunc object lets you change the global error pointer | 19 // A ScopedXmlErrorFunc object lets you change the global error pointer |
22 // for the duration of the object's lifetime. | 20 // for the duration of the object's lifetime. |
23 class ScopedXmlErrorFunc { | 21 class ScopedXmlErrorFunc { |
24 public: | 22 public: |
25 ScopedXmlErrorFunc(void* context, xmlGenericErrorFunc func) { | 23 ScopedXmlErrorFunc(void* context, xmlGenericErrorFunc func) { |
(...skipping 16 matching lines...) Expand all Loading... |
42 public: | 40 public: |
43 XmlReader(); | 41 XmlReader(); |
44 ~XmlReader(); | 42 ~XmlReader(); |
45 | 43 |
46 // Load a document into the reader from memory. |input| must be UTF-8 and | 44 // Load a document into the reader from memory. |input| must be UTF-8 and |
47 // exist for the lifetime of this object. Returns false on error. | 45 // exist for the lifetime of this object. Returns false on error. |
48 // TODO(evanm): handle encodings other than UTF-8? | 46 // TODO(evanm): handle encodings other than UTF-8? |
49 bool Load(const std::string& input); | 47 bool Load(const std::string& input); |
50 | 48 |
51 // Load a document into the reader from a file. Returns false on error. | 49 // Load a document into the reader from a file. Returns false on error. |
52 bool LoadFile(const FilePath& file_path); | 50 bool LoadFile(const std::string& file_path); |
53 | 51 |
54 // Wrappers around libxml functions ----------------------------------------- | 52 // Wrappers around libxml functions ----------------------------------------- |
55 | 53 |
56 // Read() advances to the next node. Returns false on EOF or error. | 54 // Read() advances to the next node. Returns false on EOF or error. |
57 bool Read() { return xmlTextReaderRead(reader_) == 1; } | 55 bool Read() { return xmlTextReaderRead(reader_) == 1; } |
58 | 56 |
59 // Next(), when pointing at an opening tag, advances to the node after | 57 // Next(), when pointing at an opening tag, advances to the node after |
60 // the matching closing tag. Returns false on EOF or error. | 58 // the matching closing tag. Returns false on EOF or error. |
61 bool Next() { return xmlTextReaderNext(reader_) == 1; } | 59 bool Next() { return xmlTextReaderNext(reader_) == 1; } |
62 | 60 |
(...skipping 20 matching lines...) Expand all Loading... |
83 // With the reader currently at (1), this returns the text of (2), | 81 // With the reader currently at (1), this returns the text of (2), |
84 // and advances past (3). | 82 // and advances past (3). |
85 // Returns false on error. | 83 // Returns false on error. |
86 bool ReadElementContent(std::string* content); | 84 bool ReadElementContent(std::string* content); |
87 | 85 |
88 // Skip to the next opening tag, returning false if we reach a closing | 86 // Skip to the next opening tag, returning false if we reach a closing |
89 // tag or EOF first. | 87 // tag or EOF first. |
90 // If currently on an opening tag, doesn't advance at all. | 88 // If currently on an opening tag, doesn't advance at all. |
91 bool SkipToElement(); | 89 bool SkipToElement(); |
92 | 90 |
93 // Returns the errors reported by libxml, if any. | |
94 // (libxml normally just dumps these errors to stderr.) | |
95 const std::string& errors() const { return errors_; } | |
96 | |
97 private: | 91 private: |
98 // A callback for libxml to report errors. | |
99 static void GenericErrorCallback(void* context, const char* msg, ...); | |
100 | |
101 // Returns the libxml node type of the current node. | 92 // Returns the libxml node type of the current node. |
102 int NodeType() { return xmlTextReaderNodeType(reader_); } | 93 int NodeType() { return xmlTextReaderNodeType(reader_); } |
103 | 94 |
104 // The underlying libxml xmlTextReader. | 95 // The underlying libxml xmlTextReader. |
105 xmlTextReaderPtr reader_; | 96 xmlTextReaderPtr reader_; |
106 | |
107 // error_func_ is used to reassign libxml's global error function | |
108 // to report errors into |errors_| for the lifetime of this object. | |
109 ScopedXmlErrorFunc error_func_; | |
110 std::string errors_; | |
111 }; | 97 }; |
112 | 98 |
113 // XmlWriter is a wrapper class around libxml's xmlWriter, | 99 // XmlWriter is a wrapper class around libxml's xmlWriter, |
114 // providing a simplified C++ API. | 100 // providing a simplified C++ API. |
115 // StartWriting must be called before other methods, and StopWriting | 101 // StartWriting must be called before other methods, and StopWriting |
116 // must be called before GetWrittenString() will return results. | 102 // must be called before GetWrittenString() will return results. |
117 class XmlWriter { | 103 class XmlWriter { |
118 public: | 104 public: |
119 XmlWriter(); | 105 XmlWriter(); |
120 ~XmlWriter(); | 106 ~XmlWriter(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 161 } |
176 | 162 |
177 private: | 163 private: |
178 // The underlying libxml xmlTextWriter. | 164 // The underlying libxml xmlTextWriter. |
179 xmlTextWriterPtr writer_; | 165 xmlTextWriterPtr writer_; |
180 | 166 |
181 // Stores the output. | 167 // Stores the output. |
182 xmlBufferPtr buffer_; | 168 xmlBufferPtr buffer_; |
183 }; | 169 }; |
184 | 170 |
185 #endif // CHROME_COMMON_LIBXML_UTILS_H__ | 171 #endif // THIRD_PARTY_LIBXML_CHROMIUM_INCLUDE_LIBXML_LIBXML_UTILS_H_ |
OLD | NEW |