All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2011 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib 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,
8  or (at your option) any later version.
9 
10  TerraLib 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 TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/serialization/xsd/Utils.cpp
22 
23  \brief Utility methods for Schema serialization.
24 */
25 
26 // TerraLib
27 #include "../../common/StringUtils.h"
28 #include "../../xml/Reader.h"
29 #include "../../xsd/Annotated.h"
30 #include "../../xsd/Identifiable.h"
31 #include "../../xsd/Occurs.h"
32 #include "../../xsd/QName.h"
33 #include "Annotation.h"
34 #include "Utils.h"
35 
36 // STL
37 #include <cassert>
38 #include <string>
39 #include <vector>
40 
42 {
43  assert(identifiable);
44 
45  std::size_t pos = reader.getAttrPosition("id");
46  if(pos != std::string::npos)
47  identifiable->setId(new std::string(reader.getAttr(pos)));
48 }
49 
51 {
52  assert(annotated);
53 
54  if((reader.getNodeType() != te::xml::START_ELEMENT) ||
55  (reader.getElementLocalName() != "annotation"))
56  return;
57 
58  te::xsd::Annotation* annotation = ReadAnnotation(reader);
59  annotated->setAnnotation(annotation);
60 }
61 
63 {
64  assert(occurs);
65 
66  // MinOccurs
67  std::size_t pos = reader.getAttrPosition("minOccurs");
68  if(pos != std::string::npos)
69  occurs->setMinOccurs(static_cast<unsigned int>(reader.getAttrAsInt32(pos)));
70 
71  // MaxOccurs
72  pos = reader.getAttrPosition("maxOccurs");
73  if(pos != std::string::npos)
74  reader.getAttr(pos) == "unbounded" ? occurs->setMaxOccurs(te::xsd::Occurs::unbounded) : occurs->setMaxOccurs(reader.getAttrAsInt32(pos));
75 }
76 
77 te::xsd::QName* te::serialize::CreateQName(const std::string& name)
78 {
79  std::vector<std::string> tokens;
80  te::common::Tokenize(name, tokens, ":");
81  assert(tokens.size() >= 2);
82 
83  return new te::xsd::QName(tokens[0], tokens[1]);
84 }
85 
87 {
88  if(name == "minExclusive")
89  return te::xsd::MinExclusive;
90  if(name == "minInclusive")
91  return te::xsd::MinInclusive;
92  if(name == "maxExclusive")
93  return te::xsd::MaxExclusive;
94  if(name == "maxInclusive")
95  return te::xsd::MaxInclusive;
96  if(name == "totalDigits")
97  return te::xsd::TotalDigits;
98  if(name == "fractionDigits")
100  if(name == "length")
101  return te::xsd::Length;
102  if(name == "minLength")
103  return te::xsd::MinLength;
104  if(name == "maxLength")
105  return te::xsd::MaxLength;
106  if(name == "enumeration")
107  return te::xsd::Enumeration;
108  if(name == "whiteSpace")
109  return te::xsd::WhiteSpace;
110 
111  return te::xsd::Pattern;
112 }
Specifies the maximum number of digits allowed.
Definition: Enums.h:103
Defines a list of acceptable values.
Definition: Enums.h:93
A base class for XSD classes that have occurs attributes.
Definition: Occurs.h:43
This class models a XML reader object.
Definition: Reader.h:55
Specifies the exact number of characters or list items allowed.
Definition: Enums.h:95
TESERIALIZATIONEXPORT te::xsd::Annotation * ReadAnnotation(te::xml::Reader &reader)
Definition: Annotation.cpp:40
void ReadAnnotated(te::xsd::Annotated *annotated, te::xml::Reader &reader)
Definition: Utils.cpp:50
FacetType
It enumerates the available facet types.
Definition: Enums.h:91
void setAnnotation(Annotation *ann)
It sets the associated annotation.
Definition: Annotated.cpp:35
Specifies the lower bounds for numeric values. (&gt;)
Definition: Enums.h:99
Defines the exact sequence of characters that are acceptable.
Definition: Enums.h:102
Specifies the upper bounds for numeric values. (&lt;=)
Definition: Enums.h:97
void setMinOccurs(unsigned int minOccurs)
It sets the minOccurs value.
Definition: Occurs.cpp:45
void ReadIdentifiable(te::xsd::Identifiable *identifiable, te::xml::Reader &reader)
Definition: Utils.cpp:41
A base class for XSD classes that must provide a unique ID property.
Definition: Identifiable.h:46
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
Specifies the minimum number of characters or list items allowed.
Definition: Enums.h:101
A class that models a XSD annotation element.
Definition: Annotation.h:55
Support for Annotation serialization.
te::xsd::QName * CreateQName(const std::string &name)
Definition: Utils.cpp:77
static const unsigned int unbounded
Identifer for &quot;unbounded&quot; max values.
Definition: Occurs.h:108
A class to be used to represent XML qualified names.
Definition: QName.h:49
void ReadOccurs(te::xsd::Occurs *occurs, te::xml::Reader &reader)
Definition: Utils.cpp:62
Utility methods for Schema serialization.
te::xsd::FacetType GetFacetType(const std::string &name)
Definition: Utils.cpp:86
void setMaxOccurs(unsigned int maxOccurs)
It sets the maxOccurs value.
Definition: Occurs.cpp:50
Specifies the lower bounds for numeric values. (&gt;=)
Definition: Enums.h:100
Specifies the maximum number of characters or list items allowed.
Definition: Enums.h:98
Specifies how white space (line feeds, tabs, spaces and carriage returns) is handled.
Definition: Enums.h:104
A base class for XSD classes that may allow annotation.
Definition: Annotated.h:49
Specifies the maximum number of decimal places allowed.
Definition: Enums.h:94
virtual NodeType getNodeType() const =0
It return the type of node read.
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
virtual boost::int32_t getAttrAsInt32(const std::string &name) const
It returns the attribute value in the case of an element node with valid attributes.
Definition: Reader.cpp:49
virtual std::size_t getAttrPosition(const std::string &name) const =0
It returns the attribute position.
void setId(std::string *id)
it sets the id.
Specifies the upper bounds for numeric values. (&lt;)
Definition: Enums.h:96
virtual std::string getAttr(const std::string &name) const =0
It returns the attribute value in the case of an element node with valid attributes.