source: proiecte/HadoopJUnit/hadoop-0.20.1/src/c++/librecordio/xmlarchive.cc @ 141

Last change on this file since 141 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 9.9 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include "xmlarchive.hh"
20#include <stdlib.h>
21
22using namespace hadoop;
23
24void hadoop::MySAXHandler::startElement(const XMLCh* const name, AttributeList& attr)
25{
26  charsValid = false;
27  char* qname = XMLString::transcode(name);
28  if(std::string("boolean") == qname ||
29    std::string("ex:i1") == qname ||
30    std::string("i4") == qname ||
31    std::string("int") == qname ||
32    std::string("ex:i8") == qname ||
33    std::string("ex:float") == qname ||
34    std::string("double") == qname ||
35    std::string("string") == qname) {
36    std::string s(qname);
37    Value v(s);
38    vlist.push_back(v);
39    charsValid = true;
40  } else if(std::string("struct") == qname ||
41    std::string("array") == qname) {
42    std::string s(qname);
43    Value v(s);
44    vlist.push_back(v);
45  }
46  XMLString::release(&qname);
47}
48
49void hadoop::MySAXHandler::endElement(const XMLCh* const name)
50{
51  charsValid = false;
52  char* qname = XMLString::transcode(name);
53  if(std::string("struct") == qname ||
54    std::string("array") == qname) {
55    std::string s = "/";
56    Value v(s + qname);
57    vlist.push_back(v);
58  }
59  XMLString::release(&qname);
60}
61
62void hadoop::MySAXHandler::characters(const XMLCh* const buf, const unsigned int len)
63{
64  if (charsValid) {
65    char *cstr = XMLString::transcode(buf);
66    Value& v = vlist.back();
67    v.addChars(cstr, strlen(cstr));
68    XMLString::release(&cstr);
69  }
70}
71
72static char hexchars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
73                          'A', 'B', 'C', 'D', 'E', 'F' };
74
75static std::string toXMLString(std::string s)
76{
77  std::string r;
78  size_t len = s.length();
79  size_t i;
80  const char* data = s.data();
81  for (i=0; i<len; i++, data++) {
82    char ch = *data;
83    if (ch == '<') {
84        r.append("&lt;");
85    } else if (ch == '&') {
86        r.append("&amp;");
87    } else if (ch == '%') {
88        r.append("%0025");
89    } else if (ch < 0x20) {
90        uint8_t* pb = (uint8_t*) &ch;
91        char ch1 = hexchars[*pb/16];
92        char ch2 = hexchars[*pb%16];
93        r.push_back('%');
94        r.push_back('0');
95        r.push_back('0');
96        r.push_back(ch1);
97        r.push_back(ch2);
98    } else {
99        r.push_back(ch);
100    }
101  }
102  return r;
103}
104
105static uint8_t h2b(char ch) {
106  if ((ch >= '0') || (ch <= '9')) {
107    return ch - '0';
108  }
109  if ((ch >= 'a') || (ch <= 'f')) {
110    return ch - 'a' + 10;
111  }
112  if ((ch >= 'A') || (ch <= 'F')) {
113    return ch - 'A' + 10;
114  }
115  return 0;
116}
117
118static std::string fromXMLString(std::string s)
119{
120  std::string r;
121  size_t len = s.length();
122  size_t i;
123  uint8_t* pb = (uint8_t*) s.data();
124  for (i = 0; i < len; i++) {
125    uint8_t b = *pb;
126    if (b == '%') {
127      char *pc = (char*) (pb+1);
128      // ignore the first two characters, which are always '0'
129      *pc++;
130      *pc++;;
131      char ch1 = *pc++;
132      char ch2 = *pc++;
133      pb += 4;
134      uint8_t cnv = h2b(ch1)*16 + h2b(ch2);
135      pc = (char*) &cnv;
136      r.push_back(*pc);
137    } else {
138      char *pc = (char*) pb;
139      r.push_back(*pc);
140    }
141    pb++;
142  }
143  return r;
144}
145
146static std::string toXMLBuffer(std::string s, size_t len)
147{
148  std::string r;
149  size_t i;
150  uint8_t* data = (uint8_t*) s.data();
151  for (i=0; i<len; i++, data++) {
152    uint8_t b = *data;
153    char ch1 = hexchars[b/16];
154    char ch2 = hexchars[b%16];
155    r.push_back(ch1);
156    r.push_back(ch2);
157  }
158  return r;
159}
160
161static std::string fromXMLBuffer(std::string s, size_t& len)
162{
163  len = s.length();
164  if (len%2 == 1) { // len is guaranteed to be even
165    throw new IOException("Errror deserializing buffer.");
166  }
167  len = len >> 1;
168  std::string t;
169  for (size_t idx = 0; idx < len; idx++) {
170    char buf[3];
171    buf[0] = s[2*idx];
172    buf[1] = s[2*idx+1];
173    buf[2] = '\0';
174    int i;
175    if (1 != sscanf(buf, "%2x", &i)) {
176      throw new IOException("Errror deserializing buffer.");
177    }
178    t.push_back((char) i);
179  }
180  len = t.length();
181  return t;
182}
183
184void hadoop::IXmlArchive::deserialize(int8_t& t, const char* tag)
185{
186  Value v = next();
187  if (v.getType() != "ex:i1") {
188    throw new IOException("Error deserializing byte");
189  }
190  t = (int8_t) strtol(v.getValue().c_str(), NULL, 10);
191}
192
193void hadoop::IXmlArchive::deserialize(bool& t, const char* tag)
194{
195  Value v = next();
196  if (v.getType() != "boolean") {
197    throw new IOException("Error deserializing boolean");
198  }
199  t = (v.getValue() == "1");
200}
201
202void hadoop::IXmlArchive::deserialize(int32_t& t, const char* tag)
203{
204  Value v = next();
205  if (v.getType() != "i4" && v.getType() != "int") {
206    throw new IOException("Error deserializing int");
207  }
208  t = (int32_t) strtol(v.getValue().c_str(), NULL, 10);
209}
210
211void hadoop::IXmlArchive::deserialize(int64_t& t, const char* tag)
212{
213  Value v = next();
214  if (v.getType() != "ex:i8") {
215    throw new IOException("Error deserializing long");
216  }
217  t = strtoll(v.getValue().c_str(), NULL, 10);
218}
219
220void hadoop::IXmlArchive::deserialize(float& t, const char* tag)
221{
222  Value v = next();
223  if (v.getType() != "ex:float") {
224    throw new IOException("Error deserializing float");
225  }
226  t = strtof(v.getValue().c_str(), NULL);
227}
228
229void hadoop::IXmlArchive::deserialize(double& t, const char* tag)
230{
231  Value v = next();
232  if (v.getType() != "double") {
233    throw new IOException("Error deserializing double");
234  }
235  t = strtod(v.getValue().c_str(), NULL);
236}
237
238void hadoop::IXmlArchive::deserialize(std::string& t, const char* tag)
239{
240  Value v = next();
241  if (v.getType() != "string") {
242    throw new IOException("Error deserializing string");
243  }
244  t = fromXMLString(v.getValue());
245}
246
247void hadoop::IXmlArchive::deserialize(std::string& t, size_t& len, const char* tag)
248{
249  Value v = next();
250  if (v.getType() != "string") {
251    throw new IOException("Error deserializing buffer");
252  }
253  t = fromXMLBuffer(v.getValue(), len);
254}
255
256void hadoop::IXmlArchive::startRecord(Record& s, const char* tag)
257{
258  Value v = next();
259  if (v.getType() != "struct") {
260    throw new IOException("Error deserializing record");
261  }
262}
263
264void hadoop::IXmlArchive::endRecord(Record& s, const char* tag)
265{
266  Value v = next();
267  if (v.getType() != "/struct") {
268    throw new IOException("Error deserializing record");
269  }
270}
271
272Index* hadoop::IXmlArchive::startVector(const char* tag)
273{
274  Value v = next();
275  if (v.getType() != "array") {
276    throw new IOException("Error deserializing vector");
277  }
278  return new XmlIndex(vlist, vidx);
279}
280
281void hadoop::IXmlArchive::endVector(Index* idx, const char* tag)
282{
283  Value v = next();
284  if (v.getType() != "/array") {
285    throw new IOException("Error deserializing vector");
286  }
287  delete idx;
288}
289
290Index* hadoop::IXmlArchive::startMap(const char* tag)
291{
292  Value v = next();
293  if (v.getType() != "array") {
294    throw new IOException("Error deserializing map");
295  }
296  return new XmlIndex(vlist, vidx);
297}
298
299void hadoop::IXmlArchive::endMap(Index* idx, const char* tag)
300{
301  Value v = next();
302  if (v.getType() != "/array") {
303    throw new IOException("Error deserializing map");
304  }
305  delete idx;
306}
307
308void hadoop::OXmlArchive::serialize(int8_t t, const char* tag)
309{
310  printBeginEnvelope(tag);
311  p("<ex:i1>");
312  char sval[5];
313  sprintf(sval, "%d", t);
314  p(sval);
315  p("</ex:i1>");
316  printEndEnvelope(tag);
317}
318
319void hadoop::OXmlArchive::serialize(bool t, const char* tag)
320{
321  printBeginEnvelope(tag);
322  p("<boolean>");
323  p(t ? "1" : "0");
324  p("</boolean>");
325  printEndEnvelope(tag);
326}
327
328void hadoop::OXmlArchive::serialize(int32_t t, const char* tag)
329{
330  printBeginEnvelope(tag);
331  p("<i4>");
332  char sval[128];
333  sprintf(sval, "%d", t);
334  p(sval);
335  p("</i4>");
336  printEndEnvelope(tag);
337}
338
339void hadoop::OXmlArchive::serialize(int64_t t, const char* tag)
340{
341  printBeginEnvelope(tag);
342  p("<ex:i8>");
343  char sval[128];
344  sprintf(sval, "%lld", t);
345  p(sval);
346  p("</ex:i8>");
347  printEndEnvelope(tag);
348
349}
350
351void hadoop::OXmlArchive::serialize(float t, const char* tag)
352{
353  printBeginEnvelope(tag);
354  p("<ex:float>");
355  char sval[128];
356  sprintf(sval, "%f", t);
357  p(sval);
358  p("</ex:float>");
359  printEndEnvelope(tag);
360}
361
362void hadoop::OXmlArchive::serialize(double t, const char* tag)
363{
364  printBeginEnvelope(tag);
365  p("<double>");
366  char sval[128];
367  sprintf(sval, "%lf", t);
368  p(sval);
369  p("</double>");
370  printEndEnvelope(tag);
371}
372
373void hadoop::OXmlArchive::serialize(const std::string& t, const char* tag)
374{
375  printBeginEnvelope(tag);
376  p("<string>");
377  std::string s = toXMLString(t);
378  stream.write(s.data(), s.length());
379  p("</string>");
380  printEndEnvelope(tag);
381}
382
383void hadoop::OXmlArchive::serialize(const std::string& t, size_t len, const char* tag)
384{
385  printBeginEnvelope(tag);
386  p("<string>");
387  std::string s = toXMLBuffer(t, len);
388  stream.write(s.data(), s.length());
389  p("</string>");
390  printEndEnvelope(tag);
391}
392
393void hadoop::OXmlArchive::startRecord(const Record& s, const char* tag)
394{
395  insideRecord(tag);
396  p("<struct>\n");
397}
398
399void hadoop::OXmlArchive::endRecord(const Record& s, const char* tag)
400{
401  p("</struct>\n");
402  outsideRecord(tag);
403}
404
405void hadoop::OXmlArchive::startVector(size_t len, const char* tag)
406{
407  insideVector(tag);
408  p("<array>\n");
409}
410
411void hadoop::OXmlArchive::endVector(size_t len, const char* tag)
412{
413  p("</array>\n");
414  outsideVector(tag);
415}
416
417void hadoop::OXmlArchive::startMap(size_t len, const char* tag)
418{
419  insideMap(tag);
420  p("<array>\n");
421}
422
423void hadoop::OXmlArchive::endMap(size_t len, const char* tag)
424{
425  p("</array>\n");
426  outsideMap(tag);
427}
428
429hadoop::OXmlArchive::~OXmlArchive()
430{
431}
Note: See TracBrowser for help on using the repository browser.