1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* util.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 /* Certo, capisci, tipo "Aspettando Godot", la parabola dell'attesa estraniata dal suo contesto sociale, come metafora della condizione dell'uomo di fronte al tempo in quanto altro da lui. */
22 
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <gdome.h>
26 #include "util.h"
27 
isSpaceStr(GdomeDOMString * domstr)28 int isSpaceStr (GdomeDOMString *domstr) {
29 	gchar *str = domstr->str;
30 
31 	for (; *str; str++)
32 		if (!isspace(*str))
33 			return 0;
34 	return 1;
35 }
36 
cleanSubTree(GdomeNode * node)37 void cleanSubTree (GdomeNode *node) {
38 	GdomeNode *child = NULL, *dchild = NULL;
39 	GdomeNodeList *nl = NULL;
40 	GdomeException exc;
41   GdomeDOMString *str;
42 	long i, nllength;
43 
44 	nl = gdome_n_childNodes (node, &exc);
45 	if ((nllength = gdome_nl_length (nl, &exc)) == 0) {
46     if (nl != NULL)
47 			gdome_nl_unref (nl, &exc);
48 		return;
49 	}
50 
51 	for (i = nllength-1; i >= 0; i--) {
52 		child = gdome_nl_item (nl, i, &exc);
53     str = NULL;
54 
55 		if (gdome_n_nodeType (child, &exc) == GDOME_COMMENT_NODE ||
56 				(gdome_n_nodeType (child, &exc) == GDOME_TEXT_NODE &&
57 				 isSpaceStr ((str = gdome_t_data ((GdomeText *)child, &exc))))) {
58 			dchild = gdome_n_removeChild (node, child, &exc);
59 
60 			if (str != NULL)
61 				gdome_str_unref (str);
62 			if (dchild != NULL)
63 				gdome_n_unref (dchild, &exc);
64 		}
65 		else if (gdome_n_hasChildNodes (child, &exc))
66 			cleanSubTree (child);
67 
68     if (child != NULL)
69 			gdome_n_unref (child, &exc);
70 	}
71 
72 	if (nl != NULL)
73     gdome_nl_unref (nl, &exc);
74 }
75 
getElContent(GdomeElement * el)76 GdomeDOMString *getElContent (GdomeElement *el) {
77   GdomeNode *child;
78   GdomeException exc;
79 
80   g_return_val_if_fail (el != NULL, NULL);
81 
82   child = gdome_el_firstChild (el, &exc);
83 	if (el == NULL)
84     fprintf (stderr, "CHILDNULL");
85   g_return_val_if_fail (child != NULL, NULL);
86 
87   if (gdome_n_nodeType (child, &exc) != GDOME_TEXT_NODE &&
88       gdome_n_nodeType (child, &exc) != GDOME_CDATA_SECTION_NODE) {
89     gdome_n_unref (child, &exc);
90     return NULL;
91   }
92 
93   return gdome_t_data ((GdomeText *)child, &exc);
94 }
95