* Optimized validation of large lists
* New xmldb_get1() returning actual cache - not a copy. This has lead to some householding instead of just deleting the copy * xml_diff rewritten to work linearly instead of O(2) * New xml_insert function using tree search. The new code uses this in insertion xmldb_put and defaults. (Note previous xml_insert renamed to xml_wrap_all)
This commit is contained in:
parent
9b9b53c4ee
commit
c79baf1b1f
16 changed files with 937 additions and 301 deletions
215
util/clixon_util_insert.c
Normal file
215
util/clixon_util_insert.c
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
*
|
||||
***** BEGIN LICENSE BLOCK *****
|
||||
|
||||
Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren
|
||||
|
||||
This file is part of CLIXON.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of
|
||||
the GNU General Public License Version 3 or later (the "GPL"),
|
||||
in which case the provisions of the GPL are applicable instead
|
||||
of those above. If you wish to allow use of your version of this file only
|
||||
under the terms of the GPL, and not to allow others to
|
||||
use your version of this file under the terms of Apache License version 2,
|
||||
indicate your decision by deleting the provisions above and replace them with
|
||||
the notice and other provisions required by the GPL. If you do not delete
|
||||
the provisions above, a recipient may use your version of this file under
|
||||
the terms of any one of the Apache License version 2 or the GPL.
|
||||
|
||||
***** END LICENSE BLOCK *****
|
||||
|
||||
See https://www.w3.org/TR/xpath/
|
||||
|
||||
* Turn this on to get an xpath test program
|
||||
* Usage: xpath [<xpath>]
|
||||
* read xpath on first line and xml on rest of lines from input
|
||||
* Example compile:
|
||||
gcc -g -o xpath -I. -I../clixon ./clixon_xsl.c -lclixon -lcligen
|
||||
* Example run:
|
||||
echo "a\n<a><b/></a>" | xpath
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "clixon_config.h" /* generated by config & autoconf */
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <fnmatch.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <syslog.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/* cligen */
|
||||
#include <cligen/cligen.h>
|
||||
|
||||
/* clixon */
|
||||
#include "clixon/clixon.h"
|
||||
|
||||
static int
|
||||
usage(char *argv0)
|
||||
{
|
||||
fprintf(stderr, "usage:%s [options]\n"
|
||||
"where options are\n"
|
||||
"\t-h \t\tHelp\n"
|
||||
"\t-D <level>\tDebug\n"
|
||||
"\t-y <file> \tYANG spec file (or stdin)\n"
|
||||
"\t-b <base> \tXML base expression\n"
|
||||
"\t-x <xml> \tXML to insert\n"
|
||||
"\t-p <xpath>\tXpath to where in base and XML\n"
|
||||
"\t-s \tSort output after insert\n"
|
||||
"Assume insert xml is first child of xpath. Ie if xml=<a><x>23 and xpath=a, then inserted element is <x>23\n",
|
||||
argv0
|
||||
);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int retval = -1;
|
||||
char *argv0 = argv[0];
|
||||
int c;
|
||||
char *filename = NULL;
|
||||
int fd = 0; /* unless overriden by argv[1] */
|
||||
char *x0str = NULL;
|
||||
char *xistr = NULL;
|
||||
char *xpath = NULL;
|
||||
yang_stmt *yspec;
|
||||
cxobj *x0 = NULL;
|
||||
cxobj *xb;
|
||||
cxobj *xi = NULL;
|
||||
int sort = 0;
|
||||
clicon_handle h;
|
||||
|
||||
clicon_log_init("clixon_insert", LOG_DEBUG, CLICON_LOG_STDERR);
|
||||
if ((h = clicon_handle_init()) == NULL)
|
||||
goto done;
|
||||
optind = 1;
|
||||
opterr = 0;
|
||||
while ((c = getopt(argc, argv, "hD:y:b:x:p:s")) != -1)
|
||||
switch (c) {
|
||||
case 'h':
|
||||
usage(argv0);
|
||||
break;
|
||||
case 'D':
|
||||
if (sscanf(optarg, "%d", &debug) != 1)
|
||||
usage(argv0);
|
||||
break;
|
||||
case 'y': /* YANG spec file */
|
||||
filename = optarg;
|
||||
if (0 && (fd = open(filename, O_RDONLY)) < 0){
|
||||
clicon_err(OE_UNIX, errno, "open(%s)", argv[1]);
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case 'b': /* Base XML expression */
|
||||
x0str = optarg;
|
||||
break;
|
||||
case 'x': /* XML to insert */
|
||||
xistr = optarg;
|
||||
break;
|
||||
case 'p': /* XPATH base */
|
||||
xpath = optarg;
|
||||
break;
|
||||
case 's': /* sort output after insert */
|
||||
sort++;
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
break;
|
||||
}
|
||||
if (xistr == NULL || x0str == NULL)
|
||||
usage(argv0);
|
||||
if (xpath == NULL)
|
||||
usage(argv0);
|
||||
if (filename == NULL)
|
||||
usage(argv0);
|
||||
clicon_debug(1, "xistr:%s", xistr);
|
||||
clicon_debug(1, "x0str:%s", x0str);
|
||||
clicon_debug(1, "xpath:%s", xpath);
|
||||
if ((yspec = yspec_new()) == NULL)
|
||||
goto done;
|
||||
#if 1
|
||||
if (yang_spec_parse_file(h, filename, yspec) < 0)
|
||||
goto done;
|
||||
#else
|
||||
if (yang_parse_file(fd, "yang test", yspec) == NULL)
|
||||
goto done;
|
||||
#endif
|
||||
/* Parse base XML */
|
||||
if (xml_parse_string(x0str, yspec, &x0) < 0){
|
||||
clicon_err(OE_XML, 0, "Parsing base xml: %s", x0str);
|
||||
goto done;
|
||||
}
|
||||
if (xml_apply(x0, CX_ELMNT, xml_spec_populate, yspec) < 0)
|
||||
goto done;
|
||||
if ((xb = xpath_first(x0, "%s", xpath)) == NULL){
|
||||
clicon_err(OE_XML, 0, "xpath: %s not found in x0", xpath);
|
||||
goto done;
|
||||
}
|
||||
if (debug){
|
||||
clicon_debug(1, "xb:");
|
||||
xml_print(stderr, xb);
|
||||
}
|
||||
/* Parse insert XML */
|
||||
if (xml_parse_string(xistr, yspec, &xi) < 0){
|
||||
clicon_err(OE_XML, 0, "Parsing insert xml: %s", xistr);
|
||||
goto done;
|
||||
}
|
||||
if (xml_apply(xi, CX_ELMNT, xml_spec_populate, yspec) < 0)
|
||||
goto done;
|
||||
if ((xi = xpath_first(xi, "%s", xpath)) == NULL){
|
||||
clicon_err(OE_XML, 0, "xpath: %s not found in xi", xpath);
|
||||
goto done;
|
||||
}
|
||||
/* Find first element child */
|
||||
if ((xi = xml_child_i_type(xi, 0, CX_ELMNT)) == NULL){
|
||||
clicon_err(OE_XML, 0, "xi has no element child");
|
||||
goto done;
|
||||
}
|
||||
/* Remove it from parent */
|
||||
if (xml_rm(xi) < 0)
|
||||
goto done;
|
||||
if (debug){
|
||||
clicon_debug(1, "xi:");
|
||||
xml_print(stderr, xi);
|
||||
}
|
||||
if (xml_insert(xb, xi) < 0)
|
||||
goto done;
|
||||
if (debug){
|
||||
clicon_debug(1, "x0:");
|
||||
xml_print(stderr, x0);
|
||||
}
|
||||
if (sort)
|
||||
xml_sort(xb, NULL);
|
||||
clicon_xml2file(stdout, xb, 0, 0);
|
||||
|
||||
retval = 0;
|
||||
done:
|
||||
if (x0)
|
||||
xml_free(x0);
|
||||
if (yspec)
|
||||
yspec_free(yspec);
|
||||
if (fd > 0)
|
||||
close(fd);
|
||||
return retval;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue