From 5ec1c8ff21b5c19e39d5bd12aa3a017a78b56b98 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 19 Feb 2010 10:31:47 +0200 Subject: Add support for translating schema paths in fpt mode New options: --schema-file-regex, --schema-file-regex-trace. --- NEWS | 5 + documentation/xsd.1 | 73 ++++++++++++-- documentation/xsd.xhtml | 61 ++++++++++-- xsd/cxx/parser/elements.cxx | 8 +- xsd/cxx/parser/generator.cxx | 44 ++++++--- xsd/cxx/parser/generator.hxx | 1 + xsd/cxx/tree/elements.cxx | 8 +- xsd/cxx/tree/generator.cxx | 38 ++++++-- xsd/cxx/tree/generator.hxx | 1 + xsd/xsd.cxx | 228 +++++++++++++++++++++++++++++-------------- 10 files changed, 351 insertions(+), 116 deletions(-) diff --git a/NEWS b/NEWS index 7cad774..650da63 100644 --- a/NEWS +++ b/NEWS @@ -24,6 +24,11 @@ Version 3.3.0 * Automatic mapping for the urn-style XML namespaces. The last component in the urn name is used to derive the C++ namespace name. + * New option, --schema-file-regex, in combination with the existing + --type-file-regex, can be used to place the generated files into + subdirectories or to resolve file name conflicts in the file-per- + type mode (--file-per-type). + * Warning id's have changed to start with a letter identifying the component issuing the warning. F - compiler frontend, D - compiler driver, P - C++/Parser mapping, T - C++/Tree mapping. diff --git a/documentation/xsd.1 b/documentation/xsd.1 index dd244ab..fc652d6 100644 --- a/documentation/xsd.1 +++ b/documentation/xsd.1 @@ -192,7 +192,7 @@ or .I replacement is not supported. -All regular expressions are pushed into a stack with the last specified +All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. Regular expressions are applied to a string in the form @@ -261,7 +261,7 @@ Add to the list of names that should not be used as identifiers. The name can optionally be followed by .B = -and the replacement name that should be used instead. All C++ keywords +and the replacement name that should be used instead. All the C++ keywords are already in this list. \" @@ -307,7 +307,7 @@ or .I replacement is not supported. -All regular expressions are pushed into a stack with the last specified +All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. As an example, the following expression transforms paths in the form @@ -571,7 +571,7 @@ or .I replacement is not supported. -All regular expressions are pushed into a stack with the last +All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. Regular expressions are applied to a string in the form @@ -629,7 +629,7 @@ Escaping of the delimiter character in .I pattern or .I replacement -is not supported. All regular expressions are pushed into a stack with the +is not supported. All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. @@ -655,14 +655,18 @@ Note that in this mode you only need to compile the root schema(s) and the code will be generated for all included and imported schemas. This compilation mode is primarily useful when some of your schemas cannot be compiled separately or have cyclic dependencies which involve type -inheritance. +inheritance. Other options related to this mode are: +.BR --type-file-regex , +.BR --schema-file-regex, +and +.BR --file-list . .IP "\fB\--type-file-regex \fIregex\fR" Add .I regex to the list of regular expressions used to translate type names to file names when the -.B --type-per-file +.B --file-per-type option is specified. .I regex is a perl-like regular expression in the form @@ -673,7 +677,7 @@ Escaping of the delimiter character in .I pattern or .I replacement -is not supported. All regular expressions are pushed into a stack with +is not supported. All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. Regular expressions are applied to a string in the form @@ -698,6 +702,57 @@ the option. Use this option to find out why your regular expressions don't do what you expected them to do. +.IP "\fB\--schema-file-regex \fIregex\fR" +Add +.I regex +to the list of regular expressions used to translate schema file names +when the +.B --file-per-type +option is specified. +.I regex +is a perl-like regular expression in the form +.BI / pattern / replacement /\fR. +Any character can be used as a delimiter instead of +.BR / . +Escaping of the delimiter character in +.I pattern +or +.I replacement +is not supported. All the regular expressions are pushed into a stack +with the last specified expression considered first. The first match +that succeeds is used. Regular expressions are applied to the absolute +filesystem path of a schema file and the result, including the directory +part, if any, is used to derive the +.B #include +directive paths as well as the generated C++ file paths. This option, along +with +.B --type-file-regex +are primarily used to place the generated files into subdirectories or to +resolve file name conflicts. + +For example, the following expression maps schema files in the +.B foo/1.0.0/ +subdirectory to the files in the +.B foo/ +subdirectory. As a result, the +.B #include +directive paths for such schemas will be in the +.B foo/schema.hxx +form and the generated C++ files will be placed into the +.B foo/ +subdirectory: + +.B %.*/foo/1.0.0/(.+)%foo/$1% + +See also the REGEX AND SHELL QUOTING section below. + +.IP "\fB\--schema-file-regex-trace\fR" +Trace the process of applying regular expressions specified with +the +.B --schema-file-regex +option. Use this option to find out why your regular expressions +don't do what you expected them to do. + .IP "\fB\--file-list \fIfile\fR" Write a list of generated C++ files to .IR file . @@ -1388,7 +1443,7 @@ Escaping of the delimiter character in .I pattern or .I replacement -is not supported. All regular expressions for each category are pushed +is not supported. All the regular expressions for each category are pushed into a category-specific stack with the last specified expression considered first. The first match that succeeds is used. For the .B --one-accessor-regex diff --git a/documentation/xsd.xhtml b/documentation/xsd.xhtml index 932689c..59aaa76 100644 --- a/documentation/xsd.xhtml +++ b/documentation/xsd.xhtml @@ -165,7 +165,7 @@ Escaping of the delimiter character in pattern or replacement is not supported. -

