1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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 gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = glib-Simple-XML-Subset-Parser.html
27  * outPack = glib
28  * outFile = SimpleXML
29  * strct   = GMarkupParseContext
30  * realStrct=
31  * ctorStrct=
32  * clss    = SimpleXML
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_markup_parse_context_
41  * 	- g_markup_
42  * omit structs:
43  * omit prefixes:
44  * omit code:
45  * omit signals:
46  * imports:
47  * 	- glib.Str
48  * 	- glib.ErrorG
49  * 	- glib.GException
50  * 	- glib.ListSG
51  * 	- gtkc.Loader
52  * 	- gtkc.paths
53  * structWrap:
54  * 	- GMarkupParseContext* -> SimpleXML
55  * 	- GSList* -> ListSG
56  * module aliases:
57  * local aliases:
58  * overrides:
59  */
60 
61 module glib.SimpleXML;
62 
63 public  import gtkc.glibtypes;
64 
65 private import gtkc.glib;
66 private import glib.ConstructionException;
67 
68 
69 private import glib.Str;
70 private import glib.ErrorG;
71 private import glib.GException;
72 private import glib.ListSG;
73 private import gtkc.Loader;
74 private import gtkc.paths;
75 
76 
77 
78 
79 /**
80  * The "GMarkup" parser is intended to parse a simple markup format
81  * that's a subset of XML. This is a small, efficient, easy-to-use
82  * parser. It should not be used if you expect to interoperate with
83  * other applications generating full-scale XML. However, it's very
84  * useful for application data files, config files, etc. where you
85  * know your application will be the only one writing the file.
86  * Full-scale XML parsers should be able to parse the subset used by
87  * GMarkup, so you can easily migrate to full-scale XML at a later
88  * time if the need arises.
89  *
90  * GMarkup is not guaranteed to signal an error on all invalid XML;
91  * the parser may accept documents that an XML parser would not.
92  * However, XML documents which are not well-formed[6]
93  * are not considered valid GMarkup documents.
94  *
95  * Simplifications to XML include:
96  *
97  * Only UTF-8 encoding is allowed
98  * No user-defined entities
99  * Processing instructions, comments and the doctype declaration
100  * are "passed through" but are not interpreted in any way
101  * No DTD or validation.
102  *
103  * The markup format does support:
104  *
105  * Elements
106  * Attributes
107  * 5 standard entities:
108  *  amp; lt; gt; quot; apos;
109  *
110  * Character references
111  * Sections marked as CDATA
112  */
113 public class SimpleXML
114 {
115 	
116 	/** the main Gtk struct */
117 	protected GMarkupParseContext* gMarkupParseContext;
118 	
119 	
120 	public GMarkupParseContext* getSimpleXMLStruct()
121 	{
122 		return gMarkupParseContext;
123 	}
124 	
125 	
126 	/** the main Gtk struct as a void* */
127 	protected void* getStruct()
128 	{
129 		return cast(void*)gMarkupParseContext;
130 	}
131 	
132 	/**
133 	 * Sets our main struct and passes it to the parent class
134 	 */
135 	public this (GMarkupParseContext* gMarkupParseContext)
136 	{
137 		this.gMarkupParseContext = gMarkupParseContext;
138 	}
139 	
140 	~this ()
141 	{
142 		if ( Linker.isLoaded(LIBRARY.GLIB) && gMarkupParseContext !is null )
143 		{
144 			g_markup_parse_context_free(gMarkupParseContext);
145 		}
146 	}
147 	
148 	/**
149 	 */
150 	
151 	/**
152 	 * Escapes text so that the markup parser will parse it verbatim.
153 	 * Less than, greater than, ampersand, etc. are replaced with the
154 	 * corresponding entities. This function would typically be used
155 	 * when writing out a file to be parsed with the markup parser.
156 	 * Note that this function doesn't protect whitespace and line endings
157 	 * from being processed according to the XML rules for normalization
158 	 * of line endings and attribute values.
159 	 * Note also that this function will produce character references in
160 	 * the range of x1; ... x1f; for all control sequences
161 	 * except for tabstop, newline and carriage return. The character
162 	 * references in this range are not valid XML 1.0, but they are
163 	 * valid XML 1.1 and will be accepted by the GMarkup parser.
164 	 * Params:
165 	 * text = some valid UTF-8 text
166 	 * length = length of text in bytes, or -1 if the text is nul-terminated
167 	 * Returns: a newly allocated string with the escaped text
168 	 */
169 	public static string escapeText(string text, gssize length)
170 	{
171 		// gchar * g_markup_escape_text (const gchar *text,  gssize length);
172 		return Str.toString(g_markup_escape_text(Str.toStringz(text), length));
173 	}
174 	
175 	/**
176 	 * Formats the data in args according to format, escaping
177 	 * all string and character arguments in the fashion
178 	 * of g_markup_escape_text(). See g_markup_printf_escaped().
179 	 * Since 2.4
180 	 * Params:
181 	 * format = printf() style format string
182 	 * args = variable argument list, similar to vprintf()
183 	 * Returns: newly allocated result from formatting operation. Free with g_free().
184 	 */
185 	public static string vprintfEscaped(string format, void* args)
186 	{
187 		// gchar * g_markup_vprintf_escaped (const char *format,  va_list args);
188 		return Str.toString(g_markup_vprintf_escaped(Str.toStringz(format), args));
189 	}
190 	
191 	/**
192 	 * Signals to the GMarkupParseContext that all data has been
193 	 * fed into the parse context with g_markup_parse_context_parse().
194 	 * This function reports an error if the document isn't complete,
195 	 * for example if elements are still open.
196 	 * Returns: TRUE on success, FALSE if an error was set
197 	 * Throws: GException on failure.
198 	 */
199 	public int endParse()
200 	{
201 		// gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,  GError **error);
202 		GError* err = null;
203 		
204 		auto p = g_markup_parse_context_end_parse(gMarkupParseContext, &err);
205 		
206 		if (err !is null)
207 		{
208 			throw new GException( new ErrorG(err) );
209 		}
210 		
211 		return p;
212 	}
213 	
214 	/**
215 	 * Frees a GMarkupParseContext.
216 	 * This function can't be called from inside one of the
217 	 * GMarkupParser functions or while a subparser is pushed.
218 	 */
219 	public void free()
220 	{
221 		// void g_markup_parse_context_free (GMarkupParseContext *context);
222 		g_markup_parse_context_free(gMarkupParseContext);
223 	}
224 	
225 	/**
226 	 * Retrieves the current line number and the number of the character on
227 	 * that line. Intended for use in error messages; there are no strict
228 	 * semantics for what constitutes the "current" line number other than
229 	 * "the best number we could come up with for error messages."
230 	 * Params:
231 	 * lineNumber = return location for a line number, or NULL. [allow-none]
232 	 * charNumber = return location for a char-on-line number, or NULL. [allow-none]
233 	 */
234 	public void getPosition(out int lineNumber, out int charNumber)
235 	{
236 		// void g_markup_parse_context_get_position (GMarkupParseContext *context,  gint *line_number,  gint *char_number);
237 		g_markup_parse_context_get_position(gMarkupParseContext, &lineNumber, &charNumber);
238 	}
239 	
240 	/**
241 	 * Retrieves the name of the currently open element.
242 	 * If called from the start_element or end_element handlers this will
243 	 * give the element_name as passed to those functions. For the parent
244 	 * elements, see g_markup_parse_context_get_element_stack().
245 	 * Since 2.2
246 	 * Returns: the name of the currently open element, or NULL
247 	 */
248 	public string getElement()
249 	{
250 		// const gchar * g_markup_parse_context_get_element (GMarkupParseContext *context);
251 		return Str.toString(g_markup_parse_context_get_element(gMarkupParseContext));
252 	}
253 	
254 	/**
255 	 * Retrieves the element stack from the internal state of the parser.
256 	 * The returned GSList is a list of strings where the first item is
257 	 * the currently open tag (as would be returned by
258 	 * g_markup_parse_context_get_element()) and the next item is its
259 	 * immediate parent.
260 	 * This function is intended to be used in the start_element and
261 	 * end_element handlers where g_markup_parse_context_get_element()
262 	 * would merely return the name of the element that is being
263 	 * processed.
264 	 * Since 2.16
265 	 * Returns: the element stack, which must not be modified
266 	 */
267 	public ListSG getElementStack()
268 	{
269 		// const GSList * g_markup_parse_context_get_element_stack  (GMarkupParseContext *context);
270 		auto p = g_markup_parse_context_get_element_stack(gMarkupParseContext);
271 		
272 		if(p is null)
273 		{
274 			return null;
275 		}
276 		
277 		return new ListSG(cast(GSList*) p);
278 	}
279 	
280 	/**
281 	 * Returns the user_data associated with context.
282 	 * This will either be the user_data that was provided to
283 	 * g_markup_parse_context_new() or to the most recent call
284 	 * of g_markup_parse_context_push().
285 	 * Since 2.18
286 	 * Returns: the provided user_data. The returned data belongs to the markup context and will be freed when g_markup_parse_context_free() is called.
287 	 */
288 	public void* getUserData()
289 	{
290 		// gpointer g_markup_parse_context_get_user_data  (GMarkupParseContext *context);
291 		return g_markup_parse_context_get_user_data(gMarkupParseContext);
292 	}
293 	
294 	/**
295 	 * Creates a new parse context. A parse context is used to parse
296 	 * marked-up documents. You can feed any number of documents into
297 	 * a context, as long as no errors occur; once an error occurs,
298 	 * the parse context can't continue to parse text (you have to
299 	 * free it and create a new parse context).
300 	 * Params:
301 	 * parser = a GMarkupParser
302 	 * flags = one or more GMarkupParseFlags
303 	 * userData = user data to pass to GMarkupParser functions
304 	 * userDataDnotify = user data destroy notifier called when
305 	 * the parse context is freed
306 	 * Throws: ConstructionException GTK+ fails to create the object.
307 	 */
308 	public this (GMarkupParser* parser, GMarkupParseFlags flags, void* userData, GDestroyNotify userDataDnotify)
309 	{
310 		// GMarkupParseContext * g_markup_parse_context_new (const GMarkupParser *parser,  GMarkupParseFlags flags,  gpointer user_data,  GDestroyNotify user_data_dnotify);
311 		auto p = g_markup_parse_context_new(parser, flags, userData, userDataDnotify);
312 		if(p is null)
313 		{
314 			throw new ConstructionException("null returned by g_markup_parse_context_new(parser, flags, userData, userDataDnotify)");
315 		}
316 		this(cast(GMarkupParseContext*) p);
317 	}
318 	
319 	/**
320 	 * Feed some data to the GMarkupParseContext.
321 	 * The data need not be valid UTF-8; an error will be signaled if
322 	 * it's invalid. The data need not be an entire document; you can
323 	 * feed a document into the parser incrementally, via multiple calls
324 	 * to this function. Typically, as you receive data from a network
325 	 * connection or file, you feed each received chunk of data into this
326 	 * function, aborting the process if an error occurs. Once an error
327 	 * is reported, no further data may be fed to the GMarkupParseContext;
328 	 * all errors are fatal.
329 	 * Params:
330 	 * text = chunk of text to parse
331 	 * textLen = length of text in bytes
332 	 * Returns: FALSE if an error occurred, TRUE on success
333 	 * Throws: GException on failure.
334 	 */
335 	public int parse(string text, gssize textLen)
336 	{
337 		// gboolean g_markup_parse_context_parse (GMarkupParseContext *context,  const gchar *text,  gssize text_len,  GError **error);
338 		GError* err = null;
339 		
340 		auto p = g_markup_parse_context_parse(gMarkupParseContext, Str.toStringz(text), textLen, &err);
341 		
342 		if (err !is null)
343 		{
344 			throw new GException( new ErrorG(err) );
345 		}
346 		
347 		return p;
348 	}
349 	
350 	/**
351 	 * Temporarily redirects markup data to a sub-parser.
352 	 * This function may only be called from the start_element handler of
353 	 * a GMarkupParser. It must be matched with a corresponding call to
354 	 * g_markup_parse_context_pop() in the matching end_element handler
355 	 * (except in the case that the parser aborts due to an error).
356 	 * All tags, text and other data between the matching tags is
357 	 * redirected to the subparser given by parser. user_data is used
358 	 * as the user_data for that parser. user_data is also passed to the
359 	 * error callback in the event that an error occurs. This includes
360 	 * errors that occur in subparsers of the subparser.
361 	 * The end tag matching the start tag for which this call was made is
362 	 * handled by the previous parser (which is given its own user_data)
363 	 * which is why g_markup_parse_context_pop() is provided to allow "one
364 	 * last access" to the user_data provided to this function. In the
365 	 * case of error, the user_data provided here is passed directly to
366 	 * the error callback of the subparser and g_markup_parse_context_pop()
367 	 * should not be called. In either case, if user_data was allocated
368 	 * then it ought to be freed from both of these locations.
369 	 * This function is not intended to be directly called by users
370 	 * interested in invoking subparsers. Instead, it is intended to be
371 	 * used by the subparsers themselves to implement a higher-level
372 	 * interface.
373 	 * As an example, see the following implementation of a simple
374 	 * parser that counts the number of tags encountered.
375 	 * $(DDOC_COMMENT example)
376 	 * In order to allow this parser to be easily used as a subparser, the
377 	 * Since 2.18
378 	 * Params:
379 	 * parser = a GMarkupParser
380 	 * userData = user data to pass to GMarkupParser functions
381 	 */
382 	public void push(GMarkupParser* parser, void* userData)
383 	{
384 		// void g_markup_parse_context_push (GMarkupParseContext *context,  const GMarkupParser *parser,  gpointer user_data);
385 		g_markup_parse_context_push(gMarkupParseContext, parser, userData);
386 	}
387 	
388 	/**
389 	 * Completes the process of a temporary sub-parser redirection.
390 	 * This function exists to collect the user_data allocated by a
391 	 * matching call to g_markup_parse_context_push(). It must be called
392 	 * in the end_element handler corresponding to the start_element
393 	 * handler during which g_markup_parse_context_push() was called.
394 	 * You must not call this function from the error callback -- the
395 	 * user_data is provided directly to the callback in that case.
396 	 * This function is not intended to be directly called by users
397 	 * interested in invoking subparsers. Instead, it is intended to
398 	 * be used by the subparsers themselves to implement a higher-level
399 	 * interface.
400 	 * Since 2.18
401 	 * Returns: the user data passed to g_markup_parse_context_push()
402 	 */
403 	public void* pop()
404 	{
405 		// gpointer g_markup_parse_context_pop (GMarkupParseContext *context);
406 		return g_markup_parse_context_pop(gMarkupParseContext);
407 	}
408 	
409 	/**
410 	 * Increases the reference count of context.
411 	 * Since 2.36
412 	 * Returns: the same context
413 	 */
414 	public SimpleXML doref()
415 	{
416 		// GMarkupParseContext * g_markup_parse_context_ref (GMarkupParseContext *context);
417 		auto p = g_markup_parse_context_ref(gMarkupParseContext);
418 		
419 		if(p is null)
420 		{
421 			return null;
422 		}
423 		
424 		return new SimpleXML(cast(GMarkupParseContext*) p);
425 	}
426 	
427 	/**
428 	 * Decreases the reference count of context. When its reference count
429 	 * drops to 0, it is freed.
430 	 * Since 2.36
431 	 */
432 	public void unref()
433 	{
434 		// void g_markup_parse_context_unref (GMarkupParseContext *context);
435 		g_markup_parse_context_unref(gMarkupParseContext);
436 	}
437 }