Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of libdom.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
  6.  * Copyright 2007 James Shaw <jshaw@netsurf-browser.org>
  7.  * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
  8.  */
  9.  
  10. #include <assert.h>
  11. #include <stdlib.h>
  12.  
  13. #include "core/string.h"
  14. #include "core/document_type.h"
  15. #include "core/namednodemap.h"
  16. #include "core/node.h"
  17. #include "utils/utils.h"
  18. #include "utils/namespace.h"
  19.  
  20. /**
  21.  * DOM DocumentType node
  22.  */
  23. struct dom_document_type {
  24.         dom_node_internal base;         /**< Base node */
  25.  
  26.         dom_string *public_id;  /**< Doctype public ID */
  27.         dom_string *system_id;  /**< Doctype system ID */
  28. };
  29.  
  30. static struct dom_document_type_vtable document_type_vtable = {
  31.         {
  32.                 {
  33.                         DOM_NODE_EVENT_TARGET_VTABLE
  34.                 },
  35.                 DOM_NODE_VTABLE_DOCUMENT_TYPE
  36.         },
  37.         DOM_DOCUMENT_TYPE_VTABLE
  38. };
  39.  
  40. static struct dom_node_protect_vtable dt_protect_vtable = {
  41.         DOM_DT_PROTECT_VTABLE
  42. };
  43.  
  44.  
  45. /*----------------------------------------------------------------------*/
  46.  
  47. /* Constructors and destructors */
  48.  
  49. /**
  50.  * Create a document type node
  51.  *
  52.  * \param qname      The qualified name of the document type
  53.  * \param public_id  The external subset public identifier
  54.  * \param system_id  The external subset system identifier
  55.  * \param alloc      Memory (de)allocation function
  56.  * \param pw         Pointer to client-specific private data
  57.  * \param doctype    Pointer to location to receive result
  58.  * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion.
  59.  *
  60.  * The doctype will be referenced, so the client need not do so
  61.  * explicitly. The client must unref the doctype once it has
  62.  * finished with it.
  63.  */
  64. dom_exception _dom_document_type_create(dom_string *qname,
  65.                 dom_string *public_id, dom_string *system_id,
  66.                 dom_document_type **doctype)
  67. {
  68.         dom_document_type *result;
  69.         dom_exception err;
  70.  
  71.         /* Create node */
  72.         result = malloc(sizeof(dom_document_type));
  73.         if (result == NULL)
  74.                 return DOM_NO_MEM_ERR;
  75.  
  76.         /* Initialise the vtable */
  77.         result->base.base.vtable = &document_type_vtable;
  78.         result->base.vtable = &dt_protect_vtable;
  79.        
  80.         err = _dom_document_type_initialise(result, qname,
  81.                         public_id, system_id);
  82.         if (err != DOM_NO_ERR) {
  83.                 free(result);
  84.                 return err;
  85.         }
  86.  
  87.         *doctype = result;
  88.  
  89.         return DOM_NO_ERR;
  90. }
  91.  
  92. /**
  93.  * Destroy a DocumentType node
  94.  *
  95.  * \param doctype  The DocumentType node to destroy
  96.  *
  97.  * The contents of ::doctype will be destroyed and ::doctype will be freed.
  98.  */
  99. void _dom_document_type_destroy(dom_node_internal *doctypenode)
  100. {
  101.         dom_document_type *doctype = (dom_document_type *) doctypenode;
  102.  
  103.         /* Finalise base class */
  104.         _dom_document_type_finalise(doctype);
  105.  
  106.         /* Free doctype */
  107.         free(doctype);
  108. }
  109.  
  110. /* Initialise this document_type */
  111. dom_exception _dom_document_type_initialise(dom_document_type *doctype,
  112.                 dom_string *qname, dom_string *public_id,
  113.                 dom_string *system_id)
  114. {
  115.         dom_string *prefix, *localname;
  116.         dom_exception err;
  117.  
  118.         err = _dom_namespace_split_qname(qname, &prefix, &localname);
  119.         if (err != DOM_NO_ERR)
  120.                 return err;
  121.  
  122.         /* TODO: I should figure out how the namespaceURI can be got */
  123.  
  124.         /* Initialise base node */
  125.         err = _dom_node_initialise(&doctype->base, NULL,
  126.                         DOM_DOCUMENT_TYPE_NODE, localname, NULL, NULL, prefix);
  127.         if (err != DOM_NO_ERR) {
  128.                 dom_string_unref(prefix);
  129.                 dom_string_unref(localname);
  130.                 return err;
  131.         }
  132.  
  133.         /* Get public and system IDs */
  134.         if (public_id != NULL)
  135.                 dom_string_ref(public_id);
  136.         doctype->public_id = public_id;
  137.  
  138.         if (system_id != NULL)
  139.                 dom_string_ref(system_id);
  140.         doctype->system_id = system_id;
  141.  
  142.         if (prefix != NULL)
  143.                 dom_string_unref(prefix);
  144.         if (localname != NULL)
  145.                 dom_string_unref(localname);
  146.  
  147.         return DOM_NO_ERR;
  148. }
  149.  
  150. /* The destructor function of dom_document_type */
  151. void _dom_document_type_finalise(dom_document_type *doctype)
  152. {
  153.         if (doctype->public_id != NULL)
  154.                 dom_string_unref(doctype->public_id);
  155.         if (doctype->system_id != NULL)
  156.                 dom_string_unref(doctype->system_id);
  157.        
  158.         assert(doctype->base.owner != NULL || doctype->base.user_data == NULL);
  159.        
  160.         _dom_node_finalise(&doctype->base);
  161. }
  162.  
  163.  
  164. /*----------------------------------------------------------------------*/
  165.  
  166. /* Virtual functions */
  167.  
  168. /**
  169.  * Retrieve a document type's name
  170.  *
  171.  * \param doc_type  Document type to retrieve name from
  172.  * \param result    Pointer to location to receive result
  173.  * \return DOM_NO_ERR.
  174.  *
  175.  * The returned string will have its reference count increased. It is
  176.  * the responsibility of the caller to unref the string once it has
  177.  * finished with it.
  178.  *
  179.  * We don't support this API now, so this function call should always
  180.  * return DOM_NOT_SUPPORTED_ERR.
  181.  */
  182. dom_exception _dom_document_type_get_name(dom_document_type *doc_type,
  183.                 dom_string **result)
  184. {
  185.         return dom_node_get_node_name(doc_type, result);
  186. }
  187.  
  188. /**
  189.  * Retrieve a document type's entities
  190.  *
  191.  * \param doc_type  Document type to retrieve entities from
  192.  * \param result    Pointer to location to receive result
  193.  * \return DOM_NO_ERR.
  194.  *
  195.  * The returned map will have its reference count increased. It is
  196.  * the responsibility of the caller to unref the map once it has
  197.  * finished with it.
  198.  *
  199.  * We don't support this API now, so this function call should always
  200.  * return DOM_NOT_SUPPORTED_ERR.
  201.  */
  202. dom_exception _dom_document_type_get_entities(
  203.                 dom_document_type *doc_type,
  204.                 dom_namednodemap **result)
  205. {
  206.         UNUSED(doc_type);
  207.         UNUSED(result);
  208.  
  209.         return DOM_NOT_SUPPORTED_ERR;
  210. }
  211.  
  212. /**
  213.  * Retrieve a document type's notations
  214.  *
  215.  * \param doc_type  Document type to retrieve notations from
  216.  * \param result    Pointer to location to receive result
  217.  * \return DOM_NO_ERR.
  218.  *
  219.  * The returned map will have its reference count increased. It is
  220.  * the responsibility of the caller to unref the map once it has
  221.  * finished with it.
  222.  *
  223.  * We don't support this API now, so this function call should always
  224.  * return DOM_NOT_SUPPORTED_ERR.
  225.  */
  226. dom_exception _dom_document_type_get_notations(
  227.                 dom_document_type *doc_type,
  228.                 dom_namednodemap **result)
  229. {
  230.         UNUSED(doc_type);
  231.         UNUSED(result);
  232.  
  233.         return DOM_NOT_SUPPORTED_ERR;
  234. }
  235.  
  236. /**
  237.  * Retrieve a document type's public id
  238.  *
  239.  * \param doc_type  Document type to retrieve public id from
  240.  * \param result    Pointer to location to receive result
  241.  * \return DOM_NO_ERR.
  242.  *
  243.  * The returned string will have its reference count increased. It is
  244.  * the responsibility of the caller to unref the string once it has
  245.  * finished with it.
  246.  */
  247. dom_exception _dom_document_type_get_public_id(
  248.                 dom_document_type *doc_type,
  249.                 dom_string **result)
  250. {
  251.         if (doc_type->public_id != NULL)
  252.                 *result = dom_string_ref(doc_type->public_id);
  253.         else
  254.                 *result = NULL;
  255.        
  256.         return DOM_NO_ERR;
  257. }
  258.  
  259. /**
  260.  * Retrieve a document type's system id
  261.  *
  262.  * \param doc_type  Document type to retrieve system id from
  263.  * \param result    Pointer to location to receive result
  264.  * \return DOM_NO_ERR.
  265.  *
  266.  * The returned string will have its reference count increased. It is
  267.  * the responsibility of the caller to unref the string once it has
  268.  * finished with it.
  269.  */
  270. dom_exception _dom_document_type_get_system_id(
  271.                 dom_document_type *doc_type,
  272.                 dom_string **result)
  273. {
  274.         if (doc_type->system_id != NULL)
  275.                 *result = dom_string_ref(doc_type->system_id);
  276.         else
  277.                 *result = NULL;
  278.        
  279.         return DOM_NO_ERR;
  280. }
  281.  
  282. /**
  283.  * Retrieve a document type's internal subset
  284.  *
  285.  * \param doc_type  Document type to retrieve internal subset from
  286.  * \param result    Pointer to location to receive result
  287.  * \return DOM_NO_ERR.
  288.  *
  289.  * The returned string will have its reference count increased. It is
  290.  * the responsibility of the caller to unref the string once it has
  291.  * finished with it.
  292.  *
  293.  * We don't support this API now, so this function call should always
  294.  * return DOM_NOT_SUPPORTED_ERR.
  295.  */
  296. dom_exception _dom_document_type_get_internal_subset(
  297.                 dom_document_type *doc_type,
  298.                 dom_string **result)
  299. {
  300.         UNUSED(doc_type);
  301.         UNUSED(result);
  302.  
  303.         return DOM_NOT_SUPPORTED_ERR;
  304. }
  305.  
  306. dom_exception _dom_document_type_get_text_content(dom_node_internal *node,
  307.                                                   dom_string **result)
  308. {
  309.         UNUSED(node);
  310.        
  311.         *result = NULL;
  312.        
  313.         return DOM_NO_ERR;
  314. }
  315.  
  316. dom_exception _dom_document_type_set_text_content(dom_node_internal *node,
  317.                                                   dom_string *content)
  318. {
  319.         UNUSED(node);
  320.         UNUSED(content);
  321.        
  322.         return DOM_NO_ERR;
  323. }
  324.  
  325. /*-----------------------------------------------------------------------*/
  326.  
  327. /* Overload protected virtual functions */
  328.  
  329. /* The virtual destroy function of this class */
  330. void _dom_dt_destroy(dom_node_internal *node)
  331. {
  332.         _dom_document_type_destroy(node);
  333. }
  334.  
  335. /* The copy constructor of this class */
  336. dom_exception _dom_dt_copy(dom_node_internal *old, dom_node_internal **copy)
  337. {
  338.         UNUSED(old);
  339.         UNUSED(copy);
  340.  
  341.         return DOM_NOT_SUPPORTED_ERR;
  342. }
  343.  
  344.