All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Schema.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/Schema.cpp
22 
23  \brief Support for Schema serialization.
24 */
25 
26 // TerraLib
27 #include "../../xml/Reader.h"
28 #include "../../xml/ReaderFactory.h"
29 #include "../../xml/Writer.h"
30 #include "../../xsd/Attribute.h"
31 #include "../../xsd/Schema.h"
32 #include "../Exception.h"
33 #include "Annotation.h"
34 #include "Attribute.h"
35 #include "AttributeGroup.h"
36 #include "ComplexType.h"
37 #include "Element.h"
38 #include "Group.h"
39 #include "Import.h"
40 #include "Include.h"
41 #include "Redefine.h"
42 #include "Schema.h"
43 #include "SimpleType.h"
44 #include "Utils.h"
45 
46 // STL
47 #include <cassert>
48 #include <memory>
49 #include <set>
50 
51 // Boost
52 #include <boost/format.hpp>
53 
54 te::xsd::Schema* te::serialize::ReadSchema(const std::string& path)
55 {
56  std::auto_ptr<te::xml::Reader> reader(te::xml::ReaderFactory::make("XERCES"));
57  reader->setValidationScheme(false);
58  reader->setIgnoreWhiteSpaces(true);
59  reader->read(path);
60 
61  if(!reader->next())
62  throw Exception((boost::format(TR_SERIALIZATION("Could not read the XSD Schema in file: %1%.")) % path).str());
63 
64  if(reader->getNodeType() != te::xml::START_ELEMENT)
65  throw Exception((boost::format(TR_SERIALIZATION("Error reading the document %1%, the start element wasn't found.")) % path).str());
66 
67  return ReadSchema(*reader);
68 }
69 
71 {
72  assert(reader.getNodeType() == te::xml::START_ELEMENT);
73  assert(reader.getElementLocalName() == "schema");
74 
75  std::auto_ptr<te::xsd::Schema> schema(new te::xsd::Schema(0));
76 
77  // Namespaces
78  std::size_t numberOfNamespaces = reader.getNumberOfNamespaces();
79  for(std::size_t i = 0; i < numberOfNamespaces; ++i)
80  {
81  std::pair<std::string, std::string> ns;
82  reader.getNamespace(i, ns);
83  schema->getNamespaces().insert(boost::bimap<std::string, std::string>::value_type(ns.first, ns.second));
84  }
85 
86  // Id
87  ReadIdentifiable(schema.get(), reader);
88 
89  // AttributeFormDefault
90  std::size_t pos = reader.getAttrPosition("attributeFormDefault");
91  if(pos != std::string::npos)
92  schema->setAttributeFormDefault(reader.getAttr(pos) == "qualified" ? te::xsd::Qualified : te::xsd::Unqualified);
93 
94  // ElementFormDefault
95  pos = reader.getAttrPosition("elementFormDefault");
96  if(pos != std::string::npos)
97  schema->setElementFormDefault(reader.getAttr(pos) == "qualified" ? te::xsd::Qualified : te::xsd::Unqualified);
98 
99  // TODO: BlockDefault and FinalDefault ?
100 
101  // TargetNamespace
102  pos = reader.getAttrPosition("targetNamespace");
103  if(pos != std::string::npos)
104  schema->setTargetNamespace(reader.getAttr(pos));
105 
106  // Version
107  pos = reader.getAttrPosition("version");
108  if(pos != std::string::npos)
109  schema->setVersion(reader.getAttr(pos));
110 
111  reader.next();
112 
113  /* Grammar: ((include|import|redefine|annotation)*,(((simpleType|complexType|
114  group|attributeGroup)|element|attribute|notation),annotation*)*) */
115 
116  /* TODO: Using a set to find the element's children. Temporary solution!
117  Suggestion: we can put this information on a static member of te::xsd classes. - Uba, 2013 */
118  std::set<std::string> children;
119  children.insert("include");
120  children.insert("import");
121  children.insert("redefine");
122  children.insert("annotation");
123  children.insert("simpleType");
124  children.insert("complexType");
125  children.insert("group");
126  children.insert("attributeGroup");
127  children.insert("element");
128  children.insert("attribute");
129  children.insert("notation");
130 
131  std::set<std::string>::iterator it;
132  while(reader.getNodeType() == te::xml::START_ELEMENT &&
133  (it = children.find(reader.getElementLocalName())) != children.end())
134  {
135  std::string tag = *it;
136  if(tag == "include")
137  {
138  schema->addInclude(ReadInclude(reader));
139  continue;
140  }
141 
142  if(tag == "import")
143  {
144  schema->addImport(ReadImport(reader));
145  continue;
146  }
147 
148  if(tag == "redefine")
149  {
150  schema->addRedefine(ReadRedefine(reader));
151  continue;
152  }
153 
154  if(tag == "annotation")
155  {
156  schema->addAnnotation(ReadAnnotation(reader));
157  continue;
158  }
159 
160  if(tag == "simpleType")
161  {
162  schema->addSimpleType(ReadSimpleType(reader));
163  continue;
164  }
165 
166  if(tag == "complexType")
167  {
168  schema->addComplexType(ReadComplexType(reader));
169  continue;
170  }
171 
172  if(tag == "group")
173  {
174  schema->addGroup(ReadGroup(reader));
175  continue;
176  }
177 
178  if(tag == "attributeGroup")
179  {
180  schema->addAttributeGroup(ReadAttributeGroup(reader));
181  continue;
182  }
183 
184  if(tag == "element")
185  {
186  schema->addElement(ReadElement(reader));
187  continue;
188  }
189 
190  if(tag == "attribute")
191  schema->addAttribute(ReadAttribute(reader));
192  }
193 
194  assert(reader.getNodeType() == te::xml::END_DOCUMENT);
195 
196  return schema.release();
197 }
198 
200 {
201 }
It indicates that the attribute attribute is not required to be qualified with the namespace prefix a...
Definition: Enums.h:115
It indicates that the attribute attribute must be qualified with the namespace prefix and the no-colo...
Definition: Enums.h:114
Support for Schema serialization.
This class models a XML reader object.
Definition: Reader.h:55
static te::xml::Reader * make()
It creates a new XML reader using the dafault implementation.
virtual bool next()=0
It gets the next event to be read.
#define TR_SERIALIZATION(message)
It marks a string in order to get translated. This is a special mark used in the XML module of TerraL...
Definition: Config.h:58
TESERIALIZATIONEXPORT te::xsd::Annotation * ReadAnnotation(te::xml::Reader &reader)
Definition: Annotation.cpp:40
TESERIALIZATIONEXPORT te::xsd::Element * ReadElement(te::xml::Reader &reader)
Definition: Element.cpp:50
TESERIALIZATIONEXPORT te::xsd::Group * ReadGroup(te::xml::Reader &reader)
Definition: Group.cpp:43
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 Include serialization.
A class that models a XML schema (XSD).
Definition: Schema.h:63
TESERIALIZATIONEXPORT te::xsd::Schema * ReadSchema(const std::string &path)
Definition: Schema.cpp:54
Support for Attribute serialization.
Support for Annotation serialization.
TESERIALIZATIONEXPORT te::xsd::Redefine * ReadRedefine(te::xml::Reader &reader)
Definition: Redefine.cpp:46
Support for SimpleType serialization.
TESERIALIZATIONEXPORT te::xsd::Include * ReadInclude(te::xml::Reader &reader)
Definition: Include.cpp:38
TESERIALIZATIONEXPORT te::xsd::Attribute * ReadAttribute(te::xml::Reader &reader)
Definition: Attribute.cpp:39
TESERIALIZATIONEXPORT te::xsd::ComplexType * ReadComplexType(te::xml::Reader &reader)
Definition: ComplexType.cpp:54
TESERIALIZATIONEXPORT te::xsd::Import * ReadImport(te::xml::Reader &reader)
Definition: Import.cpp:38
Utility methods for Schema serialization.
Support for Import serialization.
Support for ComplexType serialization.
virtual NodeType getNodeType() const =0
It return the type of node read.
TESERIALIZATIONEXPORT te::xsd::AttributeGroup * ReadAttributeGroup(te::xml::Reader &reader)
virtual std::size_t getNumberOfNamespaces() const =0
Support for AttributeGroup serialization.
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 Redefine serialization.
virtual void getNamespace(std::size_t i, std::pair< std::string, std::string > &ns) const =0
Support for Group 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