132 lines
4.3 KiB
C#
Executable File
132 lines
4.3 KiB
C#
Executable File
/* LaurisParagraph.cs
|
|
* part of zaaReloaded2
|
|
*
|
|
* Copyright 2015 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.Dictionaries;
|
|
using zaaReloaded2.LabModel;
|
|
|
|
namespace zaaReloaded2.Importer.ZaaImporter
|
|
{
|
|
/// <summary>
|
|
/// Parses an entire Lauris paragraph (such as "Klinische Chemie: ...")
|
|
/// and creates a list of <see cref="LabItem"/>s.
|
|
/// </summary>
|
|
public class LaurisParagraph
|
|
{
|
|
#region Public properties
|
|
|
|
/// <summary>
|
|
/// Gets a collection of <see cref="LabItem"/>s found in this paragraph.
|
|
/// </summary>
|
|
public IItemDictionary Items { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the caption that was extracted from the <see cref="OriginalParagraph"/>,
|
|
/// e.g. "Klin. Chemie" in "Klin. Chemie: Natrium ...".
|
|
/// </summary>
|
|
public string Caption { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the original paragraph that this object was constructed from.
|
|
/// </summary>
|
|
public string OriginalParagraph { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Is true if the <see cref="OriginalParagraph"/> matches the expected
|
|
/// format and contains <see cref="LabItem"/>s.
|
|
/// </summary>
|
|
public bool IsLaurisParagraph { get; private set; }
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public LaurisParagraph(string paragraph)
|
|
{
|
|
OriginalParagraph = paragraph;
|
|
Parse();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a <see cref="LaurisParagraph"/> object from a given
|
|
/// Lauris paragraph, using a <paramref name="parameterDictionary"/>
|
|
/// and a <paramref name="unitDictionary"/> to translate the individual
|
|
/// items' properties.
|
|
/// </summary>
|
|
/// <param name="paragraph">lauris paragraph to parse.</param>
|
|
/// <param name="parameterDictionary">ParameterDictionary that contains
|
|
/// canonical names and material types.</param>
|
|
/// <param name="unitDictionary">Unit dictionary that contains canonical
|
|
/// unit names.</param>
|
|
public LaurisParagraph(string paragraph,
|
|
Dictionaries.ParameterDictionary parameterDictionary,
|
|
Dictionaries.UnitDictionary unitDictionary)
|
|
{
|
|
OriginalParagraph = paragraph;
|
|
_parameterDictionary = parameterDictionary;
|
|
_unitDictionary = unitDictionary;
|
|
Parse();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private methods
|
|
|
|
/// <summary>
|
|
/// Attempts to parse a Lauris paragraph.
|
|
/// </summary>
|
|
void Parse()
|
|
{
|
|
Match m = _expectedFormat.Match(OriginalParagraph);
|
|
if (m.Success)
|
|
{
|
|
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.Add(i.QualifiedName, i);
|
|
}
|
|
IsLaurisParagraph = Items.Count > 0;
|
|
}
|
|
else
|
|
{
|
|
IsLaurisParagraph = false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Fields
|
|
|
|
static readonly Regex _expectedFormat = new Regex(@"(?<caption>[^:]+:\s*)?(?<items>[^:]+:\s*[^;]+;)*");
|
|
Dictionaries.ParameterDictionary _parameterDictionary;
|
|
Dictionaries.UnitDictionary _unitDictionary;
|
|
|
|
#endregion
|
|
}
|
|
}
|