Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | /** |
| 19 | * XML "SAX" (stream) parsing support from GNU Common C++. |
| 20 | * @file ucommon/xml.h |
| 21 | */ |
| 22 | |
| 23 | #ifndef _UCOMMON_STRING_H_ |
| 24 | #include <ucommon/string.h> |
| 25 | #endif |
| 26 | |
| 27 | #ifndef _UCOMMON_PROTOCOLS_H_ |
| 28 | #include <ucommon/protocols.h> |
| 29 | #endif |
| 30 | |
| 31 | #ifndef _UCOMMON_XML_H_ |
| 32 | #define _UCOMMON_XML_H_ |
| 33 | |
| 34 | NAMESPACE_UCOMMON |
| 35 | |
| 36 | /** |
| 37 | * XML streaming parser. This class implements a basic XML stream parser |
| 38 | * that can be used to examine an XML resource thru virtual I/O methods. |
| 39 | * This class must be derived into one that can implement the physical I/O |
| 40 | * required to parse actual data. A mixer class using XMLParser and |
| 41 | * tcpstream would be one example of this. This can also be used to |
| 42 | * parse xml content in memory buffers easily. This parser is only concerned |
| 43 | * with well-formedness, and does not perform validation. |
| 44 | * |
| 45 | * @author David Sugar <dyfet@gnutelephony.org> |
| 46 | */ |
| 47 | class __EXPORT XMLParser |
| 48 | { |
| 49 | private: |
| 50 | int ecount, dcount; |
| 51 | enum {TAG, CDATA, COMMENT, DTD, AMP, NONE, END} state; |
| 52 | char *buffer; |
| 53 | unsigned bufpos, bufsize; |
| 54 | __LOCAL bool parseTag(void); |
| 55 | __LOCAL void putBuffer(char c); |
| 56 | __LOCAL void clearBuffer(void); |
| 57 | |
| 58 | protected: |
| 59 | /** |
| 60 | * Create xml parser. |
| 61 | * @param size of XML data buffer. |
| 62 | */ |
| 63 | XMLParser(unsigned size = 8192); |
| 64 | |
| 65 | /** |
| 66 | * Destroy xml parser. |
| 67 | */ |
| 68 | virtual ~XMLParser(); |
| 69 | |
| 70 | /** |
| 71 | * Virtual to receive embedded comments in XML document being parsed. |
| 72 | * @param text received. |
| 73 | * @param size of text received. |
| 74 | */ |
| 75 | virtual void comment(caddr_t text, size_t size); |
| 76 | |
| 77 | /** |
| 78 | * Virtual to receive character text extracted from the document. |
| 79 | * @param text received. |
| 80 | * @param size of text received. |
| 81 | */ |
| 82 | virtual void characters(caddr_t text, size_t size); |
| 83 | |
| 84 | /** |
| 85 | * Notify start of document event. |
| 86 | */ |
| 87 | virtual void startDocument(void); |
| 88 | |
| 89 | /** |
| 90 | * Notify end of document event. |
| 91 | */ |
| 92 | virtual void endDocument(void); |
| 93 | |
| 94 | /** |
| 95 | * Notify start of an element in the document. |
| 96 | * @param name of element found. |
| 97 | * @param attr list of attributes extracted. |
| 98 | */ |
| 99 | virtual void startElement(caddr_t name, caddr_t *attr) = 0; |
| 100 | |
| 101 | /** |
| 102 | * Notify end of an element in the document. |
| 103 | * @param name of element ending. |
| 104 | */ |
| 105 | virtual void endElement(caddr_t name) = 0; |
| 106 | |
| 107 | /** |
| 108 | * Parse a chunk of data and return parser completion flag. This is |
| 109 | * used to externally drive data into the XML parser. The return |
| 110 | * status can be used to determine when a document has been fully |
| 111 | * parsed. This can be called multiple times to push stream data |
| 112 | * into the parser. |
| 113 | * @param address of data to parse. |
| 114 | * @param size of data to parse. |
| 115 | */ |
| 116 | bool partial(const char *address, size_t size); |
| 117 | |
| 118 | /** |
| 119 | * Parse a stream buffer and return parser document completion flag. |
| 120 | * This is used to scan a stream buffer for a complete XML document. |
| 121 | * The stream is scanned until the document is complete or EOF. |
| 122 | * Multiple XML document instances can be scanned from a continues |
| 123 | * XML streaming source. |
| 124 | * @param stream buffer to parse. |
| 125 | * @return true if parse complete, false if invalid or EOF. |
| 126 | */ |
| 127 | bool parse(CharacterProtocol& stream); |
| 128 | |
| 129 | /** |
| 130 | * Parse a file buffer and return parser document completion flag. |
| 131 | * This is used to scan a file buffer for a complete XML document. |
| 132 | * The file is scanned until the document is complete or EOF. |
| 133 | * Multiple XML document instances can be scanned from a continues |
| 134 | * XML streaming source. |
| 135 | * @param file buffer to parse. |
| 136 | * @return true if parse complete, false if invalid or EOF. |
| 137 | */ |
| 138 | bool parse(FILE *file); |
| 139 | |
| 140 | /** |
| 141 | * End of document check. |
| 142 | * @return true if end of document. |
| 143 | */ |
| 144 | bool end(void) |
| 145 | {return state == END;}; |
| 146 | }; |
| 147 | |
| 148 | END_NAMESPACE |
| 149 | |
| 150 | #endif |