1
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
9
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());
25
26        parser->addAttribute(attr->Name(), attr->Value());
27        attr = attr->Next();
28    }
29    //printf("\n");
30
31    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    }
40
41    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    }
54
55    walk_elem(parser, doc.RootElement());
56    return true;
57}
58
59bool SkXMLParser::parse(SkStream& stream)
60{
61    size_t size = stream.read(NULL, 0);
62
63    SkAutoMalloc    buffer(size + 1);
64    char*           buf = (char*)buffer.get();
65
66    stream.read(buf, size);
67    buf[size] = 0;
68
69    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();
76
77    memcpy(buf, doc, len);
78    buf[len] = 0;
79
80    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}
88
89