Ejemplo n.º 1
0
		XamlResourceKey(BamlNode node) {
			KeyNode = node;
			StaticResources = new List<BamlNode>();

			IBamlDeferRecord keyRecord;
			if (node is BamlBlockNode)
				keyRecord = (IBamlDeferRecord)((BamlBlockNode)node).Header;
			else
				keyRecord = (IBamlDeferRecord)((BamlRecordNode)node).Record;

			if (keyRecord.Record.Type == BamlRecordType.ElementEnd) {
				Debug.Assert(node.Parent.Footer == keyRecord.Record);
				node.Parent.Annotation = this;
				node.Annotation = this;
				return;
			}

			if (keyRecord.Record.Type != BamlRecordType.ElementStart) {
				Debug.WriteLine(string.Format("Key record @{0} must be attached to ElementStart (actual {1})",
					keyRecord.Position, keyRecord.Record.Type));
			}

			foreach (var child in node.Parent.Children) {
				if (child.Record != keyRecord.Record)
					continue;

				child.Annotation = this;
				node.Annotation = this;
				return;
			}
			Debug.WriteLine("Cannot find corresponding value element of key record @" + keyRecord.Position);
		}
Ejemplo n.º 2
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (ConnectionIdRecord)((BamlRecordNode)node).Record;

			parent.Xaml.Element.AddAnnotation(new BamlConnectionId(record.ConnectionId));

			return null;
		}
Ejemplo n.º 3
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (TextWithIdRecord)((BamlRecordNode)node).Record;

			parent.Xaml.Element.Add(ctx.ResolveString(record.ValueId));

			return null;
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (DeferableContentStartRecord)((BamlRecordNode)node).Record;

			Debug.Assert(record.Record == ((BamlBlockNode)parent.Node).Footer);

			return null;
		}
Ejemplo n.º 5
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (TextRecord)((BamlRecordNode)node).Record;

			parent.Xaml.Element.Add(record.Value);

			return null;
		}
Ejemplo n.º 6
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var doc = new BamlElement(node);
			doc.Xaml = new XElement(ctx.GetPseudoName("Document"));

			HandlerMap.ProcessChildren(ctx, (BamlBlockNode)node, doc);
			return doc;
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (OptimizedStaticResourceRecord)((BamlRecordNode)node).Record;
			var key = XamlResourceKey.FindKeyInSiblings(node);

			key.StaticResources.Add(node);
			return null;
		}
Ejemplo n.º 8
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (ElementStartRecord)((BamlBlockNode)node).Header;
			var doc = new BamlElement(node);

			var elemType = ctx.ResolveType(record.TypeId);
			doc.Xaml = new XElement(elemType.ToXName(ctx));

			doc.Xaml.Element.AddAnnotation(elemType);
			parent.Xaml.Element.Add(doc.Xaml.Element);

			HandlerMap.ProcessChildren(ctx, (BamlBlockNode)node, doc);
			var key = node.Annotation as XamlResourceKey;
			if (key != null && key.KeyNode.Record != node.Record) {
				var handler = (IDeferHandler)HandlerMap.LookupHandler(key.KeyNode.Record.Type);
				var keyElem = handler.TranslateDefer(ctx, key.KeyNode, doc);

				doc.Children.Add(keyElem);
				keyElem.Parent = doc;
			}

			elemType.ResolveNamespace(doc.Xaml, ctx);
			doc.Xaml.Element.Name = elemType.ToXName(ctx);

			return doc;
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (PropertyWithStaticResourceIdRecord)((BamlRecordNode)node).Record;
			var doc = new BamlElement(node);

			var elemAttr = ctx.ResolveProperty(record.AttributeId);
			doc.Xaml = new XElement(elemAttr.ToXName(ctx, null));

			doc.Xaml.Element.AddAnnotation(elemAttr);
			parent.Xaml.Element.Add(doc.Xaml.Element);

			BamlNode found = node;
			XamlResourceKey key;
			do {
				key = XamlResourceKey.FindKeyInAncestors(found.Parent, out found);
			} while (key != null && record.StaticResourceId >= key.StaticResources.Count);

			if (key == null)
				throw new Exception("Cannot find StaticResource @" + node.Record.Position);

			var resNode = key.StaticResources[record.StaticResourceId];

			var handler = (IDeferHandler)HandlerMap.LookupHandler(resNode.Type);
			var resElem = handler.TranslateDefer(ctx, resNode, doc);

			doc.Children.Add(resElem);
			resElem.Parent = doc;

			elemAttr.DeclaringType.ResolveNamespace(doc.Xaml, ctx);
			doc.Xaml.Element.Name = elemAttr.ToXName(ctx, null);

			return doc;
		}
Ejemplo n.º 10
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (DefAttributeRecord)((BamlRecordNode)node).Record;

			var attrName = ctx.ResolveString(record.NameId);
			parent.Xaml.Element.Add(new XAttribute(ctx.GetXamlNsName(attrName), record.Value));

			return null;
		}
Ejemplo n.º 11
0
		public static XamlResourceKey FindKeyInSiblings(BamlNode node) {
			var children = node.Parent.Children;
			var index = children.IndexOf(node);
			for (int i = index; i >= 0; i--) {
				if (children[i].Annotation is XamlResourceKey)
					return (XamlResourceKey)children[i].Annotation;
			}
			return null;
		}
Ejemplo n.º 12
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (LiteralContentRecord)((BamlRecordNode)node).Record;

			var elem = new XElement(ctx.GetXamlNsName("XData", parent.Xaml));
			var content = XElement.Parse(record.Value);
			elem.Add(content);

			parent.Xaml.Element.Add(elem);

			return null;
		}
Ejemplo n.º 13
0
		public static XamlResourceKey FindKeyInAncestors(BamlNode node, out BamlNode found) {
			BamlNode n = node;
			do {
				if (n.Annotation is XamlResourceKey) {
					found = n;
					return (XamlResourceKey)n.Annotation;
				}
				n = n.Parent;
			} while (n != null);
			found = null;
			return null;
		}
		public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (DefAttributeKeyStringRecord)((BamlRecordNode)node).Record;
			var key = (XamlResourceKey)node.Annotation;

			var bamlElem = new BamlElement(node);
			bamlElem.Xaml = new XElement(ctx.GetXamlNsName("Key", parent.Xaml));
			parent.Xaml.Element.Add(bamlElem.Xaml.Element);
			bamlElem.Xaml.Element.Value = ctx.ResolveString(record.ValueId);
			key.KeyElement = bamlElem;

			return bamlElem;
		}
Ejemplo n.º 15
0
		public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (KeyElementStartRecord)((BamlBlockNode)node).Header;
			var key = (XamlResourceKey)node.Annotation;

			var bamlElem = new BamlElement(node);
			bamlElem.Xaml = new XElement(ctx.GetXamlNsName("Key", parent.Xaml));
			parent.Xaml.Element.Add(bamlElem.Xaml.Element);
			key.KeyElement = bamlElem;
			base.Translate(ctx, node, bamlElem);

			return bamlElem;
		}
Ejemplo n.º 16
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (PropertyRecord)((BamlRecordNode)node).Record;

			var elemType = parent.Xaml.Element.Annotation<XamlType>();
			var xamlProp = ctx.ResolveProperty(record.AttributeId);
			var value = XamlUtils.Escape(record.Value);

			var attr = new XAttribute(xamlProp.ToXName(ctx, parent.Xaml, xamlProp.IsAttachedTo(elemType)), value);
			parent.Xaml.Element.Add(attr);

			return null;
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (ConstructorParameterTypeRecord)((BamlRecordNode)node).Record;

			var elem = new XElement(ctx.GetXamlNsName("TypeExtension", parent.Xaml));
			elem.AddAnnotation(ctx.ResolveType(0xfd4d)); // Known type - TypeExtension

			var bamlElem = new BamlElement(node);
			bamlElem.Xaml = elem;
			parent.Xaml.Element.Add(elem);

			var type = ctx.ResolveType(record.TypeId);
			var typeName = ctx.ToString(parent.Xaml, type);
			elem.Add(new XElement(ctx.GetPseudoName("Ctor"), typeName));

			return bamlElem;
		}
Ejemplo n.º 18
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (PropertyComplexStartRecord)((BamlBlockNode)node).Header;
			var doc = new BamlElement(node);

			var elemAttr = ctx.ResolveProperty(record.AttributeId);
			doc.Xaml = new XElement(elemAttr.ToXName(ctx, null));

			doc.Xaml.Element.AddAnnotation(elemAttr);
			parent.Xaml.Element.Add(doc.Xaml.Element);

			HandlerMap.ProcessChildren(ctx, (BamlBlockNode)node, doc);
			elemAttr.DeclaringType.ResolveNamespace(doc.Xaml, ctx);
			doc.Xaml.Element.Name = elemAttr.ToXName(ctx, null);

			return doc;
		}
Ejemplo n.º 19
0
		public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (DefAttributeKeyTypeRecord)((BamlRecordNode)node).Record;
			var type = ctx.ResolveType(record.TypeId);
			var typeName = ctx.ToString(parent.Xaml, type);
			var key = (XamlResourceKey)node.Annotation;

			var bamlElem = new BamlElement(node);
			bamlElem.Xaml = new XElement(ctx.GetXamlNsName("Key", parent.Xaml));
			parent.Xaml.Element.Add(bamlElem.Xaml.Element);

			var typeElem = new XElement(ctx.GetXamlNsName("TypeExtension", parent.Xaml));
			typeElem.AddAnnotation(ctx.ResolveType(0xfd4d)); // Known type - TypeExtension
			typeElem.Add(new XElement(ctx.GetPseudoName("Ctor"), typeName));
			bamlElem.Xaml.Element.Add(typeElem);

			key.KeyElement = bamlElem;

			return bamlElem;
		}
Ejemplo n.º 20
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (XmlnsPropertyRecord)((BamlRecordNode)node).Record;
			foreach (var asmId in record.AssemblyIds) {
				var assembly = ctx.Baml.ResolveAssembly(asmId);
				ctx.XmlNs.Add(new NamespaceMap(record.Prefix, assembly, record.XmlNamespace));

				if (assembly is AssemblyDef) {
					foreach (var clrNs in ResolveCLRNamespaces((AssemblyDef)assembly, record.XmlNamespace))
						ctx.XmlNs.Add(new NamespaceMap(record.Prefix, assembly, record.XmlNamespace, clrNs));
				}
			}

			XName xmlnsDef;
			if (string.IsNullOrEmpty(record.Prefix))
				xmlnsDef = "xmlns";
			else
				xmlnsDef = XNamespace.Xmlns + XmlConvert.EncodeLocalName(record.Prefix);
			parent.Xaml.Element.Add(new XAttribute(xmlnsDef, ctx.GetXmlNamespace(record.XmlNamespace)));

			return null;
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (PropertyTypeReferenceRecord)((BamlRecordNode)node).Record;
			var attr = ctx.ResolveProperty(record.AttributeId);
			var type = ctx.ResolveType(record.TypeId);
			var typeName = ctx.ToString(parent.Xaml, type);

			var elem = new BamlElement(node);

			var elemAttr = ctx.ResolveProperty(record.AttributeId);
			elem.Xaml = new XElement(elemAttr.ToXName(ctx, null));

			elem.Xaml.Element.AddAnnotation(elemAttr);
			parent.Xaml.Element.Add(elem.Xaml.Element);

			var typeElem = new XElement(ctx.GetXamlNsName("TypeExtension", parent.Xaml));
			typeElem.AddAnnotation(ctx.ResolveType(0xfd4d)); // Known type - TypeExtension
			typeElem.Add(new XElement(ctx.GetPseudoName("Ctor"), typeName));
			elem.Xaml.Element.Add(typeElem);

			elemAttr.DeclaringType.ResolveNamespace(elem.Xaml, ctx);
			elem.Xaml.Element.Name = elemAttr.ToXName(ctx, null);

			return elem;
		}
		public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (OptimizedStaticResourceRecord)((BamlRecordNode)node).Record;
			var bamlElem = new BamlElement(node);
			object key;
			if (record.IsType) {
				var value = ctx.ResolveType(record.ValueId);

				var typeElem = new XElement(ctx.GetXamlNsName("TypeExtension", parent.Xaml));
				typeElem.AddAnnotation(ctx.ResolveType(0xfd4d)); // Known type - TypeExtension
				typeElem.Add(new XElement(ctx.GetPseudoName("Ctor"), ctx.ToString(parent.Xaml, value)));
				key = typeElem;
			}
			else if (record.IsStatic) {
				string attrName;
				if (record.ValueId > 0x7fff) {
					bool isKey = true;
					short bamlId = (short)-record.ValueId;
					if (bamlId > 232 && bamlId < 464) {
						bamlId -= 232;
						isKey = false;
					}
					else if (bamlId > 464 && bamlId < 467) {
						bamlId -= 231;
					}
					else if (bamlId > 467 && bamlId < 470) {
						bamlId -= 234;
						isKey = false;
					}
					var res = ctx.Baml.KnownThings.Resources(bamlId);
					string name;
					if (isKey)
						name = res.Item1 + "." + res.Item2;
					else
						name = res.Item1 + "." + res.Item3;
					var xmlns = ctx.GetXmlNamespace("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
					attrName = ctx.ToString(parent.Xaml, xmlns.GetName(name));
				}
				else {
					var value = ctx.ResolveProperty(record.ValueId);

					value.DeclaringType.ResolveNamespace(parent.Xaml, ctx);
					var xName = value.ToXName(ctx, parent.Xaml);

					attrName = ctx.ToString(parent.Xaml, xName);
				}

				var staticElem = new XElement(ctx.GetXamlNsName("StaticExtension", parent.Xaml));
				staticElem.AddAnnotation(ctx.ResolveType(0xfda6)); // Known type - StaticExtension
				staticElem.Add(new XElement(ctx.GetPseudoName("Ctor"), attrName));
				key = staticElem;
			}
			else
				key = ctx.ResolveString(record.ValueId);

			var extType = ctx.ResolveType(0xfda5);
			var resElem = new XElement(extType.ToXName(ctx));
			resElem.AddAnnotation(extType); // Known type - StaticResourceExtension
			bamlElem.Xaml = resElem;
			parent.Xaml.Element.Add(resElem);

			var attrElem = new XElement(ctx.GetPseudoName("Ctor"));
			attrElem.Add(key);
			resElem.Add(attrElem);

			return bamlElem;
		}
Ejemplo n.º 23
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (PropertyCustomRecord)((BamlRecordNode)node).Record;
			var serTypeId = ((short)record.SerializerTypeId & 0xfff);
			bool valueType = ((short)record.SerializerTypeId & 0x4000) == 0x4000;

			var elemType = parent.Xaml.Element.Annotation<XamlType>();
			var xamlProp = ctx.ResolveProperty(record.AttributeId);

			string value = Deserialize(ctx, parent.Xaml, (KnownTypes)serTypeId, record.Data);
			var attr = new XAttribute(xamlProp.ToXName(ctx, parent.Xaml, xamlProp.IsAttachedTo(elemType)), value);
			parent.Xaml.Element.Add(attr);

			return null;
		}
Ejemplo n.º 24
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			return null;
		}
Ejemplo n.º 25
0
		public BamlElement(BamlNode node) {
			Node = node;
			Children = new List<BamlElement>();
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			XamlResourceKey.Create(node);
			return null;
		}
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (PropertyWithExtensionRecord)((BamlRecordNode)node).Record;
			var extTypeId = ((short)record.Flags & 0xfff);
			bool valTypeExt = ((short)record.Flags & 0x4000) == 0x4000;
			bool valStaticExt = ((short)record.Flags & 0x2000) == 0x2000;

			var elemType = parent.Xaml.Element.Annotation<XamlType>();
			var xamlProp = ctx.ResolveProperty(record.AttributeId);
			var extType = ctx.ResolveType((ushort)-extTypeId);
			extType.ResolveNamespace(parent.Xaml, ctx);

			var ext = new XamlExtension(extType);
			if (valTypeExt || extTypeId == (short)KnownTypes.TypeExtension) {
				var value = ctx.ResolveType(record.ValueId);
				ext.Initializer = new object[] { ctx.ToString(parent.Xaml, value) };
			}
			else if (extTypeId == (short)KnownTypes.TemplateBindingExtension) {
				var value = ctx.ResolveProperty(record.ValueId);

				value.DeclaringType.ResolveNamespace(parent.Xaml, ctx);
				var xName = value.ToXName(ctx, parent.Xaml, false);

				ext.Initializer = new object[] { ctx.ToString(parent.Xaml, xName) };
			}
			else if (valStaticExt || extTypeId == (short)KnownTypes.StaticExtension) {
				string attrName;
				if (record.ValueId > 0x7fff) {
					bool isKey = true;
					short bamlId = (short)-record.ValueId;
					if (bamlId > 232 && bamlId < 464) {
						bamlId -= 232;
						isKey = false;
					}
					else if (bamlId > 464 && bamlId < 467) {
						bamlId -= 231;
					}
					else if (bamlId > 467 && bamlId < 470) {
						bamlId -= 234;
						isKey = false;
					}
					var res = ctx.Baml.KnownThings.Resources(bamlId);
					string name;
					if (isKey)
						name = res.Item1 + "." + res.Item2;
					else
						name = res.Item1 + "." + res.Item3;
					var xmlns = ctx.GetXmlNamespace("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
					attrName = ctx.ToString(parent.Xaml, xmlns.GetName(name));
				}
				else {
					var value = ctx.ResolveProperty(record.ValueId);

					value.DeclaringType.ResolveNamespace(parent.Xaml, ctx);
					var xName = value.ToXName(ctx, parent.Xaml);

					attrName = ctx.ToString(parent.Xaml, xName);
				}
				ext.Initializer = new object[] { attrName };
			}
			else {
				ext.Initializer = new object[] { XamlUtils.Escape(ctx.ResolveString(record.ValueId)) };
			}

			var extValue = ext.ToString(ctx, parent.Xaml);
			var attr = new XAttribute(xamlProp.ToXName(ctx, parent.Xaml, xamlProp.IsAttachedTo(elemType)), extValue);
			parent.Xaml.Element.Add(attr);

			return null;
		}
Ejemplo n.º 28
0
		public static XamlResourceKey FindKeyInAncestors(BamlNode node) {
			BamlNode found;
			return FindKeyInAncestors(node, out found);
		}
Ejemplo n.º 29
0
		public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent) {
			var record = (ContentPropertyRecord)((BamlRecordNode)node).Record;
			// TODO: What to do here?

			return null;
		}
Ejemplo n.º 30
0
		public static XamlResourceKey Create(BamlNode node) {
			return new XamlResourceKey(node);
		}