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