aboutsummaryrefslogtreecommitdiff
path: root/xsd-frontend
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-02-19 10:26:27 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-02-19 10:26:27 +0200
commit1bdeb8aa9049a943de8d39c730d66b41d577a534 (patch)
tree100ec7579e6dadb50db5aa037424d6aee742bf39 /xsd-frontend
parent63bc2f3535bb87d017cc277d7833b79e33853e08 (diff)
Add support for translating schema file paths
Diffstat (limited to 'xsd-frontend')
-rw-r--r--xsd-frontend/transformations/schema-per-type.cxx92
-rw-r--r--xsd-frontend/transformations/schema-per-type.hxx16
2 files changed, 86 insertions, 22 deletions
diff --git a/xsd-frontend/transformations/schema-per-type.cxx b/xsd-frontend/transformations/schema-per-type.cxx
index 1ceb73b..2cc6457 100644
--- a/xsd-frontend/transformations/schema-per-type.cxx
+++ b/xsd-frontend/transformations/schema-per-type.cxx
@@ -103,7 +103,7 @@ namespace XSDFrontend
SemanticGraph::Schema& xsd,
TypeSchemaMap& tsm,
FileSet& file_set,
- Transformations::TypeSchemaTranslator& trans)
+ Transformations::SchemaPerTypeTranslator& trans)
{
using namespace SemanticGraph;
@@ -129,19 +129,24 @@ namespace XSDFrontend
// Add a new schema node.
//
Path path;
- String wbase (trans.translate (ns.name (), name));
+ String tn (trans.translate_type (ns.name (), name));
+ String wbase (tn ? tn : name);
try
{
NarrowString base (wbase.to_narrow ());
- // Escape directory separators.
+ // Escape directory separators unless they came from the
+ // translator.
//
- for (NarrowString::Iterator i (base.begin ()), e (base.end ());
- i != e; ++i)
+ if (!tn)
{
- if (*i == '/' || *i == '\\')
- *i = '_';
+ for (NarrowString::Iterator i (base.begin ()), e (base.end ());
+ i != e; ++i)
+ {
+ if (*i == '/' || *i == '\\')
+ *i = '_';
+ }
}
// Make sure it is unique.
@@ -158,7 +163,6 @@ namespace XSDFrontend
}
file_set.insert (file_name);
-
file_name += ".xsd";
try
@@ -170,7 +174,7 @@ namespace XSDFrontend
wcerr << "error: '" << file_name.c_str () << "' is not a valid "
<< "filesystem path" << endl;
- wcerr << "info: use type to file name translation mechanisms "
+ wcerr << "info: use type to file name translation mechanism "
<< "to resolve this" << endl;
throw Failed ();
@@ -181,7 +185,7 @@ namespace XSDFrontend
wcerr << "error: '" << wbase << "' cannot be represented as a "
<< "narrow string" << endl;
- wcerr << "info: use type to file name translation mechanisms "
+ wcerr << "info: use type to file name translation mechanism "
<< "to resolve this" << endl;
throw Failed ();
@@ -304,7 +308,7 @@ namespace XSDFrontend
namespace Transformations
{
SchemaPerType::
- SchemaPerType (TypeSchemaTranslator& trans, Char const* by_value_key)
+ SchemaPerType (SchemaPerTypeTranslator& trans, Char const* by_value_key)
: by_value_key_ (by_value_key), trans_ (trans)
{
}
@@ -338,11 +342,65 @@ namespace XSDFrontend
for (Schemas::Iterator i (schemas.begin ()); i != schemas.end (); ++i)
{
- NarrowString s ((*i)->used_begin ()->path ().leaf ());
+ 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.string ();
+ }
+ catch (SemanticGraph::InvalidPath const&)
+ {
+ abs_path = path.native_file_string ();
+ }
+
+ NarrowString tf (trans_.translate_schema (abs_path));
+ NarrowString file (tf ? tf : path.leaf ());
+
+ Size p (file.rfind ('.'));
+ NarrowString ext (
+ p != NarrowString::npos ? NarrowString (file, p) : "");
+
+ NarrowString base (
+ p != NarrowString::npos ? NarrowString (file, 0, p) : file);
- Size p (s.rfind ('.'));
- file_set.insert (
- p != NarrowString::npos ? NarrowString (s, 0, p) : s);
+ // Make sure it is unique.
+ //
+ NarrowString new_name (base);
+
+ for (UnsignedLong 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.
@@ -370,8 +428,8 @@ namespace XSDFrontend
return schemas;
}
- TypeSchemaTranslator::
- ~TypeSchemaTranslator ()
+ SchemaPerTypeTranslator::
+ ~SchemaPerTypeTranslator ()
{
}
}
diff --git a/xsd-frontend/transformations/schema-per-type.hxx b/xsd-frontend/transformations/schema-per-type.hxx
index 79912f7..89b6d83 100644
--- a/xsd-frontend/transformations/schema-per-type.hxx
+++ b/xsd-frontend/transformations/schema-per-type.hxx
@@ -18,14 +18,20 @@ namespace XSDFrontend
{
using namespace Cult::Types;
- class TypeSchemaTranslator
+ class SchemaPerTypeTranslator
{
public:
virtual
- ~TypeSchemaTranslator ();
+ ~SchemaPerTypeTranslator ();
+ // The following two functions should return empty string if
+ // there is no match.
+ //
virtual WideString
- translate (WideString const& ns, WideString const& name) = 0;
+ translate_type (WideString const& ns, WideString const& name) = 0;
+
+ virtual NarrowString
+ translate_schema (NarrowString const& abs_path) = 0;
};
// This transformation restructures the semantic graph to have
@@ -40,14 +46,14 @@ namespace XSDFrontend
// with the by_value_key key and it is true, then the schema
// for this type is included "strongly".
//
- SchemaPerType (TypeSchemaTranslator&, Char const* by_value_key = 0);
+ SchemaPerType (SchemaPerTypeTranslator&, Char const* by_value_key = 0);
Cult::Containers::Vector<SemanticGraph::Schema*>
transform (SemanticGraph::Schema&);
private:
Char const* by_value_key_;
- TypeSchemaTranslator& trans_;
+ SchemaPerTypeTranslator& trans_;
};
}
}