Libcroco
cr-doc-handler.c
Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of version 2.1 of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00018  * USA
00019  * 
00020  * See COPRYRIGHTS file for copyright information.
00021  */
00022 
00023 #include <string.h>
00024 #include "cr-doc-handler.h"
00025 #include "cr-parser.h"
00026 
00027 /**
00028  *@CRDocHandler:
00029  *
00030  *The definition of the CRDocHandler class.
00031  *Contains methods to instantiate, destroy,
00032  *and initialyze instances of #CRDocHandler
00033  *to custom values.
00034  */
00035 
00036 #define PRIVATE(obj) (obj)->priv
00037 
00038 struct _CRDocHandlerPriv {
00039         /**
00040          *This pointer is to hold an application parsing context.
00041          *For example, it used by the Object Model parser to 
00042          *store it parsing context. #CRParser does not touch it, but
00043          *#CROMParser does. #CROMParser allocates this pointer at
00044          *the beginning of the css document, and frees it at the end
00045          *of the document.
00046          */
00047         gpointer context;
00048 
00049         /**
00050          *The place where #CROMParser puts the result of its parsing, if
00051          *any.
00052          */
00053         gpointer result;
00054         /**
00055          *a pointer to the parser used to parse
00056          *the current document.
00057          */
00058         CRParser *parser ;
00059 };
00060 
00061 /**
00062  * cr_doc_handler_new:
00063  *Constructor of #CRDocHandler.
00064  *
00065  *Returns the newly built instance of
00066  *#CRDocHandler
00067  *
00068  */
00069 CRDocHandler *
00070 cr_doc_handler_new (void)
00071 {
00072         CRDocHandler *result = NULL;
00073 
00074         result = g_try_malloc (sizeof (CRDocHandler));
00075 
00076         g_return_val_if_fail (result, NULL);
00077 
00078         memset (result, 0, sizeof (CRDocHandler));
00079         result->ref_count++;
00080 
00081         result->priv = g_try_malloc (sizeof (CRDocHandlerPriv));
00082         if (!result->priv) {
00083                 cr_utils_trace_info ("Out of memory exception");
00084                 g_free (result);
00085                 return NULL;
00086         }
00087 
00088         cr_doc_handler_set_default_sac_handler (result);
00089 
00090         return result;
00091 }
00092 
00093 /**
00094  * cr_doc_handler_get_ctxt:
00095  *@a_this: the current instance of #CRDocHandler.
00096  *@a_ctxt: out parameter. The new parsing context.
00097  *
00098  *Gets the private parsing context associated to the document handler
00099  *The private parsing context is used by libcroco only.
00100  *
00101  *Returns CR_OK upon successfull completion, an error code otherwise.
00102  */
00103 enum CRStatus
00104 cr_doc_handler_get_ctxt (CRDocHandler const * a_this, gpointer * a_ctxt)
00105 {
00106         g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
00107 
00108         *a_ctxt = a_this->priv->context;
00109 
00110         return CR_OK;
00111 }
00112 
00113 /**
00114  * cr_doc_handler_set_ctxt:
00115  *@a_this: the current instance of #CRDocHandler
00116  *@a_ctxt: a pointer to the parsing context.
00117  *
00118  *Sets the private parsing context.
00119  *This is used by libcroco only.
00120  *Returns CR_OK upon successfull completion, an error code otherwise.
00121  */
00122 enum CRStatus
00123 cr_doc_handler_set_ctxt (CRDocHandler * a_this, gpointer a_ctxt)
00124 {
00125         g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
00126         a_this->priv->context = a_ctxt;
00127         return CR_OK;
00128 }
00129 
00130 /**
00131  * cr_doc_handler_get_result:
00132  *@a_this: the current instance of #CRDocHandler
00133  *@a_result: out parameter. The returned result.
00134  *
00135  *Gets the private parsing result.
00136  *The private parsing result is used by libcroco only.
00137  *
00138  *Returns CR_OK upon successfull completion, an error code otherwise.
00139  */
00140 enum CRStatus
00141 cr_doc_handler_get_result (CRDocHandler const * a_this, gpointer * a_result)
00142 {
00143         g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
00144 
00145         *a_result = a_this->priv->result;
00146 
00147         return CR_OK;
00148 }
00149 
00150 /**
00151  * cr_doc_handler_set_result:
00152  *@a_this: the current instance of #CRDocHandler
00153  *@a_result: the new result.
00154  *
00155  *Sets the private parsing context.
00156  *This is used by libcroco only.
00157  *
00158  *Returns CR_OK upon successfull completion, an error code otherwise.
00159  */
00160 enum CRStatus
00161 cr_doc_handler_set_result (CRDocHandler * a_this, gpointer a_result)
00162 {
00163         g_return_val_if_fail (a_this && a_this->priv, CR_BAD_PARAM_ERROR);
00164         a_this->priv->result = a_result;
00165         return CR_OK;
00166 }
00167 
00168 /**
00169  *cr_doc_handler_set_default_sac_handler:
00170  *@a_this: a pointer to the current instance of #CRDocHandler.
00171  *
00172  *Sets the sac handlers contained in the current
00173  *instance of DocHandler to the default handlers.
00174  *For the time being the default handlers are
00175  *test handlers. This is expected to change in a
00176  *near future, when the libcroco gets a bit debugged.
00177  *
00178  *Returns CR_OK upon successfull completion, an error code otherwise.
00179  */
00180 enum CRStatus
00181 cr_doc_handler_set_default_sac_handler (CRDocHandler * a_this)
00182 {
00183         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00184 
00185         a_this->start_document = NULL;
00186         a_this->end_document = NULL;
00187         a_this->import_style = NULL;
00188         a_this->namespace_declaration = NULL;
00189         a_this->comment = NULL;
00190         a_this->start_selector = NULL;
00191         a_this->end_selector = NULL;
00192         a_this->property = NULL;
00193         a_this->start_font_face = NULL;
00194         a_this->end_font_face = NULL;
00195         a_this->start_media = NULL;
00196         a_this->end_media = NULL;
00197         a_this->start_page = NULL;
00198         a_this->end_page = NULL;
00199         a_this->ignorable_at_rule = NULL;
00200         a_this->error = NULL;
00201         a_this->unrecoverable_error = NULL;
00202         return CR_OK;
00203 }
00204 
00205 /**
00206  * cr_doc_handler_ref:
00207  *@a_this: the current instance of #CRDocHandler.
00208  */
00209 void
00210 cr_doc_handler_ref (CRDocHandler * a_this)
00211 {
00212         g_return_if_fail (a_this);
00213 
00214         a_this->ref_count++;
00215 }
00216 
00217 /**
00218  * cr_doc_handler_unref:
00219  *@a_this: the currrent instance of #CRDocHandler.
00220  *
00221  *Decreases the ref count of the current instance of #CRDocHandler.
00222  *If the ref count reaches '0' then, destroys the instance.
00223  *
00224  *Returns TRUE if the instance as been destroyed, FALSE otherwise.
00225  */
00226 gboolean
00227 cr_doc_handler_unref (CRDocHandler * a_this)
00228 {
00229         g_return_val_if_fail (a_this, FALSE);
00230 
00231         if (a_this->ref_count > 0) {
00232                 a_this->ref_count--;
00233         }
00234 
00235         if (a_this->ref_count == 0) {
00236                 cr_doc_handler_destroy (a_this);
00237                 return TRUE;
00238         }
00239         return FALSE ;
00240 }
00241 
00242 /**
00243  * cr_doc_handler_destroy:
00244  *@a_this: the instance of #CRDocHandler to
00245  *destroy.
00246  *
00247  *The destructor of the #CRDocHandler class.
00248  */
00249 void
00250 cr_doc_handler_destroy (CRDocHandler * a_this)
00251 {
00252         g_return_if_fail (a_this);
00253 
00254         if (a_this->priv) {
00255                 g_free (a_this->priv);
00256                 a_this->priv = NULL;
00257         }
00258         g_free (a_this);
00259 }
00260 
00261 /**
00262  * cr_doc_handler_associate_a_parser:
00263  *Associates a parser to the current document handler
00264  *
00265  *@a_this: the current instance of document handler.
00266  *@a_parser: the parser to associate.
00267  */
00268 void
00269 cr_doc_handler_associate_a_parser (CRDocHandler *a_this,
00270                                    gpointer a_parser)
00271 {
00272         g_return_if_fail (a_this && PRIVATE (a_this) 
00273                           && a_parser) ;
00274 
00275         PRIVATE (a_this)->parser = a_parser ;
00276 }