NanoXLSX.Writer 3.0.0-rc.3
Loading...
Searching...
No Matches
PlainText.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
9using NanoXLSX.Utils;
10using NanoXLSX.Utils.Xml;
11using System;
12
14{
19 internal class PlainText : IFormattableText
20 {
21 private const string TAG_NAME = "t";
22 private const string PRESERVE_ATTRIBUTE_NAME = "space";
23 private const string PRESERVE_ATTRIBUTE_PREFIX_NAME = "xml";
24 private const string PRESERVE_ATTRIBUTE_VALUE = "preserve";
25
29 public string Value { private get; set; }
30
35 public XmlElement GetXmlElement()
36 {
37 if (string.IsNullOrEmpty(Value))
38 {
39 return XmlElement.CreateElement(TAG_NAME);
40 }
41 string value = XmlUtils.SanitizeXmlValue(Value);
42 value = ParserUtils.NormalizeNewLines(value);
43 XmlElement element = null;
44 if (Char.IsWhiteSpace(value, 0) || Char.IsWhiteSpace(value, value.Length - 1))
45 {
46 element = XmlElement.CreateElementWithAttribute(TAG_NAME, PRESERVE_ATTRIBUTE_NAME, PRESERVE_ATTRIBUTE_VALUE, "", PRESERVE_ATTRIBUTE_PREFIX_NAME);
47 }
48 else
49 {
50 element = XmlElement.CreateElement(TAG_NAME);
51 }
52 element.InnerValue = value;
53 return element;
54 }
55
60 public PlainText(string value)
61 {
62 this.Value = value;
63 }
64
70 public override bool Equals(object obj)
71 {
72 if (this.Value == null && obj == null || (this.Value == null && ((PlainText)obj).Value == null))
73 {
74 return true;
75 }
76 else if (this.Value != null && !(obj is PlainText) || this.Value == null && ((PlainText)obj).Value != null)
77 {
78 return false;
79 }
80 return this.Value.Equals(((PlainText)obj).Value, StringComparison.Ordinal);
81 }
82
87 public override int GetHashCode()
88 {
89 if (this.Value == null)
90 {
91 return 0;
92 }
93 return this.Value.GetHashCode();
94 }
95
96 }
97}