1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
/* Copyright (c) MediaArea.net SARL. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license that can
* be found in the License.html file in the root of the source tree.
*/
//---------------------------------------------------------------------------
#ifdef __BORLANDC__
#pragma hdrstop
#endif
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include "Policies.h"
#include "Policy.h"
#include "XsltPolicy.h"
#include "Xslt.h"
#include <iostream>
#include <sstream>
#include <string.h>
#include "generated/PolicyTransformXml.h"
//---------------------------------------------------------------------------
namespace MediaConch {
//***************************************************************************
// XsltPolicyNode
//***************************************************************************
//---------------------------------------------------------------------------
XsltPolicyNode::XsltPolicyNode() : parent_id((size_t)-1)
{
}
//---------------------------------------------------------------------------
XsltPolicyNode::~XsltPolicyNode()
{
}
//---------------------------------------------------------------------------
XsltPolicyNode::XsltPolicyNode(const XsltPolicyNode* n)
{
if (n == this)
return;
this->kind = n->kind;
this->parent_id = n->parent_id;
this->node_name = n->node_name;
}
//---------------------------------------------------------------------------
XsltPolicyNode::XsltPolicyNode(const XsltPolicyNode& n)
{
if (&n == this)
return;
this->kind = n.kind;
this->parent_id = n.parent_id;
this->node_name = n.node_name;
}
//***************************************************************************
// XsltPolicyRule
//***************************************************************************
size_t XsltPolicyRule::rule_id = 0;
//---------------------------------------------------------------------------
XsltPolicyRule::XsltPolicyRule() : XsltPolicyNode()
{
kind = XSLT_POLICY_RULE;
source = NULL;
}
//---------------------------------------------------------------------------
XsltPolicyRule::~XsltPolicyRule()
{
if (source)
delete source;
}
//---------------------------------------------------------------------------
XsltPolicyRule::XsltPolicyRule(const XsltPolicyRule* r) : XsltPolicyNode(r)
{
if (r == this)
return;
this->id = rule_id++;
this->ope = r->ope;
this->track_type = r->track_type;
this->field = r->field;
this->scope = r->scope;
this->level = r->level;
this->occurrence = r->occurrence;
this->value = r->value;
if (r->source)
{
if (this->source)
delete this->source;
this->source = new Source;
this->source->track_type = r->source->track_type;
this->source->field = r->source->field;
this->source->scope = r->source->scope;
this->source->occurrence = r->source->occurrence;
}
else
this->source = NULL;
}
//---------------------------------------------------------------------------
XsltPolicyRule::XsltPolicyRule(const XsltPolicyRule& r) : XsltPolicyNode(r)
{
if (&r == this)
return;
this->id = r.id;
this->ope = r.ope;
this->track_type = r.track_type;
this->field = r.field;
this->scope = r.scope;
this->level = r.level;
this->occurrence = r.occurrence;
this->value = r.value;
if (r.source)
{
if (this->source)
delete this->source;
this->source = new Source;
this->source->track_type = r.source->track_type;
this->source->field = r.source->field;
this->source->scope = r.source->scope;
this->source->occurrence = r.source->occurrence;
}
else
this->source = NULL;
}
//---------------------------------------------------------------------------
int XsltPolicyRule::edit_policy_rule(const XsltPolicyRule* rule, std::string&)
{
this->node_name = rule->node_name;
this->ope = rule->ope;
this->track_type = rule->track_type;
this->field = rule->field;
this->scope = rule->scope;
this->level = rule->level;
this->occurrence = rule->occurrence;
this->value = rule->value;
if (rule->source)
{
if (this->source)
delete this->source;
this->source = new Source;
this->source->track_type = rule->source->track_type;
this->source->field = rule->source->field;
this->source->scope = rule->source->scope;
this->source->occurrence = rule->source->occurrence;
}
else
this->source = NULL;
return 0;
}
void XsltPolicyRule::create_default_name(std::string& name)
{
if (scope.size())
name = scope + " " + field;
else
name = track_type + "/" + field;
if (ope == "=")
name += " is ";
else if (ope == "!=")
name += " is not ";
else if (ope == ">")
name += " is greater than ";
else if (ope == "<")
name += " is less than ";
else if (ope == ">=")
name += " is greater or equal than ";
else if (ope == "<=")
name += " is less or equal than ";
else if (ope == "")
name += " exists";
else
name += " " + ope + " ";
name += value;
}
//***************************************************************************
// XsltPolicy
//***************************************************************************
//---------------------------------------------------------------------------
XsltPolicy::XsltPolicy(Policies *p, bool no_https) : Policy(p, Policies::POLICY_XSLT, no_https), XsltPolicyNode()
{
kind = XSLT_POLICY_POLICY;
}
//---------------------------------------------------------------------------
XsltPolicy::XsltPolicy(const XsltPolicy* s) : Policy(s), XsltPolicyNode(s)
{
type = Policies::POLICY_XSLT;
this->ope = s->ope;
this->name = this->node_name;
for (size_t i = 0; i < s->nodes.size(); ++i)
{
if (!s->nodes[i])
continue;
XsltPolicyNode *node = NULL;
if (s->nodes[i]->kind == XSLT_POLICY_POLICY)
node = new XsltPolicy((XsltPolicy*)s->nodes[i]);
else
node = new XsltPolicyRule((XsltPolicyRule*)s->nodes[i]);
node->parent_id = this->id;
this->nodes.push_back(node);
}
}
//---------------------------------------------------------------------------
XsltPolicy::XsltPolicy(const XsltPolicy& s, bool is_system) : Policy(s), XsltPolicyNode(s)
{
if (&s == this)
return;
this->type = Policies::POLICY_XSLT;
this->ope = s.ope;
this->name = this->node_name;
this->is_system = is_system;
for (size_t i = 0; i < s.nodes.size(); ++i)
{
if (!s.nodes[i])
continue;
XsltPolicyNode *node = NULL;
if (s.nodes[i]->kind == XSLT_POLICY_POLICY)
node = new XsltPolicy(*(XsltPolicy*)s.nodes[i], is_system);
else
node = new XsltPolicyRule(*(XsltPolicyRule*)s.nodes[i]);
node->parent_id = this->id;
this->nodes.push_back(node);
}
}
//---------------------------------------------------------------------------
XsltPolicy::~XsltPolicy()
{
for (size_t i = 0; i < nodes.size(); ++i)
{
delete nodes[i];
nodes[i] = NULL;
}
nodes.clear();
}
//---------------------------------------------------------------------------
int XsltPolicy::get_final_xslt(std::string& xslt, const std::map<std::string, std::string>& opts)
{
if (dump_schema(xslt) < 0)
return -1;
if (policies->transform_with_xslt_memory(xslt, policy_transform_xml, opts, xslt) < 0)
return -1;
replace_aliasxsl_in_policy(xslt);
replace_xlmns_in_policy(xslt);
return 0;
}
//***************************************************************************
// XsltPolicy Parsing
//***************************************************************************
//---------------------------------------------------------------------------
int XsltPolicy::import_schema_from_doc(xmlDocPtr doc, const std::string& filename)
{
if (!doc)
{
error = "The XML doc is not valid";
return -1;
}
xmlNodePtr root = xmlDocGetRootElement(doc);
if (!root)
{
error = "No root node, leaving";
return -1;
}
this->filename = filename;
//try the root if the XML is badly formated
if (run_over_siblings_nodes(root, true, NULL) < 0)
{
for (size_t i = 0; i < nodes.size(); ++i)
delete nodes[i];
nodes.clear();
return -1;
}
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::run_over_siblings_nodes(xmlNodePtr node, bool is_root, XsltPolicy* current)
{
int ret = 0;
for (; node; node = node->next)
{
switch (node->type)
{
case XML_TEXT_NODE:
case XML_COMMENT_NODE:
continue;
default:;
}
int tmp = find_policy_node(node, is_root, current);
if (tmp < 0)
{
ret = -1;
break;
}
}
return ret;
}
//---------------------------------------------------------------------------
int XsltPolicy::find_policy_node(xmlNodePtr node, bool is_root, XsltPolicy* current)
{
if (!node->name)
return 0;
std::string name((const char *)node->name);
if (name == "policy")
return parse_policy_policy(node, is_root, current);
else if (name == "rule")
return parse_policy_rule(node, is_root, current);
else if (name == "description")
{
// Get description
xmlChar *value = xmlNodeGetContent(node);
if (value)
{
current->description = std::string((const char*)value);
xmlFree(value);
}
return 0;
}
else if (name == "tag")
{
// Get tag
xmlChar *value = xmlNodeGetContent(node);
if (value)
{
current->tags.push_back(std::string((const char*)value));
xmlFree(value);
}
return 0;
}
error = "The XML policy tag accepted are 'policy' or 'rule'";
return -1;
}
//---------------------------------------------------------------------------
int XsltPolicy::parse_policy_policy(xmlNodePtr node, bool is_root, XsltPolicy* current)
{
XsltPolicyNode *new_node = NULL;
if (is_root)
new_node = this;
else
{
new_node = new XsltPolicy(policies, no_https);;
new_node->parent_id = current->id;
current->nodes.push_back(new_node);
}
new_node->kind = XSLT_POLICY_POLICY;
XsltPolicy *p = (XsltPolicy*)new_node;
//Get name
xmlChar *name = xmlGetNoNsProp(node, (const unsigned char*)"name");
if (name)
{
p->name = std::string((const char*)name);
p->node_name = p->name;
xmlFree(name);
}
// Get type
xmlChar *type = xmlGetNoNsProp(node, (const unsigned char*)"type");
if (type)
{
p->ope = std::string((const char*)type);
xmlFree(type);
}
else
p->ope = "and";
if (p->ope != "and" && p->ope != "or")
{
error = "The XML policy type accepted are 'and' or 'or'";
return -1;
}
xmlChar *is_public = xmlGetNoNsProp(node, (const unsigned char*)"isPublic");
if (is_public && is_root)
{
std::string is_public_str = std::string((const char*)is_public);
if (is_public_str == "true")
p->is_public = true;
xmlFree(is_public);
}
xmlChar *level = xmlGetNoNsProp(node, (const unsigned char*)"level");
if (level)
{
p->level = std::string((const char*)level);
xmlFree(level);
}
xmlChar *license = xmlGetNoNsProp(node, (const unsigned char*)"license");
if (license && is_root)
{
p->license = std::string((const char*)license);
xmlFree(license);
}
if (!node->children)
return 0;
return run_over_siblings_nodes(node->children, false, p);
}
//---------------------------------------------------------------------------
int XsltPolicy::parse_policy_rule(xmlNodePtr node, bool is_root, XsltPolicy* current)
{
XsltPolicyNode *new_node = NULL;
if (is_root)
{
error = "Policy has to start with a policy node";
return -1;
}
else
{
new_node = new XsltPolicyRule;
((XsltPolicyRule*)new_node)->id = ((XsltPolicyRule*)new_node)->rule_id++;
new_node->parent_id = current->id;
current->nodes.push_back(new_node);
}
new_node->kind = XSLT_POLICY_RULE;
XsltPolicyRule *r = (XsltPolicyRule*)new_node;
//Get name
xmlChar *name = xmlGetNoNsProp(node, (const unsigned char*)"name");
if (name)
{
r->node_name = std::string((const char*)name);
xmlFree(name);
}
//Get ope
xmlChar *ope = xmlGetNoNsProp(node, (const unsigned char*)"operator");
if (ope)
{
r->ope = std::string((const char*)ope);
xmlFree(ope);
}
//Get trackType
xmlChar *track_type = xmlGetNoNsProp(node, (const unsigned char*)"tracktype");
if (track_type)
{
r->track_type = std::string((const char*)track_type);
xmlFree(track_type);
}
//Get field
xmlChar *field = xmlGetNoNsProp(node, (const unsigned char*)"value");
if (field)
{
r->field = std::string((const char*)field);
xmlFree(field);
// Handle old typo in MediaInfoLib
if (r->field == "TimeCode_Striped")
r->field = "TimeCode_Stripped";
}
//Get scope
xmlChar *scope = xmlGetNoNsProp(node, (const unsigned char*)"scope");
if (scope)
{
r->scope = std::string((const char*)scope);
xmlFree(scope);
}
//Get level
xmlChar *level = xmlGetNoNsProp(node, (const unsigned char*)"level");
if (level)
{
r->level = std::string((const char*)level);
xmlFree(level);
}
//Get occurrence
xmlChar *occurrence = xmlGetNoNsProp(node, (const unsigned char*)"occurrence");
if (occurrence)
{
r->occurrence = std::string((const char*)occurrence);
xmlFree(occurrence);
}
//Get source
for (xmlNodePtr child = node->children; child; child = child->next)
{
if (child->type == XML_ELEMENT_NODE && child->name && std::string((const char*)child->name) == "source")
{
if (r->source)
delete r->source;
r->source = new XsltPolicyRule::Source;
//Get source trackType
xmlChar *track_type = xmlGetNoNsProp(child, (const unsigned char*)"tracktype");
if (track_type)
{
r->source->track_type = std::string((const char*)track_type);
xmlFree(track_type);
}
//Get source field
xmlChar *field = xmlGetNoNsProp(child, (const unsigned char*)"value");
if (field)
{
r->source->field = std::string((const char*)field);
xmlFree(field);
}
//Get source scope
xmlChar *scope = xmlGetNoNsProp(child, (const unsigned char*)"scope");
if (scope)
{
r->source->scope = std::string((const char*)scope);
xmlFree(scope);
}
//Get source occurrence
xmlChar *occurrence = xmlGetNoNsProp(child, (const unsigned char*)"occurrence");
if (occurrence)
{
r->source->occurrence = std::string((const char*)occurrence);
xmlFree(occurrence);
}
break;
}
}
//Get value
if (!r->source)
{
xmlChar *value = xmlNodeGetContent(node);
if (value)
{
r->value = std::string((const char*)value);
xmlFree(value);
}
}
return 0;
}
//***************************************************************************
// XsltPolicy Serializing
//***************************************************************************
//---------------------------------------------------------------------------
int XsltPolicy::create_node_policy_child(xmlNodePtr& node, XsltPolicy *current)
{
node = xmlNewNode(NULL, (const xmlChar*)"policy");
if (!node)
{
error = "Cannot create the policy children";
return -1;
}
//type ("and", "or")
if (current->ope.size())
xmlNewProp(node, (const xmlChar *)"type", (const xmlChar *)current->ope.c_str());
else
xmlNewProp(node, (const xmlChar *)"type", (const xmlChar *)"and");
//name
if (current->node_name.size())
xmlNewProp(node, (const xmlChar *)"name", (const xmlChar *)current->node_name.c_str());
//description
if (current->description.size())
{
xmlNodePtr new_node = xmlNewNode(NULL, (const xmlChar*)"description");
if (!new_node)
{
error = "Cannot create the policy children";
return -1;
}
xmlNodeSetContent(new_node, (const xmlChar*)current->description.c_str());
if (!xmlAddChild(node, new_node))
{
error = "Cannot add child to policy children";
return -1;
}
}
//tags
if (current->tags.size())
{
for (size_t i = 0; i < current->tags.size(); ++i)
{
xmlNodePtr new_node = xmlNewNode(NULL, (const xmlChar*)"tag");
if (!new_node)
{
error = "Cannot create the policy children";
return -1;
}
xmlNodeSetContent(new_node, (const xmlChar*)current->tags[i].c_str());
if (!xmlAddChild(node, new_node))
{
error = "Cannot add child to policy children";
return -1;
}
}
}
//level
if (current->level.size())
xmlNewProp(node, (const xmlChar *)"level", (const xmlChar *)current->level.c_str());
//isPublic
if (current->keep_public && current->is_public)
xmlNewProp(node, (const xmlChar *)"isPublic", (const xmlChar *)"true");
//license
if (current->license.size())
xmlNewProp(node, (const xmlChar *)"license", (const xmlChar *)current->license.c_str());
if (write_nodes_children(node, current) < 0)
{
error = "Cannot create the policy children";
return -1;
}
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::create_node_rule_child(xmlNodePtr& node, XsltPolicyRule *current)
{
node = xmlNewNode(NULL, (const xmlChar *)"rule");
if (!node)
{
error = "Cannot create the rule children";
return -1;
}
//name
if (current->node_name.size())
xmlNewProp(node, (const xmlChar *)"name", (const xmlChar *)current->node_name.c_str());
//field
if (current->field.size())
xmlNewProp(node, (const xmlChar *)"value", (const xmlChar *)current->field.c_str());
//track type
if (current->track_type.size())
xmlNewProp(node, (const xmlChar *)"tracktype", (const xmlChar *)current->track_type.c_str());
//occurrence
if (current->occurrence.size())
xmlNewProp(node, (const xmlChar *)"occurrence", (const xmlChar *)current->occurrence.c_str());
//operator
if (current->ope.size())
xmlNewProp(node, (const xmlChar *)"operator", (const xmlChar *)current->ope.c_str());
//scope
if (current->scope.size())
xmlNewProp(node, (const xmlChar *)"scope", (const xmlChar *)current->scope.c_str());
//level
if (current->level.size())
xmlNewProp(node, (const xmlChar *)"level", (const xmlChar *)current->level.c_str());
//source or value
if (current->source)
{
xmlNodePtr child = xmlNewNode(NULL, (const xmlChar *)"source");
if (!child)
{
error = "Cannot create the source children";
return -1;
}
//field
if (current->source->field.size())
xmlNewProp(child, (const xmlChar *)"value", (const xmlChar *)current->source->field.c_str());
//track type
if (current->source->track_type.size())
xmlNewProp(child, (const xmlChar *)"tracktype", (const xmlChar *)current->source->track_type.c_str());
//scope
if (current->source->scope.size())
xmlNewProp(child, (const xmlChar *)"scope", (const xmlChar *)current->source->scope.c_str());
//occurrence
if (current->source->occurrence.size())
xmlNewProp(child, (const xmlChar *)"occurrence", (const xmlChar *)current->source->occurrence.c_str());
xmlAddChild(node, child);
}
else if (current->value.size())
xmlNodeSetContent(node, (const xmlChar*)current->value.c_str());
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::write_nodes_children(xmlNodePtr node, XsltPolicy *current)
{
for (size_t i = 0; i < current->nodes.size(); ++i)
{
if (!current->nodes[i])
continue;
xmlNodePtr new_node = NULL;
if (current->nodes[i]->kind == XSLT_POLICY_POLICY)
{
if (create_node_policy_child(new_node, (XsltPolicy*)current->nodes[i]))
return -1;
}
else
{
if (create_node_rule_child(new_node, (XsltPolicyRule*)current->nodes[i]))
return -1;
}
if (!xmlAddChild(node, new_node))
{
error = "Cannot add child to policy children";
return -1;
}
}
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::write_root_nodes_children(xmlDocPtr doc)
{
xmlNodePtr node = NULL;
if (kind == XSLT_POLICY_POLICY)
{
if (create_node_policy_child(node, this) < 0)
return -1;
}
else
{
if (create_node_rule_child(node, (XsltPolicyRule*)this) < 0)
return -1;
}
if (!node)
{
error = "Cannot create the policy children";
return -1;
}
xmlDocSetRootElement(doc, node);
return 0;
}
//---------------------------------------------------------------------------
xmlDocPtr XsltPolicy::create_doc()
{
xmlDocPtr doc = xmlNewDoc((const xmlChar *)"1.0");
if (write_root_nodes_children(doc) < 0)
{
xmlFreeDoc(doc);
return NULL;
}
return doc;
}
//***************************************************************************
// XsltPolicy creation from file
//***************************************************************************
//---------------------------------------------------------------------------
int XsltPolicy::create_rule_from_media_track_child(xmlNodePtr node, const std::string& type, const std::string& prefix)
{
std::string current;
size_t index = 0;
for (xmlNodePtr child = node->children; child; child = child->next)
{
if (child->type != XML_ELEMENT_NODE || !child->name)
continue;
std::string name((const char*)child->name);
if (name == "FileSize"
|| name == "Duration"
|| name == "DURATION"
|| name == "OverallBitRate"
|| name == "FrameCount"
|| name == "StreamSize"
|| name == "BitRate"
|| name == "Delay"
|| name == "Count"
|| name == "CodecID_Url"
|| name.find("/String") != std::string::npos
|| name == "ConformanceErrors"
|| name == "ConformanceWarnings"
|| name == "ConformanceInfos")
continue;
if(xmlChildElementCount(child))
{
if (current != name)
{
index = 0;
current = name;
}
std::stringstream ss;
ss << "[" << ++index << "]/";
create_rule_from_media_track_child(child, type, prefix + name + ss.str());
continue;
}
XsltPolicyRule *rule = new XsltPolicyRule;
rule->id = rule->rule_id++;
rule->track_type = type;
rule->field = prefix + (const char*)child->name;
rule->ope = "=";
rule->occurrence = "*";
xmlChar *content = xmlNodeGetContent(child);
if (content)
rule->value = (const char*)content;
rule->create_default_name(rule->node_name);
nodes.push_back(rule);
}
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::find_media_track_node(xmlNodePtr node, std::string& type)
{
xmlChar *property = xmlGetNoNsProp(node, (const unsigned char*)"type");
if (!property)
return -1;
type = std::string((const char*)property);
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::find_media_track_node(xmlNodePtr node)
{
for (xmlNodePtr child = node->children; child; child = child->next)
{
std::string def("track");
if (!child->name || def.compare((const char*)child->name))
continue;
std::string type;
if (find_media_track_node(child, type) < 0)
return -1;
if (create_rule_from_media_track_child(child, type) < 0)
return -1;
}
return 0;
}
//---------------------------------------------------------------------------
int XsltPolicy::create_policy_from_mi(const std::string& report)
{
Xslt s(no_https);
xmlSetGenericErrorFunc(&s, &s.manage_generic_error);
xmlDocPtr doc = xmlParseMemory(report.c_str(), report.length());
xmlSetGenericErrorFunc(NULL, NULL);
if (!doc)
{
// maybe put the errors from s.errors
error = "The MediaInfo report given cannot be parsed";
return -1;
}
xmlNodePtr root = xmlDocGetRootElement(doc);
if (!root)
{
error = "No root node, leaving";
xmlFreeDoc(doc);
return 0;
}
for (xmlNodePtr child = root->children; child; child = child->next)
{
std::string def("media");
if (child->type == XML_TEXT_NODE || !child->name || def.compare((const char*)child->name))
continue;
if (find_media_track_node(child) < 0)
{
xmlFreeDoc(doc);
return -1;
}
break;
}
xmlFreeDoc(doc);
return 0;
}
XsltPolicyRule* XsltPolicy::get_policy_rule(int id, std::string& err)
{
if (id < 0)
{
err = "Policy Rule does not exist";
return NULL;
}
for (size_t i = 0; i < nodes.size(); ++i)
{
if (!nodes[i])
continue;
if (nodes[i]->kind == XSLT_POLICY_RULE && ((XsltPolicyRule*)nodes[i])->id == (size_t)id)
return (XsltPolicyRule*)nodes[i];
if (nodes[i]->kind == XSLT_POLICY_POLICY)
{
XsltPolicyRule* r = ((XsltPolicy*)nodes[i])->get_policy_rule(id, err);
if (r)
return r;
}
}
err = "Policy Rule does not exist";
return NULL;
}
//---------------------------------------------------------------------------
int XsltPolicy::delete_policy_rule(int rule_id, bool& found, std::string& err)
{
for (size_t i = 0; i < nodes.size(); ++i)
{
if (!nodes[i])
continue;
if (nodes[i]->kind == XSLT_POLICY_RULE && ((XsltPolicyRule*)nodes[i])->id == (size_t)rule_id)
{
found = true;
nodes.erase(nodes.begin() + i);
return 0;
}
else if (nodes[i]->kind == XSLT_POLICY_POLICY)
{
int ret = ((XsltPolicy*)nodes[i])->delete_policy_rule(rule_id, found, err);
if (found)
return ret;
}
}
return -1;
}
//---------------------------------------------------------------------------
int XsltPolicy::delete_policy_rule(int rule_id, std::string& err)
{
if (rule_id < 0)
{
err = "Policy rule does not exist";
return -1;
}
bool found = false;
int ret = delete_policy_rule(rule_id, found, err);
if (found)
return ret;
err = "Policy rule does not exist";
return -1;
}
// HELPER
void XsltPolicy::replace_xlmns_in_policy(std::string& xslt)
{
//replace xmlns:xsl=\"my:namespace\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
size_t pos;
while ((pos = xslt.find("xmlns:xsl=\"my:namespace\"")) != std::string::npos)
xslt.replace(pos, 24, "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"");
}
void XsltPolicy::replace_aliasxsl_in_policy(std::string& xslt)
{
//replace aliasxsl by xsl
size_t pos;
while ((pos = xslt.find("aliasxsl")) != std::string::npos)
xslt.replace(pos, 8, "xsl");
}
}
|