1/* libs/graphics/ports/SkXMLParser_tinyxml.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "SkXMLParser.h"
19#include "SkStream.h"
20#include "SkTemplates.h"
21#include "tinyxml.h"
22
23static void walk_elem(SkXMLParser* parser, const TiXmlElement* elem)
24{
25    //printf("walk_elem(%s) ", elem->Value());
26
27    parser->startElement(elem->Value());
28
29    const TiXmlAttribute* attr = elem->FirstAttribute();
30    while (attr)
31    {
32        //printf("walk_elem_attr(%s=\"%s\") ", attr->Name(), attr->Value());
33
34        parser->addAttribute(attr->Name(), attr->Value());
35        attr = attr->Next();
36    }
37    //printf("\n");
38
39    const TiXmlNode* node = elem->FirstChild();
40    while (node)
41    {
42        if (node->ToElement())
43            walk_elem(parser, node->ToElement());
44        else if (node->ToText())
45            parser->text(node->Value(), strlen(node->Value()));
46        node = node->NextSibling();
47    }
48
49    parser->endElement(elem->Value());
50}
51
52static bool load_buf(SkXMLParser* parser, const char buf[])
53{
54    TiXmlDocument                   doc;
55
56    (void)doc.Parse(buf);
57    if (doc.Error())
58    {
59        printf("tinyxml error: <%s> row[%d] col[%d]\n", doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol());
60        return false;
61    }
62
63    walk_elem(parser, doc.RootElement());
64    return true;
65}
66
67bool SkXMLParser::parse(SkStream& stream)
68{
69    size_t size = stream.read(NULL, 0);
70
71    SkAutoMalloc    buffer(size + 1);
72    char*           buf = (char*)buffer.get();
73
74    stream.read(buf, size);
75    buf[size] = 0;
76
77    return load_buf(this, buf);
78}
79
80bool SkXMLParser::parse(const char doc[], size_t len)
81{
82    SkAutoMalloc    buffer(len + 1);
83    char*           buf = (char*)buffer.get();
84
85    memcpy(buf, doc, len);
86    buf[len] = 0;
87
88    return load_buf(this, buf);
89}
90
91void SkXMLParser::GetNativeErrorString(int error, SkString* str)
92{
93    if (str)
94        str->set("GetNativeErrorString not implemented for TinyXml");
95}
96
97