예제 #1
0
        private void AppendErrorDescription(
            IEnumerable <XElement> errors,
            StringBuilder description,
            bool needsNewLine)
        {
            int errorCount = 0;

            foreach (XElement error in errors)
            {
                XAttribute?code = error.Attribute(_code);
                if (code is {   } &&
예제 #2
0
 /// <summary>
 /// XML 属性の値を int として取得する
 /// </summary>
 /// <param name="attr">値を取得する XML 属性</param>
 /// <returns>変換済みの属性値</returns>
 /// <exception cref="XmlFormatException">属性が無い、または無効な値の場合</exception>
 public static int GetInt(this XAttribute?attr)
 {
     try
     {
         var value = attr?.Value ?? throw XmlFormatException.CreateFrom(attr);
         return(int.Parse(value, CultureInfo.InvariantCulture));
     }
     catch (SystemException exception)
     {
         throw XmlFormatException.CreateFrom(attr, exception);
     }
 }
예제 #3
0
        public static T?GetAttributeValue <T>(this XElement element, string name) where T : struct
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            XAttribute?attribute = element.Attribute(name);

            if (attribute == null)
            {
                return(default);
        private static bool AttributeEquals(XAttribute?source, XAttribute?target)
        {
            if (source is null && target is null)
            {
                return(true);
            }

            if (source is null || target is null)
            {
                return(false);
            }
            return(source.Name == target.Name && source.Value == target.Value);
        }
예제 #5
0
        public static void UpdateAttribute([NotNull] this XElement element, string attributeName, [NotNull] string value)
        {
            XAttribute?att = element.Attributes(attributeName).FirstOrDefault();

            if (att is null)
            {
                element.Add(new XAttribute(attributeName, value));
            }
            else
            {
                att.Value = value;
            }
        }
예제 #6
0
 protected override void OnChanging(object?sender, XObjectChangeEventArgs args)
 {
     if (_value == null)
     {
         return;
     }
     switch (args.ObjectChange)
     {
     case XObjectChange.Remove:
         XAttribute?a = sender as XAttribute;
         _changeState = a != null && _value.element == a.Parent && _value.name == a.Name ? a : null;
         break;
     }
 }
예제 #7
0
        public override bool ReadAttributeValue()
        {
            if (!IsInteractive)
            {
                return(false);
            }
            XAttribute?a = _source as XAttribute;

            if (a != null)
            {
                return(ReadIntoAttribute(a));
            }
            return(false);
        }
예제 #8
0
        private static XAttribute?GetFirstNamespaceDeclarationGlobal(XElement e)
        {
            XElement?ce = e;

            do
            {
                XAttribute?a = GetFirstNamespaceDeclarationLocal(ce);
                if (a != null)
                {
                    return(a);
                }
                ce = ce.Parent;
            } while (ce != null);
            return(null);
        }
예제 #9
0
        public override string?GetAttribute(string name)
        {
            if (!IsInteractive)
            {
                return(null);
            }
            XElement?e = GetElementInAttributeScope();

            if (e != null)
            {
                string?localName, namespaceName;
                GetNameInAttributeScope(name, e, out localName, out namespaceName);
                XAttribute?a = e.lastAttr;
                if (a != null)
                {
                    do
                    {
                        a = a.next !;
                        if (a.Name.LocalName == localName && a.Name.NamespaceName == namespaceName)
                        {
                            if (_omitDuplicateNamespaces && IsDuplicateNamespaceAttribute(a))
                            {
                                return(null);
                            }
                            else
                            {
                                return(a.Value);
                            }
                        }
                    } while (a != e.lastAttr);
                }
                return(null);
            }
            XDocumentType?n = _source as XDocumentType;

            if (n != null)
            {
                switch (name)
                {
                case "PUBLIC":
                    return(n.PublicId);

                case "SYSTEM":
                    return(n.SystemId);
                }
            }
            return(null);
        }
예제 #10
0
 public static string GetAttributeValue(XElement parent, string name)
 {
     if (parent == null)
     {
         throw new Exception($"A null parent was passed when attempting to get attribute '{name}'");
     }
     else
     {
         XAttribute?attr = parent.Attribute(name);
         if (attr != null)
         {
             return(attr.Value.Trim());
         }
     }
     return(string.Empty);
 }
예제 #11
0
        /// <summary>
        /// Returns attribute value, or null if not present
        /// </summary>
        public static string GetAttributeValue(this XElement element, string name)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            XAttribute?attribute = element.Attribute(name);

            if (attribute == null)
            {
                return(string.Empty);
            }

            return(attribute.Value);
        }
예제 #12
0
        public override bool MoveToAttribute(string localName, string?namespaceName)
        {
            if (!IsInteractive)
            {
                return(false);
            }
            XElement?e = GetElementInAttributeScope();

            if (e != null)
            {
                if (localName == "xmlns")
                {
                    if (namespaceName != null && namespaceName.Length == 0)
                    {
                        return(false);
                    }
                    if (namespaceName == XNamespace.xmlnsPrefixNamespace)
                    {
                        namespaceName = string.Empty;
                    }
                }
                XAttribute?a = e.lastAttr;
                if (a != null)
                {
                    do
                    {
                        a = a.next !;
                        if (a.Name.LocalName == localName &&
                            a.Name.NamespaceName == namespaceName)
                        {
                            if (_omitDuplicateNamespaces && IsDuplicateNamespaceAttribute(a))
                            {
                                // If it's a duplicate namespace attribute just act as if it doesn't exist
                                return(false);
                            }
                            else
                            {
                                _source = a;
                                _parent = null;
                                return(true);
                            }
                        }
                    } while (a != e.lastAttr);
                }
            }
            return(false);
        }
예제 #13
0
        private void PushElement(XElement e)
        {
            _resolver.PushScope();
            XAttribute?a = e.lastAttr;

            if (a != null)
            {
                do
                {
                    a = a.next !;
                    if (a.IsNamespaceDeclaration)
                    {
                        _resolver.Add(a.Name.NamespaceName.Length == 0 ? string.Empty : a.Name.LocalName, XNamespace.Get(a.Value));
                    }
                } while (a != e.lastAttr);
            }
        }
예제 #14
0
        public override bool MoveToNextNamespace(XPathNamespaceScope scope)
        {
            XAttribute?a = _source as XAttribute;

            if (a != null && _parent != null && !IsXmlNamespaceDeclaration(a))
            {
                switch (scope)
                {
                case XPathNamespaceScope.Local:
                    if (a.GetParent() != _parent)
                    {
                        return(false);
                    }
                    a = GetNextNamespaceDeclarationLocal(a);
                    break;

                case XPathNamespaceScope.ExcludeXml:
                    do
                    {
                        a = GetNextNamespaceDeclarationGlobal(a);
                    } while (a != null &&
                             (a.Name.LocalName == "xml" ||
                              HasNamespaceDeclarationInScope(a, _parent)));
                    break;

                case XPathNamespaceScope.All:
                    do
                    {
                        a = GetNextNamespaceDeclarationGlobal(a);
                    } while (a != null &&
                             HasNamespaceDeclarationInScope(a, _parent));
                    if (a == null &&
                        !HasNamespaceDeclarationInScope(GetXmlNamespaceDeclaration(), _parent))
                    {
                        a = GetXmlNamespaceDeclaration();
                    }
                    break;
                }
                if (a != null)
                {
                    _source = a;
                    return(true);
                }
            }
            return(false);
        }
예제 #15
0
		public static T GetAttributeValue<T>(
			this XElement element,
			XName attributeName,
			T defaultValue,
			Func<string, T> converter)
		{
			T result = defaultValue;

			XAttribute? attr = element.Attribute(attributeName);
			if (attr != null)
			{
				string value = attr.Value;
				result = converter(value);
			}

			return result;
		}
예제 #16
0
        private void SetupSeasonRules([NotNull] XElement xmlSettings)
        {
            foreach (XElement rulesSet in xmlSettings.Descendants("Rules"))
            {
                XAttribute?value = rulesSet.Attribute("SeasonNumber");
                if (value is null)
                {
                    continue;
                }

                int snum = int.Parse(value.Value);
                SeasonRules[snum] = new List <ShowRule>();

                foreach (XElement ruleData in rulesSet.Descendants("Rule"))
                {
                    SeasonRules[snum].Add(new ShowRule(ruleData));
                }
            }
        }
예제 #17
0
        public override string?GetAttribute(string localName, string?namespaceName)
        {
            if (!IsInteractive)
            {
                return(null);
            }
            XElement?e = GetElementInAttributeScope();

            if (e != null)
            {
                if (localName == "xmlns")
                {
                    if (namespaceName != null && namespaceName.Length == 0)
                    {
                        return(null);
                    }
                    if (namespaceName == XNamespace.xmlnsPrefixNamespace)
                    {
                        namespaceName = string.Empty;
                    }
                }
                XAttribute?a = e.lastAttr;
                if (a != null)
                {
                    do
                    {
                        a = a.next !;
                        if (a.Name.LocalName == localName && a.Name.NamespaceName == namespaceName)
                        {
                            if (_omitDuplicateNamespaces && IsDuplicateNamespaceAttribute(a))
                            {
                                return(null);
                            }
                            else
                            {
                                return(a.Value);
                            }
                        }
                    } while (a != e.lastAttr);
                }
            }
            return(null);
        }
예제 #18
0
        /// <summary>
        /// Removes each <see cref="XAttribute"/> represented in this <see cref="IEnumerable"/> of
        /// <see cref="XAttribute"/>.  Note that this method uses snapshot semantics (copies the
        /// attributes to an array before deleting each).
        /// </summary>
        public static void Remove(this IEnumerable <XAttribute?> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            int count;

            XAttribute?[] attributes = EnumerableHelpers.ToArray(source, out count);
            for (int i = 0; i < count; i++)
            {
                XAttribute?a = attributes[i];
                if (a != null)
                {
                    a.Remove();
                }
            }
        }
예제 #19
0
        private void WriteStartElement(XElement e)
        {
            PushElement(e);
            XNamespace ns = e.Name.Namespace;

            _writer.WriteStartElement(GetPrefixOfNamespace(ns, true), e.Name.LocalName, ns.NamespaceName);
            XAttribute?a = e.lastAttr;

            if (a != null)
            {
                do
                {
                    a  = a.next !;
                    ns = a.Name.Namespace;
                    string localName     = a.Name.LocalName;
                    string namespaceName = ns.NamespaceName;
                    _writer.WriteAttributeString(GetPrefixOfNamespace(ns, false), localName, namespaceName.Length == 0 && localName == "xmlns" ? XNamespace.xmlnsPrefixNamespace : namespaceName, a.Value);
                } while (a != e.lastAttr);
            }
        }
예제 #20
0
        private async Task WriteStartElementAsync(XElement e, CancellationToken cancellationToken)
        {
            PushElement(e);
            XNamespace ns = e.Name.Namespace;
            await _writer.WriteStartElementAsync(GetPrefixOfNamespace(ns, true), e.Name.LocalName, ns.NamespaceName).ConfigureAwait(false);

            XAttribute?a = e.lastAttr;

            if (a != null)
            {
                do
                {
                    a  = a.next !;
                    ns = a.Name.Namespace;
                    string localName     = a.Name.LocalName;
                    string namespaceName = ns.NamespaceName;
                    await _writer.WriteAttributeStringAsync(GetPrefixOfNamespace(ns, false), localName, namespaceName.Length == 0 && localName == "xmlns"?XNamespace.xmlnsPrefixNamespace : namespaceName, a.Value).ConfigureAwait(false);
                } while (a != e.lastAttr);
            }
        }
예제 #21
0
        private string GetNamespaceURI()
        {
            XElement?e = _source as XElement;

            if (e != null)
            {
                return(e.Name.NamespaceName);
            }
            XAttribute?a = _source as XAttribute;

            if (a != null)
            {
                if (_parent != null)
                {
                    return(string.Empty); // backcompat
                }
                return(a.Name.NamespaceName);
            }
            return(string.Empty);
        }
예제 #22
0
        /// <summary>
        /// Adds an element to the array collection.
        /// </summary>
        /// <param name="element">The <see cref="XElement"/> to be added.</param>
        public void AddElement(XElement element)
        {
            if (element is null)
            {
                throw new System.ArgumentNullException(nameof(element));
            }

            string?indexValue   = element.Attribute("index")?.Value ?? element.Element("index")?.Attribute("value")?.Value;
            string?removedValue = element.Attribute("removed")?.Value ?? element.Element("removed")?.Attribute("value")?.Value;

            if (int.TryParse(indexValue, out int indexResult) && _xElementByIndex.TryGetValue(indexResult, out XElement? existingElement) && string.IsNullOrEmpty(removedValue))
            {
                foreach (XAttribute attribute in existingElement.Attributes())
                {
                    XAttribute?currentAttribute = element.Attribute(attribute.Name.LocalName);
                    if (currentAttribute == null)
                    {
                        element.Add(attribute);
                    }
                    else
                    {
                        element.SetAttributeValue(attribute.Name.LocalName, currentAttribute.Value);
                    }
                }

                _xElementByIndex[indexResult] = element;
            }
            else if (int.TryParse(removedValue, out int removedResult) && removedResult == 1)
            {
                _xElementByIndex.Remove(indexResult);
            }
            else
            {
                if (_xElementByIndex.ContainsKey(MaxIndex))
                {
                    MaxIndex++;
                }

                _xElementByIndex.Add(MaxIndex, element);
            }
        }
예제 #23
0
        private static XAttribute?GetNextNamespaceDeclarationLocal(XAttribute a)
        {
            XElement?e = a.Parent;

            if (e == null)
            {
                return(null);
            }
            XAttribute?ca = a;

            ca = ca.NextAttribute;
            while (ca != null)
            {
                if (ca.IsNamespaceDeclaration)
                {
                    return(ca);
                }
                ca = ca.NextAttribute;
            }
            return(null);
        }
예제 #24
0
        private static XAttribute?GetNextNamespaceDeclarationGlobal(XAttribute a)
        {
            XElement?e = (XElement?)a.GetParent();

            if (e == null)
            {
                return(null);
            }
            XAttribute?next = GetNextNamespaceDeclarationLocal(a);

            if (next != null)
            {
                return(next);
            }
            e = e.Parent;
            if (e == null)
            {
                return(null);
            }
            return(GetFirstNamespaceDeclarationGlobal(e));
        }
예제 #25
0
        public override bool MoveToNextAttribute()
        {
            XAttribute?currentAttribute = _source as XAttribute;

            if (currentAttribute != null && _parent == null)
            {
                XElement?e = (XElement?)currentAttribute.GetParent();
                if (e != null)
                {
                    for (XAttribute?attribute = currentAttribute.NextAttribute; attribute != null; attribute = attribute.NextAttribute)
                    {
                        if (!attribute.IsNamespaceDeclaration)
                        {
                            _source = attribute;
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
예제 #26
0
 private static IEnumerable <XAttribute> GetAttributes(IEnumerable <XElement?> source, XName?name)
 {
     foreach (XElement?e in source)
     {
         if (e != null)
         {
             XAttribute?a = e.lastAttr;
             if (a != null)
             {
                 do
                 {
                     a = a.next !;
                     if (name == null || a.name == name)
                     {
                         yield return(a);
                     }
                 } while (a.parent == e && a != e.lastAttr);
             }
         }
     }
 }
예제 #27
0
        public override bool MoveToElement()
        {
            if (!IsInteractive)
            {
                return(false);
            }

            XAttribute?a =
                _source as XAttribute ??
                _parent as XAttribute;

            if (a != null)
            {
                if (a.parent != null)
                {
                    _source = a.parent;
                    _parent = null;
                    return(true);
                }
            }
            return(false);
        }
예제 #28
0
        /// <summary>
        /// Gets the specified attribute's value or a default value if the attribute isn't present.
        /// </summary>
        /// <param name="element">The element to get the attribute from.</param>
        /// <param name="name">The name of the attribute to read.</param>
        /// <param name="defaultValue">The value to return if the attribute isn't found.</param>
        /// <param name="useDefaultIfEmptyValue">If the attribute is present but with an empty value,
        /// then this parameter determines whether the <paramref name="defaultValue"/> should be
        /// returned (if true) or the actual, empty attribute value should be returned (if false).
        /// Normally, this should be true, but false is useful if you need to allow the user to explicitly
        /// set an empty attribute value to override a non-empty default value.
        /// </param>
        /// <returns>The value of the attribute, or the default value if the attribute isn't found.</returns>
        public static string?GetAttributeValueN(this XElement element, XName name, string?defaultValue, bool useDefaultIfEmptyValue)
        {
            // It's ok if defaultValue is null.
            Conditions.RequireReference(element, nameof(element));
            Conditions.RequireReference(name, nameof(name));

            string?result = defaultValue;

            XAttribute?attr = element.Attribute(name);

            if (attr != null)
            {
                result = attr.Value;

                if (useDefaultIfEmptyValue && string.IsNullOrEmpty(result))
                {
                    result = defaultValue;
                }
            }

            return(result);
        }
예제 #29
0
        public override bool MoveToFirstNamespace(XPathNamespaceScope scope)
        {
            XElement?e = _source as XElement;

            if (e != null)
            {
                XAttribute?a = null;
                switch (scope)
                {
                case XPathNamespaceScope.Local:
                    a = GetFirstNamespaceDeclarationLocal(e);
                    break;

                case XPathNamespaceScope.ExcludeXml:
                    a = GetFirstNamespaceDeclarationGlobal(e);
                    while (a != null && a.Name.LocalName == "xml")
                    {
                        a = GetNextNamespaceDeclarationGlobal(a);
                    }
                    break;

                case XPathNamespaceScope.All:
                    a = GetFirstNamespaceDeclarationGlobal(e);
                    if (a == null)
                    {
                        a = GetXmlNamespaceDeclaration();
                    }
                    break;
                }
                if (a != null)
                {
                    _source = a;
                    _parent = e;
                    return(true);
                }
            }
            return(false);
        }
예제 #30
0
        public override bool MoveToAttribute(string name)
        {
            if (!IsInteractive)
            {
                return(false);
            }
            XElement?e = GetElementInAttributeScope();

            if (e != null)
            {
                string?localName, namespaceName;
                GetNameInAttributeScope(name, e, out localName, out namespaceName);
                XAttribute?a = e.lastAttr;
                if (a != null)
                {
                    do
                    {
                        a = a.next !;
                        if (a.Name.LocalName == localName &&
                            a.Name.NamespaceName == namespaceName)
                        {
                            if (_omitDuplicateNamespaces && IsDuplicateNamespaceAttribute(a))
                            {
                                // If it's a duplicate namespace attribute just act as if it doesn't exist
                                return(false);
                            }
                            else
                            {
                                _source = a;
                                _parent = null;
                                return(true);
                            }
                        }
                    } while (a != e.lastAttr);
                }
            }
            return(false);
        }