summaryrefslogtreecommitdiffstats
path: root/src/st/croco/cr-doc-handler.c
blob: b0ef13c0726cda836222454dc13373088b9b193a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 * This file is part of The Croco Library
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 * 
 * See COPRYRIGHTS file for copyright information.
 */

#include <string.h>
#include "cr-doc-handler.h"
#include "cr-parser.h"

/**
 *@CRDocHandler:
 *
 *The definition of the CRDocHandler class.
 *Contains methods to instantiate, destroy,
 *and initialize instances of #CRDocHandler
 *to custom values.
 */

#define PRIVATE(obj) (obj)->priv

struct _CRDocHandlerPriv {
	/**
	 *This pointer is to hold an application parsing context.
	 *For example, it used by the Object Model parser to 
	 *store it parsing context. #CRParser does not touch it, but
	 *#CROMParser does. #CROMParser allocates this pointer at
	 *the beginning of the css document, and frees it at the end
	 *of the document.
	 */
        gpointer context;

	/**
	 *The place where #CROMParser puts the result of its parsing, if
	 *any.
	 */
        gpointer result;
	/**
	 *a pointer to the parser used to parse
	 *the current document.
	 */
	CRParser *parser ;
};

/**
 * cr_doc_handler_new:
 *Constructor of #CRDocHandler.
 *
 *Returns the newly built instance of
 *#CRDocHandler
 *
 */
CRDocHandler *
cr_doc_handler_new (void)
{
        CRDocHandler *result = NULL;

        result = g_try_malloc (sizeof (CRDocHandler));

        g_return_val_if_fail (result, NULL);

        memset (result, 0, sizeof (CRDocHandler));
        result->ref_count++;

        result->priv = g_try_malloc (sizeof (CRDocHandlerPriv));
        if (!result->priv) {
                cr_utils_trace_info ("Out of memory exception");
                g_free (result);
                return NULL;
        }

        cr_doc_handler_set_default_sac_handler (result);

        return result;
}

/**
 * cr_doc_handler_get_ctxt:
 *@a_this: the current instance of #CRDocHandler.
 *@a_ctxt: out parameter. The new parsing context.
 *
 *Gets the private parsing context associated to the document handler
 *The private parsing context is used by libcroco only.
 *
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_doc_handler_get_ctxt (CRDocHandler const * a_this, gpointer * a_ctxt)
{
        g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);

        *a_ctxt = a_this->priv->context;

        return CR_OK;
}

/**
 * cr_doc_handler_set_ctxt:
 *@a_this: the current instance of #CRDocHandler
 *@a_ctxt: a pointer to the parsing context.
 *
 *Sets the private parsing context.
 *This is used by libcroco only.
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt)
{
        g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
        a_this->priv->context = a_ctxt;
        return CR_OK;
}

/**
 * cr_doc_handler_get_result:
 *@a_this: the current instance of #CRDocHandler
 *@a_result: out parameter. The returned result.
 *
 *Gets the private parsing result.
 *The private parsing result is used by libcroco only.
 *
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_doc_handler_get_result (CRDocHandler const * a_this, gpointer * a_result)
{
        g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);

        *a_result = a_this->priv->result;

        return CR_OK;
}

/**
 * cr_doc_handler_set_result:
 *@a_this: the current instance of #CRDocHandler
 *@a_result: the new result.
 *
 *Sets the private parsing context.
 *This is used by libcroco only.
 *
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_doc_handler_set_result (CRDocHandler * a_this, gpointer a_result)
{
        g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
        a_this->priv->result = a_result;
        return CR_OK;
}

/**
 *cr_doc_handler_set_default_sac_handler:
 *@a_this: a pointer to the current instance of #CRDocHandler.
 *
 *Sets the sac handlers contained in the current
 *instance of DocHandler to the default handlers.
 *For the time being the default handlers are
 *test handlers. This is expected to change in a
 *near future, when the libcroco gets a bit debugged.
 *
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_doc_handler_set_default_sac_handler (CRDocHandler * a_this)
{
        g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);

        a_this->start_document = NULL;
        a_this->end_document = NULL;
        a_this->import_style = NULL;
        a_this->namespace_declaration = NULL;
        a_this->comment = NULL;
        a_this->start_selector = NULL;
        a_this->end_selector = NULL;
        a_this->property = NULL;
        a_this->start_font_face = NULL;
        a_this->end_font_face = NULL;
        a_this->start_media = NULL;
        a_this->end_media = NULL;
        a_this->start_page = NULL;
        a_this->end_page = NULL;
        a_this->ignorable_at_rule = NULL;
        a_this->error = NULL;
        a_this->unrecoverable_error = NULL;
        return CR_OK;
}

/**
 * cr_doc_handler_ref:
 *@a_this: the current instance of #CRDocHandler.
 */
void
cr_doc_handler_ref (CRDocHandler * a_this)
{
        g_return_if_fail (a_this);

        a_this->ref_count++;
}

/**
 * cr_doc_handler_unref:
 *@a_this: the current instance of #CRDocHandler.
 *
 *Decreases the ref count of the current instance of #CRDocHandler.
 *If the ref count reaches '0' then, destroys the instance.
 *
 *Returns TRUE if the instance as been destroyed, FALSE otherwise.
 */
gboolean
cr_doc_handler_unref (CRDocHandler * a_this)
{
        g_return_val_if_fail (a_this, FALSE);

        if (a_this->ref_count > 0) {
                a_this->ref_count--;
        }

        if (a_this->ref_count == 0) {
                cr_doc_handler_destroy (a_this);
                return TRUE;
        }
        return FALSE ;
}

/**
 * cr_doc_handler_destroy:
 *@a_this: the instance of #CRDocHandler to
 *destroy.
 *
 *The destructor of the #CRDocHandler class.
 */
void
cr_doc_handler_destroy (CRDocHandler * a_this)
{
        g_return_if_fail (a_this);

        if (a_this->priv) {
                g_free (a_this->priv);
                a_this->priv = NULL;
        }
        g_free (a_this);
}

/**
 * cr_doc_handler_associate_a_parser:
 *Associates a parser to the current document handler
 *
 *@a_this: the current instance of document handler.
 *@a_parser: the parser to associate.
 */
void
cr_doc_handler_associate_a_parser (CRDocHandler *a_this,
				   gpointer a_parser)
{
	g_return_if_fail (a_this && PRIVATE (a_this) 
			  && a_parser) ;

	PRIVATE (a_this)->parser = a_parser ;
}