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 | 8 |
9 #include "SkDOM.h" | 9 #include "SkDOM.h" |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 SkDOM::Node* fRoot; | 303 SkDOM::Node* fRoot; |
304 bool fNeedToFlush; | 304 bool fNeedToFlush; |
305 | 305 |
306 // state needed for flushAttributes() | 306 // state needed for flushAttributes() |
307 SkTDArray<SkDOM::Attr> fAttrs; | 307 SkTDArray<SkDOM::Attr> fAttrs; |
308 char* fElemName; | 308 char* fElemName; |
309 SkDOM::Type fElemType; | 309 SkDOM::Type fElemType; |
310 int fLevel; | 310 int fLevel; |
311 }; | 311 }; |
312 | 312 |
313 const SkDOM::Node* SkDOM::build(const char doc[], size_t len) | 313 const SkDOM::Node* SkDOM::build(SkStream& docStream) { |
314 { | |
315 SkDOMParser parser(&fAlloc); | 314 SkDOMParser parser(&fAlloc); |
316 if (!parser.parse(doc, len)) | 315 if (!parser.parse(docStream)) |
317 { | 316 { |
318 SkDEBUGCODE(SkDebugf("xml parse error, line %d\n", parser.fParserError.g
etLineNumber());) | 317 SkDEBUGCODE(SkDebugf("xml parse error, line %d\n", parser.fParserError.g
etLineNumber());) |
319 fRoot = nullptr; | 318 fRoot = nullptr; |
320 fAlloc.reset(); | 319 fAlloc.reset(); |
321 return nullptr; | 320 return nullptr; |
322 } | 321 } |
323 fRoot = parser.getRoot(); | 322 fRoot = parser.getRoot(); |
324 return fRoot; | 323 return fRoot; |
325 } | 324 } |
326 | 325 |
| 326 const SkDOM::Node* SkDOM::build(const char doc[], size_t len) { |
| 327 SkMemoryStream docStream(doc, len); |
| 328 return this->build(docStream); |
| 329 } |
| 330 |
327 /////////////////////////////////////////////////////////////////////////// | 331 /////////////////////////////////////////////////////////////////////////// |
328 | 332 |
329 static void walk_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLParser* par
ser) | 333 static void walk_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLParser* par
ser) |
330 { | 334 { |
331 const char* elem = dom.getName(node); | 335 const char* elem = dom.getName(node); |
332 if (dom.getType(node) == SkDOM::kText_Type) { | 336 if (dom.getType(node) == SkDOM::kText_Type) { |
333 SkASSERT(dom.countChildren(node) == 0); | 337 SkASSERT(dom.countChildren(node) == 0); |
334 parser->text(elem, SkToInt(strlen(elem))); | 338 parser->text(elem, SkToInt(strlen(elem))); |
335 return; | 339 return; |
336 } | 340 } |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 void SkDOM::dump(const Node* node, int level) const | 472 void SkDOM::dump(const Node* node, int level) const |
469 { | 473 { |
470 if (node == nullptr) | 474 if (node == nullptr) |
471 node = this->getRootNode(); | 475 node = this->getRootNode(); |
472 | 476 |
473 SkDebugWStream debugStream; | 477 SkDebugWStream debugStream; |
474 SkXMLStreamWriter xmlWriter(&debugStream); | 478 SkXMLStreamWriter xmlWriter(&debugStream); |
475 xmlWriter.writeDOM(*this, node, false); | 479 xmlWriter.writeDOM(*this, node, false); |
476 } | 480 } |
477 | 481 |
478 void SkDOM::UnitTest() | |
479 { | |
480 #ifdef SK_SUPPORT_UNITTEST | |
481 static const char gDoc[] = | |
482 "<root a='1' b='2'>" | |
483 "<elem1 c='3' />" | |
484 "<elem2 d='4' />" | |
485 "<elem3 e='5'>" | |
486 "<subelem1/>" | |
487 "<subelem2 f='6' g='7'/>" | |
488 "</elem3>" | |
489 "<elem4 h='8'/>" | |
490 "</root>" | |
491 ; | |
492 | |
493 SkDOM dom; | |
494 | |
495 SkASSERT(dom.getRootNode() == nullptr); | |
496 | |
497 const Node* root = dom.build(gDoc, sizeof(gDoc) - 1); | |
498 SkASSERT(root && dom.getRootNode() == root); | |
499 | |
500 const char* v = dom.findAttr(root, "a"); | |
501 SkASSERT(v && !strcmp(v, "1")); | |
502 v = dom.findAttr(root, "b"); | |
503 SkASSERT(v && !strcmp(v, "2")); | |
504 v = dom.findAttr(root, "c"); | |
505 SkASSERT(v == nullptr); | |
506 | |
507 SkASSERT(dom.getFirstChild(root, "elem1")); | |
508 SkASSERT(!dom.getFirstChild(root, "subelem1")); | |
509 | |
510 dom.dump(); | |
511 #endif | 482 #endif |
512 } | |
513 | |
514 #endif | |
OLD | NEW |