private static bool MergeTextWithBIRDDoc(ref FDO.IText newText, TextCreationParams textParams)
{
s_importOptions = textParams.ImportOptions;
Interlineartext interlinText = textParams.InterlinText;
FdoCache cache = textParams.Cache;
IThreadedProgress progress = textParams.Progress;
if (s_importOptions.CheckAndAddLanguages == null)
s_importOptions.CheckAndAddLanguages = CheckAndAddLanguagesInternal;
ILgWritingSystemFactory wsFactory = cache.WritingSystemFactory;
char space = ' ';
//handle the languages(writing systems) section alerting the user if new writing systems are encountered
if (!s_importOptions.CheckAndAddLanguages(cache, interlinText, wsFactory, progress))
return false;
//handle the header(info or meta) information as well as any media-files sections
SetTextMetaAndMergeMedia(cache, interlinText, wsFactory, newText, true);
IStText newContents = null;
//create all the paragraphs NOTE: Currently the paragraph guids are being ignored, this might be wrong.
foreach (var paragraph in interlinText.paragraphs)
{
if (newContents == null)
{
newContents = cache.ServiceLocator.GetInstance<IStTextFactory>().Create();
newText.ContentsOA = newContents;
}
IStTxtPara newTextPara = newContents.AddNewTextPara("");
int offset = 0;
if (paragraph.phrases == null)
{
continue;
}
foreach (var phrase in paragraph.phrases)
{
ICmObject oldSegment = null;
//Try and locate a segment with this Guid.
if(!String.IsNullOrEmpty(phrase.guid))
{
if (cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(phrase.guid), out oldSegment))
{
if (oldSegment as ISegment != null) //The segment matches, add it into our paragraph.
newTextPara.SegmentsOS.Add(oldSegment as ISegment);
else if(oldSegment == null) //The segment is identified by a Guid, but apparently we don't have it in our current document, so make one
oldSegment = cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset, cache, new Guid(phrase.guid));
else //The Guid is in use, but not by a segment. This is bad.
{
return false;
}
}
}
//set newSegment to the old, or create a brand new one.
ISegment newSegment = oldSegment as ISegment ?? cache.ServiceLocator.GetInstance<ISegmentFactory>().Create(newTextPara, offset);
var tsStrFactory = cache.ServiceLocator.GetInstance<ITsStrFactory>();
//Fill in the ELAN time information if it is present.
AddELANInfoToSegment(cache, phrase, newSegment);
ITsString phraseText = null;
bool textInFile = false;
//Add all of the data from <item> elements into the segment.
AddSegmentItemData(cache, wsFactory, phrase, newSegment, tsStrFactory, ref textInFile, ref phraseText);
bool lastWasWord = false;
if (phrase.WordsContent != null && phrase.WordsContent.Words != null)
{
foreach (var word in phrase.WordsContent.Words)
{
//If the text of the phrase was not found in a "txt" item for this segment then build it from the words.
if (!textInFile)
{
UpdatePhraseTextForWordItems(wsFactory, tsStrFactory, ref phraseText, word, ref lastWasWord, space);
}
MergeWordToSegment(newSegment, word, tsStrFactory);
}
}
UpdateParagraphTextForPhrase(newTextPara, ref offset, phraseText);
}
}
return true;
}