protected virtual IEnumerable <XmlConnectionGroup> ReadConnectionGroup(ComponentDescription description, XElement connectionGroupElement, XmlConnectionGroup parentGroup) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = connectionGroupElement.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { yield break; } } var connectionGroup = new XmlConnectionGroup(new ConditionTree(ConditionTree.ConditionOperator.AND, parentGroup.Conditions, conditionCollection)); autoRotateOptionsReader.TrySetAutoRotateOptions(connectionGroupElement, parentGroup, connectionGroup); var childGroups = connectionGroupElement.Elements().SelectMany(x => ReadElement(x, description, connectionGroup)); yield return(connectionGroup); foreach (var child in childGroups) { yield return(child); } }
public void ReadSection(XElement connectionsElement, ComponentDescription description) { List <ConnectionGroup> parsedConnectionGroups = new List <ConnectionGroup>(); var connectionGroupNodes = connectionsElement.Elements(XmlLoader.ComponentNamespace + "group"); foreach (var connectionGroupNode in connectionGroupNodes) { IConditionTreeItem conditionCollection = ConditionTree.Empty; List <ConnectionDescription> connections = new List <ConnectionDescription>(); var conditionsAttribute = connectionGroupNode.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { continue; } } foreach (var connectionNode in connectionGroupNode.Elements(XmlLoader.ComponentNamespace + "connection")) { ConnectionEdge edge = ConnectionEdge.None; if (connectionNode.Attribute("edge") != null) { string edgeText = connectionNode.Attribute("edge").Value.ToLowerInvariant(); if (edgeText == "start") { edge = ConnectionEdge.Start; } else if (edgeText == "end") { edge = ConnectionEdge.End; } else if (edgeText == "both") { edge = ConnectionEdge.Both; } } string connectionName = "#"; if (connectionNode.Attribute("name") != null) { connectionName = connectionNode.Attribute("name").Value; } if (!componentPointParser.TryParse(connectionNode.Attribute("start"), out var start) || !componentPointParser.TryParse(connectionNode.Attribute("end"), out var end)) { continue; } connections.Add(new ConnectionDescription(start, end, edge, connectionName)); } parsedConnectionGroups.Add(new ConnectionGroup(conditionCollection, connections.ToArray())); } description.Connections = parsedConnectionGroups.ToArray(); }
protected virtual RenderDescription ReadRenderGroup(ComponentDescription description, XElement renderNode) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = renderNode.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { return(null); } } var commands = new List <IRenderCommand>(); foreach (var renderCommandNode in renderNode.Descendants()) { string commandType = renderCommandNode.Name.LocalName; if (commandType == "line") { if (ReadLineCommand(renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "rect") { if (ReadRectCommand(renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "ellipse") { if (ReadEllipseCommand(description.Metadata.FormatVersion, renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "text") { if (ReadTextCommand(renderCommandNode, description, out var command)) { commands.Add(command); } } else if (commandType == "path") { if (ReadPathCommand(renderCommandNode, out var command)) { commands.Add(command); } } } return(new RenderDescription(conditionCollection, commands.ToArray())); }
public IAttribute?Parse(XmlReader reader, IParsingContext parsingContext) { var result = conditionParser.Parse(reader.Value); if (result.Condition is not null) { return(new WhenAttribute(result.Condition)); } parsingContext.LogError(reader, "Condição 'when' inválida. " + result.Message); return(null); }
public async Task ShouldUpdateParentContextEnabledWhen() { var reader = A.Dummy <XmlReader>(); var context = A.Fake <IParsingContext>(i => i.Strict()); var parsedCondition = A.Dummy <IParsedCondition>(); A.CallTo(() => conditionParser.Parse("condição")).Returns(parsedCondition); var optionNode = A.Fake <IChoiceOptionNodeSetter>(i => i.Strict()); A.CallToSet(() => optionNode.EnabledWhen).To(parsedCondition.Condition).DoesNothing(); var parentContext = A.Fake <IChoiceOptionParentParsingContext>(i => i.Strict()); A.CallTo(() => parentContext.Option).Returns(optionNode); A.CallTo(() => elementParser.ParseAsync(reader, context, A <IParentParsingContext> .Ignored, sut.Settings)) .Invokes(i => i.GetArgument <IParentParsingContext>(2).ParsedText = "condição"); await sut.ParseAsync(reader, context, parentContext); A.CallToSet(() => optionNode.EnabledWhen).To(parsedCondition.Condition).MustHaveHappenedOnceExactly(); }
public IConditionTreeItem Parse(ComponentDescription description, string input) { var version = description.Metadata.FormatVersion; if (version < new Version(1, 1)) { return(new LegacyConditionParser(description).Parse(description, input)); } if (version == new Version(1, 1)) { return(parserV11.Parse(description, input)); } return(parserLatest.Parse(description, input)); }
public static bool Parse(this IConditionParser parser, XAttribute conditionsAttribute, ComponentDescription description, IXmlLoadLogger logger, out IConditionTreeItem value) { try { value = parser.Parse(description, conditionsAttribute.Value); return(true); } catch (ConditionFormatException ex) { IXmlLineInfo line = conditionsAttribute; int startCol = line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart; var position = new FileRange(line.LineNumber, startCol, line.LineNumber, startCol + ex.Length); logger.Log(LogLevel.Error, position, ex.Message, null); value = null; return(false); } }
public async Task ParseAsync(XmlReader reader, IParsingContext parsingContext, IParentParsingContext parentParsingContext) { var myContext = new TextParentParsingContext(); await elementParser.ParseAsync(reader, parsingContext, myContext, Settings); if (myContext.ParsedText is null) { return; } var result = conditionParser.Parse(myContext.ParsedText); if (result.Condition is null) { parsingContext.LogError(reader, "Condição EnabledWhen inválida. " + result.Message); return; } var ctx = (IChoiceOptionParentParsingContext)parentParsingContext; ctx.Option.EnabledWhen = result.Condition; }
public void ReadSection(XElement element, ComponentDescription description) { var definitions = new Dictionary <string, ConditionalCollection <string> >(); foreach (var definitionNode in element.Elements(XmlLoader.ComponentNamespace + "def")) { if (!definitionNode.GetAttributeValue("name", logger, out var name)) { continue; } var values = new ConditionalCollection <string>(); foreach (var valueWhen in definitionNode.Elements(XmlLoader.ComponentNamespace + "when")) { valueWhen.GetAttribute("conditions", logger, out var conditionsAttribute); valueWhen.GetAttributeValue("value", logger, out var whenValue); conditionParser.Parse(conditionsAttribute, description, logger, out var conditions); values.Add(new Conditional <string>(whenValue, conditions)); } definitions.Add(name, values); } foreach (var constNode in element.Elements(XmlLoader.ComponentNamespace + "const")) { if (!constNode.GetAttributeValue("name", logger, out var name) || !constNode.GetAttributeValue("value", logger, out var value)) { continue; } var values = new ConditionalCollection <string>(); values.Add(new Conditional <string>(value, ConditionTree.Empty)); definitions.Add(name, values); } sectionRegistry.RegisterSection(new DefinitionsSection(definitions)); }
protected IEnumerable <RenderDescription> ReadRenderDescriptions(ComponentDescription description, XElement renderNode) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = renderNode.Attribute("conditions"); if (conditionsAttribute != null) { if (!conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection)) { yield break; } } var declaredDefinitions = new List <string>(); var whenDefinedAttribute = renderNode.Attribute("whenDefined"); if (whenDefinedAttribute != null) { var usedVariables = whenDefinedAttribute.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); declaredDefinitions.AddRange(usedVariables.Select(x => x.Substring(1))); } var allUndefined = declaredDefinitions.Except(availableDefinitions).ToHashSet(); if (allUndefined.Any()) { foreach (var undefined in allUndefined) { logger.LogError(whenDefinedAttribute, $"Usage of undefined variable: ${undefined}"); } yield break; } var commands = new List <IRenderCommand>(); foreach (var renderCommandNode in renderNode.Descendants()) { string commandType = renderCommandNode.Name.LocalName; if (commandType == "line") { foreach (var renderLineDescription in ReadLineCommand(renderCommandNode, conditionCollection, declaredDefinitions)) { yield return(renderLineDescription); } } else if (commandType == "rect") { foreach (var renderRectDescription in ReadRectCommand(renderCommandNode, conditionCollection, declaredDefinitions)) { yield return(renderRectDescription); } } else if (commandType == "ellipse") { if (ReadEllipseCommand(description.Metadata.Version, renderCommandNode, out var command)) { commands.Add(command); } } else if (commandType == "text") { foreach (var renderTextDescription in ReadTextCommand(renderCommandNode, description, conditionCollection, declaredDefinitions)) { yield return(renderTextDescription); } } else if (commandType == "path") { if (ReadPathCommand(renderCommandNode, out var command)) { commands.Add(command); } } } yield return(new RenderDescription(conditionCollection, commands.ToArray())); }
private ComponentProperty ReadPropertyNode(ComponentDescription description, XElement propertyElement) { string propertyName = propertyElement.Attribute("name").Value; string type = propertyElement.Attribute("type").Value; string defaultValue = propertyElement.Attribute("default").Value; string serializeAs = propertyElement.Attribute("serialize").Value; string display = propertyElement.Attribute("display").Value; PropertyType propertyType; switch (type.ToLowerInvariant()) { case "double": // TODO: Add warning case "decimal": propertyType = PropertyType.Decimal; break; case "int": propertyType = PropertyType.Integer; break; case "bool": propertyType = PropertyType.Boolean; break; default: propertyType = PropertyType.String; break; } var propertyDefaultValue = PropertyValue.Parse(defaultValue, propertyType.ToPropertyType()); List <string> propertyOptions = null; if (type == "enum") { propertyOptions = new List <string>(); var optionNodes = propertyElement.Elements(propertyElement.GetDefaultNamespace() + "option"); foreach (var optionNode in optionNodes) { propertyOptions.Add(optionNode.Value); } } List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>(); if (propertyElement.Attribute("format") != null) { formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty)); } else { var formatRuleNodes = propertyElement.Elements(propertyElement.GetDefaultNamespace() + "formatting") .SelectMany(x => x.Elements(propertyElement.GetDefaultNamespace() + "format")); foreach (var formatNode in formatRuleNodes) { IConditionTreeItem conditionCollection = ConditionTree.Empty; var conditionsAttribute = formatNode.Attribute("conditions"); if (conditionsAttribute != null) { conditionParser.Parse(conditionsAttribute, description, logger, out conditionCollection); } formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection)); } } Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>(); var otherConditionsNodes = propertyElement.Elements(propertyElement.GetDefaultNamespace() + "other") .SelectMany(x => x.Elements(propertyElement.GetDefaultNamespace() + "conditions")); foreach (var otherConditionNode in otherConditionsNodes) { if (otherConditionNode?.Attribute("for") != null && otherConditionNode.Attribute("value") != null) { string conditionsFor = otherConditionNode.Attribute("for").Value; if (!conditionParser.Parse(otherConditionNode.Attribute("value"), description, logger, out var conditionCollection)) { continue; } if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor)) { otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection); } } } ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray())); return(property); }