blob: 5f9f3a68de8d54892d16975a5d00c77ba002ebc1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkXMLParser.h"
11#include "SkStream.h"
12#include "SkTemplates.h"
13#include "tinyxml.h"
14
15static void walk_elem(SkXMLParser* parser, const TiXmlElement* elem)
16{
17 //printf("walk_elem(%s) ", elem->Value());
18
19 parser->startElement(elem->Value());
20
21 const TiXmlAttribute* attr = elem->FirstAttribute();
22 while (attr)
23 {
24 //printf("walk_elem_attr(%s=\"%s\") ", attr->Name(), attr->Value());
rmistry@google.comd6176b02012-08-23 18:14:13 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 parser->addAttribute(attr->Name(), attr->Value());
27 attr = attr->Next();
28 }
29 //printf("\n");
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 const TiXmlNode* node = elem->FirstChild();
32 while (node)
33 {
34 if (node->ToElement())
35 walk_elem(parser, node->ToElement());
36 else if (node->ToText())
37 parser->text(node->Value(), strlen(node->Value()));
38 node = node->NextSibling();
39 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 parser->endElement(elem->Value());
42}
43
44static bool load_buf(SkXMLParser* parser, const char buf[])
45{
46 TiXmlDocument doc;
47
48 (void)doc.Parse(buf);
49 if (doc.Error())
50 {
51 printf("tinyxml error: <%s> row[%d] col[%d]\n", doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol());
52 return false;
53 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 walk_elem(parser, doc.RootElement());
56 return true;
57}
58
59bool SkXMLParser::parse(SkStream& stream)
60{
djsollen@google.com5dd45022013-03-21 13:30:54 +000061 size_t size = stream.getLength();
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 SkAutoMalloc buffer(size + 1);
64 char* buf = (char*)buffer.get();
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 stream.read(buf, size);
67 buf[size] = 0;
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 return load_buf(this, buf);
70}
71
72bool SkXMLParser::parse(const char doc[], size_t len)
73{
74 SkAutoMalloc buffer(len + 1);
75 char* buf = (char*)buffer.get();
rmistry@google.comd6176b02012-08-23 18:14:13 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 memcpy(buf, doc, len);
78 buf[len] = 0;
rmistry@google.comd6176b02012-08-23 18:14:13 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 return load_buf(this, buf);
81}
82
83void SkXMLParser::GetNativeErrorString(int error, SkString* str)
84{
85 if (str)
86 str->set("GetNativeErrorString not implemented for TinyXml");
87}