[Nmailserver-commits] SF.net SVN: nmailserver: [286] NMail/trunk
Brought to you by:
dframpton-oss,
tmyroadctfig
|
From: <tmy...@us...> - 2007-10-30 04:26:38
|
Revision: 286
http://nmailserver.svn.sourceforge.net/nmailserver/?rev=286&view=rev
Author: tmyroadctfig
Date: 2007-10-29 21:26:42 -0700 (Mon, 29 Oct 2007)
Log Message:
-----------
Final checkin.
Modified Paths:
--------------
NMail/trunk/CommandLineParser/CommandLineParser.csproj
NMail/trunk/CommandLineParser/ParameterParser.cs
NMail/trunk/CommandLineParser.UnitTests/CommandLineParser.UnitTests.csproj
NMail/trunk/CommandLineParser.UnitTests/ParserFixture.cs
NMail/trunk/NMail.LocalStoreData.Admin/Action.cs
NMail/trunk/NMail.LocalStoreData.Admin/CommandLineOptions.cs
NMail/trunk/NMail.LocalStoreData.Admin/NMail.LocalStoreData.Admin.csproj
NMail/trunk/NMail.LocalStoreData.Admin/XmlFormatter.cs
NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/NMail.LocalStoreData.Admin.UnitTests.csproj
NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/XmlFormatterFixture.cs
Added Paths:
-----------
NMail/trunk/CommandLineParser/LongFlagAttribute.cs
NMail/trunk/CommandLineParser.UnitTests/FlagOptions.cs
Modified: NMail/trunk/CommandLineParser/CommandLineParser.csproj
===================================================================
--- NMail/trunk/CommandLineParser/CommandLineParser.csproj 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/CommandLineParser/CommandLineParser.csproj 2007-10-30 04:26:42 UTC (rev 286)
@@ -1,4 +1,4 @@
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -36,6 +36,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EnumOptionAttribute.cs" />
+ <Compile Include="LongFlagAttribute.cs" />
<Compile Include="ParameterParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WarningEventArgs.cs" />
Added: NMail/trunk/CommandLineParser/LongFlagAttribute.cs
===================================================================
--- NMail/trunk/CommandLineParser/LongFlagAttribute.cs (rev 0)
+++ NMail/trunk/CommandLineParser/LongFlagAttribute.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2004-2007 Luke Quinane
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace CommandLineParser {
+ [AttributeUsage(AttributeTargets.Property)]
+ public class LongFlagAttribute : Attribute {
+
+ public LongFlagAttribute(string flag) {
+ _flag = flag;
+ }
+
+ private string _flag;
+
+ /// <summary>
+ /// The flag that corresponds to this option.
+ /// </summary>
+ public string Flag {
+ get { return _flag; }
+ }
+ }
+}
Modified: NMail/trunk/CommandLineParser/ParameterParser.cs
===================================================================
--- NMail/trunk/CommandLineParser/ParameterParser.cs 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/CommandLineParser/ParameterParser.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -27,6 +27,7 @@
public void Parse(string[] arguments, object optionsObject) {
Dictionary<PropertyInfo, EnumOptionAttribute> enumAttributes = new Dictionary<PropertyInfo, EnumOptionAttribute>();
+ Dictionary<PropertyInfo, LongFlagAttribute> longFlags = new Dictionary<PropertyInfo, LongFlagAttribute>();
foreach (PropertyInfo propInfo in optionsObject.GetType().GetProperties()) {
foreach (EnumOptionAttribute attribute in propInfo.GetCustomAttributes(typeof(EnumOptionAttribute), false)) {
@@ -37,12 +38,33 @@
enumAttributes.Add(propInfo, attribute);
break;
}
+
+ foreach (LongFlagAttribute attribute in propInfo.GetCustomAttributes(typeof(LongFlagAttribute), false)) {
+ if (propInfo.PropertyType.UnderlyingSystemType != typeof(bool)) {
+ throw new InvalidOperationException("The long flag attribute can only be placed on boolean properties/fields.");
+ }
+
+ longFlags.Add(propInfo, attribute);
+ break;
+ }
}
foreach (string argument in arguments) {
bool foundArgumentHandler = false;
- foreach (KeyValuePair<PropertyInfo,EnumOptionAttribute> enumOption in enumAttributes) {
+ foreach (KeyValuePair<PropertyInfo, LongFlagAttribute> flagOption in longFlags) {
+ if ("--" + flagOption.Value.Flag == argument) {
+ object[] invokeArgs = { true };
+ flagOption.Key.GetSetMethod().Invoke(optionsObject, invokeArgs);
+
+ foundArgumentHandler = true;
+ break;
+ }
+ }
+
+ if (foundArgumentHandler) continue;
+
+ foreach (KeyValuePair<PropertyInfo, EnumOptionAttribute> enumOption in enumAttributes) {
Type enumType = enumOption.Key.PropertyType;
try {
Modified: NMail/trunk/CommandLineParser.UnitTests/CommandLineParser.UnitTests.csproj
===================================================================
--- NMail/trunk/CommandLineParser.UnitTests/CommandLineParser.UnitTests.csproj 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/CommandLineParser.UnitTests/CommandLineParser.UnitTests.csproj 2007-10-30 04:26:42 UTC (rev 286)
@@ -1,4 +1,4 @@
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -40,6 +40,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EnumOptions.cs" />
+ <Compile Include="FlagOptions.cs" />
<Compile Include="ParserFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Added: NMail/trunk/CommandLineParser.UnitTests/FlagOptions.cs
===================================================================
--- NMail/trunk/CommandLineParser.UnitTests/FlagOptions.cs (rev 0)
+++ NMail/trunk/CommandLineParser.UnitTests/FlagOptions.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2004-2007 Luke Quinane
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace CommandLineParser.UnitTests {
+ class FlagOptions {
+ private bool _flag1;
+
+ [LongFlag("flag1")]
+ public bool Flag1 {
+ get { return _flag1; }
+ set { _flag1 = value; }
+ }
+
+ private bool _flag2;
+
+ [LongFlag("flag2")]
+ public bool Flag2 {
+ get { return _flag2; }
+ set { _flag2 = value; }
+ }
+ }
+}
Modified: NMail/trunk/CommandLineParser.UnitTests/ParserFixture.cs
===================================================================
--- NMail/trunk/CommandLineParser.UnitTests/ParserFixture.cs 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/CommandLineParser.UnitTests/ParserFixture.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -41,6 +41,16 @@
}
[Test]
+ public void ShouldHandleLongFlagAttribute() {
+ string[] arguments = { "--flag1" };
+ FlagOptions flagOptions = new FlagOptions();
+ parser.Parse(arguments, flagOptions);
+
+ Assert.That(flagOptions.Flag1, Is.True, "Flag1 set.");
+ Assert.That(flagOptions.Flag2, Is.False, "Flag2 not set.");
+ }
+
+ [Test]
public void ShouldWarnOnUnknownArgument() {
bool warned = false;
string[] arguments = { "unknown" };
Modified: NMail/trunk/NMail.LocalStoreData.Admin/Action.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.Admin/Action.cs 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/NMail.LocalStoreData.Admin/Action.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -32,6 +32,11 @@
/// <summary>
/// The action to export the complete localstore database.
/// </summary>
- ExportComplete
+ ExportComplete,
+
+ /// <summary>
+ /// Reinstalls the database schema destorying the current data.
+ /// </summary>
+ ReinstallSchema
}
}
Modified: NMail/trunk/NMail.LocalStoreData.Admin/CommandLineOptions.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.Admin/CommandLineOptions.cs 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/NMail.LocalStoreData.Admin/CommandLineOptions.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -31,6 +31,14 @@
public Action Action {
get { return action; }
set { action = value; }
- }
+ }
+
+ private bool forceReinstallSchema;
+
+ [LongFlag("force-reinstall-schema")]
+ public bool ForceReinstallSchema {
+ get { return forceReinstallSchema; }
+ set { forceReinstallSchema = value; }
+ }
}
}
Modified: NMail/trunk/NMail.LocalStoreData.Admin/NMail.LocalStoreData.Admin.csproj
===================================================================
--- NMail/trunk/NMail.LocalStoreData.Admin/NMail.LocalStoreData.Admin.csproj 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/NMail.LocalStoreData.Admin/NMail.LocalStoreData.Admin.csproj 2007-10-30 04:26:42 UTC (rev 286)
@@ -50,6 +50,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\Castle\Castle.Windsor.dll</HintPath>
</Reference>
+ <Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\References\log4net.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
@@ -82,6 +86,10 @@
</ItemGroup>
<ItemGroup>
<None Include="..\NMail.snk" />
+ <EmbeddedResource Include="XmlFormatterSchema.xsd">
+ <SubType>
+ </SubType>
+ </EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Content Include="LocalStoreAdminIoc.xml">
Modified: NMail/trunk/NMail.LocalStoreData.Admin/XmlFormatter.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.Admin/XmlFormatter.cs 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/NMail.LocalStoreData.Admin/XmlFormatter.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -18,15 +18,45 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Reflection;
using System.Text;
+using System.Xml;
+using System.Xml.Schema;
+using log4net;
+
using NMail.DataTypes.LocalStore;
namespace NMail.LocalStoreData.Admin {
public class XmlFormatter : IFormatter {
+ /// <summary>
+ /// Logging support for this class.
+ /// </summary>
+ private static ILog _log = LogManager.GetLogger(typeof(XmlFormatter));
+
+ MemoryStream _stream;
+ XmlTextWriter _writer;
+
+ public XmlFormatter() {
+ _stream = new MemoryStream();
+ _writer = new XmlTextWriter(_stream, Encoding.Unicode);
+ }
+
+ private void CopyStreamToOutputStream(Stream outputStream) {
+ _writer.Flush();
+
+ byte[] buffer = new byte[_stream.Length];
+ _stream.Read(buffer, 0, buffer.Length);
+ outputStream.Write(buffer, 0, buffer.Length);
+ }
+
#region IFormatter Members
public void AppendPreamble(Stream outputStream) {
+ _writer.WriteStartDocument();
+ _writer.WriteStartElement("LocalStoreData");
+
+ CopyStreamToOutputStream(outputStream);
}
public void AppendUserList(Stream outputStream, IList<LocalStoreUser> userList) {
@@ -34,8 +64,17 @@
}
public void AppendPostamble(Stream outputStream) {
-
+ _writer.WriteEndElement();
+ _writer.WriteEndDocument();
+
+ CopyStreamToOutputStream(outputStream);
}
#endregion
+
+ public static XmlSchema GetXmlSchema() {
+ Stream stream = Assembly.GetAssembly(typeof(XmlFormatter)).GetManifestResourceStream("NMail.LocalStoreData.Admin.XmlFormatterSchema.xsd");
+ XmlTextReader reader = new XmlTextReader(stream);
+ return XmlSchema.Read(reader, null);
+ }
}
}
Modified: NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/NMail.LocalStoreData.Admin.UnitTests.csproj
===================================================================
--- NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/NMail.LocalStoreData.Admin.UnitTests.csproj 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/NMail.LocalStoreData.Admin.UnitTests.csproj 2007-10-30 04:26:42 UTC (rev 286)
@@ -50,6 +50,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\Castle\Castle.Windsor.dll</HintPath>
</Reference>
+ <Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\References\log4net.dll</HintPath>
+ </Reference>
<Reference Include="nunit.framework, Version=2.4.3.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\nunit.framework.dll</HintPath>
Modified: NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/XmlFormatterFixture.cs
===================================================================
--- NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/XmlFormatterFixture.cs 2007-10-14 00:55:02 UTC (rev 285)
+++ NMail/trunk/NMail.LocalStoreData.Admin.UnitTests/XmlFormatterFixture.cs 2007-10-30 04:26:42 UTC (rev 286)
@@ -18,23 +18,50 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Reflection;
using System.Text;
+using System.Xml;
+using System.Xml.Schema;
+using log4net;
using NUnit.Framework;
namespace NMail.LocalStoreData.Admin.UnitTests {
[TestFixture]
public class XmlFormatterFixture {
+ // <summary>
+ /// Logging support for this class.
+ /// </summary>
+ private static ILog _log = LogManager.GetLogger(typeof(XmlFormatterFixture));
+ [TestFixtureSetUp]
+ public void FixtureSetup() {
+ log4net.Config.BasicConfigurator.Configure();
+ }
+
XmlFormatter _formatter = new XmlFormatter();
[Test]
- public void ShouldAppendPreamble() {
+ public void PreambleAndPostambleShouldBeValidXml() {
MemoryStream output = new MemoryStream();
_formatter.AppendPreamble(output);
+ _formatter.AppendPostamble(output);
+ byte[] buffer = new byte[output.Length];
+ Array.Copy(output.GetBuffer(), buffer, buffer.Length);
+ _log.Debug(string.Format("Xml: '{0}'", Encoding.Unicode.GetString(buffer)));
- //Assert.That(
+ XmlReaderSettings readerSettings = new XmlReaderSettings();
+ readerSettings.Schemas.Add(XmlFormatter.GetXmlSchema());
+ readerSettings.ValidationType = ValidationType.Schema;
+ readerSettings.ValidationEventHandler += delegate(object sender, ValidationEventArgs e) {
+ _log.Debug("Error validating formatter output: " + e.Message);
+ throw e.Exception;
+ };
+
+ XmlReader reader = XmlReader.Create(output, readerSettings);
+ XmlDocument doc = new XmlDocument();
+ doc.Load(reader);
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|