| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 24 окт. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_FMT_XML_PULLPARSER_H_ |
| 23 |
#define LSP_PLUG_IN_FMT_XML_PULLPARSER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/io/IInStream.h> |
| 28 |
#include <lsp-plug.in/io/IInSequence.h> |
| 29 |
#include <lsp-plug.in/io/Path.h> |
| 30 |
#include <lsp-plug.in/fmt/xml/const.h> |
| 31 |
#include <lsp-plug.in/lltl/parray.h> |
| 32 |
|
| 33 |
namespace lsp |
| 34 |
{ |
| 35 |
namespace xml |
| 36 |
{ |
| 37 |
class PullParser |
| 38 |
{ |
| 39 |
protected: |
| 40 |
enum parse_state_t |
| 41 |
{ |
| 42 |
PS_READ_MISC, |
| 43 |
PS_READ_ELEMENTS, |
| 44 |
PS_READ_ATTRIBUTES, |
| 45 |
PS_READ_ELEMENT_DATA, |
| 46 |
PS_READ_REFERENCE, |
| 47 |
PS_READ_CHARACTERS, |
| 48 |
PS_READ_SQ_ATTRIBUTE, |
| 49 |
PS_READ_DQ_ATTRIBUTE, |
| 50 |
PS_END_DOCUMENT |
| 51 |
}; |
| 52 |
|
| 53 |
enum xml_flags_t |
| 54 |
{ |
| 55 |
XF_STANDALONE = 1 << 0, |
| 56 |
XF_ENCODING = 1 << 1, |
| 57 |
XF_VERSION = 1 << 2, |
| 58 |
XF_ROOT = 1 << 3, |
| 59 |
XF_HEADER = 1 << 4, |
| 60 |
XF_DOCTYPE = 1 << 5, |
| 61 |
XF_DOCTYPE_SYS = 1 << 6, |
| 62 |
XF_DOCTYPE_PUB = 1 << 7, |
| 63 |
}; |
| 64 |
|
| 65 |
protected: |
| 66 |
io::IInSequence *pIn; |
| 67 |
size_t nWFlags; |
| 68 |
status_t nToken; |
| 69 |
parse_state_t nState; |
| 70 |
xml_version_t enVersion; |
| 71 |
|
| 72 |
lsp_swchar_t vUngetch[4]; |
| 73 |
size_t nUngetch; |
| 74 |
parse_state_t vStates[4]; |
| 75 |
size_t nStates; |
| 76 |
|
| 77 |
// Misc data |
| 78 |
size_t nFlags; // Field presence |
| 79 |
LSPString sVersion; // Document version |
| 80 |
LSPString sEncoding; // Document encoding |
| 81 |
LSPString sName; // Property/Tag name |
| 82 |
LSPString sValue; // Property value |
| 83 |
LSPString sRefName; // Reference name |
| 84 |
LSPString sDoctype; // Document type |
| 85 |
LSPString sSystem; // System literal |
| 86 |
LSPString sPublic; // Public literal |
| 87 |
lltl::parray<LSPString> vTags; |
| 88 |
lltl::parray<LSPString> vAtts; |
| 89 |
|
| 90 |
protected: |
| 91 |
static void drop_list(lltl::parray<LSPString> *list); |
| 92 |
|
| 93 |
inline lsp_swchar_t getch(); |
| 94 |
inline void ungetch(lsp_swchar_t c); |
| 95 |
inline void push_state(parse_state_t override); |
| 96 |
inline void pop_state(); |
| 97 |
|
| 98 |
bool skip_spaces(); |
| 99 |
status_t check_duplicate_attribute(); |
| 100 |
|
| 101 |
status_t read_version(); |
| 102 |
status_t read_encoding(); |
| 103 |
status_t read_standalone(); |
| 104 |
|
| 105 |
status_t read_text(const char *text); |
| 106 |
status_t read_start_document(); |
| 107 |
status_t read_end_document(); |
| 108 |
status_t read_name(LSPString *name); |
| 109 |
status_t read_attribute_value(lsp_swchar_t qc); |
| 110 |
status_t read_header(); |
| 111 |
status_t read_misc(); |
| 112 |
status_t read_comment(); |
| 113 |
status_t read_doctype(); |
| 114 |
status_t read_tag_open(); |
| 115 |
status_t read_tag_close(bool copy); |
| 116 |
status_t read_tag_attribute(); |
| 117 |
status_t read_tag_content(); |
| 118 |
status_t read_processing_instruction(); |
| 119 |
status_t read_cdata(); |
| 120 |
status_t read_characters(); |
| 121 |
status_t read_entity_reference(LSPString *cdata); |
| 122 |
status_t read_system_literal(LSPString *dst); |
| 123 |
status_t read_pubid_literal(LSPString *dst); |
| 124 |
|
| 125 |
status_t read_token(); |
| 126 |
|
| 127 |
public: |
| 128 |
explicit PullParser(); |
| 129 |
virtual ~PullParser(); |
| 130 |
|
| 131 |
public: |
| 132 |
/** |
| 133 |
* Open parser |
| 134 |
* @param path UTF-8 path to the file |
| 135 |
* @param charset character set |
| 136 |
* @return status of operation |
| 137 |
*/ |
| 138 |
status_t open(const char *path, const char *charset = NULL); |
| 139 |
|
| 140 |
/** |
| 141 |
* Open parser |
| 142 |
* @param path string representation of path to the file |
| 143 |
* @param charset character set |
| 144 |
* @return status of operation |
| 145 |
*/ |
| 146 |
status_t open(const LSPString *path, const char *charset = NULL); |
| 147 |
|
| 148 |
/** |
| 149 |
* Open parser |
| 150 |
* @param path path to the file |
| 151 |
* @param charset character set |
| 152 |
* @return status of operation |
| 153 |
*/ |
| 154 |
status_t open(const io::Path *path, const char *charset = NULL); |
| 155 |
|
| 156 |
/** |
| 157 |
* Wrap string with parser |
| 158 |
* @param str string to wrap |
| 159 |
* @param charset character set |
| 160 |
* @return status of operation |
| 161 |
*/ |
| 162 |
status_t wrap(const char *str, const char *charset = NULL); |
| 163 |
|
| 164 |
/** |
| 165 |
* Wrap string with parser |
| 166 |
* @param str string to wrap |
| 167 |
* @return status of operation |
| 168 |
*/ |
| 169 |
status_t wrap(const LSPString *str); |
| 170 |
|
| 171 |
/** |
| 172 |
* Wrap input sequence with parser |
| 173 |
* @param seq sequence to use for reads |
| 174 |
* @return status of operation |
| 175 |
*/ |
| 176 |
status_t wrap(io::IInSequence *seq, size_t flags = WRAP_NONE); |
| 177 |
|
| 178 |
/** |
| 179 |
* Wrap input stream with parser |
| 180 |
* @param is input stream |
| 181 |
* @param flags wrap flags |
| 182 |
* @param charset character set |
| 183 |
* @return status of operation |
| 184 |
*/ |
| 185 |
status_t wrap(io::IInStream *is, size_t flags = WRAP_NONE, const char *charset = NULL); |
| 186 |
|
| 187 |
/** |
| 188 |
* Close parser |
| 189 |
* @return status of operation |
| 190 |
*/ |
| 191 |
status_t close(); |
| 192 |
|
| 193 |
public: |
| 194 |
/** |
| 195 |
* Read next element |
| 196 |
* @return XT_ element code or negative status of operation |
| 197 |
*/ |
| 198 |
status_t read_next(); |
| 199 |
|
| 200 |
/** |
| 201 |
* Get current element |
| 202 |
* @return XT_ element code or negative status of operation |
| 203 |
*/ |
| 204 |
status_t get_current(); |
| 205 |
|
| 206 |
/** |
| 207 |
* Resolve entity with specified value |
| 208 |
* @param value value to resolve |
| 209 |
* @return status of operation |
| 210 |
*/ |
| 211 |
status_t set_value(const LSPString *value); |
| 212 |
|
| 213 |
/** |
| 214 |
* Resolve entity with specified value |
| 215 |
* @param value value to resolve |
| 216 |
* @param charset character set encoding |
| 217 |
* @return status of operation |
| 218 |
*/ |
| 219 |
status_t set_value(const char *value, const char *charset = NULL); |
| 220 |
|
| 221 |
public: |
| 222 |
/** |
| 223 |
* Check whethere document is standalone |
| 224 |
* @return true if document is standalone |
| 225 |
*/ |
| 226 |
inline bool is_standalone() const { return nFlags & XF_STANDALONE; } |
| 227 |
|
| 228 |
/** |
| 229 |
* Get version string |
| 230 |
* @return version string or NULL is version string is not present in header |
| 231 |
*/ |
| 232 |
inline const LSPString *version() const { return (nFlags & XF_VERSION) ? &sVersion : NULL; } |
| 233 |
|
| 234 |
/** |
| 235 |
* Get actual parser version |
| 236 |
* @return actual parser version |
| 237 |
*/ |
| 238 |
inline xml_version_t xml_version() const { return enVersion; } |
| 239 |
|
| 240 |
/** |
| 241 |
* Get document encoding |
| 242 |
* @return document encoding or NULL if encoding string is not present in header |
| 243 |
*/ |
| 244 |
inline const LSPString *encoding() const { return (nFlags & XF_ENCODING) ? &sEncoding : NULL; } |
| 245 |
|
| 246 |
/** |
| 247 |
* Return name of current property, tag or processing instruction |
| 248 |
* @return name name |
| 249 |
*/ |
| 250 |
const LSPString *name() const; |
| 251 |
|
| 252 |
/** |
| 253 |
* Return value of current property, comment or processing instruction |
| 254 |
* @return value value |
| 255 |
*/ |
| 256 |
const LSPString *value() const; |
| 257 |
|
| 258 |
/** |
| 259 |
* Get current element nesting level |
| 260 |
* @return current element nesting level |
| 261 |
*/ |
| 262 |
inline size_t level() const { return vTags.size(); } |
| 263 |
|
| 264 |
/** |
| 265 |
* Get document type name |
| 266 |
* @return document type name or NULL if not present |
| 267 |
*/ |
| 268 |
inline const LSPString *doctype() const { return (nFlags & XF_DOCTYPE) ? &sDoctype : NULL; } |
| 269 |
|
| 270 |
/** |
| 271 |
* Get document type public literal |
| 272 |
* @return document type public literal or NULL if not present |
| 273 |
*/ |
| 274 |
inline const LSPString *pub_literal() const { return (nFlags & XF_DOCTYPE_PUB) ? &sPublic: NULL; } |
| 275 |
|
| 276 |
/** |
| 277 |
* Get document type system literal |
| 278 |
* @return document type system literal or NULL if not present |
| 279 |
*/ |
| 280 |
inline const LSPString *sys_literal() const { return (nFlags & XF_DOCTYPE_SYS) ? &sSystem: NULL; } |
| 281 |
}; |
| 282 |
|
| 283 |
} /* namespace xml */ |
| 284 |
} /* namespace lsp */ |
| 285 |
|
| 286 |
#endif /* LSP_PLUG_IN_FMT_XML_PULLPARSER_H_ */ |