blob: 4dbd06d4dd5f08715c84549db0b9808bc8713dd6 [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
2 * @file xml.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic XML parser routines.
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_XML_H_
16#define LY_XML_H_
17
18#include <stdint.h>
19
20#include "context.h"
21#include "set.h"
22
23struct lyxml_ns {
Radek Krejci4b74d5e2018-09-26 14:30:55 +020024 const char *element; /* element where the namespace is defined */
25 char *prefix; /* prefix of the namespace, NULL for the default namespace */
26 char *uri; /* namespace URI */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020027};
28
29enum LYXML_PARSER_STATUS {
30 LYXML_STATUS_CDSECT, /* CDATA section */
31 LYXML_STATUS_COMMENT, /* XML comment */
32};
33
34struct lyxml_context {
35 struct ly_ctx *ctx;
36 uint64_t line;
Radek Krejci4b74d5e2018-09-26 14:30:55 +020037 struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020038};
39
40#endif /* LY_XML_H_ */
Radek Krejci4b74d5e2018-09-26 14:30:55 +020041
42/**
43 * @brief Parse input expecting an XML element.
44 *
45 * Able to silently skip comments, PIs and CData. DOCTYPE is not parsable, so it is reported as LY_EVALID error.
46 * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue
47 * with parsing input as text content.
48 *
49 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
50 * is returned.
51 *
52 * @param[in] context XML context to track lines or store errors into libyang context.
53 * @param[in,out] input Input string to process, updated according to the processed/read data.
54 * @param[in] options Currently unused options to modify input processing.
55 * @param[out] prefix Pointer to prefix if present in the element name, NULL otherwise.
56 * @param[out] prefix_len Length of the prefix if any.
57 * @param[out] name Element name. LY_SUCCESS can be returned with NULL name only in case the
58 * end of the input string was reached (EOF).
59 * @param[out] name_len Length of the element name.
60 * @return LY_ERR values.
61 */
62LY_ERR lyxml_get_element(struct lyxml_context *context, const char **input,
63 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len);
64
65/**
66 * @brief Parse input expecting an XML attribute (including XML namespace).
67 *
68 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
69 * is returned.
70 *
71 * In case of a namespace definition, prefix just contains xmlns string. In case of the default namespace,
72 * prefix is NULL and the attribute name is xmlns.
73 *
74 * @param[in] context XML context to track lines or store errors into libyang context.
75 * @param[in,out] input Input string to process, updated according to the processed/read data so,
76 * when succeeded, it points to the opening quote of the attribute's value.
77 * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise.
78 * @param[out] prefix_len Length of the prefix if any.
79 * @param[out] name Attribute name. LY_SUCCESS can be returned with NULL name only in case the
80 * end of the element tag was reached.
81 * @param[out] name_len Length of the element name.
82 * @return LY_ERR values.
83 */
84LY_ERR lyxml_get_attribute(struct lyxml_context *context, const char **input,
85 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len);
86
87/**
88 * @brief Parse input as XML text (attribute's values and element's content).
89 *
90 * Mixed content of XML elements is not allowed. Formating whitespaces before child element are ignored,
91 * LY_EINVAL is returned in such a case (buffer is not filled, no error is printed) and input is moved
92 * to the beginning of a child definition.
93 *
94 * In the case of attribute's values, the input string is expected to start on a quotation mark to
95 * select which delimiter (single or double quote) is used. Otherwise, the element content is being
96 * parsed expected to be terminated by '<' character.
97 *
98 * If function succeeds, the string in output buffer is always NULL-terminated.
99 *
100 * @param[in] context XML context to track lines or store errors into libyang context.
101 * @param[in,out] input Input string to process, updated according to the processed/read data.
102 * @param[out] buffer Storage of the output string. If NULL, the buffer is allocated. Otherwise, the buffer
103 * is used and enlarged when necessary.
104 * @param[out] buffer_size Allocated size of the returned buffer. If a buffer is provided by a caller, it
105 * is not being reduced even if the string is shorter. On the other hand, it can be enlarged if needed.
106 * @return LY_ERR value.
107 */
108LY_ERR lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size);
109
110/**
111 * @brief Add namespace definition into XML context.
112 *
113 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
114 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
115 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
116 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
117 *
118 * When leaving processing of a subtree of some element, caller is supposed to call lyxml_ns_rm() to remove all the namespaces
119 * defined in such an element from the context.
120 *
121 * @param[in] context XML context to work with.
122 * @param[in] element_name Pointer to the element name where the namespace is defined. Serve as an identifier to select
123 * which namespaces are supposed to be removed via lyxml_ns_rm() when leaving the element's subtree.
124 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute(). Can be NULL for default namespace.
125 * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute()).
126 * @param[in] uri Namespace URI (value) to store. Value can be obtained via lyxml_get_string() and caller is not supposed to
127 * work with the pointer when the function succeeds.
128 * @return LY_ERR values.
129 */
130LY_ERR lyxml_ns_add(struct lyxml_context *context, const char *element_name, const char *prefix, size_t prefix_len, char *uri);
131
132/**
133 * @brief Get a namespace record for the given prefix in the current context.
134 *
135 * @param[in] context XML context to work with.
136 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute() or lyxml_get_element().
137 * Can be NULL for default namespace.
138 * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute() or
139 * lyxml_get_element()).
140 * @return The namespace record or NULL if the record for the specified prefix not found.
141 */
142const struct lyxml_ns *lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len);
143
144/**
145 * @brief Remove all the namespaces defined in the given element.
146 *
147 * @param[in] context XML context to work with.
148 * @param[in] element_name Pointer to the element name where the namespaces are defined. Serve as an identifier previously provided
149 * by lyxml_get_element()
150 * @return LY_ERR values.
151 */
152LY_ERR lyxml_ns_rm(struct lyxml_context *context, const char *element_name);