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;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
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 = CommonTypes.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) => XmppServerModule.IsAdmin(Caller.From);
134
138 public async Task ExecuteCommandAsync()
139 {
140 Encoding Encoding = System.Text.Encoding.GetEncoding(this.Encoding)
141 ?? throw new ArgumentException("Invalid encoding.", nameof(this.Encoding));
142
143 StringBuilder sb = new StringBuilder();
144 bool First = true;
145
146 foreach (string Row in this.Content)
147 {
148 if (First)
149 First = false;
150 else
151 sb.AppendLine();
152
153 sb.Append(Row);
154 }
155
156 string Text = sb.ToString();
157 byte[] Bin = Encoding.GetBytes(Text);
158 byte[] Bom = Encoding.GetPreamble();
159 bool HasBom = EditTextCommand.HasBom(Bin, Bom);
160
161 using (FileStream fs = File.OpenWrite(this.fileNode.FileName))
162 {
163 if (!HasBom)
164 await fs.WriteAsync(Bom, 0, Bom.Length);
165
166 await fs.WriteAsync(Bin, 0, Bin.Length);
167 await fs.FlushAsync();
168
169 fs.SetLength(fs.Position);
170 }
171 }
172
173 internal static bool HasBom(byte[] Bin, byte[] Bom)
174 {
175 int i, c = Bom.Length;
176
177 if (c > Bin.Length)
178 return false;
179 else if (c == 0)
180 return true;
181 else
182 {
183 bool HasBom = true;
184
185 for (i = 0; i < c; i++)
186 {
187 if (Bom[i] != Bin[i])
188 {
189 HasBom = false;
190 break;
191 }
192 }
193
194 return HasBom;
195 }
196 }
197
204
209 public ICommand Copy()
210 {
211 return new EditTextCommand(this.fileNode);
212 }
213 }
214}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
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: CommonTypes.cs:966
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 name of data source.
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.
Service Module hosting the XMPP broker and its components.
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
Interface for commands.
Definition: ICommand.cs:32
CommandType
Command type.
Definition: ICommand.cs:11