All regular expressions are pushed into a stack with the last +

All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. Regular expressions are applied to a string in the form

@@ -221,7 +221,7 @@
Add name to the list of names that should not be used as identifiers. The name can optionally be followed by = and the replacement name that should be - used instead. All C++ keywords are already in this list. + used instead. All the C++ keywords are already in this list.
@@ -255,7 +255,7 @@ Escaping of the delimiter character in pattern or replacement is not supported. -

All regular expressions are pushed into a stack with the last +

All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used.

@@ -507,7 +507,7 @@ Escaping of the delimiter character in pattern or replacement is not supported. -

All regular expressions are pushed into a stack with the last +

All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. Regular expressions are applied to a string in the form

@@ -557,7 +557,7 @@ /pattern/replacement/. Any character can be used as a delimiter instead of /. Escaping of the delimiter character in pattern or - replacement is not supported. All regular + replacement is not supported. All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. @@ -585,19 +585,23 @@ schema(s) and the code will be generated for all included and imported schemas. This compilation mode is primarily useful when some of your schemas cannot be compiled separately or have cyclic - dependencies which involve type inheritance. + dependencies which involve type inheritance. Other options related + to this mode are: + --type-file-regex, + --schema-file-regex, and + --file-list.
--type-file-regex regex
Add regex to the list of regular expressions used to translate type names to file names when the - --type-per-file option is specified. + --file-per-type option is specified. regex is a perl-like regular expression in the form /pattern/replacement/. Any character can be used as a delimiter instead of /. Escaping of the delimiter character in pattern or - replacement is not supported. All regular + replacement is not supported. All the regular expressions are pushed into a stack with the last specified expression considered first. The first match that succeeds is used. Regular expressions are applied to a string in the form @@ -620,6 +624,45 @@ them to do.
+
--schema-file-regex regex
+
Add regex to the list of regular expressions + used to translate schema file names when the + --file-per-type option is specified. + regex is a perl-like regular expression in the form + /pattern/replacement/. + Any character can be used as a delimiter instead of /. + Escaping of the delimiter character in pattern or + replacement is not supported. All the regular + expressions are pushed into a stack with the last specified + expression considered first. The first match that succeeds is used. + Regular expressions are applied to the absolute filesystem path + of a schema file and the result, including the directory part, + if any, is used to derive the #include directive + paths as well as the generated C++ file paths. This option, along + with --type-file-regex are primarily used to + place the generated files into subdirectories or to resolve file + name conflicts. + +

