119 lines
4.0 KiB
C#
Executable File
119 lines
4.0 KiB
C#
Executable File
using Microsoft.Office.Interop.Word;
|
|
/* AutoDetect.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.Linq;
|
|
using System.Text;
|
|
|
|
namespace zaaReloaded2.Importer.ZaaImporter
|
|
{
|
|
static class AutoDetect
|
|
{
|
|
/// <summary>
|
|
/// Attempts to automatically detect laboratory data in the Word
|
|
/// document.
|
|
/// </summary>
|
|
/// <param name="document">Document which to parse for laboratory
|
|
/// data.</param>
|
|
/// <returns>True if laboratory data was detected, false if not.</returns>
|
|
/// <exception cref="ArgumentNullException">if <paramref name="document"/>
|
|
/// is null.</exception>
|
|
public static bool Detect(Document document)
|
|
{
|
|
if (document == null)
|
|
{
|
|
throw new ArgumentNullException(
|
|
"Automatic laboratory detection requires a document.");
|
|
}
|
|
|
|
// TODO: Try to make this algorithm more elegant.
|
|
Paragraph start = null;
|
|
Paragraph end = null;
|
|
int i = 1;
|
|
|
|
if (document.Bookmarks.Exists("Labor"))
|
|
{
|
|
i = GetParagraphIndex(
|
|
document,
|
|
document.Bookmarks["Labor"].Range.Paragraphs[1]);
|
|
}
|
|
|
|
while (i <= document.Paragraphs.Count)
|
|
{
|
|
// Expect the first paragraph of a Lauris block to be
|
|
// a time stamp. This prevents erroneous detection of
|
|
// lines such as "Tel. (09 31) 201-39432; -39126", which
|
|
// happen to structurally resemble a paragraph with
|
|
// laboratory items.
|
|
if (LaurisTimePoint.IsTimeStampLine(
|
|
document.Paragraphs[i].Range.Text))
|
|
{
|
|
start = document.Paragraphs[i];
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
if (start != null)
|
|
{
|
|
end = start;
|
|
while (i <= document.Paragraphs.Count - 1)
|
|
{
|
|
if (!IsLabParagraph(document.Paragraphs[i+1]))
|
|
{
|
|
end = document.Paragraphs[i];
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
document.Range(start.Range.Start, end.Range.End).Select();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if a paragraph is either a time stamp line
|
|
/// or a paragraph with laboratory items.
|
|
/// </summary>
|
|
/// <param name="paragraph"></param>
|
|
/// <returns></returns>
|
|
private static bool IsLabParagraph(Paragraph paragraph)
|
|
{
|
|
string text = paragraph.Range.Text;
|
|
return (LaurisParagraph.ResemblesLaurisParagraph(text)
|
|
|| LaurisTimePoint.IsTimeStampLine(text));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the index of a paragraph.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// http://word.mvps.org/faqs/macrosvba/GetIndexNoOfPara.htm
|
|
/// </remarks>
|
|
/// <param name="paragraph">Paragraph whose index to return.</param>
|
|
/// <returns>Index of the paragraph.</returns>
|
|
private static int GetParagraphIndex(Document document, Paragraph paragraph)
|
|
{
|
|
return document.Range(0, paragraph.Range.Start).Paragraphs.Count;
|
|
}
|
|
}
|
|
}
|