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 = ( CRDocHandler *)g_try_malloc (sizeof (CRDocHandler));
70 g_return_val_if_fail (result, NULL);
72 memset (result, 0, sizeof (CRDocHandler));
74 result->priv = (CRDocHandlerPriv *)g_try_malloc (sizeof (CRDocHandlerPriv));
75 if (!result->priv) {
76 cr_utils_trace_info ("Out of memory exception");
77 g_free (result);
78 return NULL;
79 }
81 cr_doc_handler_set_default_sac_handler (result);
83 return result;
84 }
86 /**
87 *Returns the private parsing context.
88 *The private parsing context is used by libcroco only.
89 *@param a_this the current instance of #CRDocHandler.
90 *@param a_ctxt out parameter. The new parsing context.
91 *@return CR_OK upon successfull completion, an error code otherwise.
92 *@return the parsing context, or NULL if an error occured.
93 */
94 enum CRStatus
95 cr_doc_handler_get_ctxt (CRDocHandler * a_this, gpointer * a_ctxt)
96 {
97 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
99 *a_ctxt = a_this->priv->context;
101 return CR_OK;
102 }
104 /**
105 *Sets the private parsing context.
106 *This is used by libcroco only.
107 *@param a_this the current instance of #CRDocHandler
108 *@param a_ctxt a pointer to the parsing context.
109 *@return CR_OK upon successfull completion, an error code otherwise.
110 */
111 enum CRStatus
112 cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt)
113 {
114 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
115 a_this->priv->context = a_ctxt;
116 return CR_OK;
117 }
119 /**
120 *Returns the private parsing result.
121 *The private parsing result is used by libcroco only.
122 *@param a_this the current instance of #CRDocHandler
123 *@param a_result out parameter. The returned result.
124 *@return CR_OK upon successfull completion, an error code otherwise.
125 */
126 enum CRStatus
127 cr_doc_handler_get_result (CRDocHandler * a_this, gpointer * a_result)
128 {
129 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
131 *a_result = a_this->priv->result;
133 return CR_OK;
134 }
136 /**
137 *Sets the private parsing context.
138 *This is used by libcroco only.
139 *@param a_this the current instance of #CRDocHandler
140 *@param a_result the new result.
141 *@return CR_OK upon successfull completion, an error code otherwise.
142 */
143 enum CRStatus
144 cr_doc_handler_set_result (CRDocHandler * a_this, gpointer a_result)
145 {
146 g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
147 a_this->priv->result = a_result;
148 return CR_OK;
149 }
151 /**
152 *Sets the sac handlers contained in the current
153 *instance of DocHandler to the default handlers.
154 *For the time being the default handlers are
155 *test handlers. This is expected to change in a
156 *near future, when the libcroco gets a bit debugged.
157 *
158 *@param a_this a pointer to the current instance of #CRDocHandler.
159 *@return CR_OK upon successfull completion, an error code otherwise.
160 */
161 enum CRStatus
162 cr_doc_handler_set_default_sac_handler (CRDocHandler * a_this)
163 {
164 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
166 a_this->start_document = NULL;
167 a_this->end_document = NULL;
168 a_this->import_style = NULL;
169 a_this->namespace_declaration = NULL;
170 a_this->comment = NULL;
171 a_this->start_selector = NULL;
172 a_this->end_selector = NULL;
173 a_this->property = NULL;
174 a_this->start_font_face = NULL;
175 a_this->end_font_face = NULL;
176 a_this->start_media = NULL;
177 a_this->end_media = NULL;
178 a_this->start_page = NULL;
179 a_this->end_page = NULL;
180 a_this->ignorable_at_rule = NULL;
181 a_this->error = NULL;
182 a_this->unrecoverable_error = NULL;
183 return CR_OK;
184 }
186 /**
187 *Increases the reference count of the doc handler
188 *@param a_this the current instance of #CRDocHandler.
189 */
190 void
191 cr_doc_handler_ref (CRDocHandler * a_this)
192 {
193 g_return_if_fail (a_this);
195 a_this->ref_count++;
196 }
198 /**
199 *Decreases the ref count of the current instance of #CRDocHandler.
200 *If the ref count reaches '0' then, destroys the instance.
201 *@param a_this the currrent instance of #CRDocHandler.
202 *@return TRUE if the instance as been destroyed, FALSE otherwise.
203 */
204 gboolean
205 cr_doc_handler_unref (CRDocHandler * a_this)
206 {
207 g_return_val_if_fail (a_this, FALSE);
209 if (a_this->ref_count > 0) {
210 a_this->ref_count--;
211 }
213 if (a_this->ref_count == 0) {
214 cr_doc_handler_destroy (a_this);
215 return TRUE;
216 }
217 return FALSE ;
218 }
220 /**
221 *The destructor of the #CRDocHandler class.
222 *@param a_this the instance of #CRDocHandler to
223 *destroy.
224 */
225 void
226 cr_doc_handler_destroy (CRDocHandler * a_this)
227 {
228 g_return_if_fail (a_this);
230 if (a_this->priv) {
231 g_free (a_this->priv);
232 a_this->priv = NULL;
233 }
234 g_free (a_this);
235 }
237 /**
238 *Associates a parser to the current document handler
239 *@param a_this the current instance of document handler.
240 *@param a_parser the parser to associate.
241 */
242 void
243 cr_doc_handler_associate_a_parser (CRDocHandler *a_this,
244 gpointer a_parser)
245 {
246 g_return_if_fail (a_this && PRIVATE (a_this)
247 && a_parser) ;
249 PRIVATE (a_this)->parser = (CRParser *)a_parser ;
250 }