protected void AddFromAttribute <T>(XmlNode node, string attrName, string entryName, bool throwIfMissing, Dictionary <string, object> dict = null) { var attr = node.Attributes[attrName]; if (attr == null) { if (throwIfMissing) { throw KmlParseException.MissingRequiredAttribute(attrName, node); } else { return; } } if (dict == null) { Properties.Add(entryName, ParseValue <T>(attr.Value)); } else { dict.Add(entryName, ParseValue <T>(attr.Value)); } }
public static List <KModule> SubmodulesNodeToKModuleList(XmlNode submodulesNode) { var list = new List <KModule>(); foreach (XmlNode node in submodulesNode.ChildNodes) { // ignore comment nodes if (node.NodeType == XmlNodeType.Comment) { continue; } if (node.Name == "repeat") { var timesAttr = node.Attributes["times"]; if (timesAttr == null) { throw KmlParseException.MissingRequiredAttribute("times", node); } int repeatCount; if (int.TryParse(timesAttr.Value, out repeatCount)) { // add multiple times for (int i = 0; i < repeatCount; i++) { list.AddRange(SubmodulesNodeToKModuleList(node)); } } else { throw KmlParseException.InvalidRepeatValue(timesAttr.Value); } } else { // add normal node list.Add(ModuleNodeToKModule(node)); } var repeatAttr = node.Attributes["repeat"]; if (repeatAttr != null) { } else { } } return(list); }
/// <summary> /// Read the value of <paramref name="attrName"/> of <paramref name="node"/>, /// parse it to the given type using <see cref="ParseValue{T}(string)"/>, /// then add the value to <see cref="Properties"></see>.<br/> /// After that, search <paramref name="node"/> for a formula or global node with attr = <paramref name="attrName"/>. /// If found, add formulas to <see cref="InternalFormulas"/> and globals to <see cref="InternalGlobals"/>, /// then set <see cref="InternalToggles"/>[<paramref name="entryName"/>] to 0 for normal value, 10 for formula and 100 for global.<br/> /// Throws when <paramref name="attrName"/> value cannot be parsed or when the attribute is not found and <paramref name="throwIfMissing"/> is true. /// </summary> /// <typeparam name="T">The type to convert the attribute value to.</typeparam> /// <param name="node">The node to read attribute value and subnodes from.</param> /// <param name="attrName">The attribute name.</param> /// <param name="entryName">The key to add to dictionaries.</param> /// <exception cref="KmlParseException"></exception> protected void AddAttrValueFormulaGlobal <T>(XmlNode node, string attrName, string entryName, bool throwIfMissing, Func <T, T> transform = null) { var attr = node.Attributes[attrName]; var globalNode = node.SelectSingleNode($"global[@attr='{attrName}']"); var formulaNode = node.SelectSingleNode($"formula[@attr='{attrName}']"); if (attr == null) { if (throwIfMissing) { throw KmlParseException.MissingRequiredAttribute(attrName, node); } else { return; } } if (attr != null) { if (transform == null) { Properties.Add(entryName, ParseValue <T>(attr.Value)); } else { Properties.Add(entryName, transform((T)ParseValue <T>(attr.Value))); } } if (globalNode != null && formulaNode != null) { throw KmlParseException.CannotSetToFormulaAndGlobal(attrName, node); } if (globalNode != null) { InternalToggles.Add(entryName, 100); AddFromAttribute(globalNode, "global", entryName, true, InternalGlobals); } if (formulaNode != null) { InternalToggles.Add(entryName, 10); AddFromContent(formulaNode, entryName, InternalFormulas); } }
public static KTouchEvent CreateFromEventNode(XmlNode eventNode) { if (eventNode.Name != "event") { throw KmlParseException.ExpectedDifferentNode("event", eventNode); } var ev = new KTouchEvent(); var typeAttr = eventNode.Attributes["type"]; if (typeAttr == null) { throw KmlParseException.MissingRequiredAttribute("type", eventNode); } ev.Type = typeAttr.Value; var actionAttr = eventNode.Attributes["action"]; if (actionAttr == null) { throw KmlParseException.MissingRequiredAttribute("action", eventNode); } ev.Action = actionAttr.Value; var globalAttr = eventNode.Attributes["global"]; if (globalAttr != null) { ev.Switch = globalAttr.Value; } ev.SwitchText = eventNode.InnerText.Trim(); return(ev); }
public static KAnimationKeyframe CreateFromNode(XmlNode node) { if (node.Name != "keyframe") { throw KmlParseException.ExpectedDifferentNode("keyframe", node); } var keyframe = new KAnimationKeyframe(); var propertyAttr = node.Attributes["property"]; if (propertyAttr == null) { throw KmlParseException.MissingRequiredAttribute("property", node); } else { keyframe.Property = propertyAttr.Value; } double dbl; var positionAttr = node.Attributes["position"]; if (positionAttr == null) { throw KmlParseException.MissingRequiredAttribute("position", node); } else { if (double.TryParse(positionAttr.Value, out dbl)) { keyframe.Position = dbl; } else { throw KmlParseException.CouldNotParseValueAsType(positionAttr.Value, typeof(double)); } } var valueAttr = node.Attributes["value"]; if (valueAttr == null) { throw KmlParseException.MissingRequiredAttribute("value", node); } else { if (double.TryParse(valueAttr.Value, out dbl)) { keyframe.Value = dbl; } else { throw KmlParseException.CouldNotParseValueAsType(valueAttr.Value, typeof(double)); } } // ease is the only non-required attribute here var easeAttr = node.Attributes["ease"]; if (easeAttr != null) { keyframe.Ease = easeAttr.Value; } return(keyframe); }