1 /**
2  *   Copyright (C) 2009  Cedric Tabin
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License along
15  *   with this program; if not, write to the Free Software Foundation, Inc.,
16  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef PRETTY_PRINTER_H
20 #define PRETTY_PRINTER_H
21 
22 /*========================================== INCLUDES ==========================================================*/
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdbool.h>
33 
34 #ifdef HAVE_GLIB
35 #include <glib.h>
36 #endif
37 
38 /*========================================== DEFINES ===========================================================*/
39 
40 #define PRETTY_PRINTER_VERSION "1.3"
41 
42 #define PRETTY_PRINTING_SUCCESS 0
43 #define PRETTY_PRINTING_INVALID_CHAR_ERROR 1
44 #define PRETTY_PRINTING_EMPTY_XML 2
45 #define PRETTY_PRINTING_NOT_SUPPORTED_YET 3
46 #define PRETTY_PRINTING_SYSTEM_ERROR 4
47 
48 #ifndef FALSE
49 #define FALSE (0)
50 #endif
51 
52 #ifndef TRUE
53 #define TRUE !(FALSE)
54 #endif
55 
56 /*========================================== STRUCTURES =======================================================*/
57 
58 /**
59  * The PrettyPrintingOptions struct allows the programmer to tell the
60  * PrettyPrinter how it must format the XML output.
61  */
62 typedef struct
63 {
64       const char* newLineChars;                                                             /* char used to generate a new line (generally \r\n) */
65       char indentChar;                                                                      /* char used for indentation */
66       int indentLength;                                                                     /* number of char to use for indentation (by default 2 spaces) */
67       bool oneLineText;                                                                     /* text is put on one line   */
68       bool inlineText;                                                                      /* if possible text are inline (no return after the opening node and before closing node) */
69       bool oneLineComment;                                                                  /* comments are put on one line */
70       bool inlineComment;                                                                   /* if possible comments are inline (no return after the opening node and before closing node) */
71       bool oneLineCdata;                                                                    /* cdata are put on one line */
72       bool inlineCdata;                                                                     /* if possible cdata are inline (no return after the opening node and before closing node) */
73       bool emptyNodeStripping;                                                              /* the empty nodes such <node></node> are set to <node/> */
74       bool emptyNodeStrippingSpace;                                                         /* put a space before the '/>' when a node is stripped */
75       bool forceEmptyNodeSplit;                                                             /* force an empty node to be splitted : <node /> becomes <node></node> (only if emptyNodeStripping = false) */
76       bool trimLeadingWhites;                                                               /* trim the leading whites in a text node */
77       bool trimTrailingWhites;                                                              /* trim the trailing whites in a text node */
78       bool alignComment;                                                                    /* align the comments. If false, comments are untouched (only if oneLineComment = false) */
79       bool alignText;                                                                       /* align the text in a node. If false, text is untouched (only if oneLineText = false) */
80       bool alignCdata;                                                                      /* align the cdata. If false, cdata is untouched (only if oneLineCdata = false) */
81 }
82 PrettyPrintingOptions;
83 
84 /*========================================== FUNCTIONS =========================================================*/
85 
86 int processXMLPrettyPrinting(const char *xml, int xml_length, char** output, int* output_length, PrettyPrintingOptions* ppOptions); /* process the pretty-printing on a valid xml string (no check done !!!). The ppOptions ARE NOT FREE-ED after processing. The method returns 0 if the pretty-printing has been done. */
87 PrettyPrintingOptions* createDefaultPrettyPrintingOptions(void);                                                                    /* creates a default PrettyPrintingOptions object */
88 
89 #endif
90