For example, the following expression maps schema files in the + foo/1.0.0/ subdirectory to the files in + the foo/ subdirectory. As a result, the + #include directive paths for such schemas + will be in the foo/schema.hxx form and + the generated C++ files will be placed into the + foo/ subdirectory:

+ +

%.*/foo/1.0.0/(.+)%foo/$1%

+ +

See also the REGEX AND SHELL QUOTING section below.

+
+ +
--schema-file-regex-trace
+
Trace the process of applying regular expressions specified with + the --schema-file-regex option. Use this option + to find out why your regular expressions don't do what you expected + them to do. +
+
--file-list file
@@ -1230,7 +1273,7 @@ Any character can be used as a delimiter instead of /. Escaping of the delimiter character in pattern or replacement is not supported. - All regular expressions for each category are pushed into a + All the regular expressions for each category are pushed into a category-specific stack with the last specified expression considered first. The first match that succeeds is used. For the --one-accessor-regex (accessors with cardinality one), diff --git a/xsd/cxx/parser/elements.cxx b/xsd/cxx/parser/elements.cxx index 392bf16..140f041 100644 --- a/xsd/cxx/parser/elements.cxx +++ b/xsd/cxx/parser/elements.cxx @@ -212,6 +212,7 @@ namespace CXX // Support for weak (forward) inclusion used in the file-per-type // compilation model. // + SemanticGraph::Schema& s (u.schema ()); Boolean weak (u.context ().count ("weak")); if (weak && (type_ == header || type_ == impl_header)) @@ -220,7 +221,7 @@ namespace CXX // in the impl files. // if (type_ == header) - schema_.dispatch (u.schema ()); + schema_.dispatch (s); return; } @@ -228,7 +229,10 @@ namespace CXX if (type_ == source && !weak) return; - SemanticGraph::Path path (u.path ()); + SemanticGraph::Path path ( + s.context ().count ("renamed") + ? s.context ().get ("renamed") + : u.path ()); // Try to use the portable representation of the path. If that // fails, fall back to the native representation. diff --git a/xsd/cxx/parser/generator.cxx b/xsd/cxx/parser/generator.cxx index 60d2785..617ff4f 100644 --- a/xsd/cxx/parser/generator.cxx +++ b/xsd/cxx/parser/generator.cxx @@ -613,6 +613,7 @@ namespace CXX generate (Parser::CLI::Options const& ops, Schema& schema, Path const& file_path, + Boolean fpt, StringLiteralMap const& string_literal_map, Boolean gen_driver, const WarningSet& disabled_warnings, @@ -935,22 +936,13 @@ namespace CXX cxx_driver_path = Path (cxx_driver_name, boost::filesystem::native); } - if (NarrowString dir = ops.value ()) + Path out_dir; + + if (NarrowString dir = ops.value ()) { try { - Path path (dir, boost::filesystem::native); - - hxx_path = path / hxx_path; - ixx_path = path / ixx_path; - cxx_path = path / cxx_path; - - if (impl || driver) - { - hxx_impl_path = path / hxx_impl_path; - cxx_impl_path = path / cxx_impl_path; - cxx_driver_path = path /cxx_driver_path; - } + out_dir = Path (dir, boost::filesystem::native); } catch (InvalidPath const&) { @@ -959,6 +951,32 @@ namespace CXX } } + if (fpt && !generate_xml_schema) + { + // In the file-per-type mode the schema files are always local + // unless the user added the directory so that we propagate this + // to the output files. + // + Path fpt_dir (file_path.branch_path ()); + + if (!fpt_dir.empty ()) + out_dir /= fpt_dir; + } + + if (!out_dir.empty ()) + { + hxx_path = out_dir / hxx_path; + ixx_path = out_dir / ixx_path; + cxx_path = out_dir / cxx_path; + + if (impl || driver) + { + hxx_impl_path = out_dir / hxx_impl_path; + cxx_impl_path = out_dir / cxx_impl_path; + cxx_driver_path = out_dir /cxx_driver_path; + } + } + // Open the impl files first so that if open fails, the skel files // are not deleted. // diff --git a/xsd/cxx/parser/generator.hxx b/xsd/cxx/parser/generator.hxx index 47220ca..eaa1ecd 100644 --- a/xsd/cxx/parser/generator.hxx +++ b/xsd/cxx/parser/generator.hxx @@ -42,6 +42,7 @@ namespace CXX generate (CLI::Options const& options, XSDFrontend::SemanticGraph::Schema&, XSDFrontend::SemanticGraph::Path const& file, + Boolean file_per_type, StringLiteralMap const&, Boolean gen_driver, const WarningSet& disabled_warnings, diff --git a/xsd/cxx/tree/elements.cxx b/xsd/cxx/tree/elements.cxx index aafc442..503ca72 100644 --- a/xsd/cxx/tree/elements.cxx +++ b/xsd/cxx/tree/elements.cxx @@ -1253,6 +1253,7 @@ namespace CXX // Type t (type_); Boolean weak (u.context ().count ("weak")); + SemanticGraph::Schema& s (u.schema ()); if (weak && t == header) { @@ -1262,7 +1263,7 @@ namespace CXX t = forward; else { - schema_.dispatch (u.schema ()); + schema_.dispatch (s); return; } } @@ -1270,7 +1271,10 @@ namespace CXX if (t == source && !weak) return; - SemanticGraph::Path path (u.path ()); + SemanticGraph::Path path ( + s.context ().count ("renamed") + ? s.context ().get ("renamed") + : u.path ()); // Try to use the portable representation of the path. If that // fails, fall back to the native representation. diff --git a/xsd/cxx/tree/generator.cxx b/xsd/cxx/tree/generator.cxx index ae86767..c13bf1f 100644 --- a/xsd/cxx/tree/generator.cxx +++ b/xsd/cxx/tree/generator.cxx @@ -834,6 +834,7 @@ namespace CXX generate (Tree::CLI::Options const& ops, Schema& schema, Path const& file_path, + Boolean fpt, StringLiteralMap const& string_literal_map, const WarningSet& disabled_warnings, FileList& file_list, @@ -1048,19 +1049,13 @@ namespace CXX Path (cxx_expr.merge (name), boost::filesystem::native)); } - if (NarrowString dir = ops.value ()) + Path out_dir; + + if (NarrowString dir = ops.value ()) { try { - Path path (dir, boost::filesystem::native); - - hxx_path = path / hxx_path; - ixx_path = path / ixx_path; - fwd_path = path / fwd_path; - - for (Paths::Iterator i (cxx_paths.begin ()); - i != cxx_paths.end (); ++i) - *i = path / *i; + out_dir = Path (dir, boost::filesystem::native); } catch (InvalidPath const&) { @@ -1069,6 +1064,29 @@ namespace CXX } } + if (fpt && !generate_xml_schema) + { + // In the file-per-type mode the schema files are always local + // unless the user added the directory so that we propagate this + // to the output files. + // + Path fpt_dir (file_path.branch_path ()); + + if (!fpt_dir.empty ()) + out_dir /= fpt_dir; + } + + if (!out_dir.empty ()) + { + hxx_path = out_dir / hxx_path; + ixx_path = out_dir / ixx_path; + fwd_path = out_dir / fwd_path; + + for (Paths::Iterator i (cxx_paths.begin ()); + i != cxx_paths.end (); ++i) + *i = out_dir / *i; + } + // // WideOutputFileStream hxx (hxx_path, ios_base::out); diff --git a/xsd/cxx/tree/generator.hxx b/xsd/cxx/tree/generator.hxx index 0bb5bb2..2e50392 100644 --- a/xsd/cxx/tree/generator.hxx +++ b/xsd/cxx/tree/generator.hxx @@ -37,6 +37,7 @@ namespace CXX generate (CLI::Options const& options, XSDFrontend::SemanticGraph::Schema&, XSDFrontend::SemanticGraph::Path const& file, + Boolean file_per_type, StringLiteralMap const&, const WarningSet& disabled_warnings, FileList& file_list, diff --git a/xsd/xsd.cxx b/xsd/xsd.cxx index f721699..5c81d7f 100644 --- a/xsd/xsd.cxx +++ b/xsd/xsd.cxx @@ -72,48 +72,52 @@ namespace CLI struct HelpOptionsSpec: Cult::CLI::OptionsSpec {}; - extern Key disable_warning = "disable-warning"; - extern Key sloc_limit = "sloc-limit"; - extern Key morph_anonymous = "morph-anonymous"; - extern Key preserve_anonymous = "preserve-anonymous"; - extern Key anonymous_regex = "anonymous-regex"; - extern Key anonymous_regex_trace = "anonymous-regex-trace"; - extern Key location_map = "location-map"; - extern Key location_regex = "location-regex"; - extern Key location_regex_trace = "location-regex-trace"; - extern Key custom_literals = "custom-literals"; - extern Key file_per_type = "file-per-type"; - extern Key type_file_regex = "type-file-regex"; - extern Key type_file_regex_trace = "type-file-regex-trace"; - extern Key file_list = "file-list"; - extern Key file_list_prologue = "file-list-prologue"; - extern Key file_list_epilogue = "file-list-epilogue"; - extern Key file_list_delim = "file-list-delim"; - extern Key disable_multi_import = "disable-multi-import"; // Undocumented. - extern Key disable_full_check = "disable-full-check"; // Undocumented. + extern Key disable_warning = "disable-warning"; + extern Key sloc_limit = "sloc-limit"; + extern Key morph_anonymous = "morph-anonymous"; + extern Key preserve_anonymous = "preserve-anonymous"; + extern Key anonymous_regex = "anonymous-regex"; + extern Key anonymous_regex_trace = "anonymous-regex-trace"; + extern Key location_map = "location-map"; + extern Key location_regex = "location-regex"; + extern Key location_regex_trace = "location-regex-trace"; + extern Key custom_literals = "custom-literals"; + extern Key file_per_type = "file-per-type"; + extern Key type_file_regex = "type-file-regex"; + extern Key type_file_regex_trace = "type-file-regex-trace"; + extern Key schema_file_regex = "schema-file-regex"; + extern Key schema_file_regex_trace = "schema-file-regex-trace"; + extern Key file_list = "file-list"; + extern Key file_list_prologue = "file-list-prologue"; + extern Key file_list_epilogue = "file-list-epilogue"; + extern Key file_list_delim = "file-list-delim"; + extern Key disable_multi_import = "disable-multi-import"; // Undocumented. + extern Key disable_full_check = "disable-full-check"; // Undocumented. typedef Cult::CLI::Options < - disable_warning, Cult::Containers::Vector, - sloc_limit, UnsignedLong, - morph_anonymous, Boolean, - preserve_anonymous, Boolean, - anonymous_regex, NarrowStrings, - anonymous_regex_trace, Boolean, - location_map, NarrowStrings, - location_regex, NarrowStrings, - location_regex_trace, Boolean, - custom_literals, NarrowString, - file_per_type, Boolean, - type_file_regex, NarrowStrings, - type_file_regex_trace, Boolean, - file_list, NarrowString, - file_list_prologue, NarrowString, - file_list_epilogue, NarrowString, - file_list_delim, NarrowString, - disable_multi_import, Boolean, - disable_full_check, Boolean + disable_warning, Cult::Containers::Vector, + sloc_limit, UnsignedLong, + morph_anonymous, Boolean, + preserve_anonymous, Boolean, + anonymous_regex, NarrowStrings, + anonymous_regex_trace, Boolean, + location_map, NarrowStrings, + location_regex, NarrowStrings, + location_regex_trace, Boolean, + custom_literals, NarrowString, + file_per_type, Boolean, + type_file_regex, NarrowStrings, + type_file_regex_trace, Boolean, + schema_file_regex, NarrowStrings, + schema_file_regex_trace, Boolean, + file_list, NarrowString, + file_list_prologue, NarrowString, + file_list_epilogue, NarrowString, + file_list_delim, NarrowString, + disable_multi_import, Boolean, + disable_full_check, Boolean > CommonOptions; @@ -174,22 +178,35 @@ private: // // -struct TypeSchemaTranslator: Transformations::TypeSchemaTranslator +struct SchemaPerTypeTranslator: Transformations::SchemaPerTypeTranslator { struct Failed {}; - TypeSchemaTranslator (NarrowStrings const& regex, Boolean trace); + SchemaPerTypeTranslator (NarrowStrings const& type_regex, + Boolean type_trace, + NarrowStrings const& schema_regex, + Boolean schema_trace); virtual WideString - translate (WideString const& ns, WideString const& name); + translate_type (WideString const& ns, WideString const& name); + + virtual NarrowString + translate_schema (NarrowString const& file); private: - typedef BackendElements::Regex::Expression Regex; - typedef BackendElements::Regex::Format RegexFormat; - typedef Cult::Containers::Vector RegexVector; + typedef BackendElements::Regex::Expression TypeRegex; + typedef BackendElements::Regex::Format TypeRegexFormat; + typedef Cult::Containers::Vector TypeRegexVector; - RegexVector regex_; - Boolean trace_; + TypeRegexVector type_regex_; + Boolean type_trace_; + + typedef BackendElements::Regex::Expression SchemaRegex; + typedef BackendElements::Regex::Format SchemaRegexFormat; + typedef Cult::Containers::Vector SchemaRegexVector; + + SchemaRegexVector schema_regex_; + Boolean schema_trace_; }; // @@ -358,7 +375,7 @@ main (Int argc, Char* argv[]) e << "--type-file-regex " << endl << " Add the provided regular expression to the list of\n" << " regular expressions used to translate type names\n" - << " to file names when the --type-per-file option is\n" + << " to file names when the --file-per-type option is\n" << " specified." << endl; @@ -367,6 +384,18 @@ main (Int argc, Char* argv[]) << " specified with the --type-file-regex option." << endl; + e << "--schema-file-regex " << endl + << " Add the provided regular expression to the list\n" + << " of regular expressions used to translate schema\n" + << " file names when the --file-per-type option is\n" + << " specified." + << endl; + + e << "--schema-file-regex-trace" << endl + << " Trace the process of applying regular expressions\n" + << " specified with the --schema-file-regex option." + << endl; + // File list options. // e << "--file-list " << endl @@ -742,6 +771,7 @@ main (Int argc, Char* argv[]) *tree_ops, *schema, tu, + false, string_literal_map, disabled_w, file_list, @@ -762,6 +792,7 @@ main (Int argc, Char* argv[]) *parser_ops, *schema, tu, + false, string_literal_map, true, disabled_w, @@ -866,9 +897,11 @@ main (Int argc, Char* argv[]) // typedef Cult::Containers::Vector Schemas; - TypeSchemaTranslator type_translator ( + SchemaPerTypeTranslator type_translator ( common_ops.value (), - common_ops.value ()); + common_ops.value (), + common_ops.value (), + common_ops.value ()); Transformations::SchemaPerType trans (type_translator); Schemas schemas (trans.transform (*schema)); @@ -879,7 +912,10 @@ main (Int argc, Char* argv[]) i != e; ++i) { SemanticGraph::Schema& s (**i); - SemanticGraph::Path path (s.used_begin ()->path ()); + SemanticGraph::Path path ( + s.context ().count ("renamed") + ? s.context ().get ("renamed") + : s.used_begin ()->path ()); if (cmd == "cxx-tree") { @@ -889,6 +925,7 @@ main (Int argc, Char* argv[]) *tree_ops, s, path, + true, string_literal_map, disabled_w, file_list, @@ -911,6 +948,7 @@ main (Int argc, Char* argv[]) *parser_ops, s, path, + true, string_literal_map, i == b, disabled_w, @@ -1004,7 +1042,7 @@ main (Int argc, Char* argv[]) { // Diagnostic has already been issued. } - catch (TypeSchemaTranslator::Failed const&) + catch (SchemaPerTypeTranslator::Failed const&) { // Diagnostic has already been issued. } @@ -1202,20 +1240,24 @@ translate (WideString const& file, return name; } -// TypeSchemaTranslator +// SchemaPerTypeTranslator // -TypeSchemaTranslator:: -TypeSchemaTranslator (NarrowStrings const& regex, Boolean trace) - : trace_ (trace) +SchemaPerTypeTranslator:: +SchemaPerTypeTranslator (NarrowStrings const& type_regex, + Boolean type_trace, + NarrowStrings const& schema_regex, + Boolean schema_trace) + : type_trace_ (type_trace), schema_trace_ (schema_trace) { - for (NarrowStrings::ConstIterator i (regex.begin ()); i != regex.end (); ++i) + for (NarrowStrings::ConstIterator i (type_regex.begin ()); + i != type_regex.end (); ++i) { try { - regex_.push_back (Regex (*i)); + type_regex_.push_back (TypeRegex (*i)); } - catch (RegexFormat const& e) + catch (TypeRegexFormat const& e) { wcerr << "error: invalid type file regex: '" << e.expression () << "': " << e.description () << endl; @@ -1223,42 +1265,86 @@ TypeSchemaTranslator (NarrowStrings const& regex, Boolean trace) throw Failed (); } } + + for (NarrowStrings::ConstIterator i (schema_regex.begin ()); + i != schema_regex.end (); ++i) + { + try + { + schema_regex_.push_back (SchemaRegex (*i)); + } + catch (SchemaRegexFormat const& e) + { + wcerr << "error: invalid type file regex: '" << + e.expression ().c_str () << "': " << e.description ().c_str () << endl; + + throw Failed (); + } + } } -WideString TypeSchemaTranslator:: -translate (WideString const& ns, WideString const& name) +WideString SchemaPerTypeTranslator:: +translate_type (WideString const& ns, WideString const& name) { - if (regex_.empty ()) - return name; - WideString s (ns + L' ' + name); - if (trace_) + if (type_trace_) wcerr << "type: '" << s << "'" << endl; - for (RegexVector::ReverseIterator i (regex_.rbegin ()); - i != regex_.rend (); ++i) + for (TypeRegexVector::ReverseIterator i (type_regex_.rbegin ()); + i != type_regex_.rend (); ++i) { - if (trace_) + if (type_trace_) wcerr << "try: '" << i->pattern () << "' : "; if (i->match (s)) { WideString r (i->merge (s)); - if (trace_) + if (type_trace_) wcerr << "'" << r << "' : +" << endl; return r; } - if (trace_) + if (type_trace_) wcerr << '-' << endl; } - // No match - return the type name. + // No match - return empty string. // - return name; + return L""; +} + +NarrowString SchemaPerTypeTranslator:: +translate_schema (NarrowString const& file) +{ + if (schema_trace_) + wcerr << "schema: '" << file.c_str () << "'" << endl; + + for (SchemaRegexVector::ReverseIterator i (schema_regex_.rbegin ()); + i != schema_regex_.rend (); ++i) + { + if (schema_trace_) + wcerr << "try: '" << i->pattern () << "' : "; + + if (i->match (file)) + { + NarrowString r (i->merge (file)); + + if (schema_trace_) + wcerr << "'" << r.c_str () << "' : +" << endl; + + return r; + } + + if (schema_trace_) + wcerr << '-' << endl; + } + + // No match - return empty string. + // + return ""; } // -- cgit v1.1