1 /* -*- Mode: C; indent-tabs-mode: ni; c-basic-offset: 8 -*- */
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 */
23 #include <string.h>
24 #include "cr-doc-handler.h"
25 #include "cr-parser.h"
27 /**
28 *@file
29 *The definition of the CRDocHandler class.
30 *Contains methods to instantiate, destroy,
31 *and initialyze instances of #CRDocHandler
32 *to custom values.
33 */
35 #define PRIVATE(obj) (obj)->priv
37 struct _CRDocHandlerPriv {
38 /**
39 *This pointer is to hold an application parsing context.
40 *For example, it used by the Object Model parser to
41 *store it parsing context. #CRParser does not touch it, but
42 *#CROMParser does. #CROMParser allocates this pointer at
43 *the beginning of the css document, and frees it at the end
44 *of the document.
45 */
46 gpointer context;
48 /**
49 *The place where #CROMParser puts the result of its parsing, if
50 *any.
51 */
52 gpointer result;
53 /**
54 *a pointer to the parser used to parse
55 *the current document.
56 */
57 CRParser *parser ;
58 };
60 /**
61 *Constructor of #CRDocHandler.
62 *@return the newly built instance of
63 *#CRDocHandler
64 */
65 CRDocHandler *
66 cr_doc_handler_new (void)
67 {
68 CRDocHandler *result = NULL;
70 result = g_try_malloc (sizeof (CRDocHandler));
72 g_return_val_if_fail (result, NULL);
74 memset (result, 0, sizeof (CRDocHandler));
76 result->priv = g_try_malloc (sizeof (CRDocHandlerPriv));
77 if (!result->priv) {
78 cr_utils_trace_info ("Out of memory exception");
79 g_free (result);
80 return NULL;
81 }
83 cr_doc_handler_set_default_sac_handler (result);
85 return result;
86 }
88 /**
89 *Returns the private parsing context.
90 *The private parsing context is used by libcroco only.
91 *@param a_this the current instance of #CRDocHandler.
92 *@param a_ctxt out parameter. The new parsing context.
93 *@return CR_OK upon successfull completion, an error code otherwise.
94 *@return the parsing context, or NULL if an error occured.
95 */
96 enum CRStatus
97 cr_doc_handler_get_ctxt (CRDocHandler * a_this, gpointer * a_ctxt)
98 {
99 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
101 *a_ctxt = a_this->priv->context;
103 return CR_OK;
104 }
106 /**
107 *Sets the private parsing context.
108 *This is used by libcroco only.
109 *@param a_this the current instance of #CRDocHandler
110 *@param a_ctxt a pointer to the parsing context.
111 *@return CR_OK upon successfull completion, an error code otherwise.
112 */
113 enum CRStatus
114 cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt)
115 {
116 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
117 a_this->priv->context = a_ctxt;
118 return CR_OK;
119 }
121 /**
122 *Returns the private parsing result.
123 *The private parsing result is used by libcroco only.
124 *@param a_this the current instance of #CRDocHandler
125 *@param a_result out parameter. The returned result.
126 *@return CR_OK upon successfull completion, an error code otherwise.
127 */
128 enum CRStatus
129 cr_doc_handler_get_result (CRDocHandler * a_this, gpointer * a_result)
130 {
131 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
133 *a_result = a_this->priv->result;
135 return CR_OK;
136 }
138 /**
139 *Sets the private parsing context.
140 *This is used by libcroco only.
141 *@param a_this the current instance of #CRDocHandler
142 *@param a_result the new result.
143 *@return CR_OK upon successfull completion, an error code otherwise.
144 */
145 enum CRStatus
146 cr_doc_handler_set_result (CRDocHandler * a_this, gpointer a_result)
147 {
148 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
149 a_this->priv->result = a_result;
150 return CR_OK;
151 }
153 /**
154 *Sets the sac handlers contained in the current
155 *instance of DocHandler to the default handlers.
156 *For the time being the default handlers are
157 *test handlers. This is expected to change in a
158 *near future, when the libcroco gets a bit debugged.
159 *
160 *@param a_this a pointer to the current instance of #CRDocHandler.
161 *@return CR_OK upon successfull completion, an error code otherwise.
162 */
163 enum CRStatus
164 cr_doc_handler_set_default_sac_handler (CRDocHandler * a_this)
165 {
166 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
168 a_this->start_document = NULL;
169 a_this->end_document = NULL;
170 a_this->import_style = NULL;
171 a_this->namespace_declaration = NULL;
172 a_this->comment = NULL;
173 a_this->start_selector = NULL;
174 a_this->end_selector = NULL;
175 a_this->property = NULL;
176 a_this->start_font_face = NULL;
177 a_this->end_font_face = NULL;
178 a_this->start_media = NULL;
179 a_this->end_media = NULL;
180 a_this->start_page = NULL;
181 a_this->end_page = NULL;
182 a_this->ignorable_at_rule = NULL;
183 a_this->error = NULL;
184 a_this->unrecoverable_error = NULL;
185 return CR_OK;
186 }
188 /**
189 *Increases the reference count of the doc handler
190 *@param a_this the current instance of #CRDocHandler.
191 */
192 void
193 cr_doc_handler_ref (CRDocHandler * a_this)
194 {
195 g_return_if_fail (a_this);
197 a_this->ref_count++;
198 }
200 /**
201 *Decreases the ref count of the current instance of #CRDocHandler.
202 *If the ref count reaches '0' then, destroys the instance.
203 *@param a_this the currrent instance of #CRDocHandler.
204 *@return TRUE if the instance as been destroyed, FALSE otherwise.
205 */
206 gboolean
207 cr_doc_handler_unref (CRDocHandler * a_this)
208 {
209 g_return_val_if_fail (a_this, FALSE);
211 if (a_this->ref_count > 0) {
212 a_this->ref_count--;
213 }
215 if (a_this->ref_count == 0) {
216 cr_doc_handler_destroy (a_this);
217 return TRUE;
218 }
219 return FALSE ;
220 }
222 /**
223 *The destructor of the #CRDocHandler class.
224 *@param a_this the instance of #CRDocHandler to
225 *destroy.
226 */
227 void
228 cr_doc_handler_destroy (CRDocHandler * a_this)
229 {
230 g_return_if_fail (a_this);
232 if (a_this->priv) {
233 g_free (a_this->priv);
234 a_this->priv = NULL;
235 }
236 g_free (a_this);
237 }
239 /**
240 *Associates a parser to the current document handler
241 *@param a_this the current instance of document handler.
242 *@param a_parser the parser to associate.
243 */
244 void
245 cr_doc_handler_associate_a_parser (CRDocHandler *a_this,
246 gpointer a_parser)
247 {
248 g_return_if_fail (a_this && PRIVATE (a_this)
249 && a_parser) ;
251 PRIVATE (a_this)->parser = a_parser ;
252 }