1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
// file : xsd-frontend/transformations/schema-per-type.cxx
// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC
// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
#include <strings.h> // strcasecmp
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <iostream>
#include <xsd-frontend/semantic-graph.hxx>
#include <xsd-frontend/traversal.hxx>
#include <xsd-frontend/transformations/schema-per-type.hxx>
using std::wcerr;
using std::endl;
namespace XSDFrontend
{
typedef Transformations::SchemaPerType::Failed Failed;
typedef std::vector<SemanticGraph::Schema*> Schemas;
typedef std::map<SemanticGraph::Type*, SemanticGraph::Schema*> TypeSchemaMap;
// Compare file paths case-insensitively.
//
struct FileComparator
{
bool
operator() (NarrowString const& x, NarrowString const& y) const
{
return strcasecmp (x.c_str (), y.c_str ()) < 0;
}
};
typedef std::set<NarrowString, FileComparator> FileSet;
namespace
{
// Go into included and imported schemas while making sure
// we don't process the same stuff more than once.
//
struct Uses: Traversal::Includes,
Traversal::Imports,
Traversal::Implies
{
Uses (Schemas& schemas, SemanticGraph::Schema*& xsd)
: schemas_ (schemas), xsd_ (xsd)
{
xsd_ = 0;
}
virtual void
traverse (SemanticGraph::Includes& i)
{
SemanticGraph::Schema& s (i.schema ());
if (!s.context ().count ("xsd-frontend-schema-per-type-seen"))
{
schemas_.push_back (&s);
s.context ().set ("xsd-frontend-schema-per-type-seen", true);
Traversal::Includes::traverse (i);
}
}
virtual void
traverse (SemanticGraph::Imports& i)
{
SemanticGraph::Schema& s (i.schema ());
if (!s.context ().count ("xsd-frontend-schema-per-type-seen"))
{
schemas_.push_back (&s);
s.context ().set ("xsd-frontend-schema-per-type-seen", true);
Traversal::Imports::traverse (i);
}
}
virtual void
traverse (SemanticGraph::Implies& i)
{
if (xsd_ == 0)
xsd_ = &i.schema ();
}
private:
Schemas& schemas_;
SemanticGraph::Schema*& xsd_;
};
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;
Path xsd_path ("XMLSchema.xsd");
Namespace& ns (dynamic_cast<Namespace&> (s.names_begin ()->named ()));
// We should be careful with iterator caching since we are going to
// remove some of the nodes.
//
for (Scope::NamesIterator i (ns.names_begin ()); i != ns.names_end ();)
{
Nameable& n (i->named ());
if (n.is_a<Type> ())
{
String name (n.name ());
// Remove from the namespace.
//
Scope::NamesIterator tmp (i++);
root.delete_edge (ns, n, *tmp);
// Add a new schema node.
//
Path path;
String tn (trans.translate_type (ns.name (), name));
String wbase (tn ? tn : name);
try
{
NarrowString base (wbase.to_narrow ());
// Escape directory separators unless they came from the
// translator.
//
if (!tn)
{
for (NarrowString::iterator i (base.begin ()), e (base.end ());
i != e; ++i)
{
if (*i == '/' || *i == '\\')
*i = '_';
}
}
// Make sure it is unique.
//
NarrowString file_name (base);
for (unsigned long i (1);
file_set.find (file_name) != file_set.end ();
++i)
{
std::ostringstream os;
os << i;
file_name = base + os.str ();
}
file_set.insert (file_name);
file_name += ".xsd";
try
{
path = Path (file_name);
}
catch (InvalidPath const&)
{
wcerr << "error: '" << file_name.c_str () << "' is not a valid "
<< "filesystem path" << endl;
wcerr << "info: use type to file name translation mechanism "
<< "to resolve this" << endl;
throw Failed ();
}
}
catch (NonRepresentable const&)
{
wcerr << "error: '" << wbase << "' cannot be represented as a "
<< "narrow string" << endl;
wcerr << "info: use type to file name translation mechanism "
<< "to resolve this" << endl;
throw Failed ();
}
Type& t (dynamic_cast<Type&> (n));
Schema& ts (root.new_node<Schema> (path, 1, 1));
root.new_edge<Implies> (ts, xsd, xsd_path);
Namespace& tns (root.new_node<Namespace> (path, 1, 1));
root.new_edge<Names> (ts, tns, ns.name ());
root.new_edge<Names> (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<Element> () &&
e.scope ().is_a<Namespace> () &&
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<Names> (tns, e, name);
}
}
}
// Add include to the original schema and enter into the
// type-schema map.
//
root.new_edge<Includes> (s, ts, path);
tsm[&t] = &ts;
}
else
++i;
}
}
struct Type: Traversal::List,
Traversal::Complex,
Traversal::Member
{
Type (SemanticGraph::Schema& schema,
SemanticGraph::Schema& root,
char const* by_value_key,
TypeSchemaMap& tsm)
: schema_ (schema),
root_ (root),
by_value_key_ (by_value_key),
tsm_ (tsm)
{
*this >> names_ >> *this;
}
virtual void
traverse (SemanticGraph::List& l)
{
// Treat item type as base type since it is impossible
// to create recursive constructs using list.
//
SemanticGraph::Type& t (l.argumented ().type ());
set_dep (t, false);
}
virtual void
traverse (SemanticGraph::Complex& c)
{
if (c.inherits_p ())
set_dep (c.inherits ().base (), false);
Traversal::Complex::names (c);
}
virtual void
traverse (SemanticGraph::Member& m)
{
SemanticGraph::Type& t (m.type ());
bool weak (
by_value_key_ == 0 ||
!t.context ().count (by_value_key_) ||
!t.context ().get<bool> (by_value_key_));
set_dep (t, weak);
}
private:
void
set_dep (SemanticGraph::Type& t, bool weak)
{
using namespace SemanticGraph;
TypeSchemaMap::iterator i (tsm_.find (&t));
// If a type is not present in the map then it must be
// a built-in type.
//
if (i == tsm_.end ())
return;
// Check if we already saw this type. Theoretically, it could
// be that we need to upgrade the type of include from weak to
// strong. But because inheritance is handled first, the type
// in the set will already be with the right type.
//
if (type_set_.find (&t) != type_set_.end ())
return;
type_set_.insert (&t);
Schema& s (*i->second);
Path path (s.used_begin ()->path ());
SemanticGraph::Uses* u;
if (s.names_begin ()->name () == schema_.names_begin ()->name ())
u = &root_.new_edge<Includes> (schema_, s, path);
else
u = &root_.new_edge<Imports> (schema_, s, path);
if (weak)
u->context().set ("weak", true);
}
private:
SemanticGraph::Schema& schema_;
SemanticGraph::Schema& root_;
char const* by_value_key_;
TypeSchemaMap& tsm_;
std::set<SemanticGraph::Type*> type_set_;
Traversal::Names names_;
};
}
namespace Transformations
{
SchemaPerType::
SchemaPerType (SchemaPerTypeTranslator& trans,
bool fat,
char const* key)
: fat_type_file_ (fat), by_value_key_ (key), trans_ (trans)
{
}
Schemas SchemaPerType::
transform (SemanticGraph::Schema& root)
{
// Collect initial schema nodes.
//
Schemas schemas;
SemanticGraph::Schema* xsd;
{
Traversal::Schema schema;
Uses uses (schemas, xsd);
schema >> uses >> schema;
// Some twisted schemas do recusive inclusions.
//
root.context ().set ("xsd-frontend-schema-per-type-seen", true);
schema.dispatch (root);
}
// wcerr << schemas.size () << " initial schema nodes" << endl;
// Add the schema file names to the file set.
//
FileSet file_set;
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<SemanticGraph::Path> ("absolute-path"));
// Translate the schema file name.
//
NarrowString abs_path;
// Try to use the portable representation of the path. If that
// fails, fall back to the native representation.
//
try
{
abs_path = path.posix_string ();
}
catch (SemanticGraph::InvalidPath const&)
{
abs_path = path.string ();
}
NarrowString tf (trans_.translate_schema (abs_path));
NarrowString file (tf ? tf : path.leaf ().string ());
size_t p (file.rfind ('.'));
NarrowString ext (
p != NarrowString::npos ? NarrowString (file, p) : "");
NarrowString base (
p != NarrowString::npos ? NarrowString (file, 0, p) : file);
// Make sure it is unique.
//
NarrowString new_name (base);
for (unsigned long n (1);
file_set.find (new_name) != file_set.end ();
++n)
{
std::ostringstream os;
os << n;
new_name = base + os.str ();
}
file_set.insert (new_name);
new_name += ext;
try
{
(*i)->context ().set ("renamed", SemanticGraph::Path (new_name));
}
catch (SemanticGraph::InvalidPath const&)
{
wcerr << "error: '" << new_name.c_str () << "' is not a valid "
<< "filesystem path" << endl;
wcerr << "info: use schema file name translation mechanism "
<< "to resolve this" << endl;
throw Failed ();
}
}
// Process each schema node.
//
TypeSchemaMap tsm;
for (Schemas::iterator i (schemas.begin ()); i != schemas.end (); ++i)
{
process_schema (**i, root, *xsd, tsm, file_set, fat_type_file_, trans_);
}
// wcerr << tsm.size () << " type schema nodes" << endl;
// 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)
{
SemanticGraph::Schema& s (*i->second);
Type t (s, root, by_value_key_, tsm);
t.dispatch (*i->first);
schemas.push_back (&s);
}
return schemas;
}
SchemaPerTypeTranslator::
~SchemaPerTypeTranslator ()
{
}
}
}
|