1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* examplec.c
3  *
4  * Copyright (C) 2001 Paolo Casarini <paolo@casarini.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <gdome.h>
25 
isSpaceStr(GdomeDOMString * domstr)26 int isSpaceStr (GdomeDOMString *domstr) {
27 	gchar *str = domstr->str;
28 
29 	for (; *str; str++)
30 		if (!isspace(*str))
31 			return 0;
32 	return 1;
33 }
34 
cleanSubTree(GdomeNode * node)35 void cleanSubTree (GdomeNode *node) {
36 	GdomeNode *child = NULL, *dchild = NULL;
37 	GdomeNodeList *nl = NULL;
38 	GdomeException exc;
39 	GdomeDOMString *str;
40 	long i, nllength;
41 
42 	nl = gdome_n_childNodes (node, &exc);
43 	if ((nllength = gdome_nl_length (nl, &exc)) == 0) {
44 		if (nl != NULL)
45 			gdome_nl_unref (nl, &exc);
46 		return;
47 	}
48 
49 	for (i = nllength-1; i >= 0; i--) {
50 		child = gdome_nl_item (nl, i, &exc);
51 		str = NULL;
52 
53 		if (gdome_n_nodeType (child, &exc) == GDOME_COMMENT_NODE ||
54 				(gdome_n_nodeType (child, &exc) == GDOME_TEXT_NODE &&
55 				 isSpaceStr ((str = gdome_t_data ((GdomeText *)child, &exc))))) {
56 			dchild = gdome_n_removeChild (node, child, &exc);
57 
58 			if (str != NULL)
59 				gdome_str_unref (str);
60 			if (dchild != NULL)
61 				gdome_n_unref (dchild, &exc);
62 		}
63 		else if (gdome_n_hasChildNodes (child, &exc))
64 			cleanSubTree (child);
65 
66 		if (child != NULL)
67 			gdome_n_unref (child, &exc);
68 	}
69 
70 	if (nl != NULL)
71     gdome_nl_unref (nl, &exc);
72 }
73 
main(int argc,char ** argv)74 int main (int argc, char **argv) {
75 	GdomeDOMImplementation *domimpl;
76 	GdomeDocument *doc;
77 	GdomeElement *rootel;
78 	GdomeException exc;
79 
80 	/* First I get a DOMImplementation reference */
81 	domimpl = gdome_di_mkref ();
82 
83 	/* I load a new document from the file name "exampleb.xml */
84 	doc = gdome_di_createDocFromURI(domimpl, LOCALDIR"/examplec.xml", GDOME_LOAD_PARSING, &exc);
85 	if (doc == NULL) {
86 		fprintf (stderr, "DOMImplementation.createDocFromURI: failed\n\tException #%d\n", exc);
87 		return 1;
88 	}
89 
90 	/* I get reference to the root element of the document */
91 	rootel = gdome_doc_documentElement (doc, &exc);
92 	if (rootel == NULL) {
93 		fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
94 		return 1;
95 	}
96 
97   cleanSubTree ((GdomeNode *)rootel);
98   gdome_el_unref (rootel, &exc);
99 
100 	if (!gdome_di_saveDocToFile (domimpl, doc, LOCALDIR"/examplec_out.xml", GDOME_SAVE_STANDARD, &exc)) {
101 		fprintf (stderr, "DOMImplementation.saveDocToFile: failed\n\tException #%d\n", exc);
102 		return 1;
103 	}
104 
105 	/* I free the document structure and the DOMImplementation */
106 	gdome_doc_unref (doc, &exc);
107 	gdome_di_unref (domimpl, &exc);
108 
109 	return 0;
110 }
111