/* 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
{
///
/// Parses an entire Lauris paragraph (such as "Klinische Chemie: ...")
/// and creates a list of s.
///
public class LaurisParagraph
{
#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,
Dictionaries.ParameterDictionary parameterDictionary,
Dictionaries.UnitDictionary unitDictionary)
{
OriginalParagraph = paragraph;
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
Parse();
}
#endregion
#region Private methods
///
/// Attempts to parse a Lauris paragraph.
///
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(@"(?
[^:]+:\s*)?(?[^:]+:\s*[^;]+;)*");
Dictionaries.ParameterDictionary _parameterDictionary;
Dictionaries.UnitDictionary _unitDictionary;
#endregion
}
}