libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNAltoUtils.cpp
Go to the documentation of this file.
1 /* Copyright 2011-2016 CoReNum
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNAltoUtils.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <CRNXml/CRNAltoUtils.h>
23 #include <algorithm>
24 
25 using namespace crn;
26 using namespace xml;
27 
34 std::vector<Id> crn::xml::GetStyleRefs(const Element &el)
35 {
36  std::vector<Id> styles;
37  StringUTF8 str = el.GetAttribute<StringUTF8>("STYLEREFS");
38  if (str.IsNotEmpty())
39  styles = str.Split(" ");
40  return styles;
41 }
42 
50 void crn::xml::AddStyleRef(Element &el, const Id &id)
51 {
52  std::vector<Id> styles(GetStyleRefs(el));
53  if (std::find(styles.begin(), styles.end(), id) != styles.end())
54  return;
55  StringUTF8 av;
56  for (const Id &sid : styles)
57  av += sid + " ";
58  av += id;
59  el.SetAttribute("STYLEREFS", av);
60 }
61 
69 void crn::xml::RemoveStyleRef(Element &el, const Id &id)
70 {
71  std::vector<Id> tmpvec;
72  std::vector<Id> styles(GetStyleRefs(el));
73  std::remove_copy(styles.begin(), styles.end(), std::back_inserter(tmpvec), id);
74  styles.swap(tmpvec);
75  StringUTF8 av;
76  for (const Id &sid : styles)
77  av += sid + " ";
78  el.SetAttribute("STYLEREFS", av);
79 }
80 
XML element.
Definition: CRNXml.h:135
std::vector< StringUTF8 > Split(const StringUTF8 &sep) const
Splits the string in multiple strings delimited by a set of separators.
std::vector< Id > GetStyleRefs(const Element &el)
Gets the list of style references.
bool IsNotEmpty() const noexcept
Checks if the string is not empty.
void AddStyleRef(Element &el, const Id &id)
Adds a style reference to an element.
void RemoveStyleRef(Element &el, const Id &id)
Removes a style reference to an element.
void SetAttribute(const StringUTF8 &name, const StringUTF8 &value)
Sets the value of an attribute.
Definition: CRNXml.cpp:595
T GetAttribute(const StringUTF8 &name, bool silent=true) const
Gets an attribute.
Definition: CRNXml.h:219
A character string class.
Definition: CRNStringUTF8.h:49