Skip to content

Commit

Permalink
More XML
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 30, 2024
1 parent 976cceb commit 96d8867
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
1 change: 1 addition & 0 deletions YangParser/SemanticModel/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public override string ToCode()
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
settings.Async = true;
StringBuilder stringBuilder = new StringBuilder();
using XmlWriter writer = XmlWriter.Create(stringBuilder, settings);
await writer.WriteStartElementAsync(null,"rpc","urn:ietf:params:xml:ns:netconf:base:1.0");
Expand Down
26 changes: 15 additions & 11 deletions YangParser/SemanticModel/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,29 @@ public Container(string argument, Metadata metadata) : base(
public override string ToCode()
{
var nodes = Children.Select(child => child.ToCode()).ToArray();
var name = Argument;
if (Parent!.Argument == Argument)
{
name = "sub-" + name;
}

TargetName = MakeName(name);

string property = $"public{KeywordString}{MakeName(name)}Container? {MakeName(name)} {{ get; set; }}";
string property = $"public{KeywordString}{TargetName}Container? {TargetName} {{ get; set; }}";
return $$"""
{{property}}
{{DescriptionString}}{{AttributeString}}
public class {{MakeName(name)}}Container
public class {{TargetName}}Container
{
{{string.Join("\n\t", nodes.Select(Indent))}}
{{Indent(XmlFunction())}}
}
""";
}

public string TargetName { get; private set; } = string.Empty;
public string TargetName
{
get
{
var name = Argument;
if (Parent!.Argument == Argument)
{
name = "sub-" + name;
}

return MakeName(name);
}
}
}
50 changes: 49 additions & 1 deletion YangParser/SemanticModel/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public Notification(YangStatement statement) : base(statement)
{
if (statement.Keyword != Keyword)
throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement);

}

public const string Keyword = "notification";
Expand All @@ -36,12 +35,61 @@ public Notification(YangStatement statement) : base(statement)
public override string ToCode()
{
var nodes = Children.Select(child => child.ToCode()).ToArray();
var xmlWrite = GetXmlWriting();
return $$"""
{{DescriptionString}}{{AttributeString}}
public class {{MakeName(Argument)}}
{
{{string.Join("\n\t", nodes.Select(Indent))}}
public async Task<string> ToXML()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
settings.Async = true;
StringBuilder stringBuilder = new StringBuilder();
using XmlWriter writer = XmlWriter.Create(stringBuilder, settings);
await writer.WriteStartElementAsync(null,"notification","urn:ietf:params:xml:ns:netconf:notification:1.0");
await writer.WriteStartElementAsync(null,"eventTime",null);
await writer.WriteStringAsync(DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ssZ"));
await writer.WriteEndElementAsync();
await writer.WriteStartElementAsync("{{XmlNamespace?.Prefix}}","{{Argument}}","{{XmlNamespace?.Namespace}}");
{{xmlWrite}}
await writer.WriteEndElementAsync();
await writer.WriteEndElementAsync();
return stringBuilder.ToString();
}
{{Indent(XmlFunction())}}
}
""";
}

private string GetXmlWriting()
{
if (Parent is Module)
{
return "await WriteXML(writer);";
}

var parent = Parent;
while (parent != null)
{
if (parent.Parent is Module or Submodule)
{
break;
}

parent = parent.Parent;
}

if (parent is IXMLSource source)
{
return $"await WriteXML(writer);";
}

throw new SemanticError(
$"Top level statement of 'notification {Argument}' ({parent?.Source.Keyword} {parent?.Argument}) was not a valid XML source",
Source);
}
}
1 change: 1 addition & 0 deletions YangParser/SemanticModel/Rpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public override string ToCode()
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
settings.Async = true;
StringBuilder stringBuilder = new StringBuilder();
using XmlWriter writer = XmlWriter.Create(stringBuilder, settings);
await writer.WriteStartElementAsync(null,"rpc","urn:ietf:params:xml:ns:netconf:base:1.0");
Expand Down

0 comments on commit 96d8867

Please sign in to comment.