示例#1
0
        private void buttonTest_Click(object sender, EventArgs e)
        {
            IEncConverter aEC = InitializeEncConverter;

            if (aEC != null)
            {
                try
                {
                    aEC.DirectionForward = !checkBoxTestReverse.Checked;
                    ecTextBoxOutput.Text = aEC.Convert(ecTextBoxInput.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Test failed! Reason: {0}", ex.Message),
                                    EncConverters.cstrCaption);
                }

                NormConversionType eType = (checkBoxTestReverse.Checked)
                                        ? EncConverter.NormalizeLhsConversionType(ConversionType) : EncConverter.NormalizeRhsConversionType(ConversionType);

                if (eType == NormConversionType.eLegacy)
                {
                    UpdateLegacyCodes(ecTextBoxOutput.Text, m_aEC.CodePageOutput, richTextBoxHexOutput);
                }
                else
                {
                    UpdateUniCodes(ecTextBoxOutput.Text, richTextBoxHexOutput);
                }
            }
        }
示例#2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Convert one line of text for display
        /// </summary>
        /// <param name="ec"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        /// ------------------------------------------------------------------------------------
        private string ConvertOneLine(IEncConverter ec, string input)
        {
            string marker    = string.Empty;
            string remainder = string.Empty;

            // if the input string is empty, don't try to convert it
            if (input == string.Empty)
            {
                return(input);
            }

            // split the marker and the remainder portions of the text line.
            if (input[0] != '\\')
            {
                remainder = input;
            }
            else
            {
                int spacePosition = input.IndexOf(" ");
                if (spacePosition == -1)
                {
                    return(input);
                }
                else
                {
                    marker    = input.Substring(0, spacePosition);
                    remainder = input.Substring(spacePosition);
                }
            }

            return(marker + ec.Convert(remainder));
        }
示例#3
0
		public FilteredFieldsForm(Dictionary<string, List<string>> mapFilteredSfms, List<string> astrSfmsToConvert,
			List<string> astrSfmsToNotConvert, Font font, Font fontConverted, IEncConverter aEC)
		{
			InitializeComponent();

			m_aEC = aEC;
			m_mapFilteredSfms = mapFilteredSfms;

			dataGridViewFilterSfms.Columns[cnColumnExampleData].DefaultCellStyle.Font = font;
			dataGridViewFilterSfms.Columns[cnColumnExampleResult].DefaultCellStyle.Font = fontConverted;

			foreach (KeyValuePair<string, List<string>> kvp in mapFilteredSfms)
			{
				if (!astrSfmsToConvert.Contains(kvp.Key) && !astrSfmsToNotConvert.Contains(kvp.Key))
				{
					System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(kvp.Value[0]));
					string strInput = kvp.Value[0];
					string strOutput = m_aEC.Convert(strInput);
					bool bOnByDefault = Properties.Settings.Default.DefaultFieldsToConvert.Contains(kvp.Key);
					object[] aoRowData = new object[4] { bOnByDefault, kvp.Key, kvp.Value[0], strOutput };
					int nIndex = dataGridViewFilterSfms.Rows.Add(aoRowData);
					DataGridViewRow theRow = dataGridViewFilterSfms.Rows[nIndex];
					theRow.Tag = 0;
				}
			}
		}
        private void buttonTest_Click(object sender, EventArgs e)
        {
            Util.DebugWriteLine(this, "BEGIN");
            IEncConverter aEC = InitializeEncConverter;

            if (aEC != null)
            {
                try
                {
                    aEC.DirectionForward = !checkBoxTestReverse.Checked;
                    //ecTextBoxOutput.Text = aEC.Convert(ecTextBoxInput.Text);
                    string result = aEC.Convert(ecTextBoxInput.Text);
                    Util.DebugWriteLine(this, "Putting in text box: '" + result + "'");
                    ecTextBoxOutput.Text = result;
                    //ecTextBoxOutput.Text = "Hello there!";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Test failed! Reason: {0}", ex.Message),
                                    EncConverters.cstrCaption);
                }

                NormConversionType eType = (checkBoxTestReverse.Checked)
                    ? EncConverter.NormalizeLhsConversionType(ConversionType) : EncConverter.NormalizeRhsConversionType(ConversionType);

                if (eType == NormConversionType.eLegacy)
                {
                    UpdateLegacyCodes(ecTextBoxOutput.Text, m_aEC.CodePageOutput, richTextBoxHexOutput);
                }
                else
                {
                    UpdateUniCodes(ecTextBoxOutput.Text, richTextBoxHexOutput);
                }
            }
        }
示例#5
0
        public FilteredFieldsForm(Dictionary <string, List <string> > mapFilteredSfms, List <string> astrSfmsToConvert,
                                  List <string> astrSfmsToNotConvert, Font font, Font fontConverted, IEncConverter aEC)
        {
            InitializeComponent();

            m_aEC             = aEC;
            m_mapFilteredSfms = mapFilteredSfms;

            dataGridViewFilterSfms.Columns[cnColumnExampleData].DefaultCellStyle.Font   = font;
            dataGridViewFilterSfms.Columns[cnColumnExampleResult].DefaultCellStyle.Font = fontConverted;

            foreach (KeyValuePair <string, List <string> > kvp in mapFilteredSfms)
            {
                if (!astrSfmsToConvert.Contains(kvp.Key) && !astrSfmsToNotConvert.Contains(kvp.Key))
                {
                    System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(kvp.Value[0]));
                    string   strInput     = kvp.Value[0];
                    string   strOutput    = m_aEC.Convert(strInput);
                    bool     bOnByDefault = Properties.Settings.Default.DefaultFieldsToConvert.Contains(kvp.Key);
                    object[] aoRowData    = new object[4] {
                        bOnByDefault, kvp.Key, kvp.Value[0], strOutput
                    };
                    int             nIndex = dataGridViewFilterSfms.Rows.Add(aoRowData);
                    DataGridViewRow theRow = dataGridViewFilterSfms.Rows[nIndex];
                    theRow.Tag = 0;
                }
            }
        }
示例#6
0
        private bool ChaChaChaChaChanges(IEncConverter aEC, string strFileName, string strWord)
        {
            string   strDummy     = null;
            int      lProcessType = (int)SpellingFixerEC.SFProcessType;
            ConvType eConvType    = (m_bLegacy) ? ConvType.Legacy_to_Legacy : ConvType.Unicode_to_Unicode;

            aEC.Initialize("dummyname", strFileName, ref strDummy, ref strDummy, ref eConvType, ref lProcessType, 0, 0, true);
            return(aEC.Convert(strWord) != strWord);
        }
示例#7
0
        public void DoFileConvert
        (
            string strConverterName,
            string strOutputFileName,
            System.Text.Encoding outEnc,
            string strInputFileName,
            System.Text.Encoding inEnc,
            bool bDirectionForward
        )
        {
            // the user *might* not give us a converter name if they simply want to change
            //  the encoding from, say, UTF8 to UTF16.
            bool bIsConverter = !(strConverterName == null);

            IEncConverter aEC = null;

            if (bIsConverter)
            {
                EncConverters aECs = new EncConverters();
                aEC = aECs[strConverterName];
            }

            // open the input and output files using the given encoding formats
            StreamReader srReadLine = new StreamReader(strInputFileName, inEnc, true);

            srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);
            StreamWriter swWriteLine = new StreamWriter(strOutputFileName, false, outEnc);

            // tell the converter to go the other way, if the user selected 'reverse'
            if (!bDirectionForward && bIsConverter)
            {
                aEC.DirectionForward = false;
            }

            // read the lines of the input file, (optionally convert,) and write them out.
            string sOutput, sInput;

            while (srReadLine.Peek() > -1)
            {
                sInput = srReadLine.ReadLine();

                if (bIsConverter)
                {
                    sOutput = aEC.Convert(sInput);
                }
                else
                {
                    sOutput = sInput;
                }

                swWriteLine.WriteLine(sOutput);
            }

            srReadLine.Close();
            swWriteLine.Close();
        }
        protected string[] GetFirstReplacementSplitArray(ref string strInput, ref string strOutput)
        {
            string[] astrSegments = strOutput.Split(m_achDelimiter);

            // must be odd (before, ||: replace, following :||), where any or all can be null
            if ((astrSegments.Length % 2) != 1)
            {
                throw new ApplicationException("Oops... can't search in this document. Please send it to '*****@*****.**' and indicate what the 'Find what' string was.");
            }

            if (astrSegments.Length > 3)
            {
                int    nInputLength     = strInput.Length;
                int    nDiffLength      = nInputLength;
                int    nTempInputLength = nInputLength;
                string strTempInput     = null;
                do
                {
                    // do a binary search until we get only one replacement
                    nDiffLength = Math.Max(nDiffLength / 2, 1);
                    if (astrSegments.Length > 3)
                    {
                        nTempInputLength = Math.Max(nTempInputLength - nDiffLength, 1);
                    }
                    else
                    {
                        nTempInputLength = Math.Min(nTempInputLength + nDiffLength, nInputLength);
                    }

                    strTempInput = strInput.Substring(0, nTempInputLength);
                    strOutput    = m_aECRegex.Convert(strTempInput);
                    astrSegments = strOutput.Split(m_achDelimiter);
                    System.Diagnostics.Trace.WriteLine(String.Format("{0}: diff:{1} segments:{2}", nTempInputLength, nDiffLength, astrSegments.Length));
                } while (astrSegments.Length != 3);

                System.Diagnostics.Trace.WriteLine(null);
                strInput = strTempInput;
            }

            return(astrSegments);
        }
示例#9
0
        private void dataGridViewFilterSfms_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex < 0) ||
                (e.RowIndex >= dataGridViewFilterSfms.Rows.Count) ||
                (e.ColumnIndex < 0) ||
                (e.ColumnIndex >= dataGridViewFilterSfms.Columns.Count))
            {
                return;
            }

            string           strInput, strOutput;
            DataGridViewRow  theRow  = dataGridViewFilterSfms.Rows[e.RowIndex];
            DataGridViewCell theCell = theRow.Cells[e.ColumnIndex];

            switch (e.ColumnIndex)
            {
            case cnColumnConvertCheckbox:
                if (false == (bool)theCell.Value)       // check for false, which means it's becoming true
                {
                    strInput  = (string)theRow.Cells[cnColumnExampleData].Value;
                    strOutput = m_aEC.Convert(strInput);
                    theRow.Cells[cnColumnExampleResult].Value = strOutput;
                }
                break;

            case cnColumnExampleData:
                int           nIndex         = (int)theRow.Tag;
                string        strMarker      = (string)theRow.Cells[cnColumnSFMs].Value;
                List <string> astrSampleData = m_mapFilteredSfms[strMarker];
                if (++nIndex >= astrSampleData.Count)
                {
                    nIndex = 0;
                }
                strInput      = astrSampleData[nIndex];
                strOutput     = m_aEC.Convert(strInput);
                theCell.Value = strInput;
                theRow.Cells[cnColumnExampleResult].Value = strOutput;
                theRow.Tag = nIndex;
                break;
            }
        }
        public string Convert(string sInput)
        {
            IEncConverter aEC = GetEncConverter;

            if (aEC != null)
            {
                return(aEC.Convert(sInput));
            }
            else
            {
                throw NoConverterException;
            }
        }
        // convert in the opposite direction (if it started as "reverse", then this
        //  means forward
        public string ConvertDirectionOpposite(string sInput)
        {
            IEncConverter aEC = EncConverters[m_strEncConverterName];

            if (aEC != null)
            {
                aEC.DirectionForward = !m_bDirectionForward;
                string strOutput = aEC.Convert(sInput);
                aEC.DirectionForward = m_bDirectionForward;
                return(strOutput);
            }
            else
            {
                throw NoConverterException;
            }
        }
        /// <summary>
        /// Use this method to use a system converter ('ConverterName') to convert the value in a
        /// cell ('Input'). You can also optionally specify the direction of conversion (True or False)
        /// and the output normalization form.
        /// </summary>
        /// <param name="Input">cell reference to convert</param>
        /// <param name="ConverterName">friendly name of converter from repository</param>
        /// <param name="Forward">TRUE for forward conversion (default), or FALSE for reverse (for bi-directional converters only)</param>
        /// <returns>the converted string</returns>
        public string ConvertString
        (
            string Input,
            object ConverterName,
            [Optional, DefaultParameterValue(true)] object Forward
            // can't really do this anyway..., [Optional, DefaultParameterValue(NormalizeFlags.None)] object NormalFormOutput
        )
        {
            IEncConverter aEC = GetEncConverters[ConverterName];

            if (!(Forward is System.Reflection.Missing))
            {
                aEC.DirectionForward = (bool)Forward;
            }

            /*
             * if( !(NormalFormOutput is System.Reflection.Missing))
             *  aEC.NormalizeOutput = (NormalizeFlags)NormalFormOutput;
             */
            strResult = aEC.Convert(Input);
            return(strResult);
        }
示例#13
0
		private void UpdateTextChanged(TextBox tbSrc, TextBox tbDst, IEncConverter aEC, bool bDirectionForward)
		{
			if (tbSrc.Text.Length > 0)
			{
				if (aEC != null)
				{
					try
					{
						tbDst.Text = null;  // to force the "TextChanged"
						aEC.DirectionForward = bDirectionForward;
						tbDst.Text = aEC.Convert(tbSrc.Text);
					}
					catch (ECException e)
					{
						if (e.ErrorCode != (int)ErrStatus.InvalidForm)
							MessageBox.Show(e.Message);
					}
				}
			}
			else
				tbDst.Text = null;
		}
示例#14
0
		private string ConvertData(IEncConverter aEC, string strInput)
		{
			bool bDirForward = this.forwardToolStripMenuItem.Checked;
			if(     !bDirForward
				&&  (   (aEC.ConversionType == ConvType.Legacy_to_Legacy)
					||  (aEC.ConversionType == ConvType.Legacy_to_Unicode)
					||  (aEC.ConversionType == ConvType.Unicode_to_Legacy)
					||  (aEC.ConversionType == ConvType.Unicode_to_Unicode)
					)
			)
			{
				// these types of converters aren't reversable! So just return null
				return null;
			}

			aEC.DirectionForward = bDirForward;

			string strOutput = null;
			if (!String.IsNullOrEmpty(strInput))
			{
				// if the input to the conversion is legacy and not encoded correctly, we have
				//  to fix that up.
				if (    (   bDirForward
						&&  (EncConverter.NormalizeLhsConversionType(aEC.ConversionType) == NormConversionType.eLegacy)
						&&  (aEC.CodePageInput != 0)
						&&  (aEC.CodePageInput != Encoding.Default.CodePage)
						)
					||  (   !bDirForward
						&&  (EncConverter.NormalizeRhsConversionType(aEC.ConversionType) == NormConversionType.eLegacy)
						&&  (aEC.CodePageOutput != 0)
						&&  (aEC.CodePageOutput != Encoding.Default.CodePage)
						)
				)
				{
					// we get the legacy data from the clipboard with Encoding 0 == CP_ACP (or the default code page
					//  for this computer), but if the CodePageInput used by EncConverters is a different code page,
					//  then this will fail.
					//  If so, then convert it to a byte array and pass that
					byte[] abyInput = Encoding.Default.GetBytes(strInput);
					strInput = ECNormalizeData.ByteArrToString(abyInput);
					EncodingForm ef = aEC.EncodingIn;
					aEC.EncodingIn = EncodingForm.LegacyBytes;
					strOutput = aEC.Convert(strInput);
					aEC.EncodingIn = ef;    // reset for later (e.g. in case the user switches directions)
				}
				else
					strOutput = aEC.Convert(strInput);

				// similarly, if the output is legacy, then if the code page used was not the same as the
				//  default code page, then we have to convert it so it'll produce the correct answer
				//  (this probably doesn't work for Legacy<>Legacy code pages)
				if (    (   bDirForward
						&&  (EncConverter.NormalizeRhsConversionType(aEC.ConversionType) == NormConversionType.eLegacy)
						&&  (aEC.CodePageOutput != 0)
						&&  (aEC.CodePageOutput != Encoding.Default.CodePage)
						)
					|| (    !bDirForward
						&&  (EncConverter.NormalizeLhsConversionType(aEC.ConversionType) == NormConversionType.eLegacy)
						&&  (aEC.CodePageInput != 0)
						&&  (aEC.CodePageInput != Encoding.Default.CodePage)
					   )
				)
				{
					int nCP = (!aEC.DirectionForward) ? aEC.CodePageInput : aEC.CodePageOutput;
					byte[] abyOutput = EncConverters.GetBytesFromEncoding(nCP, strOutput, true);
					strOutput = new string(Encoding.Default.GetChars(abyOutput));
				}
			}

			return strOutput;
		}
示例#15
0
		protected void ConvertEmbeddedMarkers(XmlAttributeCollection xnAttributes, string strAttributeName, IEncConverter aEC, List<string> astrSfmsToConvert)
		{
			XmlAttribute xa = xnAttributes[strAttributeName];
			if (xa != null)
			{
				string strValue = xa.Value;
				if (!String.IsNullOrEmpty(strValue))
				{
					// iterate thru the chunks of sfm data and convert them with the source converter if in the
					//  list of SFMs to convert
					int nIndexThat, nIndexBOC, nLengthContents, nIndexThis = strValue.IndexOf('\\');
					List<string> astrOpenedMarkers = new List<string>();
					while (nIndexThis != -1)
					{
						string strMarker, strContents;
						if (FindMarkerAndContents(strValue, nIndexThis, out nIndexThat, out nIndexBOC,
							out strMarker, out strContents, out nLengthContents, astrOpenedMarkers))
						{
							// check if it's in the list of SFMs to convert
							if (astrSfmsToConvert.Contains(strMarker))
							{
								string strContentsConverted = aEC.Convert(strContents);
								strValue = ReplaceSubstring(strValue, nIndexThis + nIndexBOC, nLengthContents, strContentsConverted);
							}
						}

						nIndexThis = nIndexThat;
					}

					xa.Value = strValue;
				}
			}
		}
示例#16
0
		// allow these to be overidden by sub-class forms (e.g. to add a round-trip refresh as well)
		protected virtual void RefreshTextBoxes(IEncConverter aEC)
		{
			aEC.DirectionForward = true;
			ForwardString = aEC.Convert(InputString);
		}
示例#17
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Convert one line of text for display
		/// </summary>
		/// <param name="ec"></param>
		/// <param name="input"></param>
		/// <returns></returns>
		/// ------------------------------------------------------------------------------------
		private string ConvertOneLine(IEncConverter ec, string input)
		{
			string marker = string.Empty;
			string remainder = string.Empty;

			// if the input string is empty, don't try to convert it
			if (input == string.Empty)
				return input;

			// split the marker and the remainder portions of the text line.
			if (input[0] != '\\')
				remainder = input;
			else
			{
				int spacePosition = input.IndexOf(" ");
				if (spacePosition == -1)
					return input;
				else
				{
					marker = input.Substring(0, spacePosition);
					remainder = input.Substring(spacePosition);
				}
			}

			return marker + ec.Convert(remainder);
		}
示例#18
0
 // allow these to be overidden by sub-class forms (e.g. to add a round-trip refresh as well)
 protected virtual void RefreshTextBoxes(IEncConverter aEC)
 {
     aEC.DirectionForward = true;
     ForwardString        = aEC.Convert(InputString);
 }
示例#19
0
		protected void ConvertProjectFile(string strProjectFileSource, string strProjectFileTarget,
			IEncConverter aEcSource, IEncConverter aEcTarget, string strFontNameSource, string strFontNameTarget)
		{
			// convert the punctuation in the legacy project file.
			Encoding enc = Encoding.Default;
			if (aEcSource != null)
			{
				// the "symbol" code page apparently isn't valid for GetEncoding, so use the cp for ISO-8859-1
				if (aEcSource.CodePageInput == EncConverters.cnSymbolFontCodePage)
					enc = Encoding.GetEncoding(EncConverters.cnIso8859_1CodePage);
				else
					enc = Encoding.GetEncoding(aEcSource.CodePageInput);
			}
			string strProjectFileContents = File.ReadAllText(strProjectFileSource, enc);

			int nIndex;
			int nLength = 0;

			// the charset value has to be 0 in a Unicode project
			// first the 'Source language'
			nIndex = strProjectFileContents.IndexOf(cstrAdaptItCharMapDelimiter);
			if (nIndex != -1)
			{
				nIndex += cstrAdaptItCharMapDelimiter.Length;
				nLength = strProjectFileContents.IndexOfAny(caSplitChars, nIndex) - nIndex;
				strProjectFileContents = ReplaceSubstring(strProjectFileContents, nIndex, nLength, "0");

				// next the target language...
				nIndex = strProjectFileContents.IndexOf(cstrAdaptItCharMapDelimiter, nIndex);
				if (nIndex != -1)
				{
					nIndex += cstrAdaptItCharMapDelimiter.Length;
					nLength = strProjectFileContents.IndexOfAny(caSplitChars, nIndex) - nIndex;
					strProjectFileContents = ReplaceSubstring(strProjectFileContents, nIndex, nLength, "0");

					// finally do the navigation language as well.
					nIndex = strProjectFileContents.IndexOf(cstrAdaptItCharMapDelimiter, nIndex);
					if (nIndex != -1)
					{
						nIndex += cstrAdaptItCharMapDelimiter.Length;
						nLength = strProjectFileContents.IndexOfAny(caSplitChars, nIndex) - nIndex;
						strProjectFileContents = ReplaceSubstring(strProjectFileContents, nIndex, nLength, "0");
					}
				}
			}

			// see about replacing the font names
			if (m_strDefaultUnicodeFontSource != m_strStatusBoxDefaultFont)
			{
				nIndex = strProjectFileContents.IndexOf(strFontNameSource);
				nLength = strFontNameSource.Length;
				strProjectFileContents = ReplaceSubstring(strProjectFileContents, nIndex, nLength, m_strDefaultUnicodeFontSource);
			}
			else
			{
				nIndex = strProjectFileContents.IndexOf(cstrAdaptItFontNameDelimiter);
				nIndex = strProjectFileContents.IndexOf(cstrAdaptItFontNameDelimiter, nIndex);
			}

			if (m_strDefaultUnicodeFontTarget != m_strStatusBoxDefaultFont)
			{
				nIndex = strProjectFileContents.IndexOf(strFontNameTarget, nIndex + nLength);
				nLength = strFontNameTarget.Length;
				strProjectFileContents = ReplaceSubstring(strProjectFileContents, nIndex, nLength, m_strDefaultUnicodeFontTarget);
			}

			int nIndexPunctDelimiter = strProjectFileContents.IndexOf(cstrAdaptItPunctuationPairsLegacy, nIndex);
			nIndex = nIndexPunctDelimiter + cstrAdaptItPunctuationPairsLegacy.Length;
			nLength = strProjectFileContents.IndexOfAny(caSplitChars, nIndex) - nIndex;
			string strPunctuation = strProjectFileContents.Substring(nIndex, nLength);

			char[] aPunctSource, aPunctTarget;
			UnpackPunctuationLegacy(strPunctuation, out aPunctSource, out aPunctTarget);

			string strNewPunctuationSource = new string(aPunctSource);
			AppendStatusOutputNl("found: legacy source punctuation: ", m_strStatusBoxDefaultFont, true);
			AppendStatusOutput(strNewPunctuationSource, strFontNameSource, true);

			string strNewPunctuationSourceConverted = strNewPunctuationSource;
			if (aEcSource != null)
				strNewPunctuationSourceConverted = aEcSource.Convert(strNewPunctuationSource);

			if (strNewPunctuationSourceConverted != strNewPunctuationSource)
				AppendStatusOutputNl("converted to: " + strNewPunctuationSourceConverted, m_strDefaultUnicodeFontSource, true);
			else
				AppendStatusOutputNl("result: no changes", m_strStatusBoxDefaultFont, true);

			string strNewPunctuationTarget = new string(aPunctTarget);
			AppendStatusOutputNl("found: legacy target punctuation: ", m_strStatusBoxDefaultFont, true);
			AppendStatusOutput(strNewPunctuationTarget, strFontNameTarget, true);

			string strNewPunctuationTargetConverted = strNewPunctuationTarget;
			if (aEcTarget != null)
				strNewPunctuationTargetConverted = aEcTarget.Convert(strNewPunctuationTarget);

			if (strNewPunctuationTargetConverted != strNewPunctuationTarget)
				AppendStatusOutputNl("converted to: " + strNewPunctuationTargetConverted, m_strDefaultUnicodeFontTarget, true);
			else
				AppendStatusOutputNl("result: no changes", m_strStatusBoxDefaultFont, true);

			nIndex = strProjectFileContents.IndexOf(cstrAdaptItPunctuationExtraLegacy, nIndex) + cstrAdaptItPunctuationExtraLegacy.Length;
			nLength = strProjectFileContents.IndexOfAny(caSplitChars, nIndex) - nIndex;

			string strProjectFileContentsNew =
				strProjectFileContents.Substring(0, nIndexPunctDelimiter) +
				cstrAdaptItPunctuationPairsNRSource + strNewPunctuationSourceConverted + CRLF +
				cstrAdaptItPunctuationPairsNRTarget + strNewPunctuationTargetConverted + CRLF +
				cstrAdaptItPunctuationExtraNR +
				strProjectFileContents.Substring(nIndex);

			// as a special case, if the 'after-conversion' fonts are both defined and are equal to each other,
			//  then go ahead and update in the project file
			if (    (strFontNameSource == strFontNameTarget)
				&&  (m_strDefaultUnicodeFontSource != m_strStatusBoxDefaultFont)
				&&  (m_strDefaultUnicodeFontTarget != m_strStatusBoxDefaultFont)
				&&  (m_strDefaultUnicodeFontSource == m_strDefaultUnicodeFontTarget))
				strProjectFileContentsNew = strProjectFileContentsNew.Replace(strFontNameSource, m_strDefaultUnicodeFontSource);

			InsureDirectoryExists(strProjectFileTarget);

			// don't use WriteAllText (c.f. ReadAllText above), because that inserts the BOM which causes AIU grief
			byte[] abyContents = Encoding.UTF8.GetBytes(strProjectFileContentsNew);
			File.WriteAllBytes(strProjectFileTarget, abyContents);
		}
示例#20
0
		protected void ConvertAttribute(XmlAttributeCollection xnAttributes, string strAttributeName, IEncConverter aEC)
		{
			XmlAttribute xa = xnAttributes[strAttributeName];
			if (xa != null)
			{
				string strValue = xa.Value;

				if (!String.IsNullOrEmpty(strValue))
				{
					string strValueConverted = (aEC != null) ? aEC.Convert(strValue) : strValue;
					xa.Value = strValueConverted;
				}
			}
		}
示例#21
0
        public void DoFileConvert
        (
            string strConverterName,
            string strOutputFileName,
            System.Text.Encoding outEnc,
            string strInputFileName,
            System.Text.Encoding inEnc,
            bool bDirectionForward
        )
        {
#if VERBOSE_DEBUGGING
            Console.WriteLine("ECFileConv: DoFileConvert() BEGIN");
            Console.WriteLine("ECFileConv: inEnc " + inEnc.ToString());
#endif
            // the user *might* not give us a converter name if they simply want to change
            //  the encoding from, say, UTF8 to UTF16.
            bool bIsConverter = !(strConverterName == null);

            IEncConverter aEC = null;
            if (bIsConverter)
            {
#if VERBOSE_DEBUGGING
                Console.WriteLine("ECFileConv: Creating EncConverters object.");
#endif
                EncConverters aECs = new EncConverters();
#if VERBOSE_DEBUGGING
                Console.WriteLine("ECFileConv: Created EncConverters object.");
#endif
                if (strConverterName.ToLower() == "askme")
                {
#if VERBOSE_DEBUGGING
                    Console.WriteLine("ECFileConv: Calling AutoSelect.");
#endif
                    aEC = aECs.AutoSelect(ConvType.Unknown);
                    if (aEC == null)
                    {
                        // user probably pressed Cancel
                        Console.WriteLine("ECFileConv: No converter was selected.");
                        return;
                    }
                }
                else
                {
                    //// here's how you'd add the map programmatically (of course,
                    ////  update the path here
                    //string mapLoc = Path.Combine(GetProjectFolder, "ToUpper.tec");
                    //Console.WriteLine("mapLoc " + mapLoc);
                    //aECs.AddConversionMap(strConverterName, Path.Combine(GetProjectFolder, "ToUpper.tec"),
                    //    ConvType.Unicode_to_from_Unicode, EncConverters.strTypeSILtec,
                    //    "UNICODE", "UNICODE", ProcessTypeFlags.DontKnow);
                    //Console.WriteLine("Added map.");
                    aEC = aECs[strConverterName];    // e.g. "Devanagri<>Latin(ICU)"
                    if (aEC == null)
                    {
                        throw new ApplicationException(
                                  String.Format("The converter '{0}' wasn't in the repository. Did you forget to add it?",
                                                strConverterName));
                    }
                }
#if VERBOSE_DEBUGGING
                Console.WriteLine("ECFileConv: Got EncConverter.");
#endif
            }

            // open the input and output files using the given encoding formats
            StreamReader srReadLine = new StreamReader(strInputFileName, inEnc, true);
            srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);
            StreamWriter swWriteLine = new StreamWriter(strOutputFileName, false, outEnc);

            // tell the converter to go the other way, if the user selected 'reverse'
            if (!bDirectionForward && bIsConverter)
            {
                aEC.DirectionForward = false;
            }

            // read the lines of the input file, (optionally convert,) and write them out.
            string sOutput, sInput;
            while (srReadLine.Peek() > -1)
            {
                sInput = srReadLine.ReadLine();

                if (bIsConverter)
                {
                    sOutput = aEC.Convert(sInput);
                }
                else
                {
                    sOutput = sInput;
                }

                swWriteLine.WriteLine(sOutput);
            }

            srReadLine.Close();
            swWriteLine.Close();
#if VERBOSE_DEBUGGING
            Console.WriteLine("ECFileConv: DoFileConvert END");
#endif
        }
示例#22
0
		private bool ChaChaChaChaChanges(IEncConverter aEC, string strFileName, string strWord)
		{
			string strDummy = null;
			int lProcessType = (int)SpellingFixerEC.SFProcessType;
			ConvType eConvType = (m_bLegacy) ? ConvType.Legacy_to_Legacy : ConvType.Unicode_to_Unicode;
			aEC.Initialize("dummyname", strFileName, ref strDummy, ref strDummy, ref eConvType, ref lProcessType, 0, 0, true);
			return (aEC.Convert(strWord) != strWord);
		}
示例#23
0
		protected void ConvertKbFile(string strKbFileSource, string strKbFileTarget,
			IEncConverter aEcSource, IEncConverter aEcTarget, string strFontNameSource, string strFontNameTarget)
		{
			// Since AdaptIt will make different records for two words which are canonically
			//  equivalent, if we use the class object to read it in via ReadXml, that will throw
			//  an exception in such a case. So see if using XmlDocument is any less restrictive
			try
			{
				AppendStatusOutputNl("Processing KB file: " + strKbFileSource, m_strStatusBoxDefaultFont, true);
				string strTempKbFileSource = strKbFileSource;
				CheckForIncorrectEncodingString(ref strTempKbFileSource);
				XmlDocument doc;
				XPathNavigator navigator;
				XmlNamespaceManager manager;
				GetXmlDocument(strTempKbFileSource, out doc, out navigator, out manager);

				XPathNodeIterator xpMapIterator = navigator.Select("/aikb:AdaptItKnowledgeBase/aikb:KB/aikb:MAP", manager);

				progressBarAdaptationFile.Visible = true;

				// can't have two source words with the same value
				List<string> astrSourceWords = new List<string>();
				while (xpMapIterator.MoveNext())
				{
					// find the first (next) source word element
					XPathNodeIterator xpSourceWords = xpMapIterator.Current.Select("aikb:TU", manager);

					progressBarAdaptationFile.Value = 0;
					progressBarAdaptationFile.Maximum = xpSourceWords.Count;
					while (xpSourceWords.MoveNext())
					{
						progressBarAdaptationFile.Value++;
						// get an iterator for the source word attribute (so we can change it later)
						XPathNodeIterator xpSourceWord = xpSourceWords.Current.Select("@k", manager);
						if (xpSourceWord.MoveNext())
						{
							// get and convert the source word value
							string strSourceWord = xpSourceWord.Current.Value;
							string strSourceWordConverted = ((aEcSource != null) && !String.IsNullOrEmpty(strSourceWord)) ? aEcSource.Convert(strSourceWord) : strSourceWord;

							// if this word has already come up before, then we have to do something else (TODO: e.g. merge the two)
							if (astrSourceWords.Contains(strSourceWordConverted))
							{
								AppendStatusOutputNl("Found two source words that have the same value: ", m_strStatusBoxDefaultFont, true);
								AppendStatusOutput(strSourceWordConverted, m_strDefaultUnicodeFontSource, true);

								while (astrSourceWords.Contains(strSourceWordConverted))
									strSourceWordConverted = "DuplicateOf:" + strSourceWordConverted;

								AppendStatusOutput(". Changing it to: ", m_strStatusBoxDefaultFont, true);
								AppendStatusOutput(strSourceWordConverted, m_strDefaultUnicodeFontSource, true);
							}

							AppendStatusOutputNl("source: ", m_strStatusBoxDefaultFont, false);
							AppendStatusOutput(strSourceWord, strFontNameSource, false);

							if (strSourceWord != strSourceWordConverted)
							{
								// otherwise, just change the value in-situ
								xpSourceWord.Current.SetValue(strSourceWordConverted);
								astrSourceWords.Add(strSourceWordConverted);

								AppendStatusOutput(", becomes: ", m_strStatusBoxDefaultFont, false);
								AppendStatusOutput(strSourceWordConverted, m_strDefaultUnicodeFontSource, false);
							}
							else
							{
								AppendStatusOutput(cstrNoChange, m_strStatusBoxDefaultFont, false);
							}

							// now get an iterator for all of the target words for the current source word
							//  (here we'll go directly to the attribute value)
							XPathNodeIterator xpTargetWords = xpSourceWords.Current.Select("aikb:RS", manager);

							// maintain a list of all target words in case the conversion merges them.
							List<string> astrTargetWords = new List<string>();
							while (xpTargetWords.MoveNext())
							{
								XPathNodeIterator xpTargetWord = xpTargetWords.Current.Select("@a", manager);
								if (xpTargetWord.MoveNext())
								{
									// get and convert the target word
									string strTargetWord = xpTargetWord.Current.Value;
									string strTargetWordConverted = ((aEcTarget != null) && !String.IsNullOrEmpty(strTargetWord)) ? aEcTarget.Convert(strTargetWord) : strTargetWord;

									// if this word has already come up before, then we have to do something else (TODO: e.g. merge the two)
									if (astrTargetWords.Contains(strTargetWordConverted))
									{
										AppendStatusOutputNl("Found two target words that have the same value: ", m_strStatusBoxDefaultFont, true);
										AppendStatusOutput(strTargetWordConverted, m_strDefaultUnicodeFontTarget, true);

										while (astrTargetWords.Contains(strTargetWordConverted))
											strTargetWordConverted = "DuplicateOf:" + strTargetWordConverted;

										AppendStatusOutput(". Changing it to: ", m_strStatusBoxDefaultFont, true);
										AppendStatusOutput(strTargetWordConverted, m_strDefaultUnicodeFontTarget, true);
									}

									AppendStatusOutputNl("target: ", m_strStatusBoxDefaultFont, false);
									AppendStatusOutput(strTargetWord, strFontNameTarget, false);

									if (strTargetWord != strTargetWordConverted)
									{
										// otherwise, just change the value in-situ
										xpTargetWord.Current.SetValue(strTargetWordConverted);
										astrTargetWords.Add(strTargetWordConverted);

										AppendStatusOutput(", becomes: ", m_strStatusBoxDefaultFont, false);
										AppendStatusOutput(strTargetWordConverted, m_strDefaultUnicodeFontTarget, false);
									}
									else
									{
										AppendStatusOutput(cstrNoChange, m_strStatusBoxDefaultFont, false);
									}
								}
							}
						}
					}
				}

				// now write it to the target folder
				XmlTextWriter writer = new XmlTextWriter(strKbFileTarget, Encoding.UTF8);
				writer.Formatting = Formatting.Indented;
				doc.Save(writer);

				progressBarAdaptationFile.Visible = false;
			}
			catch (System.Data.DataException ex)
			{
				if (ex.Message == "A child row has multiple parents.")
				{
					// this happens when the knowledge base has invalid data in it (e.g. when there is two
					//  canonically equivalent words in different records). This is technically a bug in
					//  AdaptIt.
					throw new ApplicationException(String.Format("The AdaptIt xml file '{0}' has invalid data in it! Contact [email protected]", strKbFileSource), ex);
				}

				throw ex;
			}
			catch (Exception ex)
			{
				throw new ApplicationException(String.Format("Unable to process the AdaptIt xml file '{0}'. Contact [email protected]", strKbFileSource), ex);
			}
		}