Skip to content

Commit

Permalink
XPath implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 30, 2024
1 parent 72355e1 commit e742a15
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 28 deletions.
5 changes: 5 additions & 0 deletions YangParser/Generator/YangGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ public class TargetAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class XPathAttribute(string path) : Attribute
{
public string Path { get; } = path;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class KeyAttribute(params string[] value) : Attribute
{
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override string ToCode()
builder.AppendLine(
$"public static {returnType} {MakeName(Argument)}(IChannel channel, int messageID{inputType})");
builder.AppendLine("{");
var ns = Parent is Module module ? "xmlns=\\\"" + module.XmlNamespace.Argument + "\\\"" : string.Empty;
var ns = Parent is Module module ? $"xmlns:{module.XmlNamespace?.Prefix}=\\\"" + module.XmlNamespace?.Namespace + "\\\"" : string.Empty;
builder.AppendLine(inputType != string.Empty
? $$"""
var xml = $"<rpc message-id=\"{messageID}\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><{{Argument}} {{ns}}>{input.ToXML()}</{{Argument}}></rpc>";
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/BelongsTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public BelongsTo(YangStatement statement) : base(statement)

public override ChildRule[] PermittedChildren { get; } =
[
new ChildRule(Prefix.Keyword, Cardinality.Required)
new ChildRule(SemanticModel.Prefix.Keyword, Cardinality.Required)
];

public override string ToCode()
Expand Down
8 changes: 4 additions & 4 deletions YangParser/SemanticModel/Grouping.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using YangParser.Generator;
using YangParser.Parser;
Expand Down Expand Up @@ -41,7 +40,7 @@ public override string ToCode()
public IStatement[] WithUse(Uses use)
{
var copy = StatementFactory.Create(Source);
Parent.Insert([copy]);
Parent!.Insert([copy]);
copy.Parent = Parent;
foreach (var child in copy.Unwrap())
{
Expand Down Expand Up @@ -123,12 +122,13 @@ public IStatement[] WithUse(Uses use)
var current = copy;
foreach (var element in path)
{
var prefix = element.Prefix(out var name);
element.Prefix(out var name);
var origin = current;
current = origin.Children.FirstOrDefault(c => c.Argument == name);
if (current is null)
{
Log.Write($"Could not find part '{name}' of path {refinement.Argument} in source {origin}");
Log.Write(
$"Could not find part '{name}' of path {refinement.Argument} in source {origin.Source.Keyword} {origin.Argument}");
break; //Target not present, nothing to refine.
}
}
Expand Down
5 changes: 4 additions & 1 deletion YangParser/SemanticModel/IStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace YangParser.SemanticModel;

public interface IStatement
{
string Argument { get; set; }
string Argument { get; set; }
ChildRule[] PermittedChildren { get; }
HashSet<string> Attributes { get; }
HashSet<string> Keywords { get; }
Expand All @@ -18,6 +18,9 @@ public interface IStatement
string ToCode();
void Replace(IStatement child, IEnumerable<IStatement> replacements);
void Insert(IEnumerable<IStatement> augments);
string XPath { get; }
(string Namespace, string Prefix)? XmlNamespace { get; set; }
string Prefix { get; }
}

public interface ICommentable : IStatement
Expand Down
11 changes: 0 additions & 11 deletions YangParser/SemanticModel/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@ public Import(YangStatement statement) : base(statement)
{
if (statement.Keyword != Keyword)
throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement);

Revision = Children.FirstOrDefault(child => child is RevisionDate)?.Argument;
Prefix = Children.First(child => child is Prefix).Argument;
var reference = Children.FirstOrDefault(child => child is Reference);
var description = Children.FirstOrDefault(child => child is Description);
AdditionalInformation = reference is null ? string.Empty : $"Reference: \"{reference.Argument}\", ";
AdditionalInformation += description is null ? string.Empty : $"Description: \"{description}\"";
}

private readonly string? Revision;
private readonly string Prefix;
private readonly string? AdditionalInformation;

public const string Keyword = "import";

public override ChildRule[] PermittedChildren { get; } =
Expand Down
7 changes: 2 additions & 5 deletions YangParser/SemanticModel/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public Module(YangStatement statement) : base(statement)
{
if (statement.Keyword != Keyword)
throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement);
XmlNamespace = Children.First(child => child is Namespace);
var localPrefix = this.GetChild<Prefix>().Argument;
var localNS = MakeNamespace(Argument) + ".YangNode.";
XmlNamespace = (Children.First(child => child is Namespace).Argument, localPrefix);
Usings = new()
{
[localPrefix] = localNS
Expand Down Expand Up @@ -64,9 +64,6 @@ public Module(YangStatement statement) : base(statement)
}

public Dictionary<string, string> ImportedModules { get; } = [];


public IStatement XmlNamespace { get; set; }
public Dictionary<string, string> Usings { get; }
public string Namespace { get; private set; }

Expand All @@ -91,7 +88,7 @@ public Module(YangStatement statement) : base(statement)
new ChildRule(SemanticModel.Namespace.Keyword, Cardinality.Required),
new ChildRule(Notification.Keyword, Cardinality.ZeroOrMore),
new ChildRule(Organization.Keyword),
new ChildRule(Prefix.Keyword, Cardinality.Required),
new ChildRule(SemanticModel.Prefix.Keyword, Cardinality.Required),
new ChildRule(Reference.Keyword),
new ChildRule(Revision.Keyword, Cardinality.ZeroOrMore),
new ChildRule(Rpc.Keyword, Cardinality.ZeroOrMore),
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Rpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override string ToCode()
builder.AppendLine(
$"public static {returnType} {MakeName(Argument)}(IChannel channel, int messageID{inputType})");
builder.AppendLine("{");
var ns = Parent is Module module ? "xmlns=\\\"" + module.XmlNamespace.Argument + "\\\"" : string.Empty;
var ns = Parent is Module module ? $"xmlns:{module.XmlNamespace?.Prefix}=\\\"" + module.XmlNamespace?.Namespace + "\\\"" : string.Empty;
builder.AppendLine(inputType != string.Empty
? $$"""
var xml = $"<rpc message-id=\"{messageID}\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><{{Argument}} {{ns}}>{input.ToXML()}</{{Argument}}></rpc>";
Expand Down
20 changes: 16 additions & 4 deletions YangParser/SemanticModel/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using YangParser.Parser;
using YangParser.SemanticModel.Builtins;
using String = System.String;

namespace YangParser.SemanticModel;

Expand All @@ -23,6 +24,12 @@ protected Statement(YangStatement statement, bool validate = true)
Children = statement.Children.Select(StatementFactory.Create).ToArray();
}

public (string Namespace, string Prefix)? XmlNamespace { get; set; }
public string Prefix => XmlNamespace?.Prefix ?? Parent?.Prefix ?? string.Empty;

public string XPath => ((Parent?.XPath ?? String.Empty) + "/").Replace("//", "/") +
(string.IsNullOrEmpty(Prefix) ? string.Empty : Prefix + ":") + Source.Argument;

public YangStatement Source { get; set; }

private IStatement[] _children = [];
Expand Down Expand Up @@ -76,7 +83,7 @@ public static string MakeName(string argument)

var prefix = argument.Prefix(out var value);
var addColon = !prefix.Contains('.') && !string.IsNullOrWhiteSpace(prefix);
foreach (var section in value.Split('-', ' ', '/', '.','^'))
foreach (var section in value.Split('-', ' ', '/', '.', '^'))
{
output.Append(Capitalize(section));
}
Expand Down Expand Up @@ -124,9 +131,14 @@ protected static string TypeName(Type type)

protected string KeywordString => " " + string.Join(" ", Keywords) + (Keywords.Count > 0 ? " " : "");

public string AttributeString => Attributes.Count > 0
? "\n" + string.Join("\n", Attributes.OrderBy(x => x.Length).Select(attr => $"[{attr}]"))
: string.Empty;
public string AttributeString
{
get
{
Attributes.Add("XPath(\"" + XPath + '"' + ')');
return "\n" + string.Join("\n", Attributes.OrderBy(x => x.Length).Select(attr => $"[{attr}]"));
}
}

public string DescriptionString => $"""
///<summary>
Expand Down

0 comments on commit e742a15

Please sign in to comment.