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 SkXMLWriter_DEFINED 18 #define SkXMLWriter_DEFINED 19 20 #include "SkTDArray.h" 21 #include "SkString.h" 22 #include "SkDOM.h" 23 24 class SkWStream; 25 class SkXMLParser; 26 27 class SkXMLWriter { 28 public: 29 SkXMLWriter(bool doEscapeMarkup = true); 30 virtual ~SkXMLWriter(); 31 32 void addS32Attribute(const char name[], int32_t value); 33 void addAttribute(const char name[], const char value[]); 34 void addAttributeLen(const char name[], const char value[], size_t length); 35 void addHexAttribute(const char name[], uint32_t value, int minDigits = 0); 36 void addScalarAttribute(const char name[], SkScalar value); endElement()37 void endElement() { this->onEndElement(); } 38 void startElement(const char elem[]); 39 void startElementLen(const char elem[], size_t length); 40 void writeDOM(const SkDOM&, const SkDOM::Node*, bool skipRoot); 41 void flush(); 42 virtual void writeHeader(); 43 44 protected: 45 virtual void onStartElementLen(const char elem[], size_t length) = 0; 46 virtual void onAddAttributeLen(const char name[], const char value[], size_t length) = 0; 47 virtual void onEndElement() = 0; 48 49 struct Elem { 50 SkString fName; 51 bool fHasChildren; 52 }; 53 void doEnd(Elem* elem); 54 bool doStart(const char name[], size_t length); 55 Elem* getEnd(); 56 const char* getHeader(); 57 SkTDArray<Elem*> fElems; 58 59 private: 60 bool fDoEscapeMarkup; 61 // illegal 62 SkXMLWriter& operator=(const SkXMLWriter&); 63 }; 64 65 class SkXMLStreamWriter : public SkXMLWriter { 66 public: 67 SkXMLStreamWriter(SkWStream*); 68 virtual ~SkXMLStreamWriter(); 69 virtual void writeHeader(); 70 SkDEBUGCODE(static void UnitTest();) 71 protected: 72 virtual void onStartElementLen(const char elem[], size_t length); 73 virtual void onEndElement(); 74 virtual void onAddAttributeLen(const char name[], const char value[], size_t length); 75 private: 76 SkWStream& fStream; 77 }; 78 79 class SkXMLParserWriter : public SkXMLWriter { 80 public: 81 SkXMLParserWriter(SkXMLParser*); 82 virtual ~SkXMLParserWriter(); 83 protected: 84 virtual void onStartElementLen(const char elem[], size_t length); 85 virtual void onEndElement(); 86 virtual void onAddAttributeLen(const char name[], const char value[], size_t length); 87 private: 88 SkXMLParser& fParser; 89 }; 90 91 92 #endif 93 94