Revision 4c8b82e2 src/template/Template.cc

View differences:

src/template/Template.cc
331 331
    attributes.erase(index.first,index.second);
332 332

  
333 333
    return j;
334

  
335 334
}
336 335

  
337 336
/* -------------------------------------------------------------------------- */
......
587 586
/* ------------------------------------------------------------------------ */
588 587
/* ------------------------------------------------------------------------ */
589 588

  
590
void Template::merge(const Template * from_tmpl)
591
{
592
    multimap<string,Attribute *>::const_iterator it;
593

  
594
    for (it = from_tmpl->attributes.begin(); it != from_tmpl->attributes.end(); ++it)
595
    {
596
        this->erase(it->first);
597
    }
598

  
599
    for (it = from_tmpl->attributes.begin(); it != from_tmpl->attributes.end(); ++it)
600
    {
601
        this->set(it->second->clone());
602
    }
603
}
604

  
605
/* ------------------------------------------------------------------------ */
606
/* ------------------------------------------------------------------------ */
607

  
608 589
void Template::rebuild_attributes(const xmlNode * root_element)
609 590
{
610 591
    xmlNode * cur_node = 0;
......
639 620
/* -------------------------------------------------------------------------- */
640 621
/* -------------------------------------------------------------------------- */
641 622

  
642
void Template::set_restricted_attributes(vector<const SingleAttribute *>& rattrs,
643
    vector<string>& restricted_attributes)
623
void Template::clear()
644 624
{
645
    for (unsigned int i = 0 ; i < rattrs.size() ; i++ )
625
    if (attributes.empty())
646 626
    {
647
        string va = rattrs[i]->value();
648
        restricted_attributes.push_back(one_util::toupper(va));
627
        return;
649 628
    }
629

  
630
    multimap<string,Attribute *>::iterator it;
631

  
632
    for ( it = attributes.begin(); it != attributes.end(); it++)
633
    {
634
        delete it->second;
635
    }
636

  
637
    attributes.clear();
650 638
}
651 639

  
640

  
641
/* -------------------------------------------------------------------------- */
642
/* -------------------------------------------------------------------------- */
643
/*   RESTRICTED ATTRIBUTES INTERFACE                                          */
652 644
/* -------------------------------------------------------------------------- */
653 645
/* -------------------------------------------------------------------------- */
654 646

  
655
static int get_attributes(
656
        multimap<string, Attribute *>& attributes,
657
        const string& name, vector<const Attribute*>& values)
647
void Template::merge(const Template * from)
658 648
{
659
    multimap<string, Attribute *>::const_iterator       i;
660
    pair<multimap<string, Attribute *>::const_iterator,
661
    multimap<string, Attribute *>::const_iterator>      index;
662
    int                                           j;
663

  
664
    index = attributes.equal_range(name);
649
    multimap<string, Attribute *>::const_iterator it, jt;
665 650

  
666
    for ( i = index.first,j=0 ; i != index.second ; i++,j++ )
651
    for (it = from->attributes.begin(); it != from->attributes.end(); ++it)
667 652
    {
668
        values.push_back(i->second);
653
        erase(it->first);
669 654
    }
670 655

  
671
    return j;
656
    for (it = from->attributes.begin(); it != from->attributes.end(); ++it)
657
    {
658
        set(it->second->clone());
659
    }
672 660
}
673 661

  
674
bool Template::check(string& rs_attr, const vector<string> &restricted_attributes)
662
/* ------------------------------------------------------------------------ */
663
/* ------------------------------------------------------------------------ */
664

  
665
void Template::parse_restricted(const vector<const SingleAttribute *>& ras,
666
        std::map<std::string, std::set<std::string> >& ras_m)
675 667
{
676
    size_t pos;
677
    string avector, vattr;
678
    vector<const Attribute *> values;
668
    vector<const SingleAttribute *>::const_iterator it;
679 669

  
680
    for (unsigned int i=0; i < restricted_attributes.size(); i++)
670
    for (it = ras.begin(); it != ras.end(); ++it)
681 671
    {
682
        pos = restricted_attributes[i].find("/");
672
        string va  = (*it)->value();
673
        size_t pos = va.find("/");
683 674

  
684 675
        if (pos != string::npos) //Vector Attribute
685 676
        {
686
            int num;
677
            string avector = va.substr(0,pos);
678
            string vattr   = va.substr(pos+1);
679

  
680
            map<std::string, std::set<string> >::iterator jt;
687 681

  
688
            avector = restricted_attributes[i].substr(0,pos);
689
            vattr   = restricted_attributes[i].substr(pos+1);
682
            jt = ras_m.find(avector);
690 683

  
691
            //Template contains the attr
692
            if ((num = get_attributes(attributes, avector, values)) > 0 )
684
            if ( jt == ras_m.end() )
693 685
            {
694
                const VectorAttribute * attr;
686
                std::set<std::string> aset;
695 687

  
696
                for (int j=0; j<num ; j++ )
697
                {
698
                    attr = dynamic_cast<const VectorAttribute *>(values[j]);
688
                aset.insert(vattr);
699 689

  
700
                    if (attr == 0)
701
                    {
702
                        continue;
703
                    }
704

  
705
                    if ( !attr->vector_value(vattr.c_str()).empty() )
706
                    {
707
                        rs_attr = restricted_attributes[i];
708
                        return true;
709
                    }
710
                }
690
                ras_m.insert(make_pair(avector, aset));
711 691
            }
712
        }
713
        else //Single Attribute
714
        {
715
            if (get_attributes(attributes, restricted_attributes[i], values) > 0)
692
            else
716 693
            {
717
                rs_attr = restricted_attributes[i];
718
                return true;
694
                jt->second.insert(vattr);
719 695
            }
720 696
        }
721
    }
697
        else
698
        {
699
            std::set<std::string> eset;
722 700

  
723
    return false;
701
            ras_m.insert(make_pair(va, eset));
702
        }
703
    }
724 704
}
725 705

  
726 706
/* -------------------------------------------------------------------------- */
727 707
/* -------------------------------------------------------------------------- */
728 708

  
729
void Template::remove_restricted()
730
{}
709
static void restricted_values(const string& vname, const set<string>& vsubs,
710
        const Template& tmpl, vector<string>& rstrings)
