Skip to content

Commit

Permalink
Refactor adapter to move FileSettings from XMLAdapter to XMLConfig (#593
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Fraser Greenroyd authored Oct 26, 2023
2 parents c6f9820 + 63d712d commit f9f3d34
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 36 deletions.
6 changes: 4 additions & 2 deletions XML_Adapter/AdapterActions/Pull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@
using BH.oM.Adapter;
using BH.oM.Base;
using BH.Engine.Adapter;
using BH.oM.Adapters.XML;

namespace BH.Adapter.XML
{
public partial class XMLAdapter : BHoMAdapter
{
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
{
if (!System.IO.File.Exists(_fileSettings.GetFullFileName()))
IXMLConfig config = actionConfig as IXMLConfig;
if (!System.IO.File.Exists(config.File.GetFullFileName()))
{
BH.Engine.Base.Compute.RecordError($"The file at {_fileSettings.GetFullFileName()} does not exist to pull from.");
BH.Engine.Base.Compute.RecordError($"The file at {config.File.GetFullFileName()} does not exist to pull from.");
return new List<IBHoMObject>();
}

Expand Down
3 changes: 2 additions & 1 deletion XML_Adapter/AdapterActions/Push.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using BH.oM.Adapters.XML;
using BH.oM.Adapters.XML.Enums;
using System.IO;
using BH.Engine.Adapter;

namespace BH.Adapter.XML
{
Expand Down Expand Up @@ -85,7 +86,7 @@ public override List<object> Push(IEnumerable<object> objects, String tag = "",
}

if (success && config.RemoveNils)
RemoveNil(_fileSettings);
RemoveNil(config.File);

return success ? objects.ToList() : new List<object>();
}
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/Bluebeam/ReadBluebeam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private IEnumerable<IBHoMObject> ReadBluebeam(Type type = null, XMLConfig config
{
BH.oM.XML.Bluebeam.BluebeamObject report = null;

TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
TextReader reader = new StreamReader(config.File.GetFullFileName());
XmlSerializer szer = new XmlSerializer(typeof(BH.oM.XML.Bluebeam.MarkupSummary));
report = (BH.oM.XML.Bluebeam.MarkupSummary)szer.Deserialize(reader);
reader.Close();
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/CSProject/CreateCSProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private bool CreateCSProject<T>(IEnumerable<T> objects, XMLConfig config)

xmlParts.Add("</Project>");

StreamWriter sw = new StreamWriter(_fileSettings.GetFullFileName());
StreamWriter sw = new StreamWriter(config.File.GetFullFileName());

xmlParts = xmlParts.Select(x => Regex.Replace(x, @"<\?xml version=""1.0"" encoding=""utf-[0-9]*""\?>\r\n", "")).ToList();
xmlParts = xmlParts.Select(x => x.Replace("q1:", "")).ToList();
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/CSProject/ReadCSProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private IEnumerable<IBHoMObject> ReadCSProject(Type type = null, XMLConfig confi
{
BH.oM.XML.CSProject.Project report = null;

TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
TextReader reader = new StreamReader(config.File.GetFullFileName());
XmlSerializer szer = new XmlSerializer(typeof(BH.oM.XML.CSProject.Project));
report = (BH.oM.XML.CSProject.Project)szer.Deserialize(reader);
reader.Close();
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/Default/CreateDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private bool CreateDefault<T>(IEnumerable<T> objects, XMLConfig config)

XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
XmlSerializer szer = new XmlSerializer(exportType, overrides);
TextWriter ms = new StreamWriter(_fileSettings.GetFullFileName());
TextWriter ms = new StreamWriter(config.File.GetFullFileName());
foreach (var obj in objects)
{
try
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/Default/ReadDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private IEnumerable<IBHoMObject> ReadDefault(Type type = null, XMLConfig config
foreach (System.Reflection.PropertyInfo pi in bhomProperties)
overrides.Add(typeof(BHoMObject), pi.Name, new XmlAttributes { XmlIgnore = true });

TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
TextReader reader = new StreamReader(config.File.GetFullFileName());
XmlSerializer szer = new XmlSerializer(type, overrides);
obj = System.Convert.ChangeType(szer.Deserialize(reader), type);
reader.Close();
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/EnergyPlus/ReadEnergyPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private IEnumerable<IBHoMObject> ReadEnergyPlus(Type type = null, XMLConfig conf
{
BH.oM.XML.EnergyPlus.EnergyPlusTabularReport report = null;

TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
TextReader reader = new StreamReader(config.File.GetFullFileName());
XmlSerializer szer = new XmlSerializer(typeof(BH.oM.XML.EnergyPlus.EnergyPlusTabularReport));
report = (BH.oM.XML.EnergyPlus.EnergyPlusTabularReport)szer.Deserialize(reader);
reader.Close();
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/GBXML/CreateGBXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private bool CreateGBXML<T>(IEnumerable<T> objects, XMLConfig config)

XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
XmlSerializer szer = new XmlSerializer(typeof(BH.Adapter.XML.GBXMLSchema.GBXML), overrides);
TextWriter ms = new StreamWriter(_fileSettings.GetFullFileName());
TextWriter ms = new StreamWriter(config.File.GetFullFileName());
szer.Serialize(ms, gbx, xns);
ms.Close();
}
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/GBXML/ReadGBXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public partial class XMLAdapter : BHoMAdapter
private IEnumerable<IBHoMObject> ReadGBXML(Type type = null, XMLConfig config = null)
{
BH.Adapter.XML.GBXMLSchema.GBXML gbx = null;
TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
TextReader reader = new StreamReader(config.File.GetFullFileName());
XmlSerializer szer = new XmlSerializer(typeof(BH.Adapter.XML.GBXMLSchema.GBXML));
gbx = (BH.Adapter.XML.GBXMLSchema.GBXML)szer.Deserialize(reader);
reader.Close();
Expand Down
2 changes: 1 addition & 1 deletion XML_Adapter/CRUD/KML/CreateKML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private bool CreateKML<T>(IEnumerable<T> objects, XMLConfig config)

XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
XmlSerializer szer = new XmlSerializer(typeof(BH.Adapter.XML.KMLSchema.KML), overrides);
TextWriter ms = new StreamWriter(_fileSettings.GetFullFileName());
TextWriter ms = new StreamWriter(config.File.GetFullFileName());
szer.Serialize(ms, kml, xns);
ms.Close();
}
Expand Down
24 changes: 4 additions & 20 deletions XML_Adapter/XMLAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,12 @@ namespace BH.Adapter.XML
{
public partial class XMLAdapter : BHoMAdapter
{
[Description("Specify XML file and properties for data transfer")]
[Input("fileSettings", "Input the file settings to get the file name and directory the XML Adapter should use")]
[Output("adapter", "Adapter to XML")]
public XMLAdapter(BH.oM.Adapter.FileSettings fileSettings = null)
[PreviousVersion("7.0", "BH.Adapter.XML.XMLAdapter(BH.oM.Adapter.FileSettings)")]
[Description("Connect to XML interoperability within the BHoM.")]
[Output("adapter", "Adapter to XML.")]
public XMLAdapter()
{
if (fileSettings == null)
{
BH.Engine.Base.Compute.RecordError("Please set the File Settings correctly to enable the XML Adapter to work correctly");
return;
}

if (!Path.HasExtension(fileSettings.FileName) || (Path.GetExtension(fileSettings.FileName) != ".xml" && Path.GetExtension(fileSettings.FileName) != ".csproj"))
{
BH.Engine.Base.Compute.RecordError("File name must contain a file extension");
return;
}

_fileSettings = fileSettings;

}

private BH.oM.Adapter.FileSettings _fileSettings { get; set; } = null;
}
}

Expand Down
35 changes: 35 additions & 0 deletions XML_oM/Config/IXMLConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM 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 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Adapter;
using BH.oM.Base;
using System;
using System.Collections.Generic;
using System.Text;

namespace BH.oM.Adapters.XML
{
public interface IXMLConfig : IObject
{
FileSettings File { get; set; }
}
}
8 changes: 4 additions & 4 deletions XML_oM/XMLConfig.cs → XML_oM/Config/XMLConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
namespace BH.oM.Adapters.XML
{
[Description("Define configuration settings for pushing and pulling XML files using the XML Adapter.")]
public class XMLConfig : ActionConfig
public class XMLConfig : ActionConfig, IXMLConfig
{
[Description("File settings for the file to push to or pull from.")]
public virtual FileSettings File { get; set; } = null;

[Description("Define the schema which the XML Adapter should be operating with.")]
public virtual Schema Schema { get; set; } = Schema.Undefined;

Expand All @@ -47,6 +50,3 @@ public class XMLConfig : ActionConfig
public virtual bool RemoveNils { get; set; } = false;
}
}



0 comments on commit f9f3d34

Please sign in to comment.