|
From: <te...@us...> - 2006-10-02 21:01:43
|
Revision: 11
http://svn.sourceforge.net/hex/?rev=11&view=rev
Author: tewald
Date: 2006-09-29 11:42:29 -0700 (Fri, 29 Sep 2006)
Log Message:
-----------
Trying commit again!
Modified Paths:
--------------
hexsdtools/trunk/HEXsd/CodeEmitter.cs
hexsdtools/trunk/HEXsd/HEXsd.csproj
hexsdtools/trunk/HEXsd/Namespace.cs
Added Paths:
-----------
hexsdtools/trunk/HEXsd/SchemaEmbedder.cs
Modified: hexsdtools/trunk/HEXsd/CodeEmitter.cs
===================================================================
--- hexsdtools/trunk/HEXsd/CodeEmitter.cs 2006-09-29 18:17:31 UTC (rev 10)
+++ hexsdtools/trunk/HEXsd/CodeEmitter.cs 2006-09-29 18:42:29 UTC (rev 11)
@@ -50,52 +50,19 @@
compilerParams.OutputAssembly = output;
// add relevant resources to assembly
- foreach (XmlSchema schema in _schemas.Schemas)
+ SchemaEmbedder schemaEmbedder = new SchemaEmbedder(compilerParams);
+ foreach (CodeNamespace ns in _unit.Namespaces)
{
- foreach (CodeNamespace ns in _unit.Namespaces)
+ foreach (XmlSchema schema in _schemas.Schemas)
{
if (ns.Name.Equals(Namespace.ConvertXmlNamespaceToClrNamespace(schema.TargetNamespace)))
{
- // need to patch schema and write results out to temp file
- string tempFilename = Path.Combine(compilerParams.TempFiles.TempDir,
- Namespace.ConvertXmlNamespaceToFileName(schema.TargetNamespace, ".xsd"));
- compilerParams.TempFiles.AddFile(tempFilename, true);
-
- foreach (XmlSchemaObject xso in schema.Includes)
- {
- if (xso is XmlSchemaImport)
- {
- XmlSchemaImport import = (XmlSchemaImport)xso;
- import.SchemaLocation = Namespace.ConvertXmlNamespaceToFileName(import.Namespace, ".xsd");
- }
- else
- {
- throw new NotImplementedException("No support for xs:include or xs:redefine");
- }
- }
-
- // TBD patch schemaLocations before saving
- using (FileStream stm = new FileStream(tempFilename, FileMode.Create, FileAccess.Write))
- {
- schema.Write(new StreamWriter(stm));
- }
-
- //Uri uri = new Uri(schema.SourceUri);
- //compilerParams.EmbeddedResources.Add(uri.LocalPath);
- compilerParams.EmbeddedResources.Add(tempFilename);
+ schemaEmbedder.EmbedSchemaAsResource(schema);
}
}
}
- // compile assembly
- //StringWriter writer = new StringWriter();
- //codeProvider.GenerateCodeFromCompileUnit(_unit, writer, new CodeGeneratorOptions());
- //writer.Flush();
- //string[] sources = new string[] { writer.GetStringBuilder().ToString() };
- //compilerParams.ReferencedAssemblies.Add("System.dll");
- //compilerParams.ReferencedAssemblies.Add("System.Xml.dll");
- //CompilerResults compilerResults = codeProvider.CompileAssemblyFromSource(compilerParams, sources);
-
+ // compile code to assembly
CompilerResults compilerResults = codeProvider.CompileAssemblyFromDom(compilerParams, _unit);
// report errors, if any
@@ -125,26 +92,6 @@
}
}
- // TBD this code is dup'd in Namespace class, fix it
- //private string ConvertXmlToClrNamespace(string xmlNamespace, bool pascalCase)
- //{
- // string converted = xmlNamespace.Replace(':', '.')
- // .Replace('/', '.')
- // .Replace('-', '.');
- // string[] parts = converted.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
- // string[] upperParts = new string[parts.Length];
- // for (int i = 0; i < parts.Length; i++)
- // {
- // string part = parts[i];
- // if (pascalCase && char.IsLower(part.ToCharArray()[0]))
- // {
- // part = string.Format("{0}{1}", part.Substring(0, 1).ToUpper(), part.Substring(1));
- // }
- // upperParts[i] = part;
- // }
- // return string.Join(".", upperParts);
- //}
-
// TBD should this be rationalized against Namespace.Convert... static methods?
private string CreateDefaultOutput(string ext)
{
Modified: hexsdtools/trunk/HEXsd/HEXsd.csproj
===================================================================
--- hexsdtools/trunk/HEXsd/HEXsd.csproj 2006-09-29 18:17:31 UTC (rev 10)
+++ hexsdtools/trunk/HEXsd/HEXsd.csproj 2006-09-29 18:42:29 UTC (rev 11)
@@ -45,11 +45,16 @@
<Compile Include="ProgramMode.cs" />
<Compile Include="ProgramParameters.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="SchemaEmbedder.cs" />
<Compile Include="SchemaEmitter.cs" />
<Compile Include="TypeOptionalInfo.cs" />
<Compile Include="XmlToClrMapper.cs" />
</ItemGroup>
<ItemGroup>
+ <None Include="tests\inner.xsd">
+ <SubType>Designer</SubType>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
<None Include="tests\OptionalTest.xsd">
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -58,6 +63,10 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+ <None Include="tests\outer.xsd">
+ <SubType>Designer</SubType>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Modified: hexsdtools/trunk/HEXsd/Namespace.cs
===================================================================
--- hexsdtools/trunk/HEXsd/Namespace.cs 2006-09-29 18:17:31 UTC (rev 10)
+++ hexsdtools/trunk/HEXsd/Namespace.cs 2006-09-29 18:42:29 UTC (rev 11)
@@ -186,13 +186,17 @@
return string.Join(".", upperParts);
}
- internal static string ConvertXmlNamespaceToFileName(string xmlNamespace, string extension)
+ internal static string ConvertXmlNamespaceToFileName(string xmlNamespace)
{
string converted = ConvertXmlNamespaceToClrNamespace(xmlNamespace);
converted = string.Join(string.Empty, converted.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries));
converted = converted.ToLower();
- // TBD is there a cleaner way to do the extension?
- return string.Format("{0}{1}", converted, extension);
+ return converted;
}
+
+ internal static string ConvertXmlNamespaceToFileName(string xmlNamespace, string extension)
+ {
+ return string.Format("{0}{1}", ConvertXmlNamespaceToFileName(xmlNamespace), extension);
+ }
}
}
Added: hexsdtools/trunk/HEXsd/SchemaEmbedder.cs
===================================================================
--- hexsdtools/trunk/HEXsd/SchemaEmbedder.cs (rev 0)
+++ hexsdtools/trunk/HEXsd/SchemaEmbedder.cs 2006-09-29 18:42:29 UTC (rev 11)
@@ -0,0 +1,144 @@
+using System;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.Collections.Generic;
+using System.IO;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Text;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.Serialization;
+
+namespace HEX.HEXsd
+{
+ internal class SchemaEmbedder
+ {
+ private CompilerParameters _compilerParams;
+
+ private Dictionary<string, Dictionary<string, int>> _entityDictionary =
+ new Dictionary<string, Dictionary<string, int>>();
+
+ internal SchemaEmbedder(CompilerParameters compilerParams)
+ {
+ _compilerParams = compilerParams;
+ }
+
+ internal string EmbedSchemaAsResource(XmlSchema schema)
+ {
+ string result = null;
+
+ string targetNamespace = schema.TargetNamespace;
+ string sourceUri = schema.SourceUri;
+
+ if (!_entityDictionary.ContainsKey(targetNamespace))
+ {
+ _entityDictionary.Add(targetNamespace, new Dictionary<string, int>());
+ }
+
+ if (_entityDictionary[targetNamespace].ContainsKey(sourceUri))
+ {
+ // this schema has already been processed, so there is nothing to do
+ int count = _entityDictionary[targetNamespace][sourceUri];
+
+ // process external schemas before saving
+ ProcessExternalSchemaReferences(schema);
+
+ // build filename
+ result = CreateSchemaFileName(targetNamespace, count);
+
+ // write out resource
+ AddEmbeddedResourceToCompilerParams(schema, result);
+ }
+ else
+ {
+ // this schema has not yet been processed
+ int count = _entityDictionary[targetNamespace].Count;
+
+ // add entity
+ _entityDictionary[targetNamespace].Add(sourceUri, count);
+
+ // process external schemas before saving
+ ProcessExternalSchemaReferences(schema);
+
+ // build filename
+ result = CreateSchemaFileName(targetNamespace, count);
+
+ // write out resource
+ AddEmbeddedResourceToCompilerParams(schema, result);
+ }
+
+ return result;
+ }
+
+ private void ProcessExternalSchemaReferences(XmlSchema schema)
+ {
+ // process referenced schemas
+ foreach (XmlSchemaExternal external in schema.Includes)
+ {
+ if (external is XmlSchemaImport)
+ {
+ XmlSchemaImport import = (XmlSchemaImport)external;
+ import.SchemaLocation = EmbedSchemaAsResource(import.Schema);
+ }
+ else if (external is XmlSchemaInclude)
+ {
+ XmlSchemaInclude include = (XmlSchemaInclude)external;
+ if (include.Schema.TargetNamespace.Equals(schema.TargetNamespace))
+ {
+ // need to recurse
+ include.SchemaLocation = EmbedSchemaAsResource(include.Schema);
+ }
+ else
+ {
+ throw new NotImplementedException("No support for including schemas without matching target namespaces (chameleons)");
+ }
+ }
+ else if (external is XmlSchemaRedefine)
+ {
+ XmlSchemaRedefine redefine = (XmlSchemaRedefine)external;
+
+ throw new NotImplementedException("No support for redefining schemas");
+ }
+ }
+ }
+
+ private void AddEmbeddedResourceToCompilerParams(XmlSchema schema, string filename)
+ {
+ // build temp file path
+ string tempFilename = Path.Combine(_compilerParams.TempFiles.TempDir, filename);
+
+ // TBD find a cleaner way to do this
+ bool found = false;
+ foreach (string file in _compilerParams.TempFiles)
+ {
+ if (file.Equals(tempFilename))
+ {
+ found = true;
+ }
+ }
+
+ if (!found)
+ {
+
+ _compilerParams.TempFiles.AddFile(tempFilename, true);
+
+ // write out file
+ using (FileStream stm = new FileStream(tempFilename, FileMode.Create, FileAccess.Write))
+ {
+ schema.Write(new StreamWriter(stm));
+ }
+
+ // add temp file to compilerParams to be embedded in compiled assembly
+ _compilerParams.EmbeddedResources.Add(tempFilename);
+ }
+ }
+
+ private string CreateSchemaFileName(string targetNamespace, int count)
+ {
+ return string.Format("{0}{1}.xsd",
+ Namespace.ConvertXmlNamespaceToFileName(targetNamespace),
+ count == 0 ? null : string.Format(".{0}", count));
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|