1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /* libcroco - Library for parsing and applying CSS
4 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
5 *
6 * This file is not part of the GNU gettext program, but is used with
7 * GNU gettext.
8 *
9 * The original copyright notice is as follows:
10 */
11
12 /*
13 * This file is part of The Croco Library
14 *
15 * Copyright (C) 2003-2004 Dodji Seketeli. All Rights Reserved.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of version 2.1 of the GNU Lesser General Public
19 * License as published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 * USA
30 */
31
32 #include <config.h>
33 #include <string.h>
34 #include "cr-doc-handler.h"
35 #include "cr-parser.h"
36
37 /**
38 *@CRDocHandler:
39 *
40 *The definition of the CRDocHandler class.
41 *Contains methods to instantiate, destroy,
42 *and initialyze instances of #CRDocHandler
43 *to custom values.
44 */
45
46 #define PRIVATE(obj) (obj)->priv
47
48 struct _CRDocHandlerPriv {
49 /**
50 *This pointer is to hold an application parsing context.
51 *For example, it used by the Object Model parser to
52 *store it parsing context. #CRParser does not touch it, but
53 *#CROMParser does. #CROMParser allocates this pointer at
54 *the beginning of the css document, and frees it at the end
55 *of the document.
56 */
57 gpointer context;
58
59 /**
60 *The place where #CROMParser puts the result of its parsing, if
61 *any.
62 */
63 gpointer result;
64 /**
65 *a pointer to the parser used to parse
66 *the current document.
67 */
68 CRParser *parser ;
69 };
70
71 /**
72 * cr_doc_handler_new:
73 *Constructor of #CRDocHandler.
74 *
75 *Returns the newly built instance of
76 *#CRDocHandler
77 *
78 */
79 CRDocHandler *
cr_doc_handler_new(void)80 cr_doc_handler_new (void)
81 {
82 CRDocHandler *result = NULL;
83
84 result = g_try_malloc (sizeof (CRDocHandler));
85
86 g_return_val_if_fail (result, NULL);
87
88 memset (result, 0, sizeof (CRDocHandler));
89 result->ref_count++;
90
91 result->priv = g_try_malloc (sizeof (CRDocHandlerPriv));
92 if (!result->priv) {
93 cr_utils_trace_info ("Out of memory exception");
94 g_free (result);
95 return NULL;
96 }
97
98 cr_doc_handler_set_default_sac_handler (result);
99
100 return result;
101 }
102
103 /**
104 * cr_doc_handler_get_ctxt:
105 *@a_this: the current instance of #CRDocHandler.
106 *@a_ctxt: out parameter. The new parsing context.
107 *
108 *Gets the private parsing context associated to the document handler
109 *The private parsing context is used by libcroco only.
110 *
111 *Returns CR_OK upon successfull completion, an error code otherwise.
112 */
113 enum CRStatus
cr_doc_handler_get_ctxt(CRDocHandler const * a_this,gpointer * a_ctxt)114 cr_doc_handler_get_ctxt (CRDocHandler const * a_this, gpointer * a_ctxt)
115 {
116 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
117
118 *a_ctxt = a_this->priv->context;
119
120 return CR_OK;
121 }
122
123 /**
124 * cr_doc_handler_set_ctxt:
125 *@a_this: the current instance of #CRDocHandler
126 *@a_ctxt: a pointer to the parsing context.
127 *
128 *Sets the private parsing context.
129 *This is used by libcroco only.
130 *Returns CR_OK upon successfull completion, an error code otherwise.
131 */
132 enum CRStatus
cr_doc_handler_set_ctxt(CRDocHandler * a_this,gpointer a_ctxt)133 cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt)
134 {
135 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
136 a_this->priv->context = a_ctxt;
137 return CR_OK;
138 }
139
140 /**
141 * cr_doc_handler_get_result:
142 *@a_this: the current instance of #CRDocHandler
143 *@a_result: out parameter. The returned result.
144 *
145 *Gets the private parsing result.
146 *The private parsing result is used by libcroco only.
147 *
148 *Returns CR_OK upon successfull completion, an error code otherwise.
149 */
150 enum CRStatus
cr_doc_handler_get_result(CRDocHandler const * a_this,gpointer * a_result)151 cr_doc_handler_get_result (CRDocHandler const * a_this, gpointer * a_result)
152 {
153 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
154
155 *a_result = a_this->priv->result;
156
157 return CR_OK;
158 }
159
160 /**
161 * cr_doc_handler_set_result:
162 *@a_this: the current instance of #CRDocHandler
163 *@a_result: the new result.
164 *
165 *Sets the private parsing context.
166 *This is used by libcroco only.
167 *
168 *Returns CR_OK upon successfull completion, an error code otherwise.
169 */
170 enum CRStatus
cr_doc_handler_set_result(CRDocHandler * a_this,gpointer a_result)171 cr_doc_handler_set_result (CRDocHandler * a_this, gpointer a_result)
172 {
173 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
174 a_this->priv->result = a_result;
175 return CR_OK;
176 }
177
178 /**
179 *cr_doc_handler_set_default_sac_handler:
180 *@a_this: a pointer to the current instance of #CRDocHandler.
181 *
182 *Sets the sac handlers contained in the current
183 *instance of DocHandler to the default handlers.
184 *For the time being the default handlers are
185 *test handlers. This is expected to change in a
186 *near future, when the libcroco gets a bit debugged.
187 *
188 *Returns CR_OK upon successfull completion, an error code otherwise.
189 */
190 enum CRStatus
cr_doc_handler_set_default_sac_handler(CRDocHandler * a_this)191 cr_doc_handler_set_default_sac_handler (CRDocHandler * a_this)
192 {
193 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
194
195 a_this->start_document = NULL;
196 a_this->end_document = NULL;
197 a_this->import_style = NULL;
198 a_this->namespace_declaration = NULL;
199 a_this->comment = NULL;
200 a_this->start_selector = NULL;
201 a_this->end_selector = NULL;
202 a_this->property = NULL;
203 a_this->start_font_face = NULL;
204 a_this->end_font_face = NULL;
205 a_this->start_media = NULL;
206 a_this->end_media = NULL;
207 a_this->start_page = NULL;
208 a_this->end_page = NULL;
209 a_this->ignorable_at_rule = NULL;
210 a_this->error = NULL;
211 a_this->unrecoverable_error = NULL;
212 return CR_OK;
213 }
214
215 /**
216 * cr_doc_handler_ref:
217 *@a_this: the current instance of #CRDocHandler.
218 */
219 void
cr_doc_handler_ref(CRDocHandler * a_this)220 cr_doc_handler_ref (CRDocHandler * a_this)
221 {
222 g_return_if_fail (a_this);
223
224 a_this->ref_count++;
225 }
226
227 /**
228 * cr_doc_handler_unref:
229 *@a_this: the currrent instance of #CRDocHandler.
230 *
231 *Decreases the ref count of the current instance of #CRDocHandler.
232 *If the ref count reaches '0' then, destroys the instance.
233 *
234 *Returns TRUE if the instance as been destroyed, FALSE otherwise.
235 */
236 gboolean
cr_doc_handler_unref(CRDocHandler * a_this)237 cr_doc_handler_unref (CRDocHandler * a_this)
238 {
239 g_return_val_if_fail (a_this, FALSE);
240
241 if (a_this->ref_count > 0) {
242 a_this->ref_count--;
243 }
244
245 if (a_this->ref_count == 0) {
246 cr_doc_handler_destroy (a_this);
247 return TRUE;
248 }
249 return FALSE ;
250 }
251
252 /**
253 * cr_doc_handler_destroy:
254 *@a_this: the instance of #CRDocHandler to
255 *destroy.
256 *
257 *The destructor of the #CRDocHandler class.
258 */
259 void
cr_doc_handler_destroy(CRDocHandler * a_this)260 cr_doc_handler_destroy (CRDocHandler * a_this)
261 {
262 g_return_if_fail (a_this);
263
264 if (a_this->priv) {
265 g_free (a_this->priv);
266 a_this->priv = NULL;
267 }
268 g_free (a_this);
269 }
270
271 /**
272 * cr_doc_handler_associate_a_parser:
273 *Associates a parser to the current document handler
274 *
275 *@a_this: the current instance of document handler.
276 *@a_parser: the parser to associate.
277 */
278 void
cr_doc_handler_associate_a_parser(CRDocHandler * a_this,gpointer a_parser)279 cr_doc_handler_associate_a_parser (CRDocHandler *a_this,
280 gpointer a_parser)
281 {
282 g_return_if_fail (a_this && PRIVATE (a_this)
283 && a_parser) ;
284
285 PRIVATE (a_this)->parser = a_parser ;
286 }
287