//******************************************************************************************************
// Tag.cs - Gbtc
//
// Copyright © 2015, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 12/14/2015 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using GSF.PQDIF.Physical;
namespace GSF.PQDIF
{
///
/// Represents a tag as defined by the PQDIF standard.
///
public class Tag
{
#region [ Members ]
// Fields
private Guid m_id;
private string m_name;
private string m_standardName;
private string m_description;
private ElementType m_elementType;
private PhysicalType m_physicalType;
private bool m_required;
private string m_formatString;
private List m_validIdentifiers;
#endregion
#region [ Constructors ]
// Tags are readonly and can only be created by
// defining them in the TagDefinitions.xml file.
private Tag(XDocument doc, XElement element)
{
m_id = Guid.Parse((string)element.Element("id"));
m_name = (string)element.Element("name");
m_standardName = (string)element.Element("standardName");
m_description = (string)element.Element("description");
m_elementType = GetElementType(element);
m_physicalType = GetPhysicalType(element);
m_required = ((string)element.Element("required") ?? "False").ParseBoolean();
m_formatString = (string)element.Element("formatString");
m_validIdentifiers = Identifier.GenerateIdentifiers(doc, this);
}
#endregion
#region [ Properties ]
///
/// Gets the globally unique identifier for the tag.
///
public Guid ID => m_id;
///
/// Gets the name of the tag as defined by the GSF.PQDIF library.
///
public string Name => m_name;
///
/// Gets the name of the tag as defined by the standard.
///
public string StandardName => m_standardName;
///
/// Gets a description of the tag.
///
public string Description => m_description;
///
/// Gets the type of the element identified by the tag
/// according to the logical structure of a PQDIF file.
///
public ElementType ElementType => m_elementType;
///
/// Gets the physical type of the value of the element identified
/// by the tag according to the logical structure of a PQDIF file.
///
public PhysicalType PhysicalType => m_physicalType;
///
/// Gets the flag that determines whether it is
/// required that this tag be specified in a PQDIF file.
///
public bool Required => m_required;
///
/// Format string specified for some tags as a
/// hint for how the value should be displayed.
///
public string FormatString => m_formatString;
///
/// Gets the collection of valid identifiers for this tag.
///
public IReadOnlyCollection ValidIdentifiers => m_validIdentifiers.AsReadOnly();
#endregion
#region [ Static ]
// Static Fields
private static Dictionary TagLookup;
// Static Methods
///
/// Gets the tag identified by the given globally unique identifier.
///
/// The globally unique identifier for the tag to be retrieved.
/// The tag, if defined, or null if the tag is not found.
public static Tag GetTag(Guid id)
{
Tag tag;
if ((object)TagLookup == null)
RefreshTags(XDocument.Load("TagDefinitions.xml"));
if (!TagLookup.TryGetValue(id, out tag))
return null;
return tag;
}
///
/// Generates a list of tags from the given XML document.
///
/// The XML document containing the tag definitions.
/// The list of tags as defined by the given XML document.
public static List GenerateTags(XDocument doc)
{
return doc.Descendants("tag")
.Select(element => new Tag(doc, element))
.ToList();
}
///
/// Refreshes the cache of tags used to return
/// tags from the method.
///
/// The XML document containing the tag definitions.
public static void RefreshTags(XDocument doc)
{
TagLookup = GenerateTags(doc).ToDictionary(t => t.ID);
}
// Attempts to parse the element type via the ElementType enumeration.
// Failing that, attempts to parse it as an integer instead.
private static ElementType GetElementType(XElement element)
{
string elementTypeName = (string)element.Element("elementType");
ElementType elementType;
byte elementTypeID;
if (Enum.TryParse(elementTypeName, out elementType))
return elementType;
else if (byte.TryParse(elementTypeName, out elementTypeID))
return (ElementType)elementTypeID;
return 0;
}
// Attempts to parse the physical type via the PhysicalType enumeration.
// Failing that, attempts to parse it as an integer instead.
private static PhysicalType GetPhysicalType(XElement element)
{
string physicalTypeName = (string)element.Element("physicalType");
PhysicalType physicalType;
byte physicalTypeID;
if (Enum.TryParse(physicalTypeName, out physicalType))
return physicalType;
else if (byte.TryParse(physicalTypeName, out physicalTypeID))
return (PhysicalType)physicalTypeID;
return 0;
}
#endregion
}
}