00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SkXMLParser_DEFINED
00018 #define SkXMLParser_DEFINED
00019
00020 #include "SkMath.h"
00021 #include "SkString.h"
00022
00023 class SkStream;
00024
00025 class SkDOM;
00026 struct SkDOMNode;
00027
00028 class SkXMLParserError {
00029 public:
00030 enum ErrorCode {
00031 kNoError,
00032 kEmptyFile,
00033 kUnknownElement,
00034 kUnknownAttributeName,
00035 kErrorInAttributeValue,
00036 kDuplicateIDs,
00037 kUnknownError
00038 };
00039
00040 SkXMLParserError();
00041 virtual ~SkXMLParserError();
00042 ErrorCode getErrorCode() const { return fCode; }
00043 virtual void getErrorString(SkString* str) const;
00044 int getLineNumber() const { return fLineNumber; }
00045 int getNativeCode() const { return fNativeCode; }
00046 bool hasError() const { return fCode != kNoError || fNativeCode != -1; }
00047 bool hasNoun() const { return fNoun.size() > 0; }
00048 void reset();
00049 void setCode(ErrorCode code) { fCode = code; }
00050 void setNoun(const SkString& str) { fNoun.set(str); }
00051 void setNoun(const char* ch) { fNoun.set(ch); }
00052 void setNoun(const char* ch, size_t len) { fNoun.set(ch, len); }
00053 protected:
00054 ErrorCode fCode;
00055 private:
00056 int fLineNumber;
00057 int fNativeCode;
00058 SkString fNoun;
00059 friend class SkXMLParser;
00060 };
00061
00062 class SkXMLParser {
00063 public:
00064 SkXMLParser(SkXMLParserError* parserError = NULL);
00065 virtual ~SkXMLParser();
00066
00069 bool parse(const char doc[], size_t len);
00070 bool parse(SkStream& docStream);
00071 bool parse(const SkDOM&, const SkDOMNode*);
00072
00073 static void GetNativeErrorString(int nativeErrorCode, SkString* str);
00074
00075 protected:
00076
00077 virtual bool onStartElement(const char elem[]);
00078 virtual bool onAddAttribute(const char name[], const char value[]);
00079 virtual bool onEndElement(const char elem[]);
00080 virtual bool onText(const char text[], int len);
00081
00082 public:
00083
00084 virtual bool startElement(const char elem[]);
00085 virtual bool addAttribute(const char name[], const char value[]);
00086 virtual bool endElement(const char elem[]);
00087 virtual bool text(const char text[], int len);
00088 void* fParser;
00089 protected:
00090 SkXMLParserError* fError;
00091 private:
00092 void reportError(void* parser);
00093 };
00094
00095 class SkXMLPullParser {
00096 public:
00097 SkXMLPullParser();
00098 explicit SkXMLPullParser(SkStream*);
00099 virtual ~SkXMLPullParser();
00100
00101 SkStream* getStream() const { return fStream; }
00102 SkStream* setStream(SkStream* stream);
00103
00104 enum EventType {
00105 ERROR = -1,
00106 START_DOCUMENT,
00107 END_DOCUMENT,
00108 START_TAG,
00109 END_TAG,
00110 TEXT,
00111 CDSECT,
00112 ENTITY_REF,
00113 IGNORABLE_WHITESPACE,
00114 PROCESSING_INSTRUCTION,
00115 COMMENT,
00116 DOCDECL
00117 };
00118
00119 EventType nextToken();
00120 EventType getEventType() const { return fCurr.fEventType; }
00121
00122 struct AttrInfo {
00123 const char* fName;
00124 const char* fValue;
00125 };
00126
00127 int getDepth() const { return fDepth; }
00128 const char* getName();
00129 int getAttributeCount();
00130 void getAttributeInfo(int, AttrInfo*);
00131 const char* getText();
00132 bool isWhitespace();
00133
00134 protected:
00135 virtual bool onEntityReplacement(const char name[],
00136 SkString* replacement);
00137
00138 public:
00139 struct Curr {
00140 EventType fEventType;
00141 const char* fName;
00142 AttrInfo* fAttrInfos;
00143 int fAttrInfoCount;
00144 bool fIsWhitespace;
00145 };
00146
00147 private:
00148
00149 bool onInit();
00150 EventType onNextToken();
00151 void onExit();
00152
00153 SkStream* fStream;
00154 Curr fCurr;
00155 int fDepth;
00156
00157 struct Impl;
00158 Impl* fImpl;
00159 };
00160
00161 #endif