Added Microsoft Universal Data Link file format

This commit is contained in:
Michael Becker 2014-08-22 09:35:56 -04:00
parent 395ffa7755
commit a112b4f450
4 changed files with 258 additions and 0 deletions

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.DataLink
{
public class DataLinkObjectModel : ObjectModel
{
private static ObjectModelReference _omr = null;
public override ObjectModelReference MakeReference()
{
if (_omr == null)
{
_omr = base.MakeReference();
_omr.Title = "Data Link";
}
return _omr;
}
public override void Clear()
{
throw new NotImplementedException();
}
public override void CopyTo(ObjectModel where)
{
throw new NotImplementedException();
}
private string mvarProviderName = String.Empty;
/// <summary>
/// The name of the OLEDB provider to use in this data link. If no Provider is specified,
/// the OLE DB Provider for ODBC (MSDASQL) is the default value. This provides backward
/// compatibility with ODBC connection strings.
/// </summary>
public string ProviderName { get { return mvarProviderName; } set { mvarProviderName = value; } }
private string mvarProviderVersion = null;
/// <summary>
/// The version number of the OLEDB provider to use. If unspecified, will use the version-
/// independent OLEDB provider.
/// </summary>
public string ProviderVersion { get { return mvarProviderVersion; } set { mvarProviderVersion = value; } }
private bool mvarPersistSecurityInformation = false;
public bool PersistSecurityInformation { get { return mvarPersistSecurityInformation; } set { mvarPersistSecurityInformation = value; } }
private string mvarDataSourceName = String.Empty;
/// <summary>
/// The name of the data source or host name of the server from which to retrieve data.
/// </summary>
public string DataSourceName { get { return mvarDataSourceName; } set { mvarDataSourceName = value; } }
private string mvarInitialCatalog = String.Empty;
/// <summary>
/// The initial catalog or name of the database from which to retrieve data.
/// </summary>
public string InitialCatalog { get { return mvarInitialCatalog; } set { mvarInitialCatalog = value; } }
private bool mvarUseIntegratedSecurity = false;
/// <summary>
/// Determines whether integrated security (e.g. Windows authentication) is to be used.
/// </summary>
public bool UseIntegratedSecurity { get { return mvarUseIntegratedSecurity; } set { mvarUseIntegratedSecurity = value; } }
private PropertyList.Property.PropertyCollection mvarProperties = new PropertyList.Property.PropertyCollection();
/// <summary>
/// Additional properties to pass into the connection string.
/// </summary>
public PropertyList.Property.PropertyCollection Properties { get { return mvarProperties; } }
}
}

View File

@ -66,6 +66,7 @@
<Compile Include="ObjectModels\Chunked\RIFFMetadata.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="ObjectModels\DataLink\DataLinkObjectModel.cs" />
<Compile Include="ObjectModels\FileSystem\CompressedFile.cs" />
<Compile Include="ObjectModels\FileSystem\DataRequestEvent.cs" />
<Compile Include="ObjectModels\FileSystem\File.cs" />

View File

