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 ITEM_TAG_NAME = "si";
22 private const string TEXT_TAG_NAME = "t";
23 private const string PRESERVE_ATTRIBUTE_NAME = "space";
24 private const string PRESERVE_ATTRIBUTE_PREFIX_NAME = "xml";
25 private const string PRESERVE_ATTRIBUTE_VALUE = "preserve";
26
30 public string Value { private get; set; }
31
36 public XmlElement GetXmlElement()
37 {
38 XmlElement siElement = XmlElement.CreateElement(ITEM_TAG_NAME);
39 if (string.IsNullOrEmpty(Value))
40 {
41 siElement.AddChildElement(TEXT_TAG_NAME);
42 return siElement;
43 }
44 string value = XmlUtils.SanitizeXmlValue(Value);
45 value = ParserUtils.NormalizeNewLines(value);
46 XmlElement element = null;
47 if (Char.IsWhiteSpace(value, 0) || Char.IsWhiteSpace(value, value.Length - 1))
48 {
49 element = XmlElement.CreateElementWithAttribute(TEXT_TAG_NAME, PRESERVE_ATTRIBUTE_NAME, PRESERVE_ATTRIBUTE_VALUE, "", PRESERVE_ATTRIBUTE_PREFIX_NAME);
50 }
51 else
52 {
53 element = XmlElement.CreateElement(TEXT_TAG_NAME);
54 }
55 element.InnerValue = value;
56 siElement.AddChildElement(element);
57 return siElement;
58 }
59
64 public PlainText(string value)
65 {
66 this.Value = value;
67 }
68
74 public override bool Equals(object obj)
75 {
76 if (this.Value == null && obj == null || (this.Value == null && ((PlainText)obj).Value == null))
77 {
78 return true;
79 }
80 else if (this.Value != null && !(obj is PlainText) || this.Value == null && ((PlainText)obj).Value != null)
81 {
82 return false;
83 }
84 return this.Value.Equals(((PlainText)obj).Value, StringComparison.Ordinal);
85 }
86
91 public override int GetHashCode()
92 {
93 if (this.Value == null)
94 {
95 return 0;
96 }
97 return this.Value.GetHashCode();
98 }
99
100 }
101}