From 8286ac511144e4f17d34eac9affb97e50646344a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 23 Jul 2014 15:25:44 +0200 Subject: Imported Upstream version 4.0.0 --- .../transformations/schema-per-type.cxx | 148 ++++++++++++--------- 1 file changed, 82 insertions(+), 66 deletions(-) (limited to 'libxsd-frontend/xsd-frontend/transformations/schema-per-type.cxx') diff --git a/libxsd-frontend/xsd-frontend/transformations/schema-per-type.cxx b/libxsd-frontend/xsd-frontend/transformations/schema-per-type.cxx index 9ac8445..2fc14b5 100644 --- a/libxsd-frontend/xsd-frontend/transformations/schema-per-type.cxx +++ b/libxsd-frontend/xsd-frontend/transformations/schema-per-type.cxx @@ -1,48 +1,42 @@ // file : xsd-frontend/transformations/schema-per-type.cxx -// author : Boris Kolpackov -// copyright : Copyright (c) 2006-2010 Code Synthesis Tools CC +// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC // license : GNU GPL v2 + exceptions; see accompanying LICENSE file -#include - -#include -#include +#include // strcasecmp -#include -#include -#include +#include +#include +#include #include #include -#include // strcasecmp +#include +#include + +#include using std::wcerr; using std::endl; namespace XSDFrontend { - using namespace Cult; - - typedef WideString String; typedef Transformations::SchemaPerType::Failed Failed; - - typedef Containers::Vector Schemas; - typedef Containers::Map TypeSchemaMap; + typedef std::vector Schemas; + typedef std::map TypeSchemaMap; // Compare file paths case-insensitively. // struct FileComparator { - Boolean + bool operator() (NarrowString const& x, NarrowString const& y) const { return strcasecmp (x.c_str (), y.c_str ()) < 0; } }; - typedef Containers::Set FileSet; + typedef std::set FileSet; namespace { @@ -59,7 +53,7 @@ namespace XSDFrontend xsd_ = 0; } - virtual Void + virtual void traverse (SemanticGraph::Includes& i) { SemanticGraph::Schema& s (i.schema ()); @@ -72,7 +66,7 @@ namespace XSDFrontend } } - virtual Void + virtual void traverse (SemanticGraph::Imports& i) { SemanticGraph::Schema& s (i.schema ()); @@ -85,7 +79,7 @@ namespace XSDFrontend } } - virtual Void + virtual void traverse (SemanticGraph::Implies& i) { if (xsd_ == 0) @@ -97,12 +91,13 @@ namespace XSDFrontend SemanticGraph::Schema*& xsd_; }; - Void + void process_schema (SemanticGraph::Schema& s, SemanticGraph::Schema& root, SemanticGraph::Schema& xsd, TypeSchemaMap& tsm, FileSet& file_set, + bool fat_type_file, Transformations::SchemaPerTypeTranslator& trans) { using namespace SemanticGraph; @@ -141,7 +136,7 @@ namespace XSDFrontend // if (!tn) { - for (NarrowString::Iterator i (base.begin ()), e (base.end ()); + for (NarrowString::iterator i (base.begin ()), e (base.end ()); i != e; ++i) { if (*i == '/' || *i == '\\') @@ -153,7 +148,7 @@ namespace XSDFrontend // NarrowString file_name (base); - for (UnsignedLong i (1); + for (unsigned long i (1); file_set.find (file_name) != file_set.end (); ++i) { @@ -167,11 +162,7 @@ namespace XSDFrontend try { -#if !defined(BOOST_FILESYSTEM_VERSION) || BOOST_FILESYSTEM_VERSION == 2 path = Path (file_name); -#else - path = Path (file_name.c_str()); -#endif } catch (InvalidPath const&) { @@ -184,7 +175,7 @@ namespace XSDFrontend throw Failed (); } } - catch (String::NonRepresentable const&) + catch (NonRepresentable const&) { wcerr << "error: '" << wbase << "' cannot be represented as a " << "narrow string" << endl; @@ -195,6 +186,8 @@ namespace XSDFrontend throw Failed (); } + Type& t (dynamic_cast (n)); + Schema& ts (root.new_node (path, 1, 1)); root.new_edge (ts, xsd, xsd_path); @@ -202,11 +195,43 @@ namespace XSDFrontend root.new_edge (ts, tns, ns.name ()); root.new_edge (tns, n, name); + // If we are generating fat type files, then also move the global + // elements this type classifies to the new schema. + // + if (fat_type_file) + { + for (Type::ClassifiesIterator j (t.classifies_begin ()); + j != t.classifies_end (); ++j) + { + Instance& e (j->instance ()); + + // We can only move a global element from the same namespace. + // + if (e.is_a () && + e.scope ().is_a () && + e.scope ().name () == ns.name ()) + { + Names& n (e.named ()); + String name (n.name ()); + + // Watch out for the iterator validity: the edge we are + // about to remove can be from the same list we are + // currently iterating. + // + if (i != ns.names_end () && &*i == &n) + ++i; + + root.delete_edge (n.scope (), e, n); + root.new_edge (tns, e, name); + } + } + } + // Add include to the original schema and enter into the // type-schema map. // root.new_edge (s, ts, path); - tsm[&dynamic_cast (n)] = &ts; + tsm[&t] = &ts; } else ++i; @@ -219,7 +244,7 @@ namespace XSDFrontend { Type (SemanticGraph::Schema& schema, SemanticGraph::Schema& root, - Char const* by_value_key, + char const* by_value_key, TypeSchemaMap& tsm) : schema_ (schema), root_ (root), @@ -229,7 +254,7 @@ namespace XSDFrontend *this >> names_ >> *this; } - virtual Void + virtual void traverse (SemanticGraph::List& l) { // Treat item type as base type since it is impossible @@ -239,7 +264,7 @@ namespace XSDFrontend set_dep (t, false); } - virtual Void + virtual void traverse (SemanticGraph::Complex& c) { if (c.inherits_p ()) @@ -248,26 +273,26 @@ namespace XSDFrontend Traversal::Complex::names (c); } - virtual Void + virtual void traverse (SemanticGraph::Member& m) { SemanticGraph::Type& t (m.type ()); - Boolean weak ( + bool weak ( by_value_key_ == 0 || !t.context ().count (by_value_key_) || - !t.context ().get (by_value_key_)); + !t.context ().get (by_value_key_)); set_dep (t, weak); } private: - Void - set_dep (SemanticGraph::Type& t, Boolean weak) + void + set_dep (SemanticGraph::Type& t, bool weak) { using namespace SemanticGraph; - TypeSchemaMap::Iterator i (tsm_.find (&t)); + TypeSchemaMap::iterator i (tsm_.find (&t)); // If a type is not present in the map then it must be // a built-in type. @@ -301,9 +326,9 @@ namespace XSDFrontend private: SemanticGraph::Schema& schema_; SemanticGraph::Schema& root_; - Char const* by_value_key_; + char const* by_value_key_; TypeSchemaMap& tsm_; - Containers::Set type_set_; + std::set type_set_; Traversal::Names names_; }; @@ -312,8 +337,10 @@ namespace XSDFrontend namespace Transformations { SchemaPerType:: - SchemaPerType (SchemaPerTypeTranslator& trans, Char const* by_value_key) - : by_value_key_ (by_value_key), trans_ (trans) + SchemaPerType (SchemaPerTypeTranslator& trans, + bool fat, + char const* key) + : fat_type_file_ (fat), by_value_key_ (key), trans_ (trans) { } @@ -344,8 +371,10 @@ namespace XSDFrontend // FileSet file_set; - for (Schemas::Iterator i (schemas.begin ()); i != schemas.end (); ++i) + for (Schemas::iterator i (schemas.begin ()); i != schemas.end (); ++i) { + // This path was already normalized by the parser. + // SemanticGraph::Path const& path ( (*i)->context ().get ("absolute-path")); @@ -353,31 +382,22 @@ namespace XSDFrontend // NarrowString abs_path; -#if !defined(BOOST_FILESYSTEM_VERSION) || BOOST_FILESYSTEM_VERSION == 2 // Try to use the portable representation of the path. If that // fails, fall back to the native representation. // try { - abs_path = path.string (); + abs_path = path.posix_string (); } catch (SemanticGraph::InvalidPath const&) { - abs_path = path.native_file_string (); + abs_path = path.string (); } -#else - // The new ABI does not have a fallback native representation - abs_path = path.string (); -#endif NarrowString tf (trans_.translate_schema (abs_path)); -#if !defined(BOOST_FILESYSTEM_VERSION) || BOOST_FILESYSTEM_VERSION == 2 - NarrowString file (tf ? tf : path.leaf ()); -#else - NarrowString file (tf ? tf : path.filename ().string()); -#endif + NarrowString file (tf ? tf : path.leaf ().string ()); - Size p (file.rfind ('.')); + size_t p (file.rfind ('.')); NarrowString ext ( p != NarrowString::npos ? NarrowString (file, p) : ""); @@ -388,7 +408,7 @@ namespace XSDFrontend // NarrowString new_name (base); - for (UnsignedLong n (1); + for (unsigned long n (1); file_set.find (new_name) != file_set.end (); ++n) { @@ -402,11 +422,7 @@ namespace XSDFrontend try { -#if !defined(BOOST_FILESYSTEM_VERSION) || BOOST_FILESYSTEM_VERSION == 2 (*i)->context ().set ("renamed", SemanticGraph::Path (new_name)); -#else - (*i)->context ().set ("renamed", SemanticGraph::Path (new_name.c_str())); -#endif } catch (SemanticGraph::InvalidPath const&) { @@ -424,9 +440,9 @@ namespace XSDFrontend // TypeSchemaMap tsm; - for (Schemas::Iterator i (schemas.begin ()); i != schemas.end (); ++i) + for (Schemas::iterator i (schemas.begin ()); i != schemas.end (); ++i) { - process_schema (**i, root, *xsd, tsm, file_set, trans_); + process_schema (**i, root, *xsd, tsm, file_set, fat_type_file_, trans_); } // wcerr << tsm.size () << " type schema nodes" << endl; @@ -434,7 +450,7 @@ namespace XSDFrontend // Establish include/import dependencies. While at it add the // new schemas to the list which we will return. // - for (TypeSchemaMap::Iterator i (tsm.begin ()); i != tsm.end (); ++i) + for (TypeSchemaMap::iterator i (tsm.begin ()); i != tsm.end (); ++i) { SemanticGraph::Schema& s (*i->second); Type t (s, root, by_value_key_, tsm); -- cgit v1.2.3