OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
| 8 #include "expat.h" |
8 | 9 |
| 10 #include "SkStream.h" |
9 #include "SkXMLParser.h" | 11 #include "SkXMLParser.h" |
10 | 12 |
11 static char const* const gErrorStrings[] = { | 13 static char const* const gErrorStrings[] = { |
12 "empty or missing file ", | 14 "empty or missing file ", |
13 "unknown element ", | 15 "unknown element ", |
14 "unknown attribute name ", | 16 "unknown attribute name ", |
15 "error in attribute value ", | 17 "error in attribute value ", |
16 "duplicate ID ", | 18 "duplicate ID ", |
17 "unknown error " | 19 "unknown error " |
18 }; | 20 }; |
(...skipping 21 matching lines...) Expand all Loading... |
40 SkXMLParser::GetNativeErrorString(fNativeCode, &temp); | 42 SkXMLParser::GetNativeErrorString(fNativeCode, &temp); |
41 str->append(temp); | 43 str->append(temp); |
42 } | 44 } |
43 | 45 |
44 void SkXMLParserError::reset() { | 46 void SkXMLParserError::reset() { |
45 fCode = kNoError; | 47 fCode = kNoError; |
46 fLineNumber = -1; | 48 fLineNumber = -1; |
47 fNativeCode = -1; | 49 fNativeCode = -1; |
48 } | 50 } |
49 | 51 |
| 52 //////////////// |
50 | 53 |
51 //////////////// | 54 namespace { |
| 55 |
| 56 const XML_Memory_Handling_Suite sk_XML_alloc = { |
| 57 sk_malloc_throw, |
| 58 sk_realloc_throw, |
| 59 sk_free |
| 60 }; |
| 61 |
| 62 void XMLCALL start_element_handler(void *data, const char* tag, const char** att
ributes) { |
| 63 SkXMLParser* parser = static_cast<SkXMLParser*>(data); |
| 64 |
| 65 parser->startElement(tag); |
| 66 |
| 67 for (size_t i = 0; attributes[i]; i += 2) { |
| 68 parser->addAttribute(attributes[i], attributes[i + 1]); |
| 69 } |
| 70 } |
| 71 |
| 72 void XMLCALL end_element_handler(void* data, const char* tag) { |
| 73 static_cast<SkXMLParser*>(data)->endElement(tag); |
| 74 } |
| 75 |
| 76 } // anonymous namespace |
52 | 77 |
53 SkXMLParser::SkXMLParser(SkXMLParserError* parserError) : fParser(nullptr), fErr
or(parserError) | 78 SkXMLParser::SkXMLParser(SkXMLParserError* parserError) : fParser(nullptr), fErr
or(parserError) |
54 { | 79 { |
55 } | 80 } |
56 | 81 |
57 SkXMLParser::~SkXMLParser() | 82 SkXMLParser::~SkXMLParser() |
58 { | 83 { |
59 } | 84 } |
60 | 85 |
61 bool SkXMLParser::parse(SkStream& docStream) | 86 bool SkXMLParser::parse(SkStream& docStream) |
62 { | 87 { |
63 return false; | 88 SkAutoTCallVProc<skstd::remove_pointer_t<XML_Parser>, XML_ParserFree> |
| 89 parser(XML_ParserCreate_MM(nullptr, &sk_XML_alloc, nullptr)); |
| 90 if (!parser) { |
| 91 SkDebugf("could not create XML parser\n"); |
| 92 return false; |
| 93 } |
| 94 |
| 95 XML_SetUserData(parser, this); |
| 96 XML_SetElementHandler(parser, start_element_handler, end_element_handler); |
| 97 |
| 98 static const int kBufferSize = 512 SkDEBUGCODE( - 507); |
| 99 bool done = false; |
| 100 do { |
| 101 void* buffer = XML_GetBuffer(parser, kBufferSize); |
| 102 if (!buffer) { |
| 103 SkDebugf("could not buffer enough to continue\n"); |
| 104 return false; |
| 105 } |
| 106 |
| 107 size_t len = docStream.read(buffer, kBufferSize); |
| 108 done = docStream.isAtEnd(); |
| 109 XML_Status status = XML_ParseBuffer(parser, SkToS32(len), done); |
| 110 if (XML_STATUS_ERROR == status) { |
| 111 XML_Error error = XML_GetErrorCode(parser); |
| 112 int line = XML_GetCurrentLineNumber(parser); |
| 113 int column = XML_GetCurrentColumnNumber(parser); |
| 114 const XML_LChar* errorString = XML_ErrorString(error); |
| 115 SkDebugf("parse error @%d:%d: %d (%s).\n", line, column, error, erro
rString); |
| 116 return false; |
| 117 } |
| 118 } while (!done); |
| 119 |
| 120 return true; |
64 } | 121 } |
65 | 122 |
66 bool SkXMLParser::parse(const char doc[], size_t len) | 123 bool SkXMLParser::parse(const char doc[], size_t len) |
67 { | 124 { |
68 return false; | 125 SkMemoryStream docStream(doc, len); |
| 126 return this->parse(docStream); |
69 } | 127 } |
70 | 128 |
71 void SkXMLParser::GetNativeErrorString(int error, SkString* str) | 129 void SkXMLParser::GetNativeErrorString(int error, SkString* str) |
72 { | 130 { |
73 | 131 |
74 } | 132 } |
75 | 133 |
76 bool SkXMLParser::startElement(const char elem[]) | 134 bool SkXMLParser::startElement(const char elem[]) |
77 { | 135 { |
78 return this->onStartElement(elem); | 136 return this->onStartElement(elem); |
(...skipping 13 matching lines...) Expand all Loading... |
92 { | 150 { |
93 return this->onText(text, len); | 151 return this->onText(text, len); |
94 } | 152 } |
95 | 153 |
96 //////////////////////////////////////////////////////////////////////////////// | 154 //////////////////////////////////////////////////////////////////////////////// |
97 | 155 |
98 bool SkXMLParser::onStartElement(const char elem[]) {return false; } | 156 bool SkXMLParser::onStartElement(const char elem[]) {return false; } |
99 bool SkXMLParser::onAddAttribute(const char name[], const char value[]) {return
false; } | 157 bool SkXMLParser::onAddAttribute(const char name[], const char value[]) {return
false; } |
100 bool SkXMLParser::onEndElement(const char elem[]) { return false; } | 158 bool SkXMLParser::onEndElement(const char elem[]) { return false; } |
101 bool SkXMLParser::onText(const char text[], int len) {return false; } | 159 bool SkXMLParser::onText(const char text[], int len) {return false; } |
OLD | NEW |