* xsd regular expression support for character classes [https://github.com/clicon/clixon/issues/68]

* added support for \c, \d, \w, \W, \s, \S
This commit is contained in:
Olof hagsand 2019-01-17 16:13:34 +01:00
parent 03ac48ab2b
commit 66f80e9304
3 changed files with 83 additions and 19 deletions

View file

@ -134,6 +134,8 @@
* <!DOCTYPE (ie DTD) is not supported. * <!DOCTYPE (ie DTD) is not supported.
### Corrected Bugs ### Corrected Bugs
* xsd regular expression support for character classes [https://github.com/clicon/clixon/issues/68]
* added support for \c, \d, \w, \W, \s, \S.
* Removing newlines from XML data [https://github.com/clicon/clixon/issues/65] * Removing newlines from XML data [https://github.com/clicon/clixon/issues/65]
* [ietf-netconf-notification@2008-07-01.yang validation problem #62](https://github.com/clicon/clixon/issues/62) * [ietf-netconf-notification@2008-07-01.yang validation problem #62](https://github.com/clicon/clixon/issues/62)
* Ignore CR(\r) in yang files for DOS files * Ignore CR(\r) in yang files for DOS files

View file

@ -641,41 +641,68 @@ clixon_trim(char *str)
* POSIX ERE regexps according to man regex(3). * POSIX ERE regexps according to man regex(3).
* @param[in] xsd Input regex string according XSD * @param[in] xsd Input regex string according XSD
* @param[out] posix Output (malloced) string according to POSIX ERE * @param[out] posix Output (malloced) string according to POSIX ERE
* @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028 * @see https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs
* @note that the translation is ad-hoc, may need more translations * @see https://www.regular-expressions.info/posixbrackets.html#class translation
* Translation is not complete but covers some character sequences:
* \d decimal digit
* \w alphanum + underscore
*/ */
int int
regexp_xsd2posix(char *xsd, regexp_xsd2posix(char *xsd,
char **posix) char **posix)
{ {
int retval = -1; int retval = -1;
char *x; cbuf *cb = NULL;
char *p = NULL; char x;
int i; int i;
int len; int esc;
len = strlen(xsd); if ((cb = cbuf_new()) == NULL){
x = xsd; clicon_err(OE_UNIX, errno, "cbuf_new");
while ((x = strstr(x, "\\d")) != NULL){
len += 3; /* \d --> [0-9] */
x += 2;
}
if ((p = malloc(len+1)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done; goto done;
} }
memset(p, 0, len+1); esc=0;
*posix = p;
for (i=0; i<strlen(xsd); i++){ for (i=0; i<strlen(xsd); i++){
if (strncmp(&xsd[i], "\\d", 2) == 0){ x = xsd[i];
strcpy(p, "[0-9]"); if (esc){
p += 5; i++; esc = 0;
switch (x){
case 'c': /* xml namechar */
cprintf(cb, "[0-9a-zA-Z\\\\.\\\\-_:]");
break;
case 'd':
cprintf(cb, "[0-9]");
break;
case 'w':
cprintf(cb, "[0-9a-zA-Z_\\\\-]");
break;
case 'W':
cprintf(cb, "[^0-9a-zA-Z_\\\\-]");
break;
case 's':
cprintf(cb, "[ \t\r\n]");
break;
case 'S':
cprintf(cb, "[^ \t\r\n]");
break;
default:
cprintf(cb, "\\%c", x);
break;
}
} }
else if (x == '\\')
esc++;
else else
*p++ = xsd[i]; cprintf(cb, "%c", x);
}
if ((*posix = strdup(cbuf_get(cb))) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
} }
retval = 0; retval = 0;
done: done:
if (cb)
cbuf_free(cb);
return retval; return retval;
} }

View file

@ -180,6 +180,16 @@ module example{
description "transitive type- exists in ex3"; description "transitive type- exists in ex3";
uses ex2:gr2; uses ex2:gr2;
} }
leaf digit4{
type string {
pattern '\d{4}';
}
}
leaf word4{
type string {
pattern '\w{4}';
}
}
} }
EOF EOF
@ -358,6 +368,31 @@ expectfn "$clixon_cli -1f $cfg -l o -y $fyang set len2 ab" 255 "^$"
new "cli range test len3 42 ok" new "cli range test len3 42 ok"
expectfn "$clixon_cli -1f $cfg -l o -y $fyang set len3 hsakjdhkjsahdkjsahdksahdksajdhsakjhd" 0 "^$" expectfn "$clixon_cli -1f $cfg -l o -y $fyang set len3 hsakjdhkjsahdkjsahdksahdksajdhsakjhd" 0 "^$"
# XSD schema -> POSIX ECE translation
new "cli yang pattern \d ok"
expectfn "$clixon_cli -1f $cfg -l o -y $fyang set digit4 0123" 0 "^$"
new "cli yang pattern \d error"
expectfn "$clixon_cli -1f $cfg -l o -y $fyang set digit4 01b2" 255 "^$"
new "cli yang pattern \w ok"
expectfn "$clixon_cli -1f $cfg -l o -y $fyang set word4 a2-_" 0 "^$"
new "cli yang pattern \w error"
expectfn "$clixon_cli -1f $cfg -l o -y $fyang set word4 ab%d3" 255 "^$"
new "netconf pattern \w"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '<rpc><edit-config><target><candidate/></target><config><word4 xmlns="urn:example:clixon">a-_9</word4></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf pattern \w valid"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf pattern \w error"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '<rpc><edit-config><target><candidate/></target><config><word4 xmlns="urn:example:clixon">ab%d3</word4></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf pattern \w valid"
expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>' '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>word4</bad-element></error-info><error-severity>error</error-severity><error-message>regexp match fail: "ab%d3" does not match \\w{4}</error-message></rpc-error></rpc-reply>]]>]]>$'
if [ $BE -eq 0 ]; then if [ $BE -eq 0 ]; then
exit # BE exit # BE
fi fi