[mapguide-commits] r4910 - in sandbox/maestro-2.5:
Maestro.Base/Editor MaestroBaseTests OSGeo.MapGuide.MaestroAPI
OSGeo.MapGuide.MaestroAPI/Exceptions
OSGeo.MapGuide.MaestroAPI/ObjectModels
OSGeo.MapGuide.MaestroAPI/Resource
OSGeo.MapGuide.MaestroAPI/Services OSGeo.MapGuide.MaestroAPI.Http
svn_mapguide at osgeo.org
svn_mapguide at osgeo.org
Wed May 19 03:55:22 EDT 2010
Author: jng
Date: 2010-05-19 03:55:21 -0400 (Wed, 19 May 2010)
New Revision: 4910
Added:
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Exceptions/SerializationException.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/UntypedResource.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ResourceTypeRegistry.cs
Modified:
sandbox/maestro-2.5/Maestro.Base/Editor/XmlEditor.cs
sandbox/maestro-2.5/MaestroBaseTests/EditorTests.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI.Http/HttpServerConnection.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/ApplicationDefinition.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/DrawingSource.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/FeatureSource.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerDefinition.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LoadProcedure.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/PrintLayout.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolDefinition.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolLibrary.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/WebLayout.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/ResourceTypeDescriptor.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ServerConnectionBase.cs
sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Services/IResourceService.cs
Log:
This submission includes the following changes:
- Remove API cruft from ServerConnectionBase
- Add a ResourceTypeRegistry which handles serialization/deserialization of resource types. It peeks at the resource content stream to determine which serializer to use for deserialization
- Added an UntypedResource class which is what is returned by IResourceService.GetResource() when no matching serializer could be found to deserialize this content. UntypedResource is just a dumb container for this XML content and as such, can only be opened by the XML editor. A new readonly property IsStronglyTyped is added to IResource to indicate whether this is the case.
Modified: sandbox/maestro-2.5/Maestro.Base/Editor/XmlEditor.cs
===================================================================
--- sandbox/maestro-2.5/Maestro.Base/Editor/XmlEditor.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/Maestro.Base/Editor/XmlEditor.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -47,11 +47,7 @@
protected override void Bind(OSGeo.MapGuide.MaestroAPI.Resource.IResource value)
{
- var stream = value.CurrentConnection.ResourceService.SerializeObject(value);
- using (var reader = new StreamReader(stream, Encoding.UTF8, true))
- {
- _editor.XmlContent = reader.ReadToEnd();
- }
+ _editor.XmlContent = value.Serialize();
this.Title = "XML Editor: " + ResourceIdentifier.GetName(value.ResourceID); //LOCALIZE
}
Modified: sandbox/maestro-2.5/MaestroBaseTests/EditorTests.cs
===================================================================
--- sandbox/maestro-2.5/MaestroBaseTests/EditorTests.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/MaestroBaseTests/EditorTests.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -107,11 +107,6 @@
throw new NotImplementedException();
}
- public T CreateResourceObject<T>()
- {
- throw new NotImplementedException();
- }
-
public Type GetResourceType(string resourceID)
{
throw new NotImplementedException();
@@ -281,6 +276,16 @@
{
get { throw new NotImplementedException(); }
}
+
+ public T CreateResourceObject<T>() where T : IResource
+ {
+ throw new NotImplementedException();
+ }
+
+ Stream IResourceService.GetResourceXmlData(string resourceID)
+ {
+ throw new NotImplementedException();
+ }
}
#endregion
Added: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Exceptions/SerializationException.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Exceptions/SerializationException.cs (rev 0)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Exceptions/SerializationException.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -0,0 +1,47 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace OSGeo.MapGuide.MaestroAPI.Exceptions
+{
+ /// <summary>
+ /// Indicates an error during the serialization process
+ /// </summary>
+ [global::System.Serializable]
+ public class SerializationException : MaestroException
+ {
+ //
+ // For guidelines regarding the creation of new exception types, see
+ // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
+ // and
+ // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
+ //
+
+ public SerializationException() { }
+ public SerializationException(string message) : base(message) { }
+ public SerializationException(string message, Exception inner) : base(message, inner) { }
+ protected SerializationException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context) { }
+ }
+}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj 2010-05-19 07:55:21 UTC (rev 4910)
@@ -170,6 +170,7 @@
<Compile Include="Exceptions\CustomPropertyNotFoundException.cs" />
<Compile Include="Exceptions\MaestroException.cs" />
<Compile Include="Exceptions\ResourceConversionException.cs" />
+ <Compile Include="Exceptions\SerializationException.cs" />
<Compile Include="Exceptions\UnsupportedResourceTypeException.cs" />
<Compile Include="Exceptions\UnsupportedServiceTypeException.cs" />
<Compile Include="IConnectionCapabilities.cs" />
@@ -197,6 +198,7 @@
<Compile Include="ObjectModels\ResourceItems.cs" />
<Compile Include="ObjectModels\SymbolDefinition.cs" />
<Compile Include="ObjectModels\SymbolLibrary.cs" />
+ <Compile Include="ObjectModels\UntypedResource.cs" />
<Compile Include="ObjectModels\WebLayout.cs" />
<Compile Include="Platform.cs" />
<Compile Include="Properties\Resources.Designer.cs">
@@ -204,6 +206,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
+ <Compile Include="ResourceTypeRegistry.cs" />
<Compile Include="Resource\Conversion\IResourceConverter.cs" />
<Compile Include="Resource\Conversion\ResourceConverter.cs" />
<Compile Include="Resource\Conversion\ResourceUpgrader.cs" />
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/ApplicationDefinition.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/ApplicationDefinition.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/ApplicationDefinition.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "ApplicationDefinition-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/DrawingSource.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/DrawingSource.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/DrawingSource.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "DrawingSource-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/FeatureSource.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/FeatureSource.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/FeatureSource.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "FeatureSource-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerDefinition.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerDefinition.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerDefinition.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "LayerDefinition-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LoadProcedure.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LoadProcedure.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/LoadProcedure.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "LoadProcedure-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -91,6 +91,12 @@
get { return "MapDefinition-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
partial class MapLayerType
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/PrintLayout.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/PrintLayout.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/PrintLayout.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "PrintLayout-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolDefinition.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolDefinition.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolDefinition.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "SymbolDefinition-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolLibrary.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolLibrary.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/SymbolLibrary.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "SymbolLibrary-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Added: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/UntypedResource.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/UntypedResource.cs (rev 0)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/UntypedResource.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -0,0 +1,93 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+using OSGeo.MapGuide.MaestroAPI.Resource;
+
+namespace OSGeo.MapGuide.MaestroAPI.ObjectModels
+{
+ /// <summary>
+ /// Represents resource content that could not be deserialized into a corresponding
+ /// strongly-typed resource class. This is just a container of arbitrary xml content.
+ /// </summary>
+ public class UntypedResource : IResource
+ {
+ internal UntypedResource(string xml, string resourceType, string version)
+ {
+ this.XmlContent = xml;
+ this.ResourceType = resourceType;
+ this.ResourceVersion = new Version(version);
+ }
+
+ public IServerConnection CurrentConnection
+ {
+ get;
+ set;
+ }
+
+ public string ValidatingSchema
+ {
+ get { return this.ResourceType + "-" + this.ResourceVersion.ToString() + ".xsd"; }
+ }
+
+ public string ResourceID
+ {
+ get;
+ set;
+ }
+
+ public string ResourceType
+ {
+ get;
+ private set;
+ }
+
+ public string XmlContent
+ {
+ get;
+ set;
+ }
+
+ public string Serialize()
+ {
+ return this.XmlContent;
+ }
+
+ public Version ResourceVersion
+ {
+ get;
+ private set;
+ }
+
+ public object Clone()
+ {
+ return this.MemberwiseClone();
+ }
+
+ public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
+
+
+ public bool IsStronglyTyped
+ {
+ get { return false; }
+ }
+ }
+}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/WebLayout.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/WebLayout.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ObjectModels/WebLayout.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -89,5 +89,11 @@
get { return "WebLayout-1.0.0.xsd"; }
set { }
}
+
+ [XmlIgnore]
+ public bool IsStronglyTyped
+ {
+ get { return true; }
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -37,6 +37,13 @@
string ResourceType { get; }
string Serialize();
+
+ /// <summary>
+ /// Indicates whether this resource is strongly typed. If false it means the implementer
+ /// is a <see cref="UntypedResource"/> object. This usually means that the matching serializer
+ /// could not be found because the resource version is unrecognised.
+ /// </summary>
+ bool IsStronglyTyped { get; }
}
public static class ResourceExtensions
@@ -46,5 +53,10 @@
string str = res.Serialize();
return new MemoryStream(Encoding.UTF8.GetBytes(str));
}
+
+ public static ResourceTypeDescriptor GetResourceTypeDescriptor(this IResource res)
+ {
+ return new ResourceTypeDescriptor(res.ResourceType, res.ResourceVersion.ToString());
+ }
}
}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/ResourceTypeDescriptor.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/ResourceTypeDescriptor.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Resource/ResourceTypeDescriptor.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -53,5 +53,55 @@
{
return this.ToString().GetHashCode();
}
+
+ public static ResourceTypeDescriptor ApplicationDefinition
+ {
+ get { return new ResourceTypeDescriptor("ApplicationDefinition", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor FeatureSource
+ {
+ get { return new ResourceTypeDescriptor("FeatureSource", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor DrawingSource
+ {
+ get { return new ResourceTypeDescriptor("DrawingSource", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor LayerDefinition
+ {
+ get { return new ResourceTypeDescriptor("LayerDefinition", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor LoadProcedure
+ {
+ get { return new ResourceTypeDescriptor("LoadProcedure", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor MapDefinition
+ {
+ get { return new ResourceTypeDescriptor("MapDefinition", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor PrintLayout
+ {
+ get { return new ResourceTypeDescriptor("PrintLayout", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor SymbolLibrary
+ {
+ get { return new ResourceTypeDescriptor("SymbolLibrary", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor SymbolDefinition
+ {
+ get { return new ResourceTypeDescriptor("SymbolDefinition", "1.0.0"); }
+ }
+
+ public static ResourceTypeDescriptor WebLayout
+ {
+ get { return new ResourceTypeDescriptor("WebLayout", "1.0.0"); }
+ }
}
}
Added: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ResourceTypeRegistry.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ResourceTypeRegistry.cs (rev 0)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ResourceTypeRegistry.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -0,0 +1,206 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+using OSGeo.MapGuide.MaestroAPI.Resource;
+using System.Xml.Serialization;
+using OSGeo.MapGuide.ObjectModels.ApplicationDefinition;
+using System.IO;
+using OSGeo.MapGuide.ObjectModels.Common;
+using OSGeo.MapGuide.ObjectModels.LayerDefinition;
+using OSGeo.MapGuide.ObjectModels.LoadProcedure;
+using OSGeo.MapGuide.ObjectModels.PrintLayout;
+using OSGeo.MapGuide.ObjectModels.SymbolDefinition;
+using OSGeo.MapGuide.ObjectModels.SymbolLibrary;
+using OSGeo.MapGuide.ObjectModels.WebLayout;
+using OSGeo.MapGuide.MaestroAPI.Exceptions;
+using System.Xml;
+using OSGeo.MapGuide.MaestroAPI.ObjectModels;
+
+namespace OSGeo.MapGuide.MaestroAPI
+{
+ public delegate IResource ResourceDeserializationCallback(string xml);
+ public delegate Stream ResourceSerializationCallback(IResource res);
+
+ public class ResourceSerializer
+ {
+ public ResourceSerializationCallback Serialize { get; set; }
+
+ public ResourceDeserializationCallback Deserialize { get; set; }
+ }
+
+ public static class ResourceTypeRegistry
+ {
+ private static Dictionary<ResourceTypeDescriptor, ResourceSerializer> _serializers;
+
+ static ResourceTypeRegistry()
+ {
+ _serializers = new Dictionary<ResourceTypeDescriptor, ResourceSerializer>();
+
+ //ApplicationDefinition 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.ApplicationDefinition,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return ApplicationDefinitionType.Deserialize(xml); }
+ });
+
+ //DrawingSource 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.DrawingSource,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return DrawingSource.Deserialize(xml); }
+ });
+
+ //FeatureSource 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.FeatureSource,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return ApplicationDefinitionType.Deserialize(xml); }
+ });
+
+ //LayerDefinition 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.LayerDefinition,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return LayerDefinition.Deserialize(xml); }
+ });
+
+ //LoadProcedure 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.LoadProcedure,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return LoadProcedure.Deserialize(xml); }
+ });
+
+ //PrintLayout 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.PrintLayout,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return PrintLayout.Deserialize(xml); }
+ });
+
+ //SymbolDefinition 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.SymbolDefinition,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return SymbolDefinitionBase.Deserialize(xml); }
+ });
+
+ //SymbolLibrary 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.SymbolLibrary,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return SymbolLibraryType.Deserialize(xml); }
+ });
+
+ //WebLayout 1.0.0
+ _serializers.Add(
+ ResourceTypeDescriptor.WebLayout,
+ new ResourceSerializer()
+ {
+ Serialize = (res) => { return res.SerializeToStream(); },
+ Deserialize = (xml) => { return WebLayoutType.Deserialize(xml); }
+ });
+ }
+
+ internal static void Init()
+ {
+ //does nothing, it's just for kicking the static constructor into gear
+ }
+
+ public static IResource Deserialize(string resourceType, Stream stream)
+ {
+ //UGLY: We have to peek inside the stream to determine the version number
+
+ //House the stream inside a rewindable memory stream
+ using (var ms = new MemoryStream())
+ {
+ Utility.CopyStream(stream, ms);
+ string version = "1.0.0";
+ ms.Position = 0L; //Rewind
+ using (var xr = XmlReader.Create(ms))
+ {
+ xr.MoveToContent();
+ if (!xr.HasAttributes)
+ throw new SerializationException("Bad Document: Expected attributes at the root level node");
+
+ //Resources post-1.0.0 have a version attribute, those without are assumed to be 1.0.0 version
+ try
+ {
+ version = xr.GetAttribute("version");
+ }
+ catch
+ {
+ version = "1.0.0";
+ }
+ finally
+ {
+ if (string.IsNullOrEmpty(version))
+ version = "1.0.0";
+ }
+ xr.Close();
+ }
+
+ ms.Position = 0L; //Rewind
+
+ var rd = new ResourceTypeDescriptor(resourceType, version);
+ using (var reader = new StreamReader(ms))
+ {
+ var xml = reader.ReadToEnd();
+ if (_serializers.ContainsKey(rd))
+ return _serializers[rd].Deserialize(xml);
+ else
+ return new UntypedResource(xml, resourceType, version);
+ }
+ }
+ }
+
+ public static Stream Serialize(IResource res)
+ {
+ var rd = res.GetResourceTypeDescriptor();
+ if (!_serializers.ContainsKey(rd))
+ throw new SerializationException("Could not find matching serializer for this resource: " + rd.ToString()); //LOCALIZE
+
+ return _serializers[rd].Serialize(res);
+ }
+
+ public static T CreateDefault<T>() where T : IResource
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ServerConnectionBase.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ServerConnectionBase.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/ServerConnectionBase.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -37,11 +37,6 @@
public abstract class ServerConnectionBase
{
/// <summary>
- /// A resource lookup table, for converting resourceID's into types.
- /// </summary>
- protected Hashtable m_resourceTypeLookup;
-
- /// <summary>
/// A list of cached serializers
/// </summary>
protected Hashtable m_serializers;
@@ -86,20 +81,12 @@
protected ServerConnectionBase()
{
+ ResourceTypeRegistry.Init();
+
m_serializers = new Hashtable();
m_validator = new XMLValidator();
m_cachedSchemas = new Hashtable();
- m_resourceTypeLookup = new Hashtable();
- m_resourceTypeLookup.Add("LayerDefinition", typeof(OSGeo.MapGuide.ObjectModels.LayerDefinition.LayerDefinition));
- m_resourceTypeLookup.Add("MapDefinition", typeof(OSGeo.MapGuide.ObjectModels.MapDefinition.MapDefinition));
- m_resourceTypeLookup.Add("WebLayout", typeof(OSGeo.MapGuide.ObjectModels.WebLayout.WebLayoutType));
- m_resourceTypeLookup.Add("FeatureSource", typeof(OSGeo.MapGuide.ObjectModels.FeatureSource.FeatureSourceType));
- m_resourceTypeLookup.Add("ApplicationDefinition", typeof(OSGeo.MapGuide.ObjectModels.ApplicationDefinition.ApplicationDefinitionType));
- m_resourceTypeLookup.Add("SymbolLibrary", typeof(OSGeo.MapGuide.ObjectModels.SymbolLibrary.SymbolLibraryType));
- m_resourceTypeLookup.Add("PrintLayout", typeof(OSGeo.MapGuide.ObjectModels.PrintLayout.PrintLayout));
- m_resourceTypeLookup.Add("LoadProcedure", typeof(OSGeo.MapGuide.ObjectModels.LoadProcedure.LoadProcedure));
-
m_schemasPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Schemas");
m_username = null;
m_password = null;
@@ -356,7 +343,7 @@
/// </summary>
/// <param name="resourceID">The full resourceID to get data from</param>
/// <returns>Raw data from the given resource</returns>
- abstract public byte[] GetResourceXmlData(string resourceID);
+ public abstract Stream GetResourceXmlData(string resourceID);
/// <summary>
/// Returns an object deserialized from server data.
@@ -366,12 +353,10 @@
/// <returns>A deserialized object.</returns>
virtual public IResource GetResource(string resourceID)
{
- Type type = GetResourceType(resourceID);
+ var stream = GetResourceXmlData(resourceID);
+ string rt = ResourceIdentifier.GetExtension(resourceID);
- if (type == null)
- throw new Exception("Unable to find type for " + resourceID);
-
- IResource o = (IResource)DeserializeObject(type, new System.IO.MemoryStream(GetResourceXmlData(resourceID)));
+ IResource o = ResourceTypeRegistry.Deserialize(rt, stream);
o.CurrentConnection = GetInterface();
o.ResourceID = resourceID;
@@ -402,48 +387,6 @@
}
/// <summary>
- /// Gets or sets a hashtable where the key is resourceID extensions, and values are System.Type values.
- /// </summary>
- virtual public Hashtable ResourceTypeLookup
- {
- get { return m_resourceTypeLookup; }
- set { m_resourceTypeLookup = value; }
- }
-
- /// <summary>
- /// Gets an object type from the resourceID.
- /// Use the ResourceTypeLookup to add or remove types.
- /// </summary>
- /// <param name="resourceID">The resourceID for the resource</param>
- /// <returns>The type of the given item, throws an exception if the type does not exist</returns>
- virtual public Type GetResourceType(string resourceID)
- {
- string extension = resourceID.Substring(resourceID.LastIndexOf(".") + 1);
- return (Type)ResourceTypeLookup[extension];
- }
-
- /// <summary>
- /// Gets an object type from the resourceID.
- /// Use the ResourceTypeLookup to add or remove types.
- /// </summary>
- /// <param name="resourceID">The resourceID for the resource</param>
- /// <returns>The type of the given item, returns null if no such type exists</returns>
- virtual public Type TryGetResourceType(string resourceID)
- {
- try
- {
- string extension = resourceID.Substring(resourceID.LastIndexOf(".") + 1);
- if (ResourceTypeLookup.ContainsKey(extension))
- return (Type)ResourceTypeLookup[extension];
- }
- catch
- {
- }
-
- return null;
- }
-
- /// <summary>
/// Returns an installed provider, given the name of the provider
/// </summary>
/// <param name="providername">The name of the provider</param>
@@ -796,7 +739,7 @@
try
{
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream(GetResourceXmlData(item.Itempath)))
+ using (var ms = GetResourceXmlData(item.Itempath))
d.Load(ms);
UpdateResourceReferences(d, oldpath, newpath, false);
@@ -925,7 +868,7 @@
try
{
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream(GetResourceXmlData(item.Itempath)))
+ using (var ms = GetResourceXmlData(item.Itempath))
d.Load(ms);
UpdateResourceReferences(d, oldpath, newpath, true);
@@ -1052,7 +995,7 @@
try
{
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream(GetResourceXmlData(item.Itempath)))
+ using (var ms = GetResourceXmlData(item.Itempath))
d.Load(ms);
UpdateResourceReferences(d, oldpath, newpath, true);
@@ -1588,27 +1531,9 @@
/// </summary>
/// <typeparam name="T">The type of the object to create</typeparam>
/// <returns>A new instance of the object</returns>
- public virtual T CreateResourceObject<T>()
+ public virtual T CreateResourceObject<T>() where T : IResource
{
- string resExt = null;
- foreach(string s in this.ResourceTypeLookup.Keys)
- if (this.ResourceTypeLookup[s] == typeof(T))
- {
- resExt = s;
- break;
- }
-
- if (resExt == null)
- throw new Exception("Unable to determine extension for object of type: " + typeof(T).FullName);
-
- System.IO.Stream rs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(this.GetType(), "Templates.Template." + resExt);
- if (rs == null)
- throw new Exception("Unable to load template for object of type: " + typeof(T).FullName + ", with extension: " + resExt);
-
- try { rs.Position = 0; }
- catch {}
-
- return this.DeserializeObject<T>(rs);
+ return ResourceTypeRegistry.CreateDefault<T>();
}
/// <summary>
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Services/IResourceService.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Services/IResourceService.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI/Services/IResourceService.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -23,6 +23,7 @@
using ObjCommon = OSGeo.MapGuide.ObjectModels.Common;
using OSGeo.MapGuide.MaestroAPI.Resource;
+using System.IO;
namespace OSGeo.MapGuide.MaestroAPI.Services
{
@@ -53,27 +54,13 @@
/// </summary>
/// <typeparam name="T">The type of the object to create</typeparam>
/// <returns>A new instance of the object</returns>
- T CreateResourceObject<T>();
+ T CreateResourceObject<T>() where T : IResource;
- /// <summary>
- /// Gets the resource type from a resourceID
- /// </summary>
- /// <param name="resourceID">The resourceID for the resource</param>
- /// <returns>The type of the given item, throws an exception if the type does not exist</returns>
- Type GetResourceType(string resourceID);
-
- /// <summary>
- /// Gets the resource type from a resourceID
- /// </summary>
- /// <param name="resourceID">The resourceID for the resource</param>
- /// <returns>The type of the given item, returns null if no such type exists</returns>
- Type TryGetResourceType(string resourceID);
-
System.IO.MemoryStream GetResourceData(string resourceID, string dataname);
ObjCommon.ResourceDocumentHeaderType GetResourceHeader(string resourceID);
ObjCommon.ResourceFolderHeaderType GetFolderHeader(string resourceID);
- byte[] GetResourceXmlData(string resourceID);
+ Stream GetResourceXmlData(string resourceID);
IResource GetResource(string resourceID);
void SetResourceData(string resourceid, string dataname, ObjCommon.ResourceDataType datatype, System.IO.Stream stream);
Modified: sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI.Http/HttpServerConnection.cs
===================================================================
--- sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI.Http/HttpServerConnection.cs 2010-05-18 11:09:02 UTC (rev 4909)
+++ sandbox/maestro-2.5/OSGeo.MapGuide.MaestroAPI.Http/HttpServerConnection.cs 2010-05-19 07:55:21 UTC (rev 4910)
@@ -34,6 +34,7 @@
using OSGeo.MapGuide.MaestroAPI.Serialization;
using OSGeo.MapGuide.MaestroAPI.Exceptions;
using OSGeo.MapGuide.MaestroAPI.Http;
+using System.IO;
namespace OSGeo.MapGuide.MaestroAPI
{
@@ -384,13 +385,11 @@
}
}
- public override byte[] GetResourceXmlData(string resourceID)
+ public override Stream GetResourceXmlData(string resourceID)
{
ResourceIdentifier.Validate(resourceID, ResourceTypes.FeatureSource);
string req = m_reqBuilder.GetResourceContent(resourceID);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- using(System.IO.Stream s = this.OpenRead(req))
- return Utility.StreamAsArray(s);
+ return this.OpenRead(req);
}
/*public object DeserializeItem(System.IO.Stream s)
More information about the mapguide-commits
mailing list