Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EditTextCommand.cs
1using System;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
8using Waher.Things;
11
13{
18 {
19 private readonly ProgramDataFile fileNode;
20 private readonly string contentType;
21
27 {
28 this.fileNode = FileNode;
29 this.contentType = FileNode.ContentType;
30
31 byte[] Bin = File.ReadAllBytes(FileNode.FileName);
32 Encoding Encoding = Strings.GetEncoding(Bin, Encoding.UTF8, out int BomLength);
33
34 string s = Encoding.GetString(Bin, BomLength, Bin.Length - BomLength);
35 string[] Rows = s.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
36
37 this.Content = Rows;
38 this.Encoding = Encoding.BodyName;
39 }
40
44 [Page(7, "File System", 100)]
45 [Header(30, "Text Content:")]
46 [ToolTip(31, "Text content in file.")]
47 [Required]
48 [DynamicContentType(nameof(GetContentType))]
49 public string[] Content { get; set; }
50
54 [Page(7, "File System", 100)]
55 [Header(32, "Text Encoding:")]
56 [ToolTip(33, "Encoding used to store text in file.")]
57 [Required]
58 [DynamicOptions(nameof(GetEncodingOptions))]
59 public string Encoding { get; set; }
60
66 {
67 List<OptionAttribute> Encodings = new List<OptionAttribute>();
68
69 foreach (EncodingInfo Encoding in System.Text.Encoding.GetEncodings())
70 Encodings.Add(new OptionAttribute(Encoding.Name));
71
72 return Encodings.ToArray();
73 }
74
80 {
81 return new ContentTypeAttribute(this.contentType);
82 }
83
87 public string CommandID => "EditText";
88
92 public CommandType Type => CommandType.Parametrized;
93
97 public string SortCategory => "Edit";
98
102 public string SortKey => "Text";
103
108 public Task<string> GetNameAsync(Language Language) => Language.GetStringAsync(typeof(ProgramDataSource), 29, "Edit File...");
109
114 public Task<string> GetConfirmationStringAsync(Language Language) => Task.FromResult(string.Empty);
115
120 public Task<string> GetFailureStringAsync(Language Language) => Task.FromResult(string.Empty);
121
126 public Task<string> GetSuccessStringAsync(Language Language) => Task.FromResult(string.Empty);
127
133 public Task<bool> CanExecuteAsync(RequestOrigin Caller)
134 {
135 return Task.FromResult(Caller.HasPrivilege("Source." + ProgramDataSource.SourceID + "Command." + this.CommandID));
136 }
137
141 public async Task ExecuteCommandAsync()
142 {
143 Encoding Encoding = System.Text.Encoding.GetEncoding(this.Encoding)
144 ?? throw new ArgumentException("Invalid encoding.", nameof(this.Encoding));
145
146 StringBuilder sb = new StringBuilder();
147 bool First = true;
148
149 foreach (string Row in this.Content)
150 {
151 if (First)
152 First = false;
153 else
154 sb.AppendLine();
155
156 sb.Append(Row);
157 }
158
159 string Text = sb.ToString();
160 byte[] Bin = Encoding.GetBytes(Text);
161 byte[] Bom = Encoding.GetPreamble();
162 bool HasBom = EditTextCommand.HasBom(Bin, Bom);
163
164 using FileStream fs = File.OpenWrite(this.fileNode.FileName);
165
166 if (!HasBom)
167 await fs.WriteAsync(Bom, 0, Bom.Length);
168
169 await fs.WriteAsync(Bin, 0, Bin.Length);
170 await fs.FlushAsync();
171
172 fs.SetLength(fs.Position);
173 }
174
175 internal static bool HasBom(byte[] Bin, byte[] Bom)
176 {
177 int i, c = Bom.Length;
178
179 if (c > Bin.Length)
180 return false;
181 else if (c == 0)
182 return true;
183 else
184 {
185 bool HasBom = true;
186
187 for (i = 0; i < c; i++)
188 {
189 if (Bom[i] != Bin[i])
190 {
191 HasBom = false;
192 break;
193 }
194 }
195
196 return HasBom;
197 }
198 }
199
206
211 public ICommand Copy()
212 {
213 return new EditTextCommand(this.fileNode);
214 }
215 }
216}
Static class managing binary representations of strings.
Definition: Strings.cs:10
static Encoding GetEncoding(byte[] Data, Encoding DefaultEncoding, out int BomLength)
Gets the encoding of a string from a its binary representation, taking any Byte Order Mark (BOM) into...
Definition: Strings.cs:23
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Task< string > GetSuccessStringAsync(Language Language)
Gets a success string, if any, of the command. If no specific success string is available,...
Task< string > GetConfirmationStringAsync(Language Language)
Gets a confirmation string, if any, of the command. If no confirmation is necessary,...
Task< string > GetFailureStringAsync(Language Language)
Gets a failure string, if any, of the command. If no specific failure string is available,...
Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
Task< bool > CanExecuteAsync(RequestOrigin Caller)
If the command can be executed by the caller.
ICommand Copy()
Creates a copy of the command object.
OptionAttribute[] GetEncodingOptions()
Gets encoding options when editing Encoding.
Task< string > GetNameAsync(Language Language)
Gets the displayable name of the command.
ContentTypeAttribute GetContentType()
Gets the Content-Type of the Content property.
EditTextCommand(ProgramDataFile FileNode)
Edits the text of a text file.
Reference to a file in the ProgramData folder of the broker.
string ContentType
If the referenced file is a text file.
Data source mirroring the ProgramData folder for the broker.
const string SourceID
Data Source ID for the ProgramData source.
Defines the Content-Type of a multi-row property. Acts as a hint to clients, on how the text can be e...
Defines an option to display when editing the parameter.
Class handling the reception of data from a query.
Definition: Query.cs:12
Tokens available in request.
Definition: RequestOrigin.cs:9
bool HasPrivilege(string Privilege)
If the origin has a given privilege.
Interface for commands.
Definition: ICommand.cs:32
CommandType
Command type.
Definition: ICommand.cs:11