[corlib] Update ValueTuple implementation
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Toolbox / ToolboxItemLoader.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Toolbox
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.IO;
10     using System.Globalization;
11
12     // This is helper class which tries to load and create tool items from flat text file.
13     // The content of a file must contain following, colon separated values
14     // 1.) fully qualified class name, which is flagged with ToolboxItemAttribute 
15     // 2.) name of the assembly (with extension) where the tool class is located
16     // 3.) an optional bitmap file (in case when activity is not marked with toolboxitem attribute)
17     // 4.) an optional display name 
18     //
19     // Positions 3 & 4 can be in mixed order. the loader checks for known graphic extension 
20     //
21     // The category information is defined by string content placed within square brackets [ ].
22     // All items under category definition fall into that category unless new category is defined.
23     // If no category is defined, default one is created
24     //
25     // All lines starting with ';' or '#' char are treaded as comments and skipped
26
27     sealed class ToolboxItemLoader
28     {
29         static readonly string FormatExceptionText =
30             "Tool has to be defined either as 'name, assembly', or 'name, assembly, bitmap'";
31         static readonly string DefaultCategory = "default";
32
33         private ToolboxItemLoader()
34         {
35         }
36
37         public static ToolboxItemLoader GetInstance()
38         {
39             return new ToolboxItemLoader();
40         }
41
42         public void LoadToolboxItems(string fileName, ToolboxCategoryItems container, bool resetContainer)
43         {
44             if (string.IsNullOrEmpty(fileName))
45             {
46                 throw FxTrace.Exception.AsError(new ArgumentNullException("fileName"));
47             }
48
49             if (null == container)
50             {
51                 throw FxTrace.Exception.AsError(new ArgumentNullException("container"));
52             }
53
54             if (resetContainer)
55             {
56                 container.Clear();
57             }
58
59             using (StreamReader reader = File.OpenText(fileName))
60             {
61                 string entry = null;
62                 ToolboxCategory category = null;
63                 while (null != (entry = reader.ReadLine()))
64                 {
65                     entry = entry.Trim();
66                     if (entry.Length > 1 && (entry[0] != ';' && entry[0] != '#'))
67                     {
68                         if (entry.StartsWith("[", StringComparison.CurrentCulture) && entry.EndsWith("]", StringComparison.CurrentCulture))
69                         {
70                             string categoryName = entry.Substring(1, entry.Length - 2);
71                             category = GetCategoryItem(container, categoryName);
72                         }
73                         else
74                         {
75                             if (null == category)
76                             {
77                                 category = GetCategoryItem(container, DefaultCategory);
78                             }
79                             string[] toolDefinition = entry.Split(';');
80
81                             string toolName = null;
82                             string assembly = null;
83                             string displayName = null;
84                             string bitmap = null;
85
86                             if (GetToolAssemblyAndName(toolDefinition, ref toolName, ref assembly))
87                             {
88                                 GetBitmap(toolDefinition, ref bitmap);
89                                 GetDisplayName(toolDefinition, ref displayName);
90                                 category.Add(new ToolboxItemWrapper(toolName, assembly, bitmap, displayName));
91                             }
92                             else
93                             {
94                                 throw FxTrace.Exception.AsError(new ArgumentOutOfRangeException(FormatExceptionText));
95                             }
96                         }
97                     }
98                 }
99             }
100         }
101
102         bool GetToolAssemblyAndName(string[] toolDefinition, ref string toolName, ref string assembly)
103         {
104             if (toolDefinition.Length >= 2)
105             {
106                 toolName = toolDefinition[0].Trim();
107                 assembly = toolDefinition[1].Trim();
108                 return true;
109             }
110             return false;
111         }
112
113         bool GetBitmap(string[] toolDefinition, ref string bitmap)
114         {
115             for (int i = 2; i < toolDefinition.Length; ++i)
116             {
117                 string current = toolDefinition[i].Trim();
118                 if (current.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) ||
119                     current.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
120                     current.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) ||
121                     current.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
122                     current.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) ||
123                     current.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) ||
124                     current.EndsWith(".exig", StringComparison.OrdinalIgnoreCase))
125                 {
126                     bitmap = current;
127                     return true;
128                 }
129             }
130             return false;
131         }
132
133         bool GetDisplayName(string[] toolDefinition, ref string displayName)
134         {
135             for (int i = 2; i < toolDefinition.Length; ++i)
136             {
137                 string current = toolDefinition[i].Trim();
138                 if (!current.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) &&
139                     !current.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) &&
140                     !current.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) &&
141                     !current.EndsWith(".png", StringComparison.OrdinalIgnoreCase) &&
142                     !current.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) &&
143                     !current.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) &&
144                     !current.EndsWith(".exig", StringComparison.OrdinalIgnoreCase))
145                 {
146                     displayName = current;
147                     return true;
148                 }
149             }
150             return false;
151         }
152
153         ToolboxCategory GetCategoryItem(ToolboxCategoryItems container, string categoryName)
154         {
155             foreach (ToolboxCategory category in container)
156             {
157                 if (0 == string.Compare(category.CategoryName, categoryName, true, CultureInfo.CurrentUICulture))
158                 {
159                     return category;
160                 }
161             }
162             ToolboxCategory newCategory = new ToolboxCategory(categoryName);
163             container.Add(newCategory);
164             return newCategory;
165         }
166     }
167 }