Fix filename casing in mobile_System.Data.dll.sources
[mono.git] / mcs / tools / mono-xsd / NewMonoXSD.cs
old mode 100755 (executable)
new mode 100644 (file)
index 0ba6ac6..48caa8d
@@ -40,8 +40,10 @@ namespace Mono.Util {
                        "   /e /element:NAME   Element from schema to generate code for.\n" +\r
                        "                      Multiple elements can be specified.\n" +\r
                        "   /u /uri:NAME       Namespace uri of the elements to generate code for.\n" +\r
-                       "   /l /language:NAME  The language to use for the generated code.\n" +\r
-                       "                      Currently, the only supported language is CS (C#).\n" +\r
+                       "   /l /language:NAME  The language, or type name of custom CodeDomProvider\n" +\r
+                       "                      to use for the generated code.\n" +\r
+                       "                      Shorthand specifiers are: \"CS\" (C#) and \"VB\" (VB.NET).\n" +\r
+                       "                      For type name, assembly qualified name is required.\n" +\r
                        "   /g /generator:TYPE Code Generator type name, followed by ','\n" + \r
                        "                      and assembly file name.\n" +\r
                        "   /o /outputdir:PATH The directory where to generate the code or schemas.\n" +\r
@@ -76,9 +78,12 @@ namespace Mono.Util {
                        {\r
                                new Driver().Run (args);\r
                        }\r
-                       catch (Exception ex)\r
+                       catch (ApplicationException ex)\r
                        {\r
                                Console.WriteLine (ex.Message);\r
+                       }\r
+                       catch (Exception ex)\r
+                       {\r
                                Console.WriteLine (ex);\r
                        }\r
                }\r
@@ -110,10 +115,13 @@ namespace Mono.Util {
 \r
                        foreach (string arg in args)\r
                        {\r
-                               if (!arg.StartsWith ("--") && !arg.StartsWith ("/")) {\r
-                                       if (arg.EndsWith (".dll") || arg.EndsWith (".exe"))\r
+                               if (!arg.StartsWith ("--") && !arg.StartsWith ("/") ||\r
+                                       (arg.StartsWith ("/") && arg.IndexOfAny (Path.InvalidPathChars) == -1)\r
+                                       ) \r
+                               {\r
+                                       if ((arg.EndsWith (".dll") || arg.EndsWith (".exe")) && !arg.Substring (1).StartsWith ("generator:") && !arg.Substring (1).StartsWith ("g:"))\r
                                        {\r
-                                               if (!readingFiles) throw new Exception (incorrectOrder);\r
+                                               if (!readingFiles) throw new ApplicationException (incorrectOrder);\r
                                                assemblies.Add (arg);\r
                                                assemblyOptions = true;\r
                                                continue;\r
@@ -132,7 +140,7 @@ namespace Mono.Util {
                                                inference = true;\r
                                                continue;\r
                                        }\r
-                                       else //if (!arg.StartsWith ("/") && !arg.StartsWith ("-"))\r
+                                       else if (!arg.StartsWith ("/"))\r
                                        {\r
                                                if (!readingFiles) Error (incorrectOrder);\r
                                                unknownFiles.Add (arg);\r
@@ -202,6 +210,10 @@ namespace Mono.Util {
                                        Console.WriteLine (helpString);\r
                                        return;\r
                                }\r
+                               else if (option == "nologo")\r
+                               {\r
+                                       // ignore, since we do not output a logo anyway\r
+                               }\r
                                else\r
                                        Error (unknownOption, option);\r
                        }\r
@@ -217,6 +229,9 @@ namespace Mono.Util {
 \r
                        if (outputDir == null) outputDir = ".";\r
 \r
+                       string typename = null;\r
+                       Type generatorType = null;\r
+\r
                        if (language != null) {\r
                                switch (language) {\r
                                case "CS":\r
@@ -226,41 +241,41 @@ namespace Mono.Util {
                                        provider = new VBCodeProvider ();\r
                                        break;\r
                                default:\r
-                                       Error (languageNotSupported, language);\r
+                                       typename = StripQuot (language);\r
+\r
+                                       generatorType = Type.GetType (typename);\r
+                                       if (generatorType == null)\r
+                                               Error (generatorTypeNotFound, typename);\r
                                        break;\r
                                }\r
                        }\r
 \r
                        if (providerOption != null) {\r
                                string param = providerOption;\r
-                               string typename;\r
-                               Type generatorType;\r
                                int comma = param.IndexOf (',');\r
                                if (comma < 0) {\r
-                                       typename = param;\r
+                                       typename = StripQuot (param);\r
                                        generatorType = Type.GetType (param);\r
                                } else {\r
                                        typename = param.Substring (0, comma);\r
                                        string asmName = param.Substring (comma + 1);\r
-#if NET_1_1\r
                                        Assembly asm = Assembly.LoadFile (asmName);\r
-#else\r
-                                       Assembly asm = Assembly.LoadFrom (asmName);\r
-#endif\r
                                        if (asm == null)\r
                                                Error (generatorAssemblyNotFound, asmName);\r
                                        generatorType = asm.GetType (typename);\r
                                }\r
                                if (generatorType == null)\r
                                        Error (generatorTypeNotFound, typename);\r
+                       }\r
+                       if (generatorType != null) {\r
                                if (!generatorType.IsSubclassOf (typeof (CodeDomProvider)))\r
                                        Error (generatorTypeIsNotCodeGenerator, typename);\r
                                try {\r
                                        provider = (CodeDomProvider) Activator.CreateInstance (generatorType, null);\r
                                } catch (Exception ex) {\r
-                                       Error (generatorThrewException, param);\r
+                                       Error (generatorThrewException, generatorType.AssemblyQualifiedName.ToString () + " --> " + ex.Message);\r
                                }\r
-                               Console.WriteLine ("Loaded custom generator type " + param + " .");\r
+                               Console.WriteLine ("Loaded custom generator type " + generatorType + " .");\r
                        }\r
                        if (provider == null)\r
                                provider = new CSharpCodeProvider ();\r
@@ -362,7 +377,7 @@ namespace Mono.Util {
                        foreach (string fileName in schemaNames)\r
                        {\r
                                StreamReader sr = new StreamReader (fileName);\r
-                               schemas.Add (XmlSchema.Read (sr, null));\r
+                               schemas.Add (XmlSchema.Read (sr, new ValidationEventHandler (HandleValidationError)));\r
                                sr.Close ();\r
 \r
                                if (targetFile == "") targetFile = Path.GetFileNameWithoutExtension (fileName);\r
@@ -387,10 +402,10 @@ namespace Mono.Util {
                        else\r
                        {\r
                                foreach (XmlSchema schema in schemas) {\r
-                                       if (!schema.IsCompiled) schema.Compile (null);\r
-                                       foreach (XmlSchemaObject ob in schema.Items)\r
-                                               if (ob is XmlSchemaElement)\r
-                                                       qnames.Add (((XmlSchemaElement)ob).QualifiedName);\r
+                                       if (!schema.IsCompiled) schema.Compile (new ValidationEventHandler (HandleValidationError));\r
+                                       foreach (XmlSchemaElement el in schema.Elements.Values)\r
+                                               if (!qnames.Contains (el.QualifiedName))\r
+                                                       qnames.Add (el.QualifiedName);\r
                                }\r
                        }\r
 \r
@@ -426,7 +441,7 @@ namespace Mono.Util {
 \r
                public void GenerateDataset ()\r
                {\r
-                       if (namesp == null) namesp = "Schemas";\r
+                       if (namesp == null) namesp = "";\r
                        if (uri == null) uri = "";\r
                        string targetFile = "";\r
 \r
@@ -460,14 +475,33 @@ namespace Mono.Util {
                        Console.WriteLine ("Written file " + genFile);\r
                }\r
 \r
+               void HandleValidationError (object o, ValidationEventArgs e)\r
+               {\r
+                       Console.WriteLine ("{0}: {1} {2}",\r
+                               e.Severity == XmlSeverityType.Error ? "Error" : "Warning",\r
+                               e.Message,\r
+                               e.Exception != null ? e.Exception.Message : null);\r
+               }\r
+\r
                public void Error (string msg)\r
                {\r
-                       throw new Exception (msg);\r
+                       throw new ApplicationException (msg);\r
                }\r
 \r
                public void Error (string msg, string param)\r
                {\r
-                       throw new Exception (string.Format(msg,param));\r
+                       throw new ApplicationException (string.Format(msg,param));\r
+               }\r
+\r
+               private string StripQuot (string input)\r
+               {\r
+                       if (input.Length < 2)\r
+                               return input;\r
+                       if (input [0] == '"' && input [input.Length -1] == '"' ||\r
+                               input [0] == '\'' && input [input.Length - 1] == '\'')\r
+                               return input.Substring (1, input.Length - 2);\r
+                       else\r
+                               return language;\r
                }\r
        }\r
 }\r