Skip to content

Commit

Permalink
Revert ":sparkles: Validate struxml before Deserialization"
Browse files Browse the repository at this point in the history
This reverts commit 7f9caf1.
  • Loading branch information
xRadne committed Dec 1, 2022
1 parent 7f9caf1 commit ce147a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
4 changes: 0 additions & 4 deletions FemDesign.Core/FemDesign.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,6 @@
<EmbeddedResource Include="Resources\loadCoefficients\loadCoefficients_S.struxml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\FD 21.01.003.xsd">
<Link>FD 21.01.003.xsd</Link>
<SubType>Designer</SubType>
</EmbeddedResource>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSHARP.Targets" />
Expand Down
36 changes: 9 additions & 27 deletions FemDesign.Core/Model/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using FemDesign.GenericClasses;

Expand All @@ -19,8 +17,6 @@ namespace FemDesign
[XmlRoot("database", Namespace = "urn:strusoft")]
public partial class Model
{
private const string _struxmlSchemaResourceName = "FemDesign.FD 21.01.003.xsd";

[XmlIgnore]
public Calculate.Application FdApp = new Calculate.Application(); // start a new FdApp to get process information.
/// <summary>
Expand Down Expand Up @@ -198,37 +194,23 @@ public static Model DeserializeFromFilePath(string filePath)
// check file extension
if (Path.GetExtension(filePath) != ".struxml")
{
throw new ArgumentException("File extension must be .struxml! Model.DeserializeModel failed.");
}

// Validate
XmlDocument xmlDocument = new XmlDocument();
Assembly assembly = Assembly.GetExecutingAssembly();

using (Stream schemaStream = assembly.GetManifestResourceStream(_struxmlSchemaResourceName))
{
XmlTextReader schemaReader = new XmlTextReader(schemaStream);
XmlSchema struxmlSchema = XmlSchema.Read(schemaReader, (o, e) => throw e.Exception);

xmlDocument.Schemas.Add(struxmlSchema);
xmlDocument.Load(filePath);
xmlDocument.Validate((o, e) => throw e.Exception);
throw new System.ArgumentException("File extension must be .struxml! Model.DeserializeModel failed.");
}

// Deserialize
//
XmlSerializer deserializer = new XmlSerializer(typeof(Model));
XmlReader reader = new XmlNodeReader(xmlDocument);
TextReader reader = new StreamReader(filePath);

object obj;
try
{
obj = deserializer.Deserialize(reader);
}
catch (InvalidOperationException ex)
catch (System.InvalidOperationException ex)
{
if (ex.InnerException != null && ex.InnerException is TargetInvocationException)
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(System.Reflection.TargetInvocationException))
{
if (ex.InnerException.InnerException != null && ex.InnerException.InnerException is Calculate.ProgramNotStartedException)
if (ex.InnerException.InnerException != null && ex.InnerException.InnerException.GetType() == typeof(Calculate.ProgramNotStartedException))
{
throw ex.InnerException.InnerException; // FEM-Design 21 - 3D Structure must be running! Start FEM-Design " + this.FdTargetVersion + " - 3D Structure and reload script
}
Expand All @@ -241,12 +223,12 @@ public static Model DeserializeFromFilePath(string filePath)
reader.Close();
}

// Cast type
// cast type
Model model = (Model)obj;

if (model.Entities == null) model.Entities = new Entities();

// Prepare elements with library reference
// prepare elements with library reference
// Check if there are any elements of type to avoid null checks on each library type (sections, materials etc.) in each method below
if (model.Entities.Bars.Any())
model.GetBars();
Expand Down Expand Up @@ -2234,7 +2216,7 @@ private void AddFoundation(IFoundationElement obj, bool overwrite)
else if (obj.GetType() == typeof(Foundations.IsolatedFoundation))
{
this.AddIsolatedFoundation((Foundations.IsolatedFoundation)obj, overwrite);
this.AddMaterial(((Foundations.IsolatedFoundation)obj).ComplexMaterialObj, overwrite);
this.AddMaterial( ((Foundations.IsolatedFoundation)obj).ComplexMaterialObj, overwrite);
}
else
{
Expand Down

0 comments on commit ce147a5

Please sign in to comment.