711
{
712
    string value;
731 713

  
732
/* -------------------------------------------------------------------------- */
733
/* -------------------------------------------------------------------------- */
714
    vector<const VectorAttribute *>::const_iterator va_it;
715
    vector<const VectorAttribute *> va;
734 716

  
735
void Template::remove_all_except_restricted()
736
{}
717
    tmpl.get(vname, va);
737 718

  
738
/* -------------------------------------------------------------------------- */
739
/* -------------------------------------------------------------------------- */
719
    for ( va_it = va.begin(); va_it != va.end() ; ++va_it )
720
    {
721
        for (set<string>::iterator jt = vsubs.begin(); jt != vsubs.end(); ++jt)
722
        {
723
            if ( (*va_it)->vector_value(*jt, value) == 0 )
724
            {
725
                rstrings.push_back(value);
726
            }
727
        }
728
    }
740 729

  
741
bool Template::has_restricted()
742
{
743
    return false;
730
    sort(rstrings.begin(), rstrings.end());
744 731
}
745 732

  
746
/* -------------------------------------------------------------------------- */
747
/* -------------------------------------------------------------------------- */
748

  
749
static int get_attributes(
750
        multimap<string, Attribute *>& attributes,
751
        const string& name, vector<Attribute*>& values)
733
bool Template::check_restricted(string& ra, const Template& base,
734
        const std::map<std::string, std::set<std::string> >& ras)
752 735
{
753
    multimap<string, Attribute *>::iterator       i;
754
    pair<multimap<string, Attribute *>::iterator,
755
    multimap<string, Attribute *>::iterator>      index;
756
    int                                           j;
736
    std::map<std::string, std::set<std::string> >::const_iterator rit;
757 737

  
758
    index = attributes.equal_range(name);
759

  
760
    for ( i = index.first,j=0 ; i != index.second ; i++,j++ )
738
    for ( rit = ras.begin(); rit != ras.end(); ++rit )
761 739
    {
762
        values.push_back(i->second);
740
        if (!(rit->second).empty())
741
        {
742
            vector<string> rvalues, rvalues_base;
743

  
744
            restricted_values(rit->first, rit->second, *this, rvalues);
745
            restricted_values(rit->first, rit->second, base, rvalues_base);
746

  
747
            if ( rvalues != rvalues_base )
748
            {
749
                ra = rit->first;
750
                return true;
751
            }
752
        }
753
        else
754
        {
755
            if ( get(rit->first, ra) )
756
            {
757
                string ra_b;
758

  
759
                base.get(rit->first, ra_b);
760

  
761
                if ( ra_b != ra )
762
                {
763
                    return true;
764
                }
765
            }
766
        }
763 767
    }
764 768

  
765
    return j;
769
    return false;
766 770
}
767 771

  
768
void Template::remove_restricted(const vector<string> &restricted_attributes)
772
/* -------------------------------------------------------------------------- */
773
/* -------------------------------------------------------------------------- */
774

  
775
bool Template::check_restricted(string& ra,
776
        const std::map<std::string, std::set<std::string> >& ras)
769 777
{
770
    size_t pos;
771
    string avector, vattr;
772
    vector<Attribute *> values;
778
    std::map<std::string, std::set<std::string> >::const_iterator rit;
773 779

  
774
    for (unsigned int i=0; i < restricted_attributes.size(); i++)
780
    for ( rit = ras.begin(); rit != ras.end(); ++rit )
775 781
    {
776
        pos = restricted_attributes[i].find("/");
782
        const std::set<std::string>& sub = rit->second;
777 783

  
778
        if (pos != string::npos) //Vector Attribute
784
        if (!sub.empty()) //Vector Attribute
779 785
        {
780
            int num;
786
            // -----------------------------------------------------------------
787
            // -----------------------------------------------------------------
788
            std::set<std::string>::iterator jt;
789
            std::vector<VectorAttribute *>::iterator va_it;
781 790

  
782
            avector = restricted_attributes[i].substr(0,pos);
783
            vattr   = restricted_attributes[i].substr(pos+1);
791
            std::vector<VectorAttribute *> va;
784 792

  
785
            //Template contains the attr
786
            if ((num = get_attributes(attributes, avector,values)) > 0 )
787
            {
788
                VectorAttribute * attr;
793
            get(rit->first, va);
789 794

  
790
                for (int j=0; j<num ; j++ )
795
            for ( va_it = va.begin(); va_it != va.end() ; ++va_it )
796
            {
797
                for ( jt = sub.begin(); jt != sub.end(); ++jt )
791 798
                {
792
                    attr = dynamic_cast<VectorAttribute *>(values[j]);
793

  
794
                    if (attr == 0)
799
                    if ( (*va_it)->vector_value(*jt, ra) == 0 )
795 800
                    {
796
                        continue;
801
                        return true;
797 802
                    }
798

  
799
                    attr->remove(vattr);
800 803
                }
801 804
            }
802 805
        }
803
        else //Single Attribute
806
        else if ( get(rit->first, ra) ) //Single Attribute
804 807
        {
805
            erase(restricted_attributes[i]);
808
            return true;
806 809
        }
807 810
    }
811

  
812
    return false;
808 813
}
809 814

  
810 815
/* -------------------------------------------------------------------------- */
811 816
/* -------------------------------------------------------------------------- */
812 817

  
813
void Template::remove_all_except_restricted(const vector<string> &restricted_attributes)
818
void Template::remove_restricted(
819
        const std::map<std::string, std::set<std::string> >& ras)
814 820
{
815
    size_t pos;
816
    string avector, vattr;
817
    vector<Attribute *> values;
821
    std::map<std::string, std::set<std::string> >::const_iterator rit;
818 822

  
819
    vector<Attribute *> restricted;
820

  
821
    for (unsigned int i=0; i < restricted_attributes.size(); i++)
823
    for (rit=ras.begin(); rit!=ras.end(); ++rit)
822 824
    {
823
        pos = restricted_attributes[i].find("/");
824

  
825
        if (pos != string::npos) //Vector Attribute
825
        if (!(rit->second).empty()) //Vector Attribute
826 826
        {
827
            int num;
827
            std::set<std::string>::iterator jt;
828
            std::vector<VectorAttribute *>::iterator vat;
828 829

  
829
            avector = restricted_attributes[i].substr(0,pos);
830
            vattr   = restricted_attributes[i].substr(pos+1);
830
            std::vector<VectorAttribute *> va;
831 831

  
832
            //Template contains the attr
833
            if ((num = get_attributes(attributes, avector,values)) > 0 )
834
            {
835
                VectorAttribute * attr;
832
            get(rit->first, va);
836 833

  
837
                for (int j=0; j<num ; j++ )
834
            for (vat = va.begin(); vat != va.end() ; ++vat)
835
            {
836
                for (jt=(rit->second).begin(); jt != (rit->second).end(); ++jt)
838 837
                {
839
                    attr = dynamic_cast<VectorAttribute *>(values[j]);
840

  
841
                    if (attr == 0)
842
                    {
843
                        continue;
844
                    }
845

  
846
                    if ( !attr->vector_value(vattr.c_str()).empty() )
847
                    {
848
                        restricted.push_back(attr);
849
                    }
838
                    (*vat)->remove(*jt);
850 839
                }
851 840
            }
852 841
        }
853
        else //Single Attribute
842
        else
854 843
        {
855
            get_attributes(attributes, restricted_attributes[i], restricted);
844
            erase(rit->first);
856 845
        }
857 846
    }
847
}
858 848

  
859
    vector<Attribute *>::iterator res_it;
849
/*
850
ostringstream& Template::print_restricted(
851
        std::map<std::string, std::set<std::string> >& rattr_m,
852
        ostringstream &oss)
853
{
854
    std::map<std::string, std::set<std::string> >::iterator rit;
860 855

  
861
    for (res_it = restricted.begin(); res_it != restricted.end(); res_it++)
856
    for (rit=rmap.begin(); rit!=rmap.end(); ++rit)
862 857
    {
863
        remove(*res_it);
864
    }
858
        oss << std::endl << rit->first << std::endl;
865 859

  
866
    clear();
860
        std::set<std::string>::iterator jt;
867 861

  
868
    for (res_it = restricted.begin(); res_it != restricted.end(); res_it++)
869
    {
870
        set(*res_it);
862
        for ( jt=(rit->second).begin(); jt != (rit->second).end(); ++jt )
863
        {
864
            oss << "\t" << *jt << std::endl;
865
        }
871 866
    }
867

  
868
    return oss;
872 869
}
873 870

  
874
/* -------------------------------------------------------------------------- */
875
/* -------------------------------------------------------------------------- */
876 871

  
877
void Template::clear()
872
bool Template::remove_non_restricted(
873
        const std::map<std::string, std::set<std::string> >& ras)
878 874
{
879
    if (attributes.empty())
880
    {
881
        return;
882
    }
883

  
884
    multimap<string,Attribute *>::iterator it;
875
    multimap<string, Attribute *>::iterator it;
885 876

  
886
    for ( it = attributes.begin(); it != attributes.end(); it++)
877
    for (it = attributes.begin() ; it != attributes.end() ; )
887 878
    {
888
        delete it->second;
879
        if ( ras.find(it->first) != ras.end() )
880
        {
881
            delete it->second;
882

  
883
            it = attributes.erase(it);
884
        }
885
        else
886
        {
887
            ++it;
888
        }
889 889
    }
890 890

  
891
    attributes.clear();
891
    //Change to void
892
    return true;
892 893
}
894
*/
893 895

  

Also available in: Unified diff