All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Element.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/se/Element.cpp
22 
23  \brief Support for Element serialization.
24 */
25 
26 // TerraLib
27 #include "../../xml/Reader.h"
28 #include "../../xml/Writer.h"
29 #include "../../xsd/ComplexType.h"
30 #include "../../xsd/Element.h"
31 #include "../../xsd/Key.h"
32 #include "../../xsd/KeyRef.h"
33 #include "../../xsd/QName.h"
34 #include "../../xsd/SimpleType.h"
35 #include "../../xsd/Unique.h"
36 #include "ComplexType.h"
37 #include "Element.h"
38 #include "Key.h"
39 #include "KeyRef.h"
40 #include "SimpleType.h"
41 #include "Unique.h"
42 #include "Utils.h"
43 
44 // STL
45 #include <cassert>
46 #include <memory>
47 #include <set>
48 #include <string>
49 
51 {
52  assert(reader.getNodeType() == te::xml::START_ELEMENT);
53  assert(reader.getElementLocalName() == "element");
54 
55  std::auto_ptr<te::xsd::Element> element(new te::xsd::Element);
56 
57  // Id
58  ReadIdentifiable(element.get(), reader);
59 
60  // Name
61  std::size_t pos = reader.getAttrPosition("name");
62  if(pos != std::string::npos)
63  element->setName(new std::string(reader.getAttr(pos)));
64 
65  // Ref
66  pos = reader.getAttrPosition("ref");
67  if(pos != std::string::npos)
68  element->setRef(CreateQName(reader.getAttr(pos)));
69 
70  // Type
71  pos = reader.getAttrPosition("type");
72  if(pos != std::string::npos)
73  element->setType(CreateQName(reader.getAttr(pos)));
74 
75  // SubstitutionGroup
76  pos = reader.getAttrPosition("substitutionGroup");
77  if(pos != std::string::npos)
78  element->setSubstitutionGroup(CreateQName(reader.getAttr(pos)));
79 
80  // Default
81  pos = reader.getAttrPosition("default");
82  if(pos != std::string::npos)
83  element->setDefaultValue(new std::string(reader.getAttr(pos)));
84 
85  // Fixed
86  pos = reader.getAttrPosition("fixed");
87  if(pos != std::string::npos)
88  element->setFixedValue(new std::string(reader.getAttr(pos)));
89 
90  // MinOccurs and MaxOccurs
91  ReadOccurs(element.get(), reader);
92 
93  // Nillable
94  pos = reader.getAttrPosition("nillable");
95  if(pos != std::string::npos)
96  element->setAsNillable(reader.getAttr(pos) == "true" ? true : false);
97 
98  // Abstract
99  pos = reader.getAttrPosition("abstract");
100  if(pos != std::string::npos)
101  element->setAsAbstract(reader.getAttr(pos) == "true" ? true : false);
102 
103  // TODO: Block and Final ?
104 
105  reader.next();
106 
107  // Grammar: annotation?,((simpleType|complexType)?,(unique|key|keyref)*))
108 
109  ReadAnnotated(element.get(), reader);
110 
111  if(reader.getElementLocalName() == "simpleType")
112  element->setContentType(ReadSimpleType(reader));
113  else if(reader.getElementLocalName() == "complexType")
114  element->setContentType(ReadComplexType(reader));
115 
116  std::set<std::string> children;
117  children.insert("unique");
118  children.insert("key");
119  children.insert("keyref");
120 
121  std::set<std::string>::iterator it;
122  while(reader.getNodeType() == te::xml::START_ELEMENT &&
123  (it = children.find(reader.getElementLocalName())) != children.end())
124  {
125  std::string tag = *it;
126  if(tag == "unique")
127  {
128  element->addIdentityConstraint(ReadUnique(reader));
129  continue;
130  }
131 
132  if(tag == "key")
133  {
134  element->addIdentityConstraint(ReadKey(reader));
135  continue;
136  }
137 
138  if(tag == "keyref")
139  element->addIdentityConstraint(ReadKeyRef(reader));
140  }
141 
142  assert(reader.getNodeType() == te::xml::END_ELEMENT);
143  reader.next();
144 
145  return element.release();
146 }
147 
149 {
150 }
TESERIALIZATIONEXPORT te::xsd::KeyRef * ReadKeyRef(te::xml::Reader &reader)
Definition: KeyRef.cpp:40
TESERIALIZATIONEXPORT te::xsd::Unique * ReadUnique(te::xml::Reader &reader)
Definition: Unique.cpp:40
This class models a XML reader object.
Definition: Reader.h:55
virtual bool next()=0
It gets the next event to be read.
void ReadAnnotated(te::xsd::Annotated *annotated, te::xml::Reader &reader)
Definition: Utils.cpp:50
TESERIALIZATIONEXPORT te::xsd::Element * ReadElement(te::xml::Reader &reader)
Definition: Element.cpp:50
This class models the element of a XML Schema.
Definition: Element.h:56
TESERIALIZATIONEXPORT void Save(const te::fe::Filter *filter, te::xml::Writer &writer)
Definition: Filter.cpp:54
void ReadIdentifiable(te::xsd::Identifiable *identifiable, te::xml::Reader &reader)
Definition: Utils.cpp:41
Support for SimpleType serialization.
te::xsd::QName * CreateQName(const std::string &name)
Definition: Utils.cpp:77
TESERIALIZATIONEXPORT te::xsd::ComplexType * ReadComplexType(te::xml::Reader &reader)
Definition: ComplexType.cpp:54
TESERIALIZATIONEXPORT te::xsd::Key * ReadKey(te::xml::Reader &reader)
Definition: Key.cpp:40
void ReadOccurs(te::xsd::Occurs *occurs, te::xml::Reader &reader)
Definition: Utils.cpp:62
Utility methods for Schema serialization.
Support for Unique serialization.
Support for KeyRef serialization.
Support for ComplexType serialization.
virtual NodeType getNodeType() const =0
It return the type of node read.
Support for Element serialization.
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
TESERIALIZATIONEXPORT te::xsd::SimpleType * ReadSimpleType(te::xml::Reader &reader)
Definition: SimpleType.cpp:44
virtual std::size_t getAttrPosition(const std::string &name) const =0
It returns the attribute position.
Support for Key serialization.
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.
This class models a XML writer object.
Definition: Writer.h:52