|
| 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