Skip to content

Commit

Permalink
Add remove nils option
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd committed Jun 21, 2023
1 parent 995a32d commit d50e2ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
40 changes: 34 additions & 6 deletions XML_Adapter/CRUD/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using BH.oM.Adapters.XML.Enums;

using BH.oM.Adapter;
using System.IO;

namespace BH.Adapter.XML
{
Expand All @@ -50,23 +51,50 @@ protected override bool ICreate<T>(IEnumerable<T> objects, ActionConfig actionCo
return false;
}

bool success = false;

switch(config.Schema)
{
case Schema.CSProject:
return CreateCSProject(objects, config);
success = CreateCSProject(objects, config);
break;
case Schema.GBXML:
return CreateGBXML(objects, config);
success = CreateGBXML(objects, config);
break;
case Schema.KML:
return CreateKML(objects, config);
success = CreateKML(objects, config);
break;
case Schema.EnergyPlusLoads:
BH.Engine.Base.Compute.RecordError("The EnergyPlusLoads Schema is not supported for push operations at this time");
return false;
success = false;
break;
case Schema.Bluebeam:
BH.Engine.Base.Compute.RecordError("The Bluebeam markup schema is not supported for push operations at this time.");
return false;
success = false;
break;
default:
return CreateDefault(objects, config);
success = CreateDefault(objects, config);
break;
}

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

return success;
}

private static bool RemoveNil(FileSettings file)
{
var path = Path.Combine(file.Directory, file.FileName);
var xmlFile = File.ReadAllLines(path);

xmlFile = xmlFile.Where(x => !x.Trim().Contains("xsi:nil")).ToArray();
xmlFile = xmlFile.Where(x => x != null).ToArray();

File.Delete(path);
File.WriteAllLines(path, xmlFile);

return true;
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions XML_oM/XMLConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@

namespace BH.oM.Adapters.XML
{
[Description("Define configuration settings for pushing and pulling XML files using the XML Adapter")]
[Description("Define configuration settings for pushing and pulling XML files using the XML Adapter.")]
public class XMLConfig : ActionConfig
{
[Description("Define the schema which the XML Adapter should be operating with")]
[Description("Define the schema which the XML Adapter should be operating with.")]
public virtual Schema Schema { get; set; } = Schema.Undefined;

[Description("Set optional settings to use when pushing or pulling XML based on the chosen schema")]
[Description("Set optional settings to use when pushing or pulling XML based on the chosen schema.")]
public virtual IXMLSettings Settings { get; set; } = null;

[Description("Determine whether 'nil' XML attributes should be removed when pushing to an XML file.")]
public virtual bool RemoveNils { get; set; } = false;
}
}

Expand Down

0 comments on commit d50e2ac

Please sign in to comment.