blob: 1a90bf71e797836faa5a09732bbd3d9a0a4910ad [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkXMLParser_DEFINED
11#define SkXMLParser_DEFINED
12
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkString.h"
14
15class SkStream;
16
17class SkDOM;
18struct SkDOMNode;
19
20class SkXMLParserError {
21public:
22 enum ErrorCode {
23 kNoError,
24 kEmptyFile,
25 kUnknownElement,
26 kUnknownAttributeName,
27 kErrorInAttributeValue,
28 kDuplicateIDs,
29 kUnknownError
30 };
31
32 SkXMLParserError();
33 virtual ~SkXMLParserError();
34 ErrorCode getErrorCode() const { return fCode; }
35 virtual void getErrorString(SkString* str) const;
36 int getLineNumber() const { return fLineNumber; }
37 int getNativeCode() const { return fNativeCode; }
38 bool hasError() const { return fCode != kNoError || fNativeCode != -1; }
39 bool hasNoun() const { return fNoun.size() > 0; }
40 void reset();
41 void setCode(ErrorCode code) { fCode = code; }
42 void setNoun(const SkString& str) { fNoun.set(str); }
43 void setNoun(const char* ch) { fNoun.set(ch); }
44 void setNoun(const char* ch, size_t len) { fNoun.set(ch, len); }
45protected:
46 ErrorCode fCode;
47private:
48 int fLineNumber;
49 int fNativeCode;
50 SkString fNoun;
51 friend class SkXMLParser;
52};
53
54class SkXMLParser {
55public:
56 SkXMLParser(SkXMLParserError* parserError = NULL);
57 virtual ~SkXMLParser();
58
59 /** Returns true for success
60 */
61 bool parse(const char doc[], size_t len);
62 bool parse(SkStream& docStream);
63 bool parse(const SkDOM&, const SkDOMNode*);
64
65 static void GetNativeErrorString(int nativeErrorCode, SkString* str);
66
67protected:
68 // override in subclasses; return true to stop parsing
69 virtual bool onStartElement(const char elem[]);
70 virtual bool onAddAttribute(const char name[], const char value[]);
71 virtual bool onEndElement(const char elem[]);
72 virtual bool onText(const char text[], int len);
73
74public:
75 // public for ported implementation, not meant for clients to call
76 virtual bool startElement(const char elem[]);
77 virtual bool addAttribute(const char name[], const char value[]);
78 virtual bool endElement(const char elem[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079 virtual bool text(const char text[], int len);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 void* fParser;
81protected:
82 SkXMLParserError* fError;
83private:
84 void reportError(void* parser);
85};
86
reed@android.come191b162009-12-18 21:33:39 +000087#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +000088class SkXMLPullParser {
89public:
90 SkXMLPullParser();
91 explicit SkXMLPullParser(SkStream*);
92 virtual ~SkXMLPullParser();
93
94 SkStream* getStream() const { return fStream; }
95 SkStream* setStream(SkStream* stream);
96
97 enum EventType {
98 ERROR = -1,
99 START_DOCUMENT,
100 END_DOCUMENT,
101 START_TAG,
102 END_TAG,
103 TEXT,
104 CDSECT,
105 ENTITY_REF,
106 IGNORABLE_WHITESPACE,
107 PROCESSING_INSTRUCTION,
108 COMMENT,
109 DOCDECL
110 };
111
112 EventType nextToken();
113 EventType getEventType() const { return fCurr.fEventType; }
114
115 struct AttrInfo {
116 const char* fName;
117 const char* fValue;
118 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 int getDepth() const { return fDepth; }
121 const char* getName();
122 int getAttributeCount();
123 void getAttributeInfo(int, AttrInfo*);
124 const char* getText();
125 bool isWhitespace();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127protected:
128 virtual bool onEntityReplacement(const char name[],
129 SkString* replacement);
130
131public:
132 struct Curr {
133 EventType fEventType;
134 const char* fName;
135 AttrInfo* fAttrInfos;
136 int fAttrInfoCount;
137 bool fIsWhitespace;
138 };
139
140private:
141 // implemented in the porting layer
142 bool onInit(); // return false on failure
143 EventType onNextToken();
144 void onExit();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkStream* fStream;
147 Curr fCurr;
148 int fDepth;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 struct Impl;
151 Impl* fImpl;
152};
reed@android.come191b162009-12-18 21:33:39 +0000153#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
155#endif