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.  */
  7.  
  8. #include <string.h>
  9.  
  10. #include <dom/core/implementation.h>
  11.  
  12. #include "core/document.h"
  13. #include "core/document_type.h"
  14.  
  15. #include "html/html_document.h"
  16.  
  17. #include "utils/namespace.h"
  18. #include "utils/utils.h"
  19. #include "utils/validate.h"
  20.  
  21. /**
  22.  * Test whether a DOM implementation implements a specific feature
  23.  * and version
  24.  *
  25.  * \param feature  The feature to test for
  26.  * \param version  The version number of the feature to test for
  27.  * \param result   Pointer to location to receive result
  28.  * \return DOM_NO_ERR.
  29.  */
  30. dom_exception dom_implementation_has_feature(
  31.                 const char *feature, const char *version,
  32.                 bool *result)
  33. {
  34.         UNUSED(feature);
  35.         UNUSED(version);
  36.         UNUSED(result);
  37.  
  38.         return DOM_NOT_SUPPORTED_ERR;
  39. }
  40.  
  41. /**
  42.  * Create a document type node
  43.  *
  44.  * \param qname      The qualified name of the document type
  45.  * \param public_id  The external subset public identifier
  46.  * \param system_id  The external subset system identifier
  47.  * \param doctype    Pointer to location to receive result
  48.  * \return DOM_NO_ERR on success,
  49.  *         DOM_INVALID_CHARACTER_ERR if ::qname is invalid,
  50.  *         DOM_NAMESPACE_ERR         if ::qname is malformed,
  51.  *         DOM_NOT_SUPPORTED_ERR     if ::impl does not support the feature
  52.  *                                   "XML" and the language exposed through
  53.  *                                   Document does not support XML
  54.  *                                   namespaces.
  55.  *
  56.  * The doctype will be referenced, so the client need not do this
  57.  * explicitly. The client must unref the doctype once it has
  58.  * finished with it.
  59.  */
  60. dom_exception dom_implementation_create_document_type(
  61.                 const char *qname, const char *public_id,
  62.                 const char *system_id,
  63.                 struct dom_document_type **doctype)
  64. {
  65.         struct dom_document_type *d;
  66.         dom_string *qname_s = NULL, *prefix = NULL, *lname = NULL;
  67.         dom_string *public_id_s = NULL, *system_id_s = NULL;
  68.         dom_exception err;
  69.  
  70.         if (qname != NULL) {
  71.                 err = dom_string_create((const uint8_t *) qname,
  72.                                 strlen(qname), &qname_s);
  73.                 if (err != DOM_NO_ERR)
  74.                         return err;
  75.         }
  76.  
  77.         err = _dom_namespace_split_qname(qname_s, &prefix, &lname);
  78.         if (err != DOM_NO_ERR) {
  79.                 dom_string_unref(qname_s);
  80.                 return err;
  81.         }
  82.  
  83.         if (public_id != NULL) {
  84.                 err = dom_string_create((const uint8_t *) public_id,
  85.                                 strlen(public_id), &public_id_s);
  86.                 if (err != DOM_NO_ERR) {
  87.                         dom_string_unref(lname);
  88.                         dom_string_unref(prefix);
  89.                         dom_string_unref(qname_s);
  90.                         return err;
  91.                 }
  92.         }
  93.  
  94.         if (system_id != NULL) {
  95.                 err = dom_string_create((const uint8_t *) system_id,
  96.                                 strlen(system_id), &system_id_s);
  97.                 if (err != DOM_NO_ERR) {
  98.                         dom_string_unref(public_id_s);
  99.                         dom_string_unref(lname);
  100.                         dom_string_unref(prefix);
  101.                         dom_string_unref(qname_s);
  102.                         return err;
  103.                 }
  104.         }
  105.  
  106.         /* Create the doctype */
  107.         err = _dom_document_type_create(qname_s, public_id_s, system_id_s, &d);
  108.  
  109.         if (err == DOM_NO_ERR)
  110.                 *doctype = d;
  111.  
  112.         dom_string_unref(system_id_s);
  113.         dom_string_unref(public_id_s);
  114.         dom_string_unref(prefix);
  115.         dom_string_unref(lname);
  116.         dom_string_unref(qname_s);
  117.  
  118.         return err;
  119. }
  120.  
  121. /**
  122.  * Create a document node
  123.  *
  124.  * \param impl_type  The type of document object to create
  125.  * \param namespace  The namespace URI of the document element
  126.  * \param qname      The qualified name of the document element
  127.  * \param doctype    The type of document to create
  128.  * \param doc        Pointer to location to receive result
  129.  * \return DOM_NO_ERR on success,
  130.  *         DOM_INVALID_CHARACTER_ERR if ::qname is invalid,
  131.  *         DOM_NAMESPACE_ERR         if ::qname is malformed, or if ::qname
  132.  *                                   has a prefix and ::namespace is NULL,
  133.  *                                   or if ::qname is NULL and ::namespace
  134.  *                                   is non-NULL, or if ::qname has a prefix
  135.  *                                   "xml" and ::namespace is not
  136.  *                                   "http://www.w3.org/XML/1998/namespace",
  137.  *                                   or if ::impl does not support the "XML"
  138.  *                                   feature and ::namespace is non-NULL,
  139.  *         DOM_WRONG_DOCUMENT_ERR    if ::doctype is already being used by a
  140.  *                                   document, or if it was not created by
  141.  *                                   ::impl,
  142.  *         DOM_NOT_SUPPORTED_ERR     if ::impl does not support the feature
  143.  *                                   "XML" and the language exposed through
  144.  *                                   Document does not support XML
  145.  *                                   namespaces.
  146.  *
  147.  * The document will be referenced, so the client need not do this
  148.  * explicitly. The client must unref the document once it has
  149.  * finished with it.
  150.  */
  151. dom_exception dom_implementation_create_document(
  152.                 uint32_t impl_type,
  153.                 const char *namespace, const char *qname,
  154.                 struct dom_document_type *doctype,
  155.                 dom_events_default_action_fetcher daf,
  156.                 void *daf_ctx,
  157.                 struct dom_document **doc)
  158. {
  159.         struct dom_document *d;
  160.         dom_string *namespace_s = NULL, *qname_s = NULL;
  161.         dom_exception err;
  162.  
  163.         if (namespace != NULL) {
  164.                 err = dom_string_create((const uint8_t *) namespace,
  165.                                 strlen(namespace), &namespace_s);
  166.                 if (err != DOM_NO_ERR)
  167.                         return err;
  168.         }
  169.  
  170.         if (qname != NULL) {
  171.                 err = dom_string_create((const uint8_t *) qname,
  172.                                 strlen(qname), &qname_s);
  173.                 if (err != DOM_NO_ERR) {
  174.                         dom_string_unref(namespace_s);
  175.                         return err;
  176.                 }
  177.         }
  178.  
  179.         if (qname_s != NULL && _dom_validate_name(qname_s) == false) {
  180.                 dom_string_unref(qname_s);
  181.                 dom_string_unref(namespace_s);
  182.                 return DOM_INVALID_CHARACTER_ERR;
  183.         }
  184.  
  185.         err = _dom_namespace_validate_qname(qname_s, namespace_s);
  186.         if (err != DOM_NO_ERR) {
  187.                 dom_string_unref(qname_s);
  188.                 dom_string_unref(namespace_s);
  189.                 return DOM_NAMESPACE_ERR;
  190.         }
  191.  
  192.         if (doctype != NULL && dom_node_get_parent(doctype) != NULL) {
  193.                 dom_string_unref(qname_s);
  194.                 dom_string_unref(namespace_s);
  195.                 return DOM_WRONG_DOCUMENT_ERR;
  196.         }
  197.  
  198.         /* Create document object that reflects the required APIs */
  199.         if (impl_type == DOM_IMPLEMENTATION_HTML) {
  200.                 dom_html_document *html_doc;
  201.  
  202.                 err = _dom_html_document_create(daf, daf_ctx, &html_doc);
  203.  
  204.                 d = (dom_document *) html_doc;
  205.         } else {
  206.                 err = _dom_document_create(daf, daf_ctx, &d);
  207.         }
  208.  
  209.         if (err != DOM_NO_ERR) {
  210.                 dom_string_unref(qname_s);
  211.                 dom_string_unref(namespace_s);
  212.                 return err;
  213.         }
  214.  
  215.         /* Set its doctype, if necessary */
  216.         if (doctype != NULL) {
  217.                 struct dom_node *ins_doctype = NULL;
  218.  
  219.                 err = dom_node_append_child((struct dom_node *) d,
  220.                                 (struct dom_node *) doctype, &ins_doctype);
  221.                 if (err != DOM_NO_ERR) {
  222.                         dom_node_unref((struct dom_node *) d);
  223.                         dom_string_unref(qname_s);
  224.                         dom_string_unref(namespace_s);
  225.                         return err;
  226.                 }
  227.  
  228.                 /* Not interested in inserted doctype */
  229.                 if (ins_doctype != NULL)
  230.                         dom_node_unref(ins_doctype);
  231.         }
  232.  
  233.         /* Create root element and attach it to document */
  234.         if (qname_s != NULL) {
  235.                 struct dom_element *e;
  236.                 struct dom_node *inserted;
  237.  
  238.                 err = dom_document_create_element_ns(d, namespace_s, qname_s, &e);
  239.                 if (err != DOM_NO_ERR) {
  240.                         dom_node_unref((struct dom_node *) d);
  241.                         dom_string_unref(qname_s);
  242.                         dom_string_unref(namespace_s);
  243.                         return err;
  244.                 }
  245.  
  246.                 err = dom_node_append_child((struct dom_node *) d,
  247.                                 (struct dom_node *) e, &inserted);
  248.                 if (err != DOM_NO_ERR) {
  249.                         dom_node_unref((struct dom_node *) e);
  250.                         dom_node_unref((struct dom_node *) d);
  251.                         dom_string_unref(qname_s);
  252.                         dom_string_unref(namespace_s);
  253.                         return err;
  254.                 }
  255.  
  256.                 /* No int32_ter interested in inserted node */
  257.                 dom_node_unref(inserted);
  258.  
  259.                 /* Done with element */
  260.                 dom_node_unref((struct dom_node *) e);
  261.         }
  262.  
  263.         /* Clean up strings we created */
  264.         dom_string_unref(qname_s);
  265.         dom_string_unref(namespace_s);
  266.  
  267.         *doc = d;
  268.  
  269.         return DOM_NO_ERR;
  270. }
  271.  
  272. /**
  273.  * Retrieve a specialized object which implements the specified
  274.  * feature and version
  275.  *
  276.  * \param feature  The requested feature
  277.  * \param version  The version number of the feature
  278.  * \param object   Pointer to location to receive object
  279.  * \return DOM_NO_ERR.
  280.  *
  281.  * Any memory allocated by this call should be allocated using
  282.  * the provided memory (de)allocation function.
  283.  */
  284. dom_exception dom_implementation_get_feature(
  285.                 const char *feature, const char *version,
  286.                 void **object)
  287. {
  288.         UNUSED(feature);
  289.         UNUSED(version);
  290.         UNUSED(object);
  291.  
  292.         return DOM_NOT_SUPPORTED_ERR;
  293. }
  294.