michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkXMLParser.h" michael@0: #include "SkStream.h" michael@0: #include "SkTemplates.h" michael@0: #include "tinyxml.h" michael@0: michael@0: static void walk_elem(SkXMLParser* parser, const TiXmlElement* elem) michael@0: { michael@0: //printf("walk_elem(%s) ", elem->Value()); michael@0: michael@0: parser->startElement(elem->Value()); michael@0: michael@0: const TiXmlAttribute* attr = elem->FirstAttribute(); michael@0: while (attr) michael@0: { michael@0: //printf("walk_elem_attr(%s=\"%s\") ", attr->Name(), attr->Value()); michael@0: michael@0: parser->addAttribute(attr->Name(), attr->Value()); michael@0: attr = attr->Next(); michael@0: } michael@0: //printf("\n"); michael@0: michael@0: const TiXmlNode* node = elem->FirstChild(); michael@0: while (node) michael@0: { michael@0: if (node->ToElement()) michael@0: walk_elem(parser, node->ToElement()); michael@0: else if (node->ToText()) michael@0: parser->text(node->Value(), strlen(node->Value())); michael@0: node = node->NextSibling(); michael@0: } michael@0: michael@0: parser->endElement(elem->Value()); michael@0: } michael@0: michael@0: static bool load_buf(SkXMLParser* parser, const char buf[]) michael@0: { michael@0: TiXmlDocument doc; michael@0: michael@0: (void)doc.Parse(buf); michael@0: if (doc.Error()) michael@0: { michael@0: printf("tinyxml error: <%s> row[%d] col[%d]\n", doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol()); michael@0: return false; michael@0: } michael@0: michael@0: walk_elem(parser, doc.RootElement()); michael@0: return true; michael@0: } michael@0: michael@0: bool SkXMLParser::parse(SkStream& stream) michael@0: { michael@0: size_t size = stream.getLength(); michael@0: michael@0: SkAutoMalloc buffer(size + 1); michael@0: char* buf = (char*)buffer.get(); michael@0: michael@0: stream.read(buf, size); michael@0: buf[size] = 0; michael@0: michael@0: return load_buf(this, buf); michael@0: } michael@0: michael@0: bool SkXMLParser::parse(const char doc[], size_t len) michael@0: { michael@0: SkAutoMalloc buffer(len + 1); michael@0: char* buf = (char*)buffer.get(); michael@0: michael@0: memcpy(buf, doc, len); michael@0: buf[len] = 0; michael@0: michael@0: return load_buf(this, buf); michael@0: } michael@0: michael@0: void SkXMLParser::GetNativeErrorString(int error, SkString* str) michael@0: { michael@0: if (str) michael@0: str->set("GetNativeErrorString not implemented for TinyXml"); michael@0: }