1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SkXMLParser_DEFINED 18 #define SkXMLParser_DEFINED 19 20 #include "SkMath.h" 21 #include "SkString.h" 22 23 class SkStream; 24 25 class SkDOM; 26 struct SkDOMNode; 27 28 class SkXMLParserError { 29 public: 30 enum ErrorCode { 31 kNoError, 32 kEmptyFile, 33 kUnknownElement, 34 kUnknownAttributeName, 35 kErrorInAttributeValue, 36 kDuplicateIDs, 37 kUnknownError 38 }; 39 40 SkXMLParserError(); 41 virtual ~SkXMLParserError(); getErrorCode()42 ErrorCode getErrorCode() const { return fCode; } 43 virtual void getErrorString(SkString* str) const; getLineNumber()44 int getLineNumber() const { return fLineNumber; } getNativeCode()45 int getNativeCode() const { return fNativeCode; } hasError()46 bool hasError() const { return fCode != kNoError || fNativeCode != -1; } hasNoun()47 bool hasNoun() const { return fNoun.size() > 0; } 48 void reset(); setCode(ErrorCode code)49 void setCode(ErrorCode code) { fCode = code; } setNoun(const SkString & str)50 void setNoun(const SkString& str) { fNoun.set(str); } setNoun(const char * ch)51 void setNoun(const char* ch) { fNoun.set(ch); } setNoun(const char * ch,size_t len)52 void setNoun(const char* ch, size_t len) { fNoun.set(ch, len); } 53 protected: 54 ErrorCode fCode; 55 private: 56 int fLineNumber; 57 int fNativeCode; 58 SkString fNoun; 59 friend class SkXMLParser; 60 }; 61 62 class SkXMLParser { 63 public: 64 SkXMLParser(SkXMLParserError* parserError = NULL); 65 virtual ~SkXMLParser(); 66 67 /** Returns true for success 68 */ 69 bool parse(const char doc[], size_t len); 70 bool parse(SkStream& docStream); 71 bool parse(const SkDOM&, const SkDOMNode*); 72 73 static void GetNativeErrorString(int nativeErrorCode, SkString* str); 74 75 protected: 76 // override in subclasses; return true to stop parsing 77 virtual bool onStartElement(const char elem[]); 78 virtual bool onAddAttribute(const char name[], const char value[]); 79 virtual bool onEndElement(const char elem[]); 80 virtual bool onText(const char text[], int len); 81 82 public: 83 // public for ported implementation, not meant for clients to call 84 virtual bool startElement(const char elem[]); 85 virtual bool addAttribute(const char name[], const char value[]); 86 virtual bool endElement(const char elem[]); 87 virtual bool text(const char text[], int len); 88 void* fParser; 89 protected: 90 SkXMLParserError* fError; 91 private: 92 void reportError(void* parser); 93 }; 94 95 class SkXMLPullParser { 96 public: 97 SkXMLPullParser(); 98 explicit SkXMLPullParser(SkStream*); 99 virtual ~SkXMLPullParser(); 100 getStream()101 SkStream* getStream() const { return fStream; } 102 SkStream* setStream(SkStream* stream); 103 104 enum EventType { 105 ERROR = -1, 106 START_DOCUMENT, 107 END_DOCUMENT, 108 START_TAG, 109 END_TAG, 110 TEXT, 111 CDSECT, 112 ENTITY_REF, 113 IGNORABLE_WHITESPACE, 114 PROCESSING_INSTRUCTION, 115 COMMENT, 116 DOCDECL 117 }; 118 119 EventType nextToken(); getEventType()120 EventType getEventType() const { return fCurr.fEventType; } 121 122 struct AttrInfo { 123 const char* fName; 124 const char* fValue; 125 }; 126 getDepth()127 int getDepth() const { return fDepth; } 128 const char* getName(); 129 int getAttributeCount(); 130 void getAttributeInfo(int, AttrInfo*); 131 const char* getText(); 132 bool isWhitespace(); 133 134 protected: 135 virtual bool onEntityReplacement(const char name[], 136 SkString* replacement); 137 138 public: 139 struct Curr { 140 EventType fEventType; 141 const char* fName; 142 AttrInfo* fAttrInfos; 143 int fAttrInfoCount; 144 bool fIsWhitespace; 145 }; 146 147 private: 148 // implemented in the porting layer 149 bool onInit(); // return false on failure 150 EventType onNextToken(); 151 void onExit(); 152 153 SkStream* fStream; 154 Curr fCurr; 155 int fDepth; 156 157 struct Impl; 158 Impl* fImpl; 159 }; 160 161 #endif 162