Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: chrome/browser/sync/notifier/communicator/xml_parse_helpers.cc

Issue 194065: Initial commit of sync engine code to browser/sync.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fixes to gtest include path, reverted syncapi. Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/notifier/communicator/xml_parse_helpers.h"
6 #include "chrome/browser/sync/notifier/communicator/xml_parse_helpers-inl.h"
7
8 #include <string>
9
10 #include "chrome/browser/sync/notifier/base/string.h"
11 #include "talk/base/basicdefs.h"
12 #include "talk/base/stream.h"
13 #include "talk/xmllite/xmlbuilder.h"
14 #include "talk/xmllite/xmlelement.h"
15 #include "talk/xmllite/xmlparser.h"
16 #include "talk/xmllite/xmlprinter.h"
17 #include "talk/xmpp/jid.h"
18
19 namespace notifier {
20
21 buzz::XmlElement* ReadXmlFromStream(talk_base::StreamInterface* stream) {
22 buzz::XmlBuilder builder;
23 buzz::XmlParser parser(&builder);
24
25 const int kBufferSize = 4 * 1024;
26 char buf[kBufferSize];
27
28 talk_base::StreamResult result = talk_base::SR_SUCCESS;
29 while(true) {
30 size_t read = 0;
31
32 // Read a chunk
33 result = stream->Read(buf, kBufferSize, &read, NULL);
34 if (result != talk_base::SR_SUCCESS)
35 break;
36
37 // Pass it to the parser
38 parser.Parse(buf, read, false);
39 }
40
41 if (result == talk_base::SR_EOS) {
42 parser.Parse(NULL, 0, true);
43 return builder.CreateElement();
44 }
45
46 return NULL;
47 }
48
49 bool ParseInt64Attr(const buzz::XmlElement* element,
50 const buzz::QName& attribute, int64* result) {
51 if (!element->HasAttr(attribute))
52 return false;
53 std::string text = element->Attr(attribute);
54 char* error = NULL;
55 #ifdef POSIX
56 *result = atoll(text.c_str());
57 #else
58 *result = _strtoi64(text.c_str(), &error, 10);
59 #endif
60 return text.c_str() != error;
61 }
62
63 bool ParseIntAttr(const buzz::XmlElement* element, const buzz::QName& attribute,
64 int* result) {
65 if (!element->HasAttr(attribute))
66 return false;
67 std::string text = element->Attr(attribute);
68 char* error = NULL;
69 *result = static_cast<int>(strtol(text.c_str(), &error, 10));
70 return text.c_str() != error;
71 }
72
73 bool ParseBoolAttr(const buzz::XmlElement* element,
74 const buzz::QName& attribute, bool* result) {
75 int int_value = 0;
76 if (!ParseIntAttr(element, attribute, &int_value))
77 return false;
78 *result = int_value != 0;
79 return true;
80 }
81
82 bool ParseStringAttr(const buzz::XmlElement* element,
83 const buzz::QName& attribute, std::string* result) {
84 if (!element->HasAttr(attribute))
85 return false;
86 *result = element->Attr(attribute);
87 return true;
88 }
89
90 void WriteXmlToStream(talk_base::StreamInterface* stream,
91 const buzz::XmlElement* xml) {
92 // Save it all to a string and then write that string out to disk.
93 //
94 // This is probably really inefficient in multiple ways. We probably
95 // have an entire string copy of the XML in memory twice -- once in the
96 // stream and once in the string. There is probably a way to get the data
97 // directly out of the stream but I don't have the time to decode the stream
98 // classes right now.
99 std::ostringstream s;
100 buzz::XmlPrinter::PrintXml(&s, xml);
101 std::string output_string = s.str();
102 stream->WriteAll(output_string.data(), output_string.length(), NULL, NULL);
103 }
104
105 bool SetInt64Attr(buzz::XmlElement* element, const buzz::QName& attribute,
106 int64 value) {
107 if (!element->HasAttr(attribute))
108 return false;
109 element->AddAttr(attribute, Int64ToString(value).c_str());
110 return true;
111 }
112
113 bool SetIntAttr(buzz::XmlElement* element, const buzz::QName& attribute,
114 int value) {
115 if (!element->HasAttr(attribute))
116 return false;
117 element->AddAttr(attribute, IntToString(value).c_str());
118 return true;
119 }
120
121 bool SetBoolAttr(buzz::XmlElement* element, const buzz::QName& attribute,
122 bool value) {
123 int int_value = 0;
124 if (value) {
125 int_value = 1;
126 }
127 return SetIntAttr(element, attribute, int_value);
128 }
129
130 bool SetStringAttr(buzz::XmlElement* element, const buzz::QName& attribute,
131 const std::string& value) {
132 if (!element->HasAttr(attribute))
133 return false;
134 element->AddAttr(attribute, value);
135 return true;
136 }
137
138
139 // XmlStream
140
141 XmlStream::XmlStream()
142 : state_(talk_base::SS_OPEN),
143 builder_(new buzz::XmlBuilder()),
144 parser_(new buzz::XmlParser(builder_.get())) {
145 }
146
147 XmlStream::~XmlStream() {
148 }
149
150 buzz::XmlElement* XmlStream::CreateElement() {
151 if (talk_base::SS_OPEN == state_) {
152 Close();
153 }
154 return builder_->CreateElement();
155 }
156
157 talk_base::StreamResult XmlStream::Read(void* buffer, size_t buffer_len,
158 size_t* read, int* error) {
159 if (error)
160 *error = -1;
161 return talk_base::SR_ERROR;
162 }
163
164 talk_base::StreamResult XmlStream::Write(const void* data, size_t data_len,
165 size_t* written, int* error) {
166 if (talk_base::SS_OPEN != state_) {
167 if (error)
168 *error = -1;
169 return talk_base::SR_ERROR;
170 }
171 parser_->Parse(static_cast<const char*>(data), data_len, false);
172 if (written)
173 *written = data_len;
174 return talk_base::SR_SUCCESS;
175 }
176
177 void XmlStream::Close() {
178 if (talk_base::SS_OPEN != state_)
179 return;
180
181 parser_->Parse(NULL, 0, true);
182 state_ = talk_base::SS_CLOSED;
183 }
184
185 } // namespace buzz
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698