ObjFW
OFXMLParser.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "OFObject.h"
18 #import "OFString.h"
19 #import "OFXMLAttribute.h"
20 
21 OF_ASSUME_NONNULL_BEGIN
22 
23 @class OFArray OF_GENERIC(ObjectType);
24 @class OFMutableData;
25 @class OFMutableArray OF_GENERIC(ObjectType);
26 @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
27 @class OFStream;
28 @class OFXMLParser;
29 
35 @protocol OFXMLParserDelegate <OFObject>
36 @optional
44 - (void)parser: (OFXMLParser *)parser
45  foundProcessingInstructions: (OFString *)pi;
46 
58 - (void)parser: (OFXMLParser *)parser
59  didStartElement: (OFString *)name
60  prefix: (nullable OFString *)prefix
61  namespace: (nullable OFString *)ns
62  attributes: (nullable OFArray OF_GENERIC(OFXMLAttribute *) *)attributes;
63 
72 - (void)parser: (OFXMLParser *)parser
73  didEndElement: (OFString *)name
74  prefix: (nullable OFString *)prefix
75  namespace: (nullable OFString *)ns;
76 
86 - (void)parser: (OFXMLParser *)parser
87  foundCharacters: (OFString *)characters;
88 
95 - (void)parser: (OFXMLParser *)parser
96  foundCDATA: (OFString *)CDATA;
97 
104 - (void)parser: (OFXMLParser *)parser
105  foundComment: (OFString *)comment;
106 
119 - (nullable OFString *)parser: (OFXMLParser *)parser
120  foundUnknownEntityNamed: (OFString *)entity;
121 @end
122 
132 {
133  id <OFXMLParserDelegate> _Nullable _delegate;
134  enum of_xml_parser_state {
135  OF_XMLPARSER_IN_BYTE_ORDER_MARK,
136  OF_XMLPARSER_OUTSIDE_TAG,
137  OF_XMLPARSER_TAG_OPENED,
138  OF_XMLPARSER_IN_PROCESSING_INSTRUCTIONS,
139  OF_XMLPARSER_IN_TAG_NAME,
140  OF_XMLPARSER_IN_CLOSE_TAG_NAME,
141  OF_XMLPARSER_IN_TAG,
142  OF_XMLPARSER_IN_ATTRIBUTE_NAME,
143  OF_XMLPARSER_EXPECT_ATTRIBUTE_EQUAL_SIGN,
144  OF_XMLPARSER_EXPECT_ATTRIBUTE_DELIMITER,
145  OF_XMLPARSER_IN_ATTRIBUTE_VALUE,
146  OF_XMLPARSER_EXPECT_TAG_CLOSE,
147  OF_XMLPARSER_EXPECT_SPACE_OR_TAG_CLOSE,
148  OF_XMLPARSER_IN_EXCLAMATIONMARK,
149  OF_XMLPARSER_IN_CDATA_OPENING,
150  OF_XMLPARSER_IN_CDATA,
151  OF_XMLPARSER_IN_COMMENT_OPENING,
152  OF_XMLPARSER_IN_COMMENT_1,
153  OF_XMLPARSER_IN_COMMENT_2,
154  OF_XMLPARSER_IN_DOCTYPE,
155  OF_XMLPARSER_NUM_STATES
156  } _state;
157  size_t _i, _last;
158  const char *_Nullable _data;
159  OFMutableData *_buffer;
160  OFString *_Nullable _name, *_Nullable _prefix;
162  OF_GENERIC(OFMutableDictionary OF_GENERIC(OFString *, OFString *) *)
163  *_namespaces;
164  OFMutableArray OF_GENERIC(OFXMLAttribute *) *_attributes;
165  OFString *_Nullable _attributeName, *_Nullable _attributePrefix;
166  char _delimiter;
167  OFMutableArray OF_GENERIC(OFString *) *_previous;
168  size_t _level;
169  bool _acceptProlog;
170  size_t _lineNumber;
171  bool _lastCarriageReturn, _finishedParsing;
172  of_string_encoding_t _encoding;
173  size_t _depthLimit;
174 }
175 
179 @property OF_NULLABLE_PROPERTY (nonatomic, assign)
180  id <OFXMLParserDelegate> delegate;
181 
189 @property (nonatomic) size_t depthLimit;
196 + (instancetype)parser;
197 
204 - (void)parseBuffer: (const char *)buffer
205  length: (size_t)length;
206 
212 - (void)parseString: (OFString *)string;
213 
219 - (void)parseStream: (OFStream *)stream;
220 
221 #ifdef OF_HAVE_FILES
222 
227 - (void)parseFile: (OFString *)path;
228 #endif
229 
235 - (size_t)lineNumber;
236 
242 - (bool)hasFinishedParsing;
243 @end
244 
245 OF_ASSUME_NONNULL_END
size_t depthLimit
Definition: OFXMLParser.h:190
size_t lineNumber()
Returns the current line number.
Definition: OFXMLParser.m:997
A class for handling strings.
Definition: OFString.h:114
A protocol that needs to be implemented by delegates for stringByXMLUnescapingWithHandler:.
nonatomic
Definition: OFXMLParser.h:180
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:39
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
An event-based XML parser.
Definition: OFXMLParser.h:131
An abstract class for storing objects in an array.
Definition: OFArray.h:89
of_string_encoding_t
The encoding of a string.
Definition: OFString.h:53
A representation of an attribute of an XML element as an object.
Definition: OFXMLAttribute.h:28
A protocol that needs to be implemented by delegates for OFXMLParser.
Definition: OFXMLParser.h:28
bool hasFinishedParsing()
Returns whether the XML parser has finished parsing.
Definition: OFXMLParser.m:1002
An abstract class for storing, adding and removing objects in an array.
Definition: OFMutableArray.h:40
A base class for different types of streams.
Definition: OFStream.h:88
A class for storing and manipulating arbitrary data in an array.
Definition: OFMutableData.h:29