Skip to content

Commit 139aaf8

Browse files
authored
Separate SimpleTypes into own files (#349)
1 parent bf3cb12 commit 139aaf8

27 files changed

+4389
-4178
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Diagnostics;
6+
7+
namespace DocumentFormat.OpenXml
8+
{
9+
/// <summary>
10+
/// Represents the xsd:base64Binary value for attributes.
11+
/// </summary>
12+
[DebuggerDisplay("{InnerText}")]
13+
public class Base64BinaryValue : OpenXmlSimpleType
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the Base64BinaryValue class.
17+
/// </summary>
18+
public Base64BinaryValue()
19+
: base()
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the Base64BinaryValue class by deep copying
25+
/// the supplied String value.
26+
/// </summary>
27+
/// <param name="base64Binary">
28+
/// The String value.
29+
/// </param>
30+
public Base64BinaryValue(string base64Binary)
31+
: base()
32+
{
33+
this.TextValue = base64Binary;
34+
}
35+
36+
/// <summary>
37+
/// Initializes a new instance of the Base64BinaryValue class by deep copying
38+
/// the supplied Base64BinaryValue class.
39+
/// </summary>
40+
/// <param name="source">
41+
/// The source Base64BinaryValue class.
42+
/// </param>
43+
public Base64BinaryValue(Base64BinaryValue source)
44+
: base(source)
45+
{
46+
if (source == null)
47+
{
48+
throw new ArgumentNullException(nameof(source));
49+
}
50+
}
51+
52+
/// <summary>
53+
/// Gets the base64Binary string value.
54+
/// </summary>
55+
public string Value
56+
{
57+
get { return this.TextValue; }
58+
set { this.TextValue = value; }
59+
}
60+
61+
/// <summary>
62+
/// Implicitly converts the specified value to a String value.
63+
/// </summary>
64+
/// <param name="xmlAttribute">The Base64BinaryValue object to convert.</param>
65+
/// <returns>The base64Binary string. Returns null when xmlAttribute is null.</returns>
66+
public static implicit operator String(Base64BinaryValue xmlAttribute)
67+
{
68+
if (xmlAttribute == null)
69+
{
70+
return null;
71+
}
72+
73+
return ToString(xmlAttribute);
74+
}
75+
76+
/// <summary>
77+
/// Initializes a new instance of a Base64BinaryValue class using the
78+
/// supplied string.
79+
/// </summary>
80+
/// <param name="value">The specified base64Binary value.</param>
81+
/// <returns>A new Base64BinaryValue instance with the value.</returns>
82+
public static implicit operator Base64BinaryValue(String value)
83+
{
84+
return FromString(value);
85+
}
86+
87+
/// <summary>
88+
/// Returns a new Base64BinaryValue object that was created from a String value.
89+
/// </summary>
90+
/// <param name="value">A String value to use to create a new Base64BinaryValue object.</param>
91+
/// <returns>A Base64BinaryValue that corresponds to the value parameter.</returns>
92+
public static Base64BinaryValue FromString(String value)
93+
{
94+
return new Base64BinaryValue(value);
95+
}
96+
97+
/// <summary>
98+
/// Returns the String value representation of a Base64BinaryValue object.
99+
/// </summary>
100+
/// <param name="xmlAttribute">
101+
/// A Base64BinaryValue object used to retrieve a String value representation.
102+
/// </param>
103+
/// <returns>A String value that represents a Base64BinaryValue object.</returns>
104+
public static String ToString(Base64BinaryValue xmlAttribute)
105+
{
106+
if (xmlAttribute == null)
107+
{
108+
throw new InvalidOperationException(ExceptionMessages.ImplicitConversionExceptionOnNull);
109+
}
110+
111+
return xmlAttribute.Value;
112+
}
113+
114+
internal override OpenXmlSimpleType CloneImp()
115+
{
116+
return new Base64BinaryValue(this);
117+
}
118+
}
119+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Xml;
7+
8+
namespace DocumentFormat.OpenXml
9+
{
10+
/// <summary>
11+
/// Represents the Boolean value for attributes.
12+
/// </summary>
13+
[DebuggerDisplay("{InnerText}")]
14+
public class BooleanValue : OpenXmlSimpleValue<Boolean>
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the BooleanValue class.
18+
/// </summary>
19+
public BooleanValue()
20+
: base()
21+
{
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the BooleanValue class using the supplied Boolean value.
26+
/// </summary>
27+
/// <param name="value">The Boolean value.</param>
28+
public BooleanValue(Boolean value)
29+
: base(value)
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the BooleanValue class by deep copying
35+
/// the supplied BooleanValue class.
36+
/// </summary>
37+
/// <param name="source">
38+
/// The source BooleanValue class.
39+
/// </param>
40+
public BooleanValue(BooleanValue source)
41+
: base(source)
42+
{
43+
if (source == null)
44+
{
45+
throw new ArgumentNullException(nameof(source));
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Gets or sets the inner XML text.
51+
/// </summary>
52+
public override string InnerText
53+
{
54+
get
55+
{
56+
if (this.TextValue == null && this.InnerValue.HasValue)
57+
{
58+
// Word use "1", "0"
59+
this.TextValue = this.InnerValue.Value ? "1" : "0";
60+
}
61+
return this.TextValue;
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Convert the text to meaningful value.
67+
/// </summary>
68+
internal override void Parse()
69+
{
70+
this.InnerValue = XmlConvert.ToBoolean(this.TextValue);
71+
}
72+
73+
/// <summary>
74+
/// Convert the text to meaningful value.
75+
/// </summary>
76+
/// <returns></returns>
77+
internal override bool TryParse()
78+
{
79+
Boolean value;
80+
this.InnerValue = null;
81+
82+
try
83+
{
84+
value = XmlConvert.ToBoolean(this.TextValue);
85+
this.InnerValue = value;
86+
return true;
87+
}
88+
catch (FormatException)
89+
{
90+
return false;
91+
}
92+
}
93+
94+
/// <summary>
95+
/// Implicitly converts the specified value to a Boolean value.
96+
/// </summary>
97+
/// <param name="xmlAttribute">The BooleanValue to convert.</param>
98+
/// <returns>The converted Boolean value.</returns>
99+
/// <exception cref="InvalidOperationException">Thrown when xmlAttribute is null.</exception>
100+
public static implicit operator Boolean(BooleanValue xmlAttribute)
101+
{
102+
if (xmlAttribute == null)
103+
{
104+
throw new InvalidOperationException(ExceptionMessages.ImplicitConversionExceptionOnNull);
105+
}
106+
107+
return ToBoolean(xmlAttribute);
108+
}
109+
110+
/// <summary>
111+
/// Initializes a new instance of the BooleanValue class by implicitly
112+
/// converting the supplied Boolean value.
113+
/// </summary>
114+
/// <param name="value">The Boolean value.</param>
115+
/// <returns>A new BooleanValue instance with the value.</returns>
116+
public static implicit operator BooleanValue(Boolean value)
117+
{
118+
return FromBoolean(value);
119+
}
120+
121+
/// <summary>
122+
/// Returns a new BooleanValue object that was created by using the supplied Boolean value.
123+
/// </summary>
124+
/// <param name="value">A Boolean value to use to create a new BooleanValue object.</param>
125+
/// <returns>A BooleanValue that corresponds to the value parameter.</returns>
126+
public static BooleanValue FromBoolean(Boolean value)
127+
{
128+
return new BooleanValue(value);
129+
}
130+
131+
/// <summary>
132+
/// Returns the Boolean representation of a BooleanValue object.
133+
/// </summary>
134+
/// <param name="xmlAttribute">
135+
/// A BooleanValue object to retrieve a Boolean representation.
136+
/// </param>
137+
/// <returns>A Boolean value that represents a BooleanValue object.</returns>
138+
public static Boolean ToBoolean(BooleanValue xmlAttribute)
139+
{
140+
if (xmlAttribute == null)
141+
{
142+
throw new InvalidOperationException(ExceptionMessages.ImplicitConversionExceptionOnNull);
143+
}
144+
145+
return xmlAttribute.Value;
146+
}
147+
148+
internal override OpenXmlSimpleType CloneImp()
149+
{
150+
return new BooleanValue(this);
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)