|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @file xml_internal.h
|
|
Packit |
8fb591 |
* @author Radek Krejci <rkrejci@cesnet.cz>
|
|
Packit |
8fb591 |
* @brief Internal part of libyang XML parser
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* Copyright (c) 2015 CESNET, z.s.p.o.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* This source code is licensed under BSD 3-Clause License (the "License").
|
|
Packit |
8fb591 |
* You may not use this file except in compliance with the License.
|
|
Packit |
8fb591 |
* You may obtain a copy of the License at
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* https://opensource.org/licenses/BSD-3-Clause
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
#ifndef LY_XML_INTERNAL_H_
|
|
Packit |
8fb591 |
#define LY_XML_INTERNAL_H_
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
#include <stdio.h>
|
|
Packit |
8fb591 |
#include "xml.h"
|
|
Packit |
8fb591 |
#include "printer.h"
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/*
|
|
Packit |
8fb591 |
* Macro to test if character is #x20 | #x9 | #xA | #xD (whitespace)
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
#define is_xmlnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
|
|
Packit |
8fb591 |
(c >= 'A' && c <= 'Z') || c == ':' || \
|
|
Packit |
8fb591 |
(c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
|
|
Packit |
8fb591 |
(c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
|
|
Packit |
8fb591 |
c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
|
|
Packit |
8fb591 |
(c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
|
|
Packit |
8fb591 |
(c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
|
|
Packit |
8fb591 |
(c >= 0x10000 && c <= 0xeffff))
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
#define is_xmlnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
|
|
Packit |
8fb591 |
(c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ':' || \
|
|
Packit |
8fb591 |
c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
|
|
Packit |
8fb591 |
(c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
|
|
Packit |
8fb591 |
c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
|
|
Packit |
8fb591 |
(c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
|
|
Packit |
8fb591 |
(c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
|
|
Packit |
8fb591 |
(c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
|
|
Packit |
8fb591 |
(c >= 0x10000 && c <= 0xeffff))
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/*
|
|
Packit |
8fb591 |
* Functions
|
|
Packit |
8fb591 |
* Tree Manipulation
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Add a child element into a parent element.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* The child is added as a last child.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* @param[in] ctx libyang context to use.
|
|
Packit |
8fb591 |
* @param[in] parent Element where to add the child.
|
|
Packit |
8fb591 |
* @param[in] child Element to be added as a last child of the parent.
|
|
Packit |
8fb591 |
* @return EXIT_SUCCESS or EXIT_FAILURE
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
int lyxml_add_child(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_elem *child);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/* copy_ns: 0 - set invalid namespaces to NULL, 1 - copy them into this subtree */
|
|
Packit |
8fb591 |
void lyxml_correct_elem_ns(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns, int correct_attrs);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
struct lyxml_elem *lyxml_dup_elem(struct ly_ctx *ctx, struct lyxml_elem *elem,
|
|
Packit |
8fb591 |
struct lyxml_elem *parent, int recursive);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Free attribute. Includes unlinking from an element if the attribute
|
|
Packit |
8fb591 |
* is placed anywhere.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* @param[in] ctx libyang context to use
|
|
Packit |
8fb591 |
* @param[in] parent Parent element where the attribute is placed
|
|
Packit |
8fb591 |
* @param[in] attr Attribute to free.
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
void lyxml_free_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Free (and unlink from their element) all attributes (including
|
|
Packit |
8fb591 |
* namespace definitions) of the specified element.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* @param[in] elem Element to modify.
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
void lyxml_free_attrs(struct ly_ctx *ctx, struct lyxml_elem *elem);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Unlink the attribute from its parent element. In contrast to
|
|
Packit |
8fb591 |
* lyxml_free_attr(), after return the caller can still manipulate with the
|
|
Packit |
8fb591 |
* attr.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* @param[in] attr Attribute to unlink from its parent (if any).
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
void lyxml_unlink_attr(struct lyxml_attr *attr);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Unlink the element from its parent. In contrast to lyxml_free_elem(),
|
|
Packit |
8fb591 |
* after return the caller can still manipulate with the elem.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* @param[in] ctx libyang context to use.
|
|
Packit |
8fb591 |
* @param[in] elem Element to unlink from its parent (if any).
|
|
Packit |
8fb591 |
* @param[in] copy_ns 0 sets NS of \p elem and children that are defined
|
|
Packit |
8fb591 |
* outside \p elem subtree to NULL,
|
|
Packit |
8fb591 |
* 1 corrects NS of \p elem and children that are defined outside \p elem
|
|
Packit |
8fb591 |
* subtree (copy NS and update pointer),
|
|
Packit |
8fb591 |
* 2 skips any NS checking.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
void lyxml_unlink_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Get the first UTF-8 character value (4bytes) from buffer
|
|
Packit |
8fb591 |
* @param[in] ctx Context to store errors in.
|
|
Packit |
8fb591 |
* @param[in] buf Pointer to the current position in input buffer.
|
|
Packit |
8fb591 |
* @param[out] read Number of processed bytes in buf (length of UTF-8
|
|
Packit |
8fb591 |
* character).
|
|
Packit |
8fb591 |
* @return UTF-8 value as 4 byte number. 0 means error, only UTF-8 characters
|
|
Packit |
8fb591 |
* valid for XML are returned, so:
|
|
Packit |
8fb591 |
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
|
Packit |
8fb591 |
* = any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
|
|
Packit |
8fb591 |
*
|
|
Packit |
8fb591 |
* UTF-8 mapping:
|
|
Packit |
8fb591 |
* 00000000 -- 0000007F: 0xxxxxxx
|
|
Packit |
8fb591 |
* 00000080 -- 000007FF: 110xxxxx 10xxxxxx
|
|
Packit |
8fb591 |
* 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
|
|
Packit |
8fb591 |
* 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
int lyxml_getutf8(struct ly_ctx *ctx, const char *buf, unsigned int *read);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Types of the XML data
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
typedef enum lyxml_data_type {
|
|
Packit |
8fb591 |
LYXML_DATA_ATTR = 1, /**< XML attribute data */
|
|
Packit |
8fb591 |
LYXML_DATA_ELEM = 2 /**< XML element data */
|
|
Packit |
8fb591 |
} LYXML_DATA_TYPE;
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
/**
|
|
Packit |
8fb591 |
* @brief Dump XML text. Converts special characters to their equivalent
|
|
Packit |
8fb591 |
* starting with '&'.
|
|
Packit |
8fb591 |
* @param[in] out Output structure.
|
|
Packit |
8fb591 |
* @param[in] text Text to dump.
|
|
Packit |
8fb591 |
* @return Number of dumped characters.
|
|
Packit |
8fb591 |
*/
|
|
Packit |
8fb591 |
int lyxml_dump_text(struct lyout *out, const char *text, LYXML_DATA_TYPE type);
|
|
Packit |
8fb591 |
|
|
Packit |
8fb591 |
#endif /* LY_XML_INTERNAL_H_ */
|