1- using System ;
21using System . Collections . Generic ;
3- using System . Globalization ;
42using System . Linq ;
5- using System . Reflection ;
63using System . Text ;
74using System . Text . RegularExpressions ;
85using System . Xml ;
@@ -23,18 +20,16 @@ namespace SIL.XForge.Scripture.Services;
2320/// * Additional null checking
2421/// * Code reformatting
2522/// </remarks>
26- public static class NotesFormatter
23+ public static partial class NotesFormatter
2724{
2825 private const string NotesSchemaVersion = "1.1" ;
2926
3027 /// <summary>
3128 /// The regular expression for finding whitespace before XML tags.
3229 /// </summary>
3330 /// <remarks>This is used by <see cref="ParseNotesToXElement"/>.</remarks>
34- private static readonly Regex WhitespaceBeforeTagsRegex = new Regex (
35- @"\n\s*<" ,
36- RegexOptions . CultureInvariant | RegexOptions . Compiled
37- ) ;
31+ [ GeneratedRegex ( @"\n\s*<" , RegexOptions . CultureInvariant ) ]
32+ private static partial Regex WhitespaceBeforeTagsRegex ( ) ;
3833
3934 #region Public methods
4035 /// <summary>
@@ -71,33 +66,10 @@ public static List<List<Comment>> ParseNotes(XElement noteXml, ParatextUser ptUs
7166 public static XElement ParseNotesToXElement ( string text )
7267 {
7368 text = text . Trim ( ) . Replace ( "\r \n " , "\n " ) ;
74- text = WhitespaceBeforeTagsRegex . Replace ( text , "<" ) ;
69+ text = WhitespaceBeforeTagsRegex ( ) . Replace ( text , "<" ) ;
7570 return XElement . Parse ( text , LoadOptions . PreserveWhitespace ) ;
7671 }
7772
78- /// <summary>
79- /// Replaces numeric tag identifiers in note XML with the corresponding Paratext comment tag data.
80- /// </summary>
81- /// <param name="notesElement">The notes XML element.</param>
82- /// <param name="commentTags">The Paratext comment tag collection.</param>
83- public static void ReplaceTagIdentifiersWithCommentTags ( XElement notesElement , CommentTags commentTags )
84- {
85- if ( notesElement == null || commentTags == null )
86- return ;
87-
88- List < XElement > tagElements = [ .. notesElement . Descendants ( "tagAdded" ) ] ;
89- foreach ( XElement tagElement in tagElements )
90- {
91- string elementValue = tagElement . Value ;
92- if ( ! int . TryParse ( elementValue , NumberStyles . Integer , CultureInfo . InvariantCulture , out int tagId ) )
93- continue ;
94-
95- CommentTag mappedTag = commentTags . Get ( tagId ) ;
96- XElement replacement = CreateTagElement ( tagElement . Name , mappedTag ) ;
97- tagElement . ReplaceWith ( replacement ) ;
98- }
99- }
100-
10173 #endregion
10274
10375 #region Private helper methods for formatting
@@ -199,48 +171,6 @@ private static XElement FormatSelection(ScriptureSelection selection)
199171 return selElem ;
200172 }
201173
202- private static XElement CreateTagElement ( XName elementName , CommentTag commentTag )
203- {
204- XElement element = new XElement ( elementName ) ;
205- if ( commentTag == null )
206- return element ;
207-
208- PropertyInfo [ ] properties = typeof ( CommentTag ) . GetProperties ( BindingFlags . Instance | BindingFlags . Public ) ;
209- foreach ( PropertyInfo property in properties . Where ( property => property . CanRead ) )
210- {
211- object value = property . GetValue ( commentTag ) ;
212- if ( value == null )
213- continue ;
214-
215- string propertyValue ;
216- if ( value is string stringValue )
217- {
218- if ( string . IsNullOrEmpty ( stringValue ) )
219- continue ;
220- propertyValue = stringValue ;
221- }
222- else if ( value is int intValue )
223- {
224- propertyValue = intValue . ToString ( CultureInfo . InvariantCulture ) ;
225- }
226- else if ( value is bool boolValue )
227- {
228- propertyValue = boolValue ? bool . TrueString : bool . FalseString ;
229- }
230- else if ( value is Enum enumValue )
231- {
232- propertyValue = enumValue . ToString ( ) ;
233- }
234- else
235- {
236- continue ;
237- }
238-
239- element . Add ( new XElement ( property . Name , propertyValue ) ) ;
240- }
241-
242- return element ;
243- }
244174 #endregion
245175
246176 #region Private helper methods for parsing
0 commit comments