/// ------------------------------------------------------------------------------------ /// <summary> /// Add a new IScrImportFileInfo to the list /// </summary> /// <param name="value">a IScrImportFileInfo object</param> /// <returns>The <c>ArrayList</c> index at which the value has been added or /// -1 if it was not added</returns> /// ------------------------------------------------------------------------------------ public override int Add(object value) { IScrImportFileInfo fileToAdd = (IScrImportFileInfo)value; // TODO: make sure it does not already exist if (fileToAdd.IsReadable) { if (!CheckForOverlaps(fileToAdd)) { return(-1); } for (int index = 0; index < Count; index++) { IScrImportFileInfo file = (IScrImportFileInfo)this[index]; if (file.StartRef > fileToAdd.StartRef) { base.Insert(index, fileToAdd); m_modified = true; return(index); } } } m_modified = true; return(base.Add(fileToAdd)); }
public void AddFileAndCheckAccessibility_Locked() { IScrImportSet importSettings = Cache.ServiceLocator.GetInstance <IScrImportSetFactory>().Create(); Cache.LanguageProject.TranslatedScriptureOA.ImportSettingsOC.Add(importSettings); importSettings.ImportTypeEnum = TypeOfImport.Other; var fileOs = new MockFileOS(); try { FileUtils.Manager.SetFileAdapter(fileOs); string filename = fileOs.MakeSfFile("EPH", @"\c 1", @"\v 1"); fileOs.LockFile(filename); IScrImportFileInfo info = importSettings.AddFile(filename, ImportDomain.Main, null, null); Assert.AreEqual(Encoding.ASCII, info.FileEncoding); Assert.AreEqual(1, importSettings.GetImportFiles(ImportDomain.Main).Count); StringCollection notFound; Assert.IsFalse(importSettings.ImportProjectIsAccessible(out notFound)); Assert.AreEqual(1, notFound.Count); Assert.AreEqual(filename, notFound[0]); } finally { FileUtils.Manager.Reset(); } }
public void TestCheckForOverlaps_KeepFile2() { DummyScrImportFileInfoFactory factory = new DummyScrImportFileInfoFactory(); IScrImportFileInfo f1 = CreateStubFileInfo(factory, "file1", new ScrReference(40, 1, 1, ScrVers.English), new ReferenceRange(40, 1, 3)); m_expectedRemovedFiles.Add(f1); IScrImportFileInfo f2 = CreateStubFileInfo(factory, "file2", new ScrReference(40, 3, 1, ScrVers.English), new ReferenceRange(40, 3, 5)); IScrImportFileInfo f3 = CreateStubFileInfo(factory, "file3", new ScrReference(40, 6, 1, ScrVers.English), new ReferenceRange(40, 6, 7)); m_resolver.Expect(x => x.ChooseFileToRemove(f2, f1)).Return(f1); m_fileList.Add(f1); m_fileList.Add(f2); m_fileList.Add(f3); Assert.AreEqual(2, m_fileList.Count); Assert.AreEqual(f2, m_fileList[0]); Assert.AreEqual(f3, m_fileList[1]); m_resolver.VerifyAllExpectations(); Assert.AreEqual(m_expectedRemovedFiles.Count, m_callCountForVerifyFileRemoved); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Open the next source file to read. Skip over any files that do not contain /// data in the desired range. /// </summary> /// <returns>true if a file was opened, else false and there are no more /// files available</returns> /// ------------------------------------------------------------------------------------ private bool OpenNextSourceFile(BCVRef startRef, BCVRef endRef) { // Clear the indicator that tells that an ID line has been encountered m_seenIdInFile = false; m_lineNumber = 0; // If the first file is being opened, then initialize the reference if (m_currentFile == null) { m_currentStartRef = new BCVRef(); m_currentEndRef = new BCVRef(); } // Find the next book in the project that has books in the import range bool fileHasBooksInRange = false; while (!fileHasBooksInRange) { if (!m_importSourceEnum.MoveNext()) { return(false); } m_currentFile = (IScrImportFileInfo)m_importSourceEnum.Current; if (m_domain != ImportDomain.Main) { m_prevDataEncoding = GetEncodingConverterForWs(m_currentFile.WsId, ScrMappingList.MarkerBook); } // make sure the file contains books in the desired range foreach (int book in m_currentFile.BooksInFile) { if (book >= startRef.Book && book <= endRef.Book) { fileHasBooksInRange = true; break; } } } try { // ASCII encoding will convert all high-bit characters to a '?'. // The magic code page will map all high-bit characters to the same // value with a high-byte of 0 in unicode. For example, character 0x84 // will end up as 0x0084. Encoding useEncoding = (m_currentFile.FileEncoding == Encoding.ASCII) ? Encoding.GetEncoding(EncodingConstants.kMagicCodePage) : m_currentFile.FileEncoding; m_sfFileReader = FileUtils.OpenFileForRead(m_currentFile.FileName, useEncoding); } catch (Exception e) { throw new ScriptureUtilsException(SUE_ErrorCode.FileError, m_currentFile.FileName, e); } return(true); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Test CheckForOverlaps where the user elects to keep file 1 in the Overlapping Files /// dialog /// </summary> /// ------------------------------------------------------------------------------------ private IScrImportFileInfo CreateStubFileInfo(DummyScrImportFileInfoFactory factory, string name, ScrReference startRef, ReferenceRange refRange) { IScrImportFileInfo f = factory.Create(name, m_mappingList, ImportDomain.Main, null, null, false); f.Stub(x => x.IsReadable).Return(true); f.Stub(x => x.IsStillReadable).Return(true); f.Stub(x => x.StartRef).Return(startRef); f.Stub(x => x.BookReferences).Return(new ReferenceRange[] { refRange }); return(f); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Display the Overlapping files dialog to allow the user to decide which file to /// remove from a pair of files which have overlapping references. /// </summary> /// <param name="file1">file info 1</param> /// <param name="file2">file info 2</param> /// <returns>The file to remove</returns> /// ------------------------------------------------------------------------------------ public IScrImportFileInfo ChooseFileToRemove(IScrImportFileInfo file1, IScrImportFileInfo file2) { m_file1 = file1; m_file2 = file2; try { if (ShowDialog() == DialogResult.OK) { return(FileToRemove); } } finally { m_file1 = null; m_file2 = null; } throw new CancelException("Import fCanceled by user."); }
/// ----------------------------------------------------------------------------------- /// <summary> /// Check to see if this file overlaps any other files. Resolve the conflict and /// insert this file if it is ok. /// </summary> /// <param name="fileToAdd"></param> /// <returns>true if fileToAdd should be inserted into the list, else false</returns> /// ------------------------------------------------------------------------------------ private bool CheckForOverlaps(IScrImportFileInfo fileToAdd) { if (m_resolver == null) { return(true); } List <IScrImportFileInfo> removedList = new List <IScrImportFileInfo>(); foreach (IScrImportFileInfo file2 in this) { if (ScrImportFileInfo.CheckForOverlap(fileToAdd, file2)) { IScrImportFileInfo removedFile; // TE-4808: First make sure file2 is still accessible. If not, just let // the added file replace it quietly. if (file2.IsStillReadable) { removedFile = m_resolver.ChooseFileToRemove(fileToAdd, file2); // If we're removing the file being added, no need to continue looking for overlaps if (removedFile == fileToAdd) { return(false); } } else { removedFile = file2; } removedList.Add(removedFile); } } // Remove all of the overlapping files from the import files collection // that we decided we didn't want. foreach (IScrImportFileInfo file in removedList) { Remove(file); } return(true); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Check two ScrImportFileInfo objects for overlap. /// </summary> /// <param name="map1">first ScrImportFileInfo to compare</param> /// <param name="map2">second ScrImportFileInfo to compare</param> /// <returns>true if an overlap exists, false otherwise</returns> /// ------------------------------------------------------------------------------------ public static bool CheckForOverlap(IScrImportFileInfo map1, IScrImportFileInfo map2) { foreach (ReferenceRange range1 in map1.BookReferences) { foreach (ReferenceRange range2 in map2.BookReferences) { if (range1.Book == range2.Book) { if (range1.StartChapter >= range2.StartChapter && range1.StartChapter <= range2.EndChapter) { return(true); } if (range2.StartChapter >= range1.StartChapter && range2.StartChapter <= range1.EndChapter) { return(true); } } } } return(false); }
public void Add_NonExistantFile() { DummyScrImportFileInfoFactory factory = new DummyScrImportFileInfoFactory(); IScrImportFileInfo f1 = CreateStubFileInfo(factory, "file1", new ScrReference(40, 1, 1, ScrVers.English), new ReferenceRange(40, 1, 3)); IScrImportFileInfo f2 = factory.Create("IdontExist.blurb", m_mappingList, ImportDomain.Main, null, null, false); f2.Stub(x => x.IsReadable).Return(false); m_fileList.Add(f1); m_fileList.Add(f2); Assert.AreEqual(2, m_fileList.Count); Assert.AreEqual(f1, m_fileList[0]); Assert.AreEqual(f2, m_fileList[1]); m_resolver.VerifyAllExpectations(); Assert.AreEqual(0, m_callCountForVerifyFileRemoved); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Display the Overlapping files dialog to allow the user to decide which file to /// remove from a pair of files which have overlapping references. /// </summary> /// <param name="file1">file info 1</param> /// <param name="file2">file info 2</param> /// <returns>The file to remove</returns> /// ------------------------------------------------------------------------------------ public IScrImportFileInfo ChooseFileToRemove(IScrImportFileInfo file1, IScrImportFileInfo file2) { m_file1 = file1; m_file2 = file2; try { if (ShowDialog() == DialogResult.OK) return FileToRemove; } finally { m_file1 = null; m_file2 = null; } throw new CancelException("Import fCanceled by user."); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Constructor /// </summary> /// <param name="fileinfo">File that was removed</param> /// ------------------------------------------------------------------------------------ public ScrImportFileEventArgs(IScrImportFileInfo fileinfo) { m_fileinfo = fileinfo; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Check two ScrImportFileInfo objects for overlap. /// </summary> /// <param name="map1">first ScrImportFileInfo to compare</param> /// <param name="map2">second ScrImportFileInfo to compare</param> /// <returns>true if an overlap exists, false otherwise</returns> /// ------------------------------------------------------------------------------------ public static bool CheckForOverlap(IScrImportFileInfo map1, IScrImportFileInfo map2) { foreach (ReferenceRange range1 in map1.BookReferences) { foreach (ReferenceRange range2 in map2.BookReferences) { if (range1.Book == range2.Book) { if (range1.StartChapter >= range2.StartChapter && range1.StartChapter <= range2.EndChapter) return true; if (range2.StartChapter >= range1.StartChapter && range2.StartChapter <= range1.EndChapter) return true; } } } return false; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Attempt to add a file to the project. If an error occurs, then show the message /// </summary> /// <param name="fileName"></param> /// <param name="info">returns a ScrImportFileInfo object representing the added /// file.</param> /// <returns>true if the file was added to the project</returns> /// ------------------------------------------------------------------------------------ private bool AddFileToProject(string fileName, out IScrImportFileInfo info) { try { info = m_ImportSettings.AddFile(fileName, m_domain, m_wsId, m_noteType, m_fileRemovedHandler); return (info != null); } catch (ScriptureUtilsException e) { Form parentWindow = FindForm(); if (parentWindow != null) { MessageBoxUtils.Show(parentWindow, e.Message, ScriptureUtilsException.GetResourceString("kstidImportErrorCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, m_helpTopicProvider.HelpFile, HelpNavigator.Topic, e.HelpTopic); } info = null; return false; } }
/// ------------------------------------------------------------------------------- /// <summary> /// Add the given SF scripture file to the list and select the first one if none /// is selected. /// </summary> /// <param name="listView">The list view to add the files to</param> /// <param name="fileInfo">list of files to be added</param> /// <param name="warnOnError">display an error if a file is not valid and /// do not add it to the list. If this is false then invalid files will /// be added to the list anyway.</param> /// ------------------------------------------------------------------------------- protected void AddFileToListView(ListView listView, IScrImportFileInfo fileInfo, bool warnOnError) { Debug.Assert(listView != null); Debug.Assert(fileInfo != null); string filename = fileInfo.FileName; ListViewItem lvi = new ListViewItem(filename); StringBuilder booksInFile = new StringBuilder(); // If the file does not exist then keep the name but do not try to read it. // It will be displayed in gray with <not found> for the list of books. if (fileInfo.IsReadable) { // Build a comma-delimited string of the 3 letter book abbreviations to go // into the second column in the listview. if (fileInfo != null) { foreach (int bookID in fileInfo.BooksInFile) { if (bookID >= 0 && bookID <= 66) { if (booksInFile.Length > 0) booksInFile.Append(", "); booksInFile.Append(ScrReference.NumberToBookCode(bookID)); } } } // Add the book list as a sub item of the file name. Then add the filename // as the item in the listview. lvi.SubItems.Add(booksInFile.ToString()); // if (listView != scrFileList) // { // lvi.SubItems.Add(GetWritingSystemNameForLocale(fileInfo.IcuLocale)); // } // Add file encoding lvi.SubItems.Add(fileInfo == null ? Encoding.ASCII.EncodingName : fileInfo.FileEncoding.EncodingName); } else { // If we want to issue an error on missing files. if (warnOnError) { ShowBadFileMessage(DriveUtil.FileNameOnly(filename)); return; } lvi.SubItems.Add(TeResourceHelper.GetResourceString("kstidNoBooks")); lvi.SubItems.Add(TeResourceHelper.GetResourceString("kstidFileMissing")); } // Store the file info in the list view item tag lvi.Tag = fileInfo; // Check to make sure the file is not already in the list bool found = false; ListViewItem item = null; int i; for (i = 0; i < listView.Items.Count; i++) { item = listView.Items[i]; if (item.SubItems[0].Text.ToLower() == lvi.Text.ToLower()) { found = true; break; } } if (!found) { // If the file is new, then find the place to insert it based on the // starting reference of the file bool inserted = false; for (i = 0; i < listView.Items.Count; i++) { ScrImportFileInfo listItemInfo = (ScrImportFileInfo)listView.Items[i].Tag; if (fileInfo.StartRef < listItemInfo.StartRef) { listView.Items.Insert(i, lvi); inserted = true; break; } } if (!inserted) listView.Items.Add(lvi); } else { if (i < listView.Items.Count) { lvi.Selected = listView.Items[i].Selected; listView.Items[i] = lvi; } } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Open the next source file to read. Skip over any files that do not contain /// data in the desired range. /// </summary> /// <returns>true if a file was opened, else false and there are no more /// files available</returns> /// ------------------------------------------------------------------------------------ private bool OpenNextSourceFile(BCVRef startRef, BCVRef endRef) { // Clear the indicator that tells that an ID line has been encountered m_seenIdInFile = false; m_lineNumber = 0; // If the first file is being opened, then initialize the reference if (m_currentFile == null) { m_currentStartRef = new BCVRef(); m_currentEndRef = new BCVRef(); } // Find the next book in the project that has books in the import range bool fileHasBooksInRange = false; while (!fileHasBooksInRange) { if (!m_importSourceEnum.MoveNext()) return false; m_currentFile = (IScrImportFileInfo)m_importSourceEnum.Current; if (m_domain != ImportDomain.Main) m_prevDataEncoding = GetEncodingConverterForWs(m_currentFile.WsId, ScrMappingList.MarkerBook); // make sure the file contains books in the desired range foreach (int book in m_currentFile.BooksInFile) { if (book >= startRef.Book && book <= endRef.Book) { fileHasBooksInRange = true; break; } } } try { // ASCII encoding will convert all high-bit characters to a '?'. // The magic code page will map all high-bit characters to the same // value with a high-byte of 0 in unicode. For example, character 0x84 // will end up as 0x0084. Encoding useEncoding = (m_currentFile.FileEncoding == Encoding.ASCII) ? Encoding.GetEncoding(EncodingConstants.kMagicCodePage) : m_currentFile.FileEncoding; m_sfFileReader = FileUtils.OpenFileForRead(m_currentFile.FileName, useEncoding); } catch (Exception e) { throw new ScriptureUtilsException(SUE_ErrorCode.FileError, m_currentFile.FileName, e); } return true; }
/// ----------------------------------------------------------------------------------- /// <summary> /// Check to see if this file overlaps any other files. Resolve the conflict and /// insert this file if it is ok. /// </summary> /// <param name="fileToAdd"></param> /// <returns>true if fileToAdd should be inserted into the list, else false</returns> /// ------------------------------------------------------------------------------------ private bool CheckForOverlaps(IScrImportFileInfo fileToAdd) { if (m_resolver == null) return true; List<IScrImportFileInfo> removedList = new List<IScrImportFileInfo>(); foreach (IScrImportFileInfo file2 in this) { if (ScrImportFileInfo.CheckForOverlap(fileToAdd, file2)) { IScrImportFileInfo removedFile; // TE-4808: First make sure file2 is still accessible. If not, just let // the added file replace it quietly. if (file2.IsStillReadable) { removedFile = m_resolver.ChooseFileToRemove(fileToAdd, file2); // If we're removing the file being added, no need to continue looking for overlaps if (removedFile == fileToAdd) return false; } else removedFile = file2; removedList.Add(removedFile); } } // Remove all of the overlapping files from the import files collection // that we decided we didn't want. foreach (IScrImportFileInfo file in removedList) Remove(file); return true; }