@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalEditor.DataFormats.PropertyList;
using UniversalEditor.ObjectModels.PropertyList;
using UniversalEditor.ObjectModels.DataLink;
namespace UniversalEditor.DataFormats.DataLink.UniversalDataLink
{
public class UDLDataFormat : WindowsConfigurationDataFormat
{
private static DataFormatReference _dfr = null;
public override DataFormatReference MakeReference()
{
if (_dfr == null)
{
_dfr = new DataFormatReference(GetType());
_dfr.Capabilities.Add(typeof(DataLinkObjectModel), DataFormatCapabilities.All);
_dfr.Capabilities.Add(typeof(PropertyListObjectModel), DataFormatCapabilities.Bootstrap);
_dfr.Filters.Add("Universal Data Link", new byte?[][] { new byte?[] { (byte)'[', (byte)'o', (byte)'l', (byte)'e', (byte)'d', (byte)'b', (byte)']' } }, new string[] { "*.udl" });
_dfr.Sources.Add("http://msdn.microsoft.com/en-us/library/ms722656%28v=vs.71%29.aspx");
}
return _dfr;
}
protected override void BeforeLoadInternal(Stack<ObjectModel> objectModels)
{
objectModels.Push(new PropertyListObjectModel());
}
protected override void AfterLoadInternal(Stack<ObjectModel> objectModels)
{
PropertyListObjectModel plom = (objectModels.Pop() as PropertyListObjectModel);
DataLinkObjectModel data = (objectModels.Pop() as DataLinkObjectModel);
Group oledb = plom.Groups["oledb"];
if (oledb == null) throw new InvalidDataFormatException("File does not contain a [oledb] group");
// if (oledb.CommentAfter != "Everything after this line is an OLE DB initstring") throw new InvalidDataFormatException("File does not contain the magic comment string \"Everything after this line is an OLE DB initstring\"");
Property provider = oledb.Properties["Provider"];
if (provider == null) throw new InvalidDataFormatException("File does not contain a \"Provider\" property in the \"oledb\" group");
string connstr = provider.Value.ToString();
string[] providerNameAndVersionParts = connstr.Split(new char[] { ';' }, 1);
string providerNameAndVersion = providerNameAndVersionParts[0];
string connstr1 = providerNameAndVersionParts[1];
string providerName = providerNameAndVersion;
string providerVersion = null;
if (providerNameAndVersion.Contains("."))
{
string[] pnav = providerNameAndVersion.Split(new char[] { '.' }, 2);
providerName = pnav[0];
providerVersion = pnav[1];
}
data.ProviderName = providerName;
data.ProviderVersion = providerVersion;
Property.PropertyCollection props = ParseConnectionString(connstr1);
foreach (Property p in props)
{
if (p.Value == null) continue;
switch (p.Name)
{
case "Data Source":
{
data.DataSourceName = p.Value.ToString();
break;
}
case "Initial Catalog":
{
data.InitialCatalog = p.Value.ToString();
break;
}
case "Integrated Security":
{
data.UseIntegratedSecurity = (p.Value.ToString() == "True");
break;
}
case "Persist Security Info":
{
data.PersistSecurityInformation = (p.Value.ToString() == "True");
break;
}
default:
{
data.Properties.Add(p.Clone() as Property);
break;
}
}
}
}
private Property.PropertyCollection ParseConnectionString(string connstr)
{
Property.PropertyCollection props = new Property.PropertyCollection();
string next = String.Empty;
Property prop = new Property();
for (int i = 0; i < connstr.Length; i++)
{
char c = connstr[i];
if (c == '=')
{
if (i < connstr.Length - 1 && connstr[i + 1] == '=')
{
next += "=";
i++;
continue;
}
else
{
prop.Name = next;
next = String.Empty;
}
}
else if (c == ';')
{
prop.Value = next;
next = String.Empty;
props.Add(prop);
prop = new Property();
}
else
{
connstr += c;
}
}
return props;
}
protected override void BeforeSaveInternal(Stack<ObjectModel> objectModels)
{
DataLinkObjectModel data = (objectModels.Pop() as DataLinkObjectModel);
PropertyListObjectModel plom = new PropertyListObjectModel();
Group oledb = new Group("oledb");
oledb.CommentAfter = "Everything after this line is an OLE DB initstring";
StringBuilder connstr = new StringBuilder();
connstr.Append(data.ProviderName);
if (data.ProviderVersion != null)
{
connstr.Append(".");
connstr.Append(data.ProviderVersion);
}
connstr.Append(";");
connstr.Append("Persist Security Info=");
if (data.PersistSecurityInformation)
{
connstr.Append("True");
}
else
{
connstr.Append("False");
}
connstr.Append(";Data Source=");
connstr.Append(data.DataSourceName);
connstr.Append(";Initial Catalog=");
connstr.Append(data.InitialCatalog);
if (data.UseIntegratedSecurity)
{
connstr.Append(";Integrated Security=SSPI");
}
foreach (Property prop in data.Properties)
{
connstr.Append(";");
connstr.Append(prop.Name.Replace("=", "=="));
connstr.Append("=");
connstr.Append(prop.Value.ToString());
}
oledb.Properties.Add("Provider", connstr.ToString());
plom.Groups.Add(oledb);
objectModels.Push(plom);
}
}
}

View File

@ -35,6 +35,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataFormats\DataLink\UniversalDataLink\UDLDataFormat.cs" />
<Compile Include="DataFormats\FileSystem\Microsoft\MSCompressed\MSCompressedCompressionMethod.cs" />
<Compile Include="DataFormats\FileSystem\Microsoft\MSCompressed\MSCompressedDataFormat.cs" />
<Compile Include="DataFormats\FileSystem\Microsoft\MSCompressed\MSCompressedKWAJCompressionMethod.cs" />