internal void Parse(ParseResult result, XElement node, string baseUri, bool addImageQuery) { if (!XmlTemplateParser.EnsureNodeOnlyHasElementsAsChildren(result, node)) { throw new IncompleteElementException(); } AttributesHelper attributes = new AttributesHelper(node.Attributes()); ParseKnownAttributes(node, attributes, result, baseUri, addImageQuery); HandleRemainingAttributes(attributes, result); // 0-n children foreach (XElement child in node.Elements()) { try { HandleChild(result, child, baseUri, addImageQuery); } catch (IncompleteElementException) { } } }
internal virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // src is optional var srcAttr = attributes.PopAttribute(ATTR_SRC); if (srcAttr != null) { Uri srcUri; if (Uri.TryCreate(srcAttr.Value, UriKind.RelativeOrAbsolute, out srcUri)) { Src = srcUri; } else { result.AddWarning("Audio src value must be a valid Uri.", XmlTemplateParser.GetErrorPositionInfo(node)); } } // loop is optional, must be bool bool boolean; if (TryParse(result, attributes, ATTR_LOOP, out boolean)) { Loop = boolean; } if (TryParse(result, attributes, ATTR_SILENT, out boolean)) { Silent = boolean; } }
internal virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // id is required XAttribute attrId = attributes.PopAttribute(ATTR_ID); if (attrId == null) { result.AddError("id attribute on action element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Id = attrId.Value; // arguments is required XAttribute attrArguments = attributes.PopAttribute(ATTR_ARGUMENTS); if (attrArguments == null) { result.AddErrorButRenderAllowed("arguments attribute on action element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Arguments = attrArguments.Value; // activationType is optional ActivationType type; if (TryParseEnum(result, attributes, ATTR_ACTIVATIONTYPE, out type)) { this.ActivationType = type; } }
internal virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result, string baseUri, bool addImageQuery) { // src is required XAttribute attrSrc = attributes.PopAttribute(ATTR_IMAGE_SRC); if (attrSrc == null) { result.AddError("src attribute on image element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Src = XmlTemplateParser.ConstructUri(attrSrc.Value, baseUri, addImageQuery); // alt is optional, I don't use it right now either attributes.PopAttribute(ATTR_IMAGE_ALT); // hint-crop is optional HintCrop hintCrop; if (TryParseEnum(result, attributes, ATTR_IMAGE_HINT_CROP, out hintCrop)) { this.HintCrop = hintCrop; } }
internal void Parse(ParseResult result, XElement node) { if (!XmlTemplateParser.EnsureNodeOnlyHasElementsAsChildren(result, node)) { throw new IncompleteElementException(); } AttributesHelper attributes = new AttributesHelper(node.Attributes()); ParseKnownAttributes(node, attributes, result); HandleRemainingAttributes(attributes, result); // 0-n children foreach (XElement child in node.Elements()) { if (!result.IsOkForRender()) { break; } try { HandleChild(result, child); } catch (IncompleteElementException) { } } }
internal void Parse(ParseResult result, XElement node) { if (!XmlTemplateParser.EnsureNodeOnlyHasElementsAsChildren(result, node)) { throw new IncompleteElementException(); } AttributesHelper attributes = new AttributesHelper(node.Attributes()); ParseKnownAttributes(node, attributes, result); HandleRemainingAttributes(attributes, result); }
internal void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // id is required XAttribute attrId = attributes.PopAttribute(ATTR_ID); if (attrId == null) { result.AddError("id attribute on input element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Id = attrId.Value; }
public object Convert(object value, Type targetType, object parameter, string language) { string str = value as string; if (str != null) { var parseResult = new XmlTemplateParser().ParseAdaptiveContent(str, FeatureSet.GetExperimental()); if (parseResult.IsOkForRender()) { return(AdaptiveRenderer.Render(parseResult.AdaptiveContent, new Thickness(0))); } } return(null); }
protected virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // id is required XAttribute attrId = attributes.PopAttribute(ATTR_ID); if (attrId == null) { result.AddError("id attribute on input element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } // type is required InputType type; if (!TryParseEnum(result, attributes, ATTR_TYPE, out type)) { result.AddErrorButRenderAllowed("type attribute on input element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Id = attrId.Value; this.Type = type; // title is optional var attrTitle = attributes.PopAttribute(ATTR_TITLE); if (attrTitle != null) { this.Title = attrTitle.Value; } // placeHolderContent is optional var attrPlaceHolderContent = attributes.PopAttribute(ATTR_PLACEHOLDERCONTENT); if (attrPlaceHolderContent != null) { this.PlaceHolderContent = attrPlaceHolderContent.Value; } // defaultInput is optional var attrDefaultInput = attributes.PopAttribute(ATTR_DEFAULTINPUT); if (attrDefaultInput != null) { this.DefaultInput = attrDefaultInput.Value; } }
static void Main(string[] args) { // ------------------------------------------------------------------------------------ // // Create parsing map from template // ------------------------------------------------------------------------------------ // var mappingConfig = new XmlTemplateMappingConfiguration("@", ":"); var xDoc = XDocument.Load("template_test.xml"); var templateMapBuilder = new TemplateMapBuilder(mappingConfig); Dictionary <string, XAttribute> map = templateMapBuilder.CreateTemplateMap(xDoc.Root); // ------------------------------------------------------------------------------------ // // Parsing document using created map // ------------------------------------------------------------------------------------ // var xmlParser = new XmlTemplateParser(map, mappingConfig); var entity = xmlParser.ParseDocument(XDocument.Load("doc_test.xml")); // ------------------------------------------------------------------------------------ // // Mapping dictionary to concrete entity // ------------------------------------------------------------------------------------ // var internalEntity = new Dictionary <string, object>(); internalEntity.Add("Value", "test_internal"); var internalList = new List <Dictionary <string, object> >(); internalList.Add(internalEntity); internalList.Add(internalEntity); var testEntity = new Dictionary <string, object>(); testEntity.Add("Name", "test"); testEntity.Add("Entity", internalEntity); testEntity.Add("Entities", internalList); var mapper = new DictionaryToEntityMapper(); var outEntity = mapper.Map <TestEntity>(testEntity); // ------------------------------------------------------------------------------------ // }
internal void Parse(ParseResult result, XElement node, string baseUri, bool addImageQuery) { if (!XmlTemplateParser.EnsureNodeOnlyHasElementsAsChildren(result, node)) { throw new IncompleteElementException(); } AttributesHelper attributes = new AttributesHelper(node.Attributes()); ParseKnownAttributes(node, attributes, result, baseUri, addImageQuery); AdaptiveContainer container = new AdaptiveContainer(Context, SupportedFeatures); container.ContinueParsing(result, node, attributes, node.Elements(), baseUri, addImageQuery); Container = container; HandleRemainingAttributes(attributes, result); }
internal virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // content is required XAttribute attrContent = attributes.PopAttribute(ATTR_CONTENT); if (attrContent == null) { result.AddError("content attribute on action element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Content = attrContent.Value; // parse activationType, arguments, state base.ParseActivatableElementAttributes(node, attributes, result); // imageUri is optional var attrImageUri = attributes.PopAttribute(ATTR_IMAGEURI); if (attrImageUri != null) { this.ImageUri = attrImageUri.Value; } // inputId is optional var attrInputId = attributes.PopAttribute(ATTR_HINT_INPUTID); if (attrInputId != null) { this.InputId = attrInputId.Value; } ActionPlacement placement; if (TryParseEnum(result, attributes, ATTR_PLACEMENT, out placement)) { this.Placement = placement; } }
protected virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // id is required XAttribute attrId = attributes.PopAttribute(ATTR_ID); if (attrId == null) { result.AddError("id attribute on input element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } // type is required XAttribute attrContent = attributes.PopAttribute(ATTR_CONTENT); if (attrContent == null) { result.AddErrorButRenderAllowed("content attribute on input element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Id = attrId.Value; this.Content = attrContent.Value; }
internal static ErrorPositionInfo GetErrorPositionInfo(XObject node) { return(XmlTemplateParser.GetErrorPositionInfo(node)); }
internal void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result, string baseUri, bool addImageQuery) { // AddImageQuery is optional { bool val; if (TryParse(result, attributes, ATTR_IMAGE_ADDIMAGEQUERY, out val)) { addImageQuery = val; // Overwrite cascaded value if it was specified AddImageQuery = val; } } // src is required XAttribute attrSrc = attributes.PopAttribute(ATTR_IMAGE_SRC); if (attrSrc == null) { result.AddError("src attribute on image element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Src = XmlTemplateParser.ConstructUri(attrSrc.Value, baseUri, addImageQuery); // alt is optional, I don't use it right now var altAttr = attributes.PopAttribute(ATTR_IMAGE_ALT); if (altAttr != null) { AlternateText = altAttr.Value; } // placement defaults to inline Placement placement; if (TryParseEnum(result, attributes, ATTR_IMAGE_PLACEMENT, out placement)) { this.Placement = placement; } switch (placement) { case Placement.Background: if (SupportedFeatures.CropCircleOnBackgroundImage) { HandleHintCrop(result, attributes); } if (SupportedFeatures.OverlayForBothBackgroundAndPeek) { HandleHintOverlay(result, attributes); } break; case Placement.Peek: if (SupportedFeatures.CropCircleOnPeekImage) { HandleHintCrop(result, attributes); } if (SupportedFeatures.OverlayForBothBackgroundAndPeek) { HandleHintOverlay(result, attributes); } break; case Placement.Hero: #if EXPERIMENTAL if (Context == NotificationType.NewsFeed) { HintHeroHeight heroHeight; if (TryParseEnum(result, attributes, "hint-heroHeight", out heroHeight)) { this.HintHeroHeight = heroHeight; } } #endif break; #if EXPERIMENTAL case Placement.HeroLogo: // No additional properties supported on hero logo break; #endif default: HandleHintCrop(result, attributes); // These only apply to tiles, and only to inline images if (Context != NotificationType.Toast || SupportedFeatures.AdaptiveToasts ) { // hint-removeMargin is optional bool hintRemoveMargin; if (TryParse(result, attributes, ATTR_IMAGE_HINT_REMOVE_MARGIN, out hintRemoveMargin)) { this.HintRemoveMargin = hintRemoveMargin; } // hint-align is optional HintImageAlign hintAlign; if (TryParseEnum(result, attributes, ATTR_IMAGE_HINT_ALIGN, out hintAlign)) { this.HintAlign = hintAlign; } } break; } }
internal static bool TryParse(ParseResult result, AttributesHelper attributes, string attributeName, out bool answer) { return(XmlTemplateParser.TryParse(result, attributes, attributeName, out answer)); }
protected void HandleRemainingAttributes(AttributesHelper attributes, ParseResult result) { XmlTemplateParser.AddWarningsForAttributesNotSupportedByVisualizer(result, attributes, GetAttributesNotSupportedByVisualizer().ToArray()); XmlTemplateParser.AddWarningsForUnknownAttributes(result, attributes); }
internal void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // We parse title first, and we only completely fail on title, since if id isn't provided, // we can at least still render something for the preview (we still display error though). // We'll assume that the developer will add the id before they send the toast. // title is required XAttribute attrTitle = attributes.PopAttribute(ATTR_TITLE); if (attrTitle == null) { result.AddErrorButRenderAllowed("title attribute on header element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); // We'll fail without a title throw new IncompleteElementException(); } else { if (string.IsNullOrWhiteSpace(attrTitle.Value)) { result.AddWarning("title attribute in header element must contain a string. The header will be dropped.", XmlTemplateParser.GetErrorPositionInfo(attrTitle)); throw new IncompleteElementException(); } else { Title = attrTitle.Value; } } // id is required XAttribute attrId = attributes.PopAttribute(ATTR_ID); if (attrId == null) { result.AddErrorButRenderAllowed("id attribute on header element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); } else { if (string.IsNullOrWhiteSpace(attrId.Value)) { result.AddWarning("id attribute in header element must contain a string. The header will be dropped.", XmlTemplateParser.GetErrorPositionInfo(attrId)); throw new IncompleteElementException(); } else { Id = attrId.Value; } } // arguments is required XAttribute attrArguments = attributes.PopAttribute(ATTR_ARGUMENTS); if (attrArguments == null) { result.AddErrorButRenderAllowed("arguments attribute on header element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); } else { // Empty string in arguments is allowed Arguments = attrArguments.Value; } // activationType is optional ActivationType type; if (TryParseEnum(result, attributes, ATTR_ACTIVATIONTYPE, out type)) { ActivationType = type; } }
internal void ParseActivatableElementAttributes(XElement node, AttributesHelper attributes, ParseResult result) { IActivatableElement el = this as IActivatableElement; // activationType is optional ActivationType type; if (TryParseEnum(result, attributes, ATTR_ACTIVATIONTYPE, out type)) { el.ActivationType = type; } // arguments is required XAttribute attrArguments = attributes.PopAttribute(ATTR_ARGUMENTS); if (attrArguments == null) { // If we're in an activation type that requires attributes if (el.ActivationType != ActivationType.None) { result.AddErrorButRenderAllowed("arguments attribute is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } } else { el.Arguments = attrArguments.Value; } }
internal void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // title is optional string bindingTitle; TryPopAttributeValueWithBinding(result, attributes, ATTR_TITLE, out bindingTitle, (val) => { Title = val; }); BindingTitle = bindingTitle; // valueStringOverride is optional string bindingValueStringOverride; TryPopAttributeValueWithBinding(result, attributes, ATTR_VALUE_STRING_OVERRIDE, out bindingValueStringOverride, (val) => { ValueStringOverride = val; }); BindingValueStringOverride = bindingValueStringOverride; // value is required string bindingValue; bool hadValue = TryPopAttributeValueWithBinding(result, attributes, ATTR_VALUE, out bindingValue, (val) => { if (val == null) { throw new ParseErrorException(new ParseError(ParseErrorType.ErrorButRenderAllowed, "progress value wasn't provided.", XmlTemplateParser.GetErrorPositionInfo(node))); } double value = 0; if (val.Equals("indeterminate", StringComparison.CurrentCultureIgnoreCase)) { IsIndeterminate = true; } else if (!double.TryParse(val, out value)) { throw new ParseErrorException(new ParseError(ParseErrorType.ErrorButRenderAllowed, "progress value must be a double between 0.0 and 1.0, or 'indeterminate'.", XmlTemplateParser.GetErrorPositionInfo(node))); } else { if (value < 0 || value > 1) { IsIndeterminate = false; Value = (value < 0) ? 0 : 1; throw new ParseErrorException(new ParseError(ParseErrorType.ErrorButRenderAllowed, "progress value must be between 0.0 and 1.0, or 'indeterminate'.", XmlTemplateParser.GetErrorPositionInfo(node))); } IsIndeterminate = false; Value = value; } }); if (!hadValue) { Value = 0; result.AddErrorButRenderAllowed("value attribute on progress element is required and must be a double between 0.0 and 1.0, or 'indeterminate'.", XmlTemplateParser.GetErrorPositionInfo(node)); } BindingValue = bindingValue; // status is optional string bindingStatus; TryPopAttributeValueWithBinding(result, attributes, ATTR_STATUS, out bindingStatus, (val) => { Status = val; }); BindingStatus = bindingStatus; }
internal void Parse(ParseResult result, XElement node) { if (!XmlTemplateParser.EnsureNodeOnlyHasElementsAsChildren(result, node)) { throw new IncompleteElementException(); } AttributesHelper attributes = new AttributesHelper(node.Attributes()); // BaseUri is optional string baseUri = null; { XAttribute attrBaseUri = attributes.PopAttribute(ATTR_VISUAL_BASEURI); if (attrBaseUri != null) { baseUri = attrBaseUri.Value; Uri uri; if (Uri.TryCreate(baseUri, UriKind.RelativeOrAbsolute, out uri)) { BaseUri = uri; } } } // AddImageQuery is optional bool addImageQuery; if (TryParse(result, attributes, ATTR_VISUAL_ADDIMAGEQUERY, out addImageQuery)) { AddImageQuery = addImageQuery; } else { addImageQuery = false; // Defaults to false if not specified } if (Context == NotificationType.Tile) { if (SupportedFeatures.ChaseableTiles) { XAttribute attrArguments = attributes.PopAttribute(ATTR_VISUAL_ARGUMENTS); if (attrArguments != null) { Arguments = attrArguments.Value; } } } ParseKnownAttributes(attributes, result, baseUri, addImageQuery); HandleRemainingAttributes(attributes, result); foreach (XElement n in node.Elements()) { try { HandleChild(result, n, baseUri, addImageQuery); } catch (IncompleteElementException) { } } }
internal virtual void ParseKnownAttributes(XElement node, AttributesHelper attributes, ParseResult result) { // target is required XAttribute attrTarget = attributes.PopAttribute(ATTR_TARGET); if (attrTarget == null) { result.AddError($"{ATTR_TARGET} attribute on {ELEMENT_NAME} element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Target = attrTarget.Value; // property is required SetterProperties property; if (TryParseEnum(result, attributes, ATTR_PROPERTY, out property)) { this.Property = property; } else { result.AddErrorButRenderAllowed($"{ATTR_PROPERTY} attribute on {ELEMENT_NAME} element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } // value is required XAttribute attrValue = attributes.PopAttribute(ATTR_VALUE); if (attrValue == null) { result.AddErrorButRenderAllowed($"{ATTR_VALUE} attribute on {ELEMENT_NAME} element is required.", XmlTemplateParser.GetErrorPositionInfo(node)); throw new IncompleteElementException(); } this.Value = attrValue.Value; }