/// ------------------------------------------------------------------------------------
        /// <summary>
        /// Writes (creates or updates) a Paratext LDS file.
        /// </summary>
        /// <param name="ldsFileName">Name of the LDS file.</param>
        /// <param name="ws">The writing system which the LDS file describes.</param>
        /// <param name="normalStyle">The normal style.</param>
        /// ------------------------------------------------------------------------------------
        public void WriteParatextLdsFile(string ldsFileName, int ws, UsfmStyEntry normalStyle)
        {
            bool fUpdateSucceeded = false;

            // If the file describing the writing system exists, update values as needed.
            if (FileUtils.FileExists(ldsFileName))
            {
                // Check to see if file is writable
                if (FileUtils.IsFileReadableAndWritable(ldsFileName))
                {
                    fUpdateSucceeded = UpdateLdsFile(ldsFileName, normalStyle, ws);
                }
                else
                {
                    return;                     // leave the existing read-only file unchanged
                }
            }

            // If the lds file does not exist, or if there was a problem updating the lds file,
            // then a new lds file must be created.
            if (!fUpdateSucceeded)
            {
                // Create the LDS file in the settings directory
                WriteLdsFileContents(ldsFileName, normalStyle, ws);
            }
        }
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Updates the lds file which describes the writing system in Paratext.
		/// </summary>
		/// <param name="ldsFileName">Name of the LDS file.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the writing system for the current export.</param>
		/// <param name="fileWriterLDS">file writer for updating the Paratext LDS file</param>
		/// ------------------------------------------------------------------------------------
		public bool UpdateLdsFile(string ldsFileName, UsfmStyEntry normalStyle, int ws,
			FileWriter fileWriterLDS)
		{
			try
			{
				string ldsContents;
				using (StreamReader sr = new StreamReader(ldsFileName))
				{
					ldsContents = sr.ReadToEnd();
				}

				// Make backup of language file, if it doesn't exist.
				if (!File.Exists(ldsFileName + ".bak"))
					File.Copy(ldsFileName, ldsFileName + ".bak");
				// REVIEW: Best way to handle if the file cannot be copied?

				fileWriterLDS.Open(ldsFileName);
				UpdateLdsContents(ldsContents, normalStyle, ws, fileWriterLDS);
			}
			catch
			{
				return false;
			}
			return true;
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Updates the lds file which describes the writing system in Paratext.
		/// </summary>
		/// <param name="ldsFileName">Name of the LDS file.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the writing system for the current export.</param>
		/// ------------------------------------------------------------------------------------
		public bool UpdateLdsFile(string ldsFileName, UsfmStyEntry normalStyle, int ws)
		{
			try
			{
				string ldsContents;
				using (TextReader tr = FileUtils.OpenFileForRead(ldsFileName, Encoding.ASCII))
				{
					ldsContents = tr.ReadToEnd();
				}

				// Make backup of language file, if it doesn't exist.
				if (!FileUtils.FileExists(ldsFileName + ".bak"))
					FileUtils.Copy(ldsFileName, ldsFileName + ".bak");
				// REVIEW: Best way to handle if the file cannot be copied?

				using (TextWriter fileWriterLDS = FileUtils.OpenFileForWrite(ldsFileName, Encoding.ASCII))
				{
					UpdateLdsContents(ldsContents, normalStyle, ws, fileWriterLDS);
				}
			}
			catch
			{
				return false;
			}
			return true;
		}
Example #4
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Updates the lds file which describes the writing system in Paratext.
        /// </summary>
        /// <param name="ldsFileName">Name of the LDS file.</param>
        /// <param name="normalStyle">The normal style.</param>
        /// <param name="ws">The HVO of the writing system for the current export.</param>
        /// <param name="fileWriterLDS">file writer for updating the Paratext LDS file</param>
        /// ------------------------------------------------------------------------------------
        public bool UpdateLdsFile(string ldsFileName, UsfmStyEntry normalStyle, int ws,
                                  FileWriter fileWriterLDS)
        {
            try
            {
                string ldsContents;
                using (StreamReader sr = new StreamReader(ldsFileName))
                {
                    ldsContents = sr.ReadToEnd();
                }

                // Make backup of language file, if it doesn't exist.
                if (!File.Exists(ldsFileName + ".bak"))
                {
                    File.Copy(ldsFileName, ldsFileName + ".bak");
                }
                // REVIEW: Best way to handle if the file cannot be copied?

                fileWriterLDS.Open(ldsFileName);
                UpdateLdsContents(ldsContents, normalStyle, ws, fileWriterLDS);
            }
            catch
            {
                return(false);
            }
            return(true);
        }
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Updates the lds file which describes the writing system in Paratext.
		/// </summary>
		/// <param name="ldsFileName">Name of the LDS file.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the writing system for the current export.</param>
		/// ------------------------------------------------------------------------------------
		public bool UpdateLdsFile(string ldsFileName, UsfmStyEntry normalStyle, int ws)
		{
			try
			{
				string ldsContents;
				using (TextReader tr = FileUtils.OpenFileForRead(ldsFileName, Encoding.ASCII))
				{
					ldsContents = tr.ReadToEnd();
				}

				// Make backup of language file, if it doesn't exist.
				if (!FileUtils.FileExists(ldsFileName + ".bak"))
					FileUtils.Copy(ldsFileName, ldsFileName + ".bak");
				// REVIEW: Best way to handle if the file cannot be copied?

				using (TextWriter fileWriterLDS = FileUtils.OpenFileForWrite(ldsFileName, Encoding.ASCII))
				{
					UpdateLdsContents(ldsContents, normalStyle, ws, fileWriterLDS);
				}
			}
			catch
			{
				return false;
			}
			return true;
		}
Example #6
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Install the control.
        /// </summary>
        /// <param name="dockHost">The control that hosts the browser</param>
        /// <param name="cache">The cache (needed in case we have to create the English LDS file
        /// on the fly)</param>
        /// <param name="normalStyle">The normal style (needed in case we have to create the
        /// English LDS file on the fly)</param>
        /// <returns>
        ///     <c>true</c> if the browser was installed successfully; <c>false</c>
        /// otherwise.
        /// </returns>
        /// ------------------------------------------------------------------------------------
        public bool Install(Control dockHost, FdoCache cache, IStStyle normalStyle)
        {
            while (true)
            {
                try
                {
                    RegistrationInfo.AllowP6RegistrationCode = true;
                    RegistrationInfo.AllowAccessToResources();
                    string paratextProjectDir = ScrImportP6Project.ProjectDir;

                    if (!String.IsNullOrEmpty(paratextProjectDir))
                    {
                        string englishLdsPathname = Path.Combine(paratextProjectDir, "English.lds");
                        if (!File.Exists(englishLdsPathname))
                        {
                            ParatextLdsFileAccessor ldsAccessor     = new ParatextLdsFileAccessor(cache);
                            UsfmStyEntry            normalUsfmStyle = new UsfmStyEntry();
                            StyleInfoTable          styleTable      = new StyleInfoTable(normalStyle.Name,
                                                                                         cache.LanguageWritingSystemFactoryAccessor);
                            normalUsfmStyle.SetPropertiesBasedOnStyle(normalStyle);
                            styleTable.Add(normalStyle.Name, normalUsfmStyle);
                            styleTable.ConnectStyles();
                            ldsAccessor.WriteParatextLdsFile(englishLdsPathname,
                                                             cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("en"), normalUsfmStyle);
                        }
                    }
                    ScrTextCollection.Initialize();
                    break;
                }
                catch (Exception e)
                {
                    try
                    {
                        ReflectionHelper.SetField(typeof(ScrTextCollection), "initialized", false);
                    }
                    catch (Exception reflectionHelperException)
                    {
                        throw new ContinuableErrorException("Paratext resource browser failed to initialize." +
                                                            Environment.NewLine + reflectionHelperException.Message, e);
                    }
                    if (MessageBox.Show(dockHost.FindForm(), String.Format(
                                            Properties.Resources.kstidCannotDisplayResourcePane,
                                            Application.ProductName, e.Message), Application.ProductName,
                                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Error,
                                        MessageBoxDefaultButton.Button2) != DialogResult.Retry)
                    {
                        return(false);
                    }
                }
            }
            m_toolStrip.Text = "USFM Resource Browser";
            m_extender       = new DockExtender(dockHost);
            dockHost.Controls.Add(this);
            m_floaty = m_extender.Attach(this, m_toolStrip, true);
            this.SendToBack();
            return(true);
        }
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Computes the settings.
		/// </summary>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the vernacular writing system.</param>
		/// ------------------------------------------------------------------------------------
		private void ComputeSettings(UsfmStyEntry normalStyle, int ws)
		{
			FontInfo fontInfo = normalStyle.FontInfoForWs(ws);
			m_fontName = normalStyle.RealFontNameForWs(ws);
			m_fontSize = (fontInfo.m_fontSize.Value / 1000).ToString();

			IWritingSystem wsObj = m_cache.ServiceLocator.WritingSystemManager.Get(ws);
			m_wsName = wsObj.DisplayLabel;
			m_RtoL = (wsObj.RightToLeftScript ? "T" : "F");
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Computes the settings.
		/// </summary>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the vernacular writing system.</param>
		/// ------------------------------------------------------------------------------------
		private void ComputeSettings(UsfmStyEntry normalStyle, int ws)
		{
			FontInfo fontInfo = normalStyle.FontInfoForWs(ws);
			m_fontName = normalStyle.RealFontNameForWs(ws);
			m_fontSize = (fontInfo.m_fontSize.Value / 1000).ToString();

			LgWritingSystem lgws = new LgWritingSystem(m_cache, ws);
			m_wsName = lgws.Name.UserDefaultWritingSystem;
			m_RtoL = (lgws.RightToLeft ? "T" : "F");
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Computes the settings.
		/// </summary>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the vernacular writing system.</param>
		/// ------------------------------------------------------------------------------------
		private void ComputeSettings(UsfmStyEntry normalStyle, int ws)
		{
			FontInfo fontInfo = normalStyle.FontInfoForWs(ws);
			m_fontName = normalStyle.RealFontNameForWs(ws);
			m_fontSize = (fontInfo.m_fontSize.Value / 1000).ToString();

			IWritingSystem wsObj = m_cache.ServiceLocator.WritingSystemManager.Get(ws);
			m_wsName = wsObj.DisplayLabel;
			m_RtoL = (wsObj.RightToLeftScript ? "T" : "F");
		}
Example #10
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Computes the settings.
        /// </summary>
        /// <param name="normalStyle">The normal style.</param>
        /// <param name="ws">The HVO of the vernacular writing system.</param>
        /// ------------------------------------------------------------------------------------
        private void ComputeSettings(UsfmStyEntry normalStyle, int ws)
        {
            FontInfo fontInfo = normalStyle.FontInfoForWs(ws);

            m_fontName = normalStyle.RealFontNameForWs(ws);
            m_fontSize = (fontInfo.m_fontSize.Value / 1000).ToString();

            LgWritingSystem lgws = new LgWritingSystem(m_cache, ws);

            m_wsName = lgws.Name.UserDefaultWritingSystem;
            m_RtoL   = (lgws.RightToLeft ? "T" : "F");
        }
Example #11
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Process a marker/contents pair.
 /// </summary>
 /// <param name="marker">text of the marker</param>
 /// <param name="source">source text to process</param>
 /// ------------------------------------------------------------------------------------
 private void ProcessSegment(string marker, string source)
 {
     if (marker == @"\Marker")
     {
         m_currentEntry = new UsfmStyEntry();
         Add(source, m_currentEntry);
     }
     else
     {
         m_currentEntry.SetUsfmStyProperty(marker, source);
     }
 }
Example #12
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Adds the given style info entry to the table.
        /// </summary>
        /// <param name="key">The key of the element to add (typically a TE Stylename, but may
        /// be another unique token (if this entry represents a style which is not known to
        /// exist)</param>
        /// <param name="value">The value of the element to add (must not be null)</param>
        /// <exception cref="T:System.ArgumentException">An element with the same key already
        /// exists in the <see cref="T:System.Collections.Generic.Dictionary`2"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">key or value is null.</exception>
        /// ------------------------------------------------------------------------------------
        public override void Add(string key, BaseStyleInfo value)
        {
            base.Add(key, value);
            // We need to set the P6 Marker based on the key, but only if it's null. If it's
            // not null, this is probably a case where an entry is being re-keyed based on the
            // TE style name, so we should leave the P6 marker as is.
            UsfmStyEntry entry = (UsfmStyEntry)value;

            if (entry.P6Marker == null)
            {
                entry.P6Marker = key.Replace(' ', '_');
            }
        }
Example #13
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Writes (creates or updates) a Paratext LDS file.
        /// </summary>
        /// <param name="ldsFileName">Name of the LDS file.</param>
        /// <param name="ws">The writing system which the LDS file describes.</param>
        /// <param name="normalStyle">The normal style.</param>
        /// ------------------------------------------------------------------------------------
        public void WriteParatextLdsFile(string ldsFileName, int ws, UsfmStyEntry normalStyle)
        {
            FileWriter fileWriterLDS = new FileWriter();

            try
            {
                WriteParatextLdsFile(ldsFileName, ws, normalStyle, fileWriterLDS);
            }
            finally
            {
                fileWriterLDS.Close();
            }
        }
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// writes the contents of the LDS file.
		/// </summary>
		/// <param name="ldsFileName">name of the Paratext LDS to be saved.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the writing system for the current export.</param>
		/// ------------------------------------------------------------------------------------
		private void WriteLdsFileContents(string ldsFileName, UsfmStyEntry normalStyle, int ws)
		{
			using (TextWriter fileWriterLds = FileUtils.OpenFileForWrite(ldsFileName, Encoding.ASCII))
			{
				ComputeSettings(normalStyle, ws);

				fileWriterLds.WriteLine("[General]");
				fileWriterLds.WriteLine("codepage=65001");
				fileWriterLds.WriteLine("RTL=" + m_RtoL);
				fileWriterLds.WriteLine("font=" + m_fontName);
				fileWriterLds.WriteLine("name=" + m_wsName);
				fileWriterLds.WriteLine("size=" + m_fontSize);
				fileWriterLds.WriteLine(string.Empty, true);
				fileWriterLds.WriteLine("[Checking]");
				fileWriterLds.WriteLine(string.Empty, true);
				fileWriterLds.WriteLine("[Characters]");
				fileWriterLds.WriteLine(string.Empty, true);
				fileWriterLds.WriteLine("[Punctuation]");
			}
		}
Example #15
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// writes the contents of the LDS file.
        /// </summary>
        /// <param name="ldsFileName">name of the Paratext LDS to be saved.</param>
        /// <param name="normalStyle">The normal style.</param>
        /// <param name="ws">The HVO of the writing system for the current export.</param>
        /// <param name="fileWriterLds">file writer for the Paratext LDS file</param>
        /// ------------------------------------------------------------------------------------
        private void WriteLdsFileContents(string ldsFileName, UsfmStyEntry normalStyle, int ws,
                                          FileWriter fileWriterLds)
        {
            Debug.Assert(fileWriterLds != null);
            fileWriterLds.Open(ldsFileName);

            ComputeSettings(normalStyle, ws);

            fileWriterLds.WriteLine("[General]");
            fileWriterLds.WriteLine("codepage=65001");
            fileWriterLds.WriteLine("RTL=" + m_RtoL);
            fileWriterLds.WriteLine("font=" + m_fontName);
            fileWriterLds.WriteLine("name=" + m_wsName);
            fileWriterLds.WriteLine("size=" + m_fontSize);
            fileWriterLds.WriteLine(string.Empty, true);
            fileWriterLds.WriteLine("[Checking]");
            fileWriterLds.WriteLine(string.Empty, true);
            fileWriterLds.WriteLine("[Characters]");
            fileWriterLds.WriteLine(string.Empty, true);
            fileWriterLds.WriteLine("[Punctuation]");
        }
Example #16
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Install the control.
		/// </summary>
		/// <param name="dockHost">The control that hosts the browser</param>
		/// <param name="cache">The cache (needed in case we have to create the English LDS file
		/// on the fly)</param>
		/// <param name="normalStyle">The normal style (needed in case we have to create the
		/// English LDS file on the fly)</param>
		/// <returns>
		/// 	<c>true</c> if the browser was installed successfully; <c>false</c>
		/// otherwise.
		/// </returns>
		/// ------------------------------------------------------------------------------------
		public bool Install(Control dockHost, FdoCache cache, IStStyle normalStyle)
		{
			while (true)
			{
				try
				{
					RegistrationInfo.AllowP6RegistrationCode = true;
					RegistrationInfo.AllowAccessToResources();
					string paratextProjectDir = ScrImportP6Project.ProjectDir;

					if (!String.IsNullOrEmpty(paratextProjectDir))
					{
						string englishLdsPathname = Path.Combine(paratextProjectDir, "English.lds");
						if (!File.Exists(englishLdsPathname))
						{
							ParatextLdsFileAccessor ldsAccessor = new ParatextLdsFileAccessor(cache);
							UsfmStyEntry normalUsfmStyle = new UsfmStyEntry();
							StyleInfoTable styleTable = new StyleInfoTable(normalStyle.Name,
								cache.LanguageWritingSystemFactoryAccessor);
							normalUsfmStyle.SetPropertiesBasedOnStyle(normalStyle);
							styleTable.Add(normalStyle.Name, normalUsfmStyle);
							styleTable.ConnectStyles();
							ldsAccessor.WriteParatextLdsFile(englishLdsPathname,
								cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("en"), normalUsfmStyle);
						}
					}
					ScrTextCollection.Initialize();
					break;
				}
				catch (Exception e)
				{
					try
					{
						ReflectionHelper.SetField(typeof(ScrTextCollection), "initialized", false);
					}
					catch (Exception reflectionHelperException)
					{
						throw new ContinuableErrorException("Paratext resource browser failed to initialize." +
							Environment.NewLine + reflectionHelperException.Message, e);
					}
					if (MessageBox.Show(dockHost.FindForm(), String.Format(
						Properties.Resources.kstidCannotDisplayResourcePane,
						Application.ProductName, e.Message), Application.ProductName,
						MessageBoxButtons.RetryCancel, MessageBoxIcon.Error,
						MessageBoxDefaultButton.Button2) != DialogResult.Retry)
					{
						return false;
					}
				}
			}
			m_toolStrip.Text = "USFM Resource Browser";
			m_extender = new DockExtender(dockHost);
			dockHost.Controls.Add(this);
			m_floaty = m_extender.Attach(this, m_toolStrip, true);
			this.SendToBack();
			return true;
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Updates the lds file which describes the writing system in Paratext.
		/// </summary>
		/// <param name="ldsContents">Contents of the existing LDS file.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The writing system</param>
		/// <param name="writer">The file writer for the Paratext LDS file</param>
		/// ------------------------------------------------------------------------------------
		public void UpdateLdsContents(string ldsContents, UsfmStyEntry normalStyle,
			int ws, TextWriter writer)
		{
			ComputeSettings(normalStyle, ws);

			string [] lines = ldsContents.Split(new string[] { Environment.NewLine },
				StringSplitOptions.RemoveEmptyEntries);

			// Read lines of text from the file, updating it as necessary.
			bool fInGeneralSection = false;
			bool fInOtherSection = false;
			bool fHasGeneralSection = false;
			bool fHasCodePage = false;
			bool fHasFont = false;
			bool fHasFontSize = false;
			bool fHasName = false;
			bool fHasRTL = false;
			foreach (string line in lines)
			{
				if (line.StartsWith("[General]"))
				{
					if (fInOtherSection)
					{
						fInOtherSection = false;
						writer.WriteLine();
					}
					fInGeneralSection = true;
					fHasGeneralSection = true;
				}
				else if (line.StartsWith("["))
				{
					if (fInGeneralSection)
					{
						WriteGeneralSection(writer, ref fHasCodePage, ref fHasFont,
							ref fHasFontSize, ref fHasName, ref fHasRTL);

						fInGeneralSection = false;
						writer.WriteLine();
					}
					else if (fInOtherSection)
						writer.WriteLine();

					fInOtherSection = true;
				}

				if (fInGeneralSection)
				{
					string replaceString = string.Empty;

					// Check line for fields that must be updated
					if (line.StartsWith("codepage="))
					{
						replaceString = "codepage=65001";
						fHasCodePage = true;
					}
					else if (line.StartsWith("font="))
					{
						replaceString = "font=" + m_fontName;
						fHasFont = true;
					}
					else if (line.StartsWith("size="))
					{
						replaceString = "size=" + m_fontSize;
						fHasFontSize = true;
					}
					else if (line.StartsWith("name="))
					{
						replaceString = "name=" + m_wsName;
						fHasName = true;
					}
					else if (line.StartsWith("RTL="))
					{
						replaceString = "RTL=" + m_RtoL;
						fHasRTL = true;
					}
					if (replaceString != string.Empty)
						writer.WriteLine(replaceString); // Add updated line to file contents list
					else
						writer.WriteLine(line); // Add line, without changes, to file contents list
				}
				else
					writer.WriteLine(line);
			}

			if (!fHasGeneralSection)
			{
				if (fInOtherSection)
				{
					fInOtherSection = false;
					writer.WriteLine();
				}
				// General section not found. Write it.
				writer.WriteLine("[General]");
			}
			WriteGeneralSection(writer, ref fHasCodePage, ref fHasFont, ref fHasFontSize,
				ref fHasName, ref fHasRTL);
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// writes the contents of the LDS file.
		/// </summary>
		/// <param name="ldsFileName">name of the Paratext LDS to be saved.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the writing system for the current export.</param>
		/// <param name="fileWriterLds">file writer for the Paratext LDS file</param>
		/// ------------------------------------------------------------------------------------
		private void WriteLdsFileContents(string ldsFileName, UsfmStyEntry normalStyle, int ws,
			FileWriter fileWriterLds)
		{
			Debug.Assert(fileWriterLds != null);
			fileWriterLds.Open(ldsFileName);

			ComputeSettings(normalStyle, ws);

			fileWriterLds.WriteLine("[General]");
			fileWriterLds.WriteLine("codepage=65001");
			fileWriterLds.WriteLine("RTL=" + m_RtoL);
			fileWriterLds.WriteLine("font=" + m_fontName);
			fileWriterLds.WriteLine("name=" + m_wsName);
			fileWriterLds.WriteLine("size=" + m_fontSize);
			fileWriterLds.WriteLine(string.Empty, true);
			fileWriterLds.WriteLine("[Checking]");
			fileWriterLds.WriteLine(string.Empty, true);
			fileWriterLds.WriteLine("[Characters]");
			fileWriterLds.WriteLine(string.Empty, true);
			fileWriterLds.WriteLine("[Punctuation]");
		}
Example #19
0
		private void EnsureEnglishLdsExists()
		{
			string paratextProjectDir = ParatextHelper.ProjectsDirectory;

			if (!String.IsNullOrEmpty(paratextProjectDir))
			{
				string englishLdsPathname = Path.Combine(paratextProjectDir, "English.lds");
				if (!File.Exists(englishLdsPathname))
				{
					IStStyle normalStyle = m_StyleSheet.FindStyle(ScrStyleNames.Normal);
					ParatextLdsFileAccessor ldsAccessor = new ParatextLdsFileAccessor(Cache);
					UsfmStyEntry normalUsfmStyle = new UsfmStyEntry();
					StyleInfoTable styleTable = new StyleInfoTable(normalStyle.Name,
						Cache.ServiceLocator.WritingSystemManager);
					normalUsfmStyle.SetPropertiesBasedOnStyle(normalStyle);
					styleTable.Add(normalStyle.Name, normalUsfmStyle);
					styleTable.ConnectStyles();
					ldsAccessor.WriteParatextLdsFile(englishLdsPathname,
						Cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("en"), normalUsfmStyle);
					// We pass the directory (rather than passing no arguments, and letting the paratext dll figure
					// it out) because the figuring out goes wrong on Linux, where both programs are simulating
					// the registry.
					ScrTextCollection.Initialize(ParatextHelper.ProjectsDirectory, false);
				}
			}
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Writes (creates or updates) a Paratext LDS file.
		/// </summary>
		/// <param name="ldsFileName">Name of the LDS file.</param>
		/// <param name="ws">The writing system which the LDS file describes.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// ------------------------------------------------------------------------------------
		public void WriteParatextLdsFile(string ldsFileName, int ws, UsfmStyEntry normalStyle)
		{
			FileWriter fileWriterLDS = new FileWriter();
			try
			{
				WriteParatextLdsFile(ldsFileName, ws, normalStyle, fileWriterLDS);
			}
			finally
			{
				fileWriterLDS.Close();
			}
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Writes (creates or updates) a Paratext LDS file.
		/// </summary>
		/// <param name="ldsFileName">Name of the LDS file.</param>
		/// <param name="ws">The writing system which the LDS file describes.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="fileWriterLDS">The file writer used to write the LDS file.</param>
		/// ------------------------------------------------------------------------------------
		private void WriteParatextLdsFile(string ldsFileName, int ws, UsfmStyEntry normalStyle,
			FileWriter fileWriterLDS)
		{
			bool fUpdateSucceeded = false;
			// If the file describing the writing system exists, update values as needed.
			if (File.Exists(ldsFileName))
			{
				// Check to see if file is writable
				if ((File.GetAttributes(ldsFileName) & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
					fUpdateSucceeded = UpdateLdsFile(ldsFileName, normalStyle, ws, fileWriterLDS);
				else
					return; // leave the existing read-only file unchanged
			}

			// If the lds file does not exist, or if there was a problem updating the lds file,
			// then a new lds file must be created.
			if (!fUpdateSucceeded)
			{
				// Create the LDS file in the settings directory
				WriteLdsFileContents(ldsFileName, normalStyle, ws, fileWriterLDS);
			}
		}
Example #22
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Sets the style mapping.
		/// </summary>
		/// <param name="styleName">Name of the style.</param>
		/// <param name="paraStyleContext">The para style context.</param>
		/// <param name="marker">The SF marker, including the leading backslash.</param>
		/// ------------------------------------------------------------------------------------
		protected void SetStyleMapping(string styleName, XmlNode paraStyleContext, string marker)
		{
			// get the style name and replace any "_" characters with spaces.
			string key;
			if (paraStyleContext != null)
				key = paraStyleContext.Value.Replace("_", " ") + "\uffff" + styleName;
			else
				key = styleName;

			if (m_markerMappings.ContainsKey(key))
			{
				// This can only happen if someone sets up TeStyles.xml wrong, but the DTD can't enforce this kind of checking.
				MessageBox.Show(string.Format(TeResourceHelper.GetResourceString("kstidMultipleStyleExportCodes"),
					styleName, m_markerMappings[key]), m_app.ApplicationName);
			}
			else
			{
				m_markerMappings[key] = marker;

				if (m_exportParatextProjectFiles)
				{
					marker = marker.Substring(1); // strip off the leading backslash
					UsfmStyEntry entry;
					if (m_UsfmStyFileAccessor.ContainsKey(marker))
					{
						entry = (UsfmStyEntry)m_UsfmStyFileAccessor[marker];
						// If this entry is keyed in the table based on the marker, we now need to move it
						// so it will be keyed by TE style name, so that StyleInfoTable.ConnectStyles will
						// be able to find the styles to connect things up correctly.
						// But don't do this for the special case where the key includes a para style context
						// because it would result in conflicts, and those special "styles" are never going
						// to be the based-on or next styles for anything.
						if (marker != styleName && paraStyleContext == null)
						{
							m_UsfmStyFileAccessor.Remove(marker);
							m_UsfmStyFileAccessor.Add(styleName, entry);
						}
					}
					else
					{
						entry = new UsfmStyEntry();
						entry.P6Marker = marker;
						m_UsfmStyFileAccessor.Add(paraStyleContext == null ? styleName : marker, entry);
					}
					IStStyle style = m_scr.FindStyle(styleName);
					if (style != null)
					{
						// Update the values of the entry based on the TE style.
						entry.SetPropertiesBasedOnStyle(style);
					}
				}
			}
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Writes (creates or updates) a Paratext LDS file.
		/// </summary>
		/// <param name="ldsFileName">Name of the LDS file.</param>
		/// <param name="ws">The writing system which the LDS file describes.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// ------------------------------------------------------------------------------------
		public void WriteParatextLdsFile(string ldsFileName, int ws, UsfmStyEntry normalStyle)
		{
			bool fUpdateSucceeded = false;
			// If the file describing the writing system exists, update values as needed.
			if (FileUtils.FileExists(ldsFileName))
			{
				// Check to see if file is writable
				if (FileUtils.IsFileReadableAndWritable(ldsFileName))
					fUpdateSucceeded = UpdateLdsFile(ldsFileName, normalStyle, ws);
				else
					return; // leave the existing read-only file unchanged
			}

			// If the lds file does not exist, or if there was a problem updating the lds file,
			// then a new lds file must be created.
			if (!fUpdateSucceeded)
			{
				// Create the LDS file in the settings directory
				WriteLdsFileContents(ldsFileName, normalStyle, ws);
			}
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Updates the lds file which describes the writing system in Paratext.
		/// </summary>
		/// <param name="ldsContents">Contents of the existing LDS file.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The writing system</param>
		/// <param name="writer">The file writer for the Paratext LDS file</param>
		/// ------------------------------------------------------------------------------------
		public void UpdateLdsContents(string ldsContents, UsfmStyEntry normalStyle,
			int ws, TextWriter writer)
		{
			ComputeSettings(normalStyle, ws);

			string [] lines = ldsContents.Split(new string[] { Environment.NewLine },
				StringSplitOptions.RemoveEmptyEntries);

			// Read lines of text from the file, updating it as necessary.
			bool fInGeneralSection = false;
			bool fInOtherSection = false;
			bool fHasGeneralSection = false;
			bool fHasCodePage = false;
			bool fHasFont = false;
			bool fHasFontSize = false;
			bool fHasName = false;
			bool fHasRTL = false;
			foreach (string line in lines)
			{
				if (line.StartsWith("[General]"))
				{
					if (fInOtherSection)
					{
						fInOtherSection = false;
						writer.WriteLine();
					}
					fInGeneralSection = true;
					fHasGeneralSection = true;
				}
				else if (line.StartsWith("["))
				{
					if (fInGeneralSection)
					{
						WriteGeneralSection(writer, ref fHasCodePage, ref fHasFont,
							ref fHasFontSize, ref fHasName, ref fHasRTL);

						fInGeneralSection = false;
						writer.WriteLine();
					}
					else if (fInOtherSection)
						writer.WriteLine();

					fInOtherSection = true;
				}

				if (fInGeneralSection)
				{
					string replaceString = string.Empty;

					// Check line for fields that must be updated
					if (line.StartsWith("codepage="))
					{
						replaceString = "codepage=65001";
						fHasCodePage = true;
					}
					else if (line.StartsWith("font="))
					{
						replaceString = "font=" + m_fontName;
						fHasFont = true;
					}
					else if (line.StartsWith("size="))
					{
						replaceString = "size=" + m_fontSize;
						fHasFontSize = true;
					}
					else if (line.StartsWith("name="))
					{
						replaceString = "name=" + m_wsName;
						fHasName = true;
					}
					else if (line.StartsWith("RTL="))
					{
						replaceString = "RTL=" + m_RtoL;
						fHasRTL = true;
					}
					if (replaceString != string.Empty)
						writer.WriteLine(replaceString); // Add updated line to file contents list
					else
						writer.WriteLine(line); // Add line, without changes, to file contents list
				}
				else
					writer.WriteLine(line);
			}

			if (!fHasGeneralSection)
			{
				if (fInOtherSection)
				{
					fInOtherSection = false;
					writer.WriteLine();
				}
				// General section not found. Write it.
				writer.WriteLine("[General]");
			}
			WriteGeneralSection(writer, ref fHasCodePage, ref fHasFont, ref fHasFontSize,
				ref fHasName, ref fHasRTL);
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// writes the contents of the LDS file.
		/// </summary>
		/// <param name="ldsFileName">name of the Paratext LDS to be saved.</param>
		/// <param name="normalStyle">The normal style.</param>
		/// <param name="ws">The HVO of the writing system for the current export.</param>
		/// ------------------------------------------------------------------------------------
		private void WriteLdsFileContents(string ldsFileName, UsfmStyEntry normalStyle, int ws)
		{
			using (TextWriter fileWriterLds = FileUtils.OpenFileForWrite(ldsFileName, Encoding.ASCII))
			{
				ComputeSettings(normalStyle, ws);

				fileWriterLds.WriteLine("[General]");
				fileWriterLds.WriteLine("codepage=65001");
				fileWriterLds.WriteLine("RTL=" + m_RtoL);
				fileWriterLds.WriteLine("font=" + m_fontName);
				fileWriterLds.WriteLine("name=" + m_wsName);
				fileWriterLds.WriteLine("size=" + m_fontSize);
				fileWriterLds.WriteLine(string.Empty, true);
				fileWriterLds.WriteLine("[Checking]");
				fileWriterLds.WriteLine(string.Empty, true);
				fileWriterLds.WriteLine("[Characters]");
				fileWriterLds.WriteLine(string.Empty, true);
				fileWriterLds.WriteLine("[Punctuation]");
			}
		}
Example #26
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Process a marker/contents pair.
		/// </summary>
		/// <param name="marker">text of the marker</param>
		/// <param name="source">source text to process</param>
		/// ------------------------------------------------------------------------------------
		private void ProcessSegment(string marker, string source)
		{
			if (marker == @"\Marker")
			{
				m_currentEntry = new UsfmStyEntry();
				Add(source, m_currentEntry);
			}
			else
			{
				m_currentEntry.SetUsfmStyProperty(marker, source);
			}
		}
Example #27
0
		protected virtual void LoadStyleTables()
		{
			XmlDocument doc = new XmlDocument();
			doc.Load(FwDirectoryFinder.TeStylesPath);
			foreach (XmlElement elem in doc.SelectNodes("/Styles/markup/tag"))
			{
				string styleName = elem.Attributes.GetNamedItem("id").InnerText.Replace("_", " ");

				bool fStyleMappingSet = false;
				// get the marker from the sfm subnode.
				foreach (XmlNode sfmNode in elem.SelectNodes("sfm"))
				{
					if (sfmNode == null)
						continue;

					SetStyleMapping(styleName, sfmNode.Attributes.GetNamedItem("paraStyleContext"),
						sfmNode.InnerText);
					fStyleMappingSet = true;

					string sContext = elem.Attributes.GetNamedItem("context").InnerText;
					XmlNode function = elem.Attributes.GetNamedItem("use");
					string sFunction = function != null ? function.InnerText : null;
					if ((sContext == "note" || sContext == "psuedoStyle" || sFunction == "footnote") &&
						elem.Attributes.GetNamedItem("type").InnerText == "character")
						m_footnoteContentMarkers.Add(sfmNode.InnerText);
				}

				if (!fStyleMappingSet && m_exportParatextProjectFiles)
				{
					IStStyle style = m_scr.FindStyle(styleName);
					if (style != null && !m_UsfmStyFileAccessor.ContainsKey(styleName))
					{
						UsfmStyEntry entry = new UsfmStyEntry();
						m_UsfmStyFileAccessor.Add(styleName, entry);
						entry.SetPropertiesBasedOnStyle(style);
					}
				}
			}

			// Make sure we have mappings for default footnote characters and cross-ref target references
			// (in case the psuedo-style was deleted from TeStyles.xml).
			if (!m_markerMappings.ContainsKey(ScrStyleNames.NormalFootnoteParagraph + "\uffff" +
				ksDefaultFootnoteCharacters))
			{
				m_markerMappings[ScrStyleNames.NormalFootnoteParagraph + "\uffff" + ksDefaultFootnoteCharacters]
					= @"\ft";
				m_footnoteContentMarkers.Add(@"\ft");
			}
			if (!m_markerMappings.ContainsKey(ScrStyleNames.CrossRefFootnoteParagraph + "\uffff" +
				ksDefaultFootnoteCharacters))
			{
				m_markerMappings[ScrStyleNames.CrossRefFootnoteParagraph + "\uffff" + ksDefaultFootnoteCharacters]
					= @"\xt";
				m_footnoteContentMarkers.Add(@"\xt");
			}
		}