libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNAltoDescription.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: CRNAltoDescription.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <CRNXml/CRNAlto.h>
23 #include <CRNException.h>
24 #include <CRNi18n.h>
25 
26 using namespace crn;
27 using namespace xml;
28 
32 Alto::Description::Description(const Element &el):
33  Element(el)
34 {
35 }
36 
41 Alto::Description::Description(const Element &el, const Path &imagename):
42  Element(el)
43 {
44  if (imagename.IsEmpty())
45  throw ExceptionInvalidArgument(_("Null image filename."));
46  Element sel = PushBackElement("MesurementUnit");
47  sel.PushBackText("pixel");
48  sel = PushBackElement("sourceImageInformation");
49  sel = sel.PushBackElement("fileName");
50  Path uri(imagename);
51  uri.ToURI();
52  sel.PushBackText(uri);
53 }
54 
59 Option<Alto::Description::MeasurementUnit> Alto::Description::GetMeasurementUnit()
60 {
62  StringUTF8 mu;
63  Element e = GetFirstChildElement("MeasurementUnit");
64  if (e)
65  {
66  mu = e.GetFirstChildText();
67  mu.ToLower();
68  if (mu == "pixel")
69  munit = MeasurementUnit::Pixel;
70  else if (mu == "mm10")
71  munit = MeasurementUnit::MM;
72  else if (mu == "inch1200")
73  munit = MeasurementUnit::Inch;
74  else
75  throw ExceptionDomain(_("Invalid measurement unit."));
76  }
77  return munit;
78 }
79 
83 Option<StringUTF8> Alto::Description::GetFilename()
84 {
86  Element e = GetFirstChildElement("sourceImageInformation");
87  if (e)
88  {
89  e = e.GetFirstChildElement("fileName");
90  if (e)
91  fn = e.GetFirstChildText();
92  }
93  return fn;
94 }
95 
96 
StringUTF8 & ToLower()
Converts the string to lowercase.
XML element.
Definition: CRNXml.h:135
#define _(String)
Definition: CRNi18n.h:51
Text PushBackText(const StringUTF8 &text, bool cdata=false)
Adds a text at the end of the children list.
Definition: CRNXml.cpp:467
A generic domain error.
Definition: CRNException.h:83
Element GetFirstChildElement(const StringUTF8 &name="")
Gets the first child element.
Definition: CRNXml.cpp:320
bool IsEmpty() const noexcept
Checks if the string is empty.
A convenience class for file paths.
Definition: CRNPath.h:39
StringUTF8 GetFirstChildText()
Gets the first child as text.
Definition: CRNXml.cpp:339
A character string class.
Definition: CRNStringUTF8.h:49
A class to store an optional value.
Definition: CRNOption.h:33
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
Definition: CRNXml.cpp:355
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107