1010using System . IO ;
1111using System . Linq ;
1212using System . Runtime . CompilerServices ;
13+ using System . Text ;
1314using System . Text . Json . Serialization ;
1415using System . Threading . Tasks ;
1516using Serilog ;
@@ -21,8 +22,9 @@ namespace WinPrint.Core.ContentTypeEngines {
2122 /// Base class for Content/File Type Engines (CTEs)
2223 /// </summary>
2324 public abstract class ContentTypeEngineBase : ModelBase , INotifyPropertyChanged {
24- private static string _defaultCteName = "text/plain" ;
25- private static string _defaultSyntaxHighlighterCteName = "text/prism" ;
25+ public static string DefaultContentType = "text/plain" ;
26+ public static string DefaultCteClassName = "AnsiCte" ;
27+ public static string DefaultSyntaxHighlighterCteNameClassName = "PrismCte" ;
2628
2729 public new event PropertyChangedEventHandler PropertyChanged ;
2830 protected new void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null ) {
@@ -49,14 +51,8 @@ protected void OnSettingsChanged(bool reflow) {
4951 /// <summary>
5052 /// ContentType identifier (shorthand for class name).
5153 /// </summary>
52- public virtual string ContentTypeEngineName => _contentTypeEngineName ;
53- private static readonly string _contentTypeEngineName = "base" ;
54-
55- public string Language {
56- get => _fileType ;
57- set => SetField ( ref _fileType , value ) ;
58- }
59- private string _fileType ;
54+ public virtual string [ ] SupportedContentTypes => _supportedContentTypes ;
55+ private static readonly string [ ] _supportedContentTypes = null ;
6056
6157 /// <summary>
6258 /// Calculated page size. Set by Sheet view model.
@@ -75,10 +71,18 @@ public string Language {
7571 /// </summary>
7672 [ JsonIgnore ]
7773 public string Document {
78- get => document ; set =>
74+ get => _document ; set =>
7975 //LogService.TraceMessage($"Document is {document.Length} chars.");
80- SetField ( ref document , value ) ;
76+ SetField ( ref _document , value ) ;
8177 }
78+ internal string _document = null ;
79+
80+ /// <summary>
81+ /// The contents encdding of the file to be printed.
82+ /// </summary>
83+ [ JsonIgnore ]
84+ public Encoding Encoding { get => _encoding ; set => SetField ( ref _encoding , value ) ; }
85+ private Encoding _encoding = Encoding . Default ;
8286
8387 /// <summary>
8488 /// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class
@@ -92,8 +96,6 @@ public static ICollection<ContentTypeEngineBase> GetDerivedClassesCollection() {
9296 return objects ;
9397 }
9498
95- internal string document = null ;
96-
9799 /// <summary>
98100 /// These are the global StringFormat settings; set here to ensure all rendering and measuring uses same settings
99101 /// </summary>
@@ -130,12 +132,14 @@ public virtual async Task<int> RenderAsync(System.Drawing.Printing.PrinterResolu
130132 /// Creates the appropriate Content Type Engine instance given a content type string.
131133 /// </summary>
132134 /// <param name="contentType"></param>
133- /// <returns></returns>
134- public static async Task < ContentTypeEngineBase > CreateContentTypeEngine ( string contentType ) {
135+ /// <returns>ContentEngine, Language </returns>
136+ public static ( ContentTypeEngineBase cte , string language ) CreateContentTypeEngine ( string contentType ) {
135137 Debug . Assert ( ! string . IsNullOrEmpty ( contentType ) ) ;
138+ Debug . Assert ( ModelLocator . Current . Associations != null ) ;
139+ Debug . Assert ( ModelLocator . Current . Associations . Languages != null ) ;
136140
137- // If contentType matches one of our CTE ContentTypeNames , this will succeed.
138- ContentTypeEngineBase cte = GetDerivedClassesCollection ( ) . FirstOrDefault ( c => c . ContentTypeEngineName == contentType ) ;
141+ // If contentType matches one of our CTE Names , this will succeed.
142+ ContentTypeEngineBase cte = GetDerivedClassesCollection ( ) . FirstOrDefault ( c => contentType == c . GetType ( ) . Name ) ;
139143 string language = string . Empty ;
140144
141145 if ( cte == null ) {
@@ -152,42 +156,60 @@ public static async Task<ContentTypeEngineBase> CreateContentTypeEngine(string c
152156 // ".ans"
153157 // },
154158 // Is it a file extension?
155- var extLanguage = ModelLocator . Current . Associations . Languages . Where ( lang => lang . Extensions . Contains ( contentType ) ) . FirstOrDefault ( ) ;
159+ var extLanguage = ModelLocator . Current . Associations . Languages
160+ . Where ( lang => lang . Extensions . Contains ( contentType ) )
161+ . FirstOrDefault ( ) ;
156162 if ( extLanguage != null && ! string . IsNullOrEmpty ( extLanguage . Id ) ) {
157163 // Is Id a Cte Name?
158- cte = GetDerivedClassesCollection ( ) . FirstOrDefault ( c => c . ContentTypeEngineName == extLanguage . Id ) ;
164+ cte = GetDerivedClassesCollection ( ) . FirstOrDefault ( c => c . SupportedContentTypes . Contains ( extLanguage . Id ) ) ;
165+
159166 if ( cte != null ) {
160- return cte ;
167+ return ( cte , string . Empty ) ;
161168 }
162169 // It is a language. Needs to be Syntax Highlighted. Use the default Syntax Highlighter CTE
163170 language = extLanguage . Id ;
164171 }
165172 else {
166173 // Is it found in a langauge alias?
167- var alias = ModelLocator . Current . Associations . Languages . Where ( lang => lang . Aliases . Contains ( contentType ) ) . FirstOrDefault ( ) ;
174+ var alias = ModelLocator . Current . Associations . Languages
175+ . Where ( lang => lang . Id == contentType || lang . Aliases . Contains ( contentType ) )
176+ . FirstOrDefault ( ) ;
168177 if ( alias != null ) {
169- // Is Id a Cte Name?
170- cte = GetDerivedClassesCollection ( ) . Where ( c => c . ContentTypeEngineName == alias . Id ) . FirstOrDefault ( ) ;
178+ // Is the Id supported directly?
179+ // If by muplitple, pick the default.
180+ var collection = GetDerivedClassesCollection ( )
181+ . Where ( c => c . SupportedContentTypes . Contains ( alias . Id ) ) ;
182+ if ( collection != null ) {
183+ if ( collection . Count ( ) > 1 ) {
184+ cte = collection . Where ( c => c . GetType ( ) . Name == DefaultCteClassName ) . First ( ) ;
185+ }
186+ else {
187+ cte = collection . FirstOrDefault ( ) ;
188+ }
189+ }
171190 if ( cte != null ) {
172- return cte ;
191+ return ( cte , string . Empty ) ;
173192 }
174193 // It is a language. Needs to be Syntax Highlighted. Use the default Syntax Highlighter CTE
175194 language = contentType ;
176195 }
177196 }
178197
179198 if ( string . IsNullOrEmpty ( language ) ) {
180- cte = GetDerivedClassesCollection ( ) . Where ( c => c . ContentTypeEngineName == _defaultCteName ) . FirstOrDefault ( ) ;
199+ cte = GetDerivedClassesCollection ( )
200+ . Where ( c => c . SupportedContentTypes . Contains ( DefaultCteClassName ) )
201+ . FirstOrDefault ( ) ;
181202 }
182203 else {
183204 // It is a language. Needs to be Syntax Highlighted. Use the default Syntax Highlighter CTE
184- cte = GetDerivedClassesCollection ( ) . Where ( c => c . ContentTypeEngineName == _defaultSyntaxHighlighterCteName ) . FirstOrDefault ( ) ;
185- cte . Language = language ;
205+ cte = GetDerivedClassesCollection ( )
206+ . Where ( c => c . GetType ( ) . Name == DefaultSyntaxHighlighterCteNameClassName )
207+ . First ( ) ;
186208 }
187209 }
188210
189211 Debug . Assert ( cte != null ) ;
190- return cte ;
212+ return ( cte , language ) ;
191213 }
192214
193215 /// <summary>
@@ -196,8 +218,8 @@ public static async Task<ContentTypeEngineBase> CreateContentTypeEngine(string c
196218 /// </summary>
197219 /// <param name="filePath"></param>
198220 /// <returns>The content type</returns>
199- public static string GetContentType ( string filePath ) {
200- var contentType = "text/plain" ;
221+ public static string GetContentTypeOrLanguage ( string filePath ) {
222+ var contentType = DefaultCteClassName ;
201223
202224 if ( string . IsNullOrEmpty ( filePath ) ) {
203225 return contentType ;
@@ -210,11 +232,18 @@ public static string GetContentType(string filePath) {
210232 var ext = Path . GetExtension ( filePath ) . ToLower ( ) ;
211233 if ( ext != string . Empty ) {
212234 if ( ModelLocator . Current . Associations . FilesAssociations . TryGetValue ( "*" + ext , out var ct ) ) {
213- contentType = ct ;
235+ // Now find Id in Languages
236+ contentType = ModelLocator . Current . Associations . Languages
237+ . Where ( lang => lang . Id == ct )
238+ . DefaultIfEmpty ( new Langauge ( ) { Id = DefaultContentType } )
239+ . First ( ) . Id ;
214240 }
215241 else {
216242 // No direct file extension, look in Languages
217- contentType = ModelLocator . Current . Associations . Languages . Where ( lang => lang . Extensions . Contains ( ext ) ) . DefaultIfEmpty ( new Langauge ( ) { Id = "text/plain" } ) . First ( ) . Id ;
243+ contentType = ModelLocator . Current . Associations . Languages
244+ . Where ( lang => lang . Extensions . Contains ( ext ) )
245+ . DefaultIfEmpty ( new Langauge ( ) { Id = DefaultContentType } )
246+ . First ( ) . Id ;
218247 }
219248 }
220249 else {
0 commit comments