Beispiel #1
0
		public void ConvertAsciiToUnicode()
		{
			CheckDisposed();

			string encFileName = string.Empty;
			EncConverters converters = new EncConverters();
			try
			{
				// Define an encoding converter
				StreamReader reader = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
					"SIL.FieldWorks.FDO.Scripture.EncTest.map"));
				encFileName = Path.Combine(Path.GetTempPath(), "test.map");
				StreamWriter writer = new StreamWriter(encFileName);
				writer.Write(reader.ReadToEnd());
				reader.Close();
				writer.Close();

				converters.Add("MyConverter", encFileName, ConvType.Legacy_to_from_Unicode, string.Empty,
					string.Empty, ProcessTypeFlags.UnicodeEncodingConversion);

				using (TempSFFileMaker fileMaker = new TempSFFileMaker())
				{
					string fileName = fileMaker.CreateFile("ROM",
						new string[] {
										 @"\mt 0123456789",
										 "\\s \u0081\u009a\u0096\u00b5",
										 @"\c 1",
										 @"\v 1"},
						Encoding.GetEncoding(28591), false);
					m_settings.AddFile(fileName, ImportDomain.Main, null, 0);

					// Set the vernacular WS to use the MyConverter encoder
					LgWritingSystem wsVern =
						new LgWritingSystem(Cache, Cache.LangProject.DefaultVernacularWritingSystem);
					wsVern.LegacyMapping = "MyConverter";

					ISCTextEnum textEnum = GetTextEnum(ImportDomain.Main,
						new ScrReference(45, 0, 0, Paratext.ScrVers.English), new ScrReference(45, 1, 1, Paratext.ScrVers.English));

					ISCTextSegment textSeg = textEnum.Next();
					Assert.IsNotNull(textSeg, "Unable to read segment");
					Assert.AreEqual(@"\id", textSeg.Marker);
					Assert.AreEqual("ROM ", textSeg.Text);

					textSeg = textEnum.Next();
					Assert.IsNotNull(textSeg, "Unable to read segment");
					Assert.AreEqual(@"\mt", textSeg.Marker);
					Assert.AreEqual("\u0966\u0967\u0968\u0969\u096a\u096b\u096c\u096d\u096e\u096f ", textSeg.Text);

					textSeg = textEnum.Next();
					Assert.IsNotNull(textSeg, "Unable to read segment");
					Assert.AreEqual(@"\s", textSeg.Marker);
					Assert.AreEqual("\u0492\u043a\u2013\u04e9 ", textSeg.Text);

					textSeg = textEnum.Next();
					Assert.IsNotNull(textSeg, "Unable to read segment");
					Assert.AreEqual(@"\c", textSeg.Marker);
					Assert.AreEqual(@" ", textSeg.Text);

					textSeg = textEnum.Next();
					Assert.IsNotNull(textSeg, "Unable to read segment");
					Assert.AreEqual(@"\v", textSeg.Marker);
					Assert.AreEqual(@" ", textSeg.Text);
				}
			}
			finally
			{
				converters.Remove("MyConverter");
				try
				{
					File.Delete(encFileName);
				}
				catch {}
			}
		}
Beispiel #2
0
		private void buttonAddNewProject_Click(object sender, System.EventArgs e)
		{
			// in case the Add New project button was default, change it to the OK button.
			this.AcceptButton = this.buttonOK;

			// get the delimiter for word boundaries and disallow the /"/ character
			m_strWordBoundaryDelimiter = this.textBoxWordBoundaryDelimiter.Text;
			if( m_strWordBoundaryDelimiter.IndexOf('"') != -1 )
			{
				MessageBox.Show("Can't use the double-quote character for the word boundary delimiter",SpellingFixerEC.cstrCaption);
				return;
			}

			bool bRewriteCCTable = false;

			string strPunctuation = EncodePunctuationForCC(this.textBoxAddlPunctuation.Text);
			if(strPunctuation == null )
				return; // it had a bad character

			else if( strPunctuation != m_strNonWordCharacters )
			{
				// this means the file must be re-written
				bRewriteCCTable = true;
				m_strNonWordCharacters = strPunctuation;
			}

			if( (!m_bLegacy) != this.checkBoxUnicode.Checked )
			{
				m_bLegacy = !this.checkBoxUnicode.Checked;
				bRewriteCCTable = true;
			}

			// check for existing EncConverter with this same project information
			string strCCTableSpec = null;
			string strPartialName = this.textBoxNewProjectName.Text;
			string strEncConverterName = FullName(strPartialName);
			EncConverters aECs = new EncConverters();
			IEncConverter aEC = aECs[strEncConverterName];
			if( aEC != null )
			{
				// if we're *not* in edit mode
				if( this.buttonAddNewProject.Text == cstrAddNewProjectButtonText )
				{
					if( MessageBox.Show(String.Format("A project already exists by the name {0}. Click 'Yes' to overwrite", this.textBoxNewProjectName.Text),SpellingFixerEC.cstrCaption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
					{
						// take it out of the check box list
						checkedListBoxProjects.Items.Remove(strPartialName);
						strCCTableSpec = aEC.ConverterIdentifier;
						if( File.Exists(strCCTableSpec) )
						{
							File.Delete(strCCTableSpec);
							strCCTableSpec = null;
						}

						// remove the existing one and we'll add a new one next
						aECs.Remove(aEC.Name);
						aEC = null;
					}
					else
						return;
				}
				else    // edit mode
				{
					if( MessageBox.Show(String.Format("Do you want to update the '{0}' project?", this.textBoxNewProjectName.Text),SpellingFixerEC.cstrCaption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
					{
						// take it out of the check box list
						checkedListBoxProjects.Items.Remove(strPartialName);

						// save the spec so that we don't make one below
						strCCTableSpec = aEC.ConverterIdentifier;

						// the remove the converter since we'll add it back again next
						aECs.Remove(aEC.Name);
						aEC = null;
					}
					else
					{
						// reset this in case we were just editing it.
						ResetNewProjectLook();
						return;
					}
				}
			}

			// if we're aren't using the old cc table, then...
			if( strCCTableSpec == null )
			{
				// now add it (put it in the normal 'MapsTables' folder in \pf\cf\sil\...)
				string strMapsTableDir = GetMapTableFolderPath;
				strCCTableSpec = strMapsTableDir + @"\" + strEncConverterName + ".cct";
				if( File.Exists(strCCTableSpec) )
				{
					// the converter doesn't exist, but a file with the name we would have
					//  given it does... ask the user if they want to overwrite it.
					// TODO: this doesn't allow for complete flexibility. It might be nicer to
					//  allow for any arbitrary name, but not if noone complains.
					if( MessageBox.Show(String.Format("A file exists by the name {0}. Click 'Yes' to overwrite", strCCTableSpec),SpellingFixerEC.cstrCaption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
					{
						File.Delete(strCCTableSpec);
#if WriteOnAdd
// if the user goes to add the first record, it'd be better if the file didn't exist because now we do some
//  preliminary testing whether the CC table already changes a word before adding a new one, but this causes
//  a non-trapable error if there are no rules in the file. So just create it when it is actually needed
						CreateCCTable(strCCTableSpec,strEncConverterName,m_strNonWordCharacters, !this.m_bLegacy);
#endif
					}
				}
#if WriteOnAdd
				else
					CreateCCTable(strCCTableSpec,strEncConverterName,m_strNonWordCharacters, !this.m_bLegacy);
#endif
				bRewriteCCTable = false;
			}

			// now add the EncConverter
			// TODO: EncConverters needs a new interface to get the defining encodingID from
			//  a FontName (so we can use it in this call) just like we can 'try' to get the
			//  code page given a font name (see 'CodePage' below)
			ConvType eConvType = (m_bLegacy)
				? ConvType.Legacy_to_Legacy : ConvType.Unicode_to_Unicode;

			aECs.Add(strEncConverterName,strCCTableSpec,eConvType,null,null,SpellingFixerEC.SFProcessType);

			Font font = null;
			try
			{
				font = new Font(comboBoxFont.SelectedItem.ToString(),Convert.ToSingle(listBoxFontSize.SelectedItem));
			}
			catch
			{
				MessageBox.Show("Couldn't create the selected font. Contact support");
				return;
			}

			// add this 'displaying font' information to the converter as properties/attributes
			ECAttributes aECAttrs = aECs.Attributes(strEncConverterName,AttributeType.Converter);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeFontToUse,font.Name);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeFontSizeToUse, font.Size);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeWordBoundaryDelimiter, m_strWordBoundaryDelimiter);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeNonWordChars, m_strNonWordCharacters);

			// if it's not Unicode, then we need a code page in order to convert from wide to
			//  narrow (when writing to the file).
			int cp = 0;
			if( m_bLegacy )
			{
				// try to get the code page from EncConverters
				try
				{
					cp = aECs.CodePage(font.Name);
				}
				catch
				{
					// if it fails, it means we don't have a mapping, so add one here.
					// TODO: it would be nice to have an encoding, but I'm loath to query
					//  the user for it here since it isn't extremely relevant to this app.
					cp = Convert.ToInt32(this.textBoxCP.Text);
					aECs.AddFont(font.Name,cp,null);
				}
			}

			if(bRewriteCCTable)    // we are going to continue using the old file... so we must re-write it.
			{
				// if it was legacy encoded, then we need to convert the data to narrow using
				//  the code page the user specified (or we got out of the repository)
				Encoding enc = null;
				if( m_bLegacy )
				{
					if (cp == EncConverters.cnSymbolFontCodePage)
						cp = EncConverters.cnIso8859_1CodePage;
					enc = Encoding.GetEncoding(cp);
				}
				else
					enc = new UTF8Encoding();

				DataTable   myTable;
				if( SpellingFixerEC.InitializeDataTableFromCCTable(strCCTableSpec, enc, m_strWordBoundaryDelimiter, out myTable) )
				{
					ReWriteCCTableHeader(strCCTableSpec,m_strNonWordCharacters,enc);
					SpellingFixerEC.AppendCCTableFromDataTable(strCCTableSpec, enc, m_strWordBoundaryDelimiter, m_strNonWordCharacters, myTable);
				}
			}

			// finally, add the new project to the now-visible checkbox list
			this.buttonOK.Enabled = checkedListBoxProjects.Visible = true;
			ClearClickedItems();
			checkedListBoxProjects.Items.Add(strPartialName,CheckState.Checked);

			// reset this in case we were just editing it.
			ResetNewProjectLook();
		}