/* LaurisParagraph.cs
* part of zaaReloaded2
*
* Copyright 2015-2017 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using zaaReloaded2.Thesaurus;
using zaaReloaded2.LabModel;
using Bovender.Extensions;
namespace zaaReloaded2.Importer.ZaaImporter
{
///
/// Parses an entire Lauris paragraph (such as "Klinische Chemie: ...")
/// and creates a list of s.
///
public class LaurisParagraph
{
#region Static methods
///
/// Investigates a paragraph and determines whether it looks
/// like a Lauris laboratory items paragraph.
///
public static bool ResemblesLaurisParagraph(string paragraph)
{
return _expectedFormat.IsMatch(paragraph);
}
#endregion
#region Public properties
///
/// Gets a collection of s found in this paragraph.
///
public IItemDictionary Items { get; private set; }
///
/// Gets the caption that was extracted from the ,
/// e.g. "Klin. Chemie" in "Klin. Chemie: Natrium ...".
///
public string Caption { get; private set; }
///
/// Gets the original paragraph that this object was constructed from.
///
public string OriginalParagraph { get; private set; }
///
/// Is true if the matches the expected
/// format and contains s.
///
public bool IsLaurisParagraph { get; private set; }
#endregion
#region Constructor
public LaurisParagraph(string paragraph)
{
OriginalParagraph = paragraph;
Parse();
}
///
/// Constructs a object from a given
/// Lauris paragraph, using a
/// and a to translate the individual
/// items' properties.
///
/// lauris paragraph to parse.
/// ParameterDictionary that contains
/// canonical names and material types.
/// Unit dictionary that contains canonical
/// unit names.
public LaurisParagraph(string paragraph,
Thesaurus.Parameters parameterDictionary,
Thesaurus.Units unitDictionary)
{
OriginalParagraph = paragraph;
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
Parse();
}
#endregion
#region Private methods
///
/// Attempts to parse a Lauris paragraph.
///
void Parse()
{
Logger.Info("Parse: \"{0}\"", OriginalParagraph.TruncateWithEllipsis(40));
Match m = _expectedFormat.Match(OriginalParagraph);
if (m.Success)
{
Logger.Info("Parse: Matches Lauris paragraph format");
Items = new ItemDictionary();
if (m.Groups["caption"].Success)
{
Caption = m.Groups["caption"].Value.Trim(new char[] {' ', ':'});
}
foreach (Capture itemCapture in m.Groups["items"].Captures)
{
LaurisItem i = new LaurisItem(itemCapture.Value, _parameterDictionary, _unitDictionary);
Items[i.QualifiedName] = i;
}
IsLaurisParagraph = Items.Count > 0;
}
else
{
Logger.Info("Parse: Does not match Lauris paragraph format");
IsLaurisParagraph = false;
}
}
#endregion
#region Fields
static readonly Regex _expectedFormat = new Regex(@"(?
[^:]+:\s*)?(?[^:]+:\s*[^;]+;)+");
Thesaurus.Parameters _parameterDictionary;
Thesaurus.Units _unitDictionary;
#endregion
#region Class logger
private static NLog.Logger Logger { get { return _logger.Value; } }
private static readonly Lazy _logger = new Lazy(() => NLog.LogManager.GetCurrentClassLogger());
#endregion
}
}