public override ParsedDocument Parse (ProjectDom dom, string fileName, string fileContent)
		{
			XmlParsedDocument doc = new XmlParsedDocument (fileName);
			doc.Flags |= ParsedDocumentFlags.NonSerializable;
			TextReader tr = new StringReader (fileContent);
			try {
				Parser xmlParser = new Parser (new XmlFreeState (), true);
				xmlParser.Parse (tr);
				doc.XDocument = xmlParser.Nodes.GetRoot ();
				doc.Add (xmlParser.Errors);
				
				if (doc.XDocument != null && doc.XDocument.RootElement != null) {
					if (!doc.XDocument.RootElement.IsEnded)
						doc.XDocument.RootElement.End (xmlParser.Location);
				}
			}
			catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError ("Unhandled error parsing xml document", ex);
			}
			finally {
				if (tr != null)
					tr.Dispose ();
			}
			return doc;
		}
		public override ParsedDocument Parse (ProjectDom dom, string fileName, string fileContent)
		{
			XmlParsedDocument doc = new XmlParsedDocument (fileName);
			doc.Flags = ParsedDocumentFlags.NonSerializable;
			
			TextReader tr = new StringReader (fileContent);
			try {
				Parser xmlParser = new Parser (
					new XmlFreeState (new HtmlTagState (true), new HtmlClosingTagState (true)),
					true);
				
				xmlParser.Parse (tr);
				doc.XDocument = xmlParser.Nodes.GetRoot ();
				doc.Add (xmlParser.Errors);
				if (doc.XDocument != null)
					doc.Add (Validate (doc.XDocument));
			}
			catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError ("Unhandled error parsing HTML document", ex);
			}
			finally {
				if (tr != null)
					tr.Dispose ();
			}
			
			return doc;
		}
		public static MSBuildResolveContext Create (XmlParsedDocument doc, MSBuildResolveContext previous)
		{
			var ctx = new MSBuildResolveContext ();
			ctx.Populate (doc.XDocument);
			if (doc.Errors.Count > 0)
				ctx.Merge (previous);
			return ctx;
		}
		public override ParsedDocument Parse (bool storeAst, string fileName, TextReader content, MonoDevelop.Projects.Project project = null)
		{
			XmlParsedDocument doc = new XmlParsedDocument (fileName);
			doc.Flags |= ParsedDocumentFlags.NonSerializable;
			try {
				Parser xmlParser = new Parser (new XmlFreeState (), true);
				xmlParser.Parse (content);
				doc.XDocument = xmlParser.Nodes.GetRoot ();
				doc.Add (xmlParser.Errors);
				
				if (doc.XDocument != null && doc.XDocument.RootElement != null) {
					if (!doc.XDocument.RootElement.IsEnded)
						doc.XDocument.RootElement.End (xmlParser.Location);
				}
			}
			catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError ("Unhandled error parsing xml document", ex);
			}
			return doc;
		}
		public override ParsedDocument Parse (bool storeAst, string fileName, TextReader tr, Project project = null)
		{
			var doc = new XmlParsedDocument (fileName);
//			doc.Flags = ParsedDocumentFlags.NonSerializable;
			
			try {
				Parser xmlParser = new Parser (
					new XmlFreeState (new HtmlTagState (true), new HtmlClosingTagState (true)),
					true);
				
				xmlParser.Parse (tr);
				doc.XDocument = xmlParser.Nodes.GetRoot ();
				doc.Add (xmlParser.Errors);
				if (doc.XDocument != null)
					doc.Add (Validate (doc.XDocument));
			}
			catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError ("Unhandled error parsing HTML document", ex);
			}
			return doc;
		}
		static void GenerateCU (XmlParsedDocument doc)
		{
			if (doc.XDocument == null || doc.XDocument.RootElement == null) {
				doc.Add (new Error (ErrorType.Error, 1, 1, "No root node found."));
				return;
			}

			XAttribute rootClass = doc.XDocument.RootElement.Attributes [new XName ("x", "Class")];
			if (rootClass == null) {
				doc.Add (new Error (ErrorType.Error, 1, 1, "Root node does not contain an x:Class attribute."));
				return;
			}

			bool isApplication = doc.XDocument.RootElement.Name.Name == "Application";
			
			string rootNamespace, rootType, rootAssembly;
			XamlG.ParseXmlns (rootClass.Value, out rootType, out rootNamespace, out rootAssembly);
			
			CompilationUnit cu = new CompilationUnit (doc.FileName);
			doc.CompilationUnit = cu;

			DomRegion rootRegion = doc.XDocument.RootElement.Region;
			if (doc.XDocument.RootElement.IsClosed)
				rootRegion.End = doc.XDocument.RootElement.ClosingTag.Region.End;
			
			DomType declType = new DomType (cu, ClassType.Class, Modifiers.Partial | Modifiers.Public, rootType,
			                                doc.XDocument.RootElement.Region.Start, rootNamespace, rootRegion);
			cu.Add (declType);
			
			DomMethod initcomp = new DomMethod ();
			initcomp.Name = "InitializeComponent";
			initcomp.Modifiers = Modifiers.Public;
			initcomp.ReturnType = DomReturnType.Void;
			declType.Add (initcomp);
			
			DomField _contentLoaded = new DomField ("_contentLoaded");
			_contentLoaded.ReturnType = new DomReturnType ("System.Boolean");

			if (isApplication)
				return;
			
			cu.Add (new DomUsing (DomRegion.Empty, "System"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Documents"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Input"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media.Animation"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Shapes"));
			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls.Primitives"));
			
//			Dictionary<string,string> namespaceMap = new Dictionary<string, string> ();
//			namespaceMap["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
			
			XName nameAtt = new XName ("x", "Name");
			
			foreach (XElement el in doc.XDocument.RootElement.AllDescendentElements) {
				XAttribute name = el.Attributes [nameAtt];
				if (name != null && name.IsComplete) {
					string type = ResolveType (el);
					if (type == null || type.Length == 0)
						doc.Add (new Error (ErrorType.Error, el.Region.Start, "Could not find namespace for '" + el.Name.FullName + "'."));
					else
						declType.Add (new DomField (name.Value, Modifiers.Internal, el.Region.Start, new DomReturnType (type)));
				}
			}
		}
Beispiel #7
0
		static void GenerateCU (XmlParsedDocument doc)
		{
			if (doc.XDocument == null || doc.XDocument.RootElement == null) {
				doc.Add (new Error (ErrorType.Error, "No root node found.", 1, 1));
				return;
			}

			XAttribute rootClass = doc.XDocument.RootElement.Attributes [new XName ("x", "Class")];
			if (rootClass == null) {
				doc.Add (new Error (ErrorType.Error, "Root node does not contain an x:Class attribute.", 1, 1));
				return;
			}

			bool isApplication = doc.XDocument.RootElement.Name.Name == "Application";
			
			string rootNamespace, rootType, rootAssembly;
			XamlG.ParseXmlns (rootClass.Value, out rootType, out rootNamespace, out rootAssembly);

			var cu = new DefaultParsedDocument (doc.FileName);

			DomRegion rootRegion = doc.XDocument.RootElement.Region;
			if (doc.XDocument.RootElement.IsClosed)
				rootRegion = new DomRegion (doc.XDocument.RootElement.Region.FileName, doc.XDocument.RootElement.Region.Begin, doc.XDocument.RootElement.ClosingTag.Region.End); 
			
			var declType = new DefaultUnresolvedTypeDefinition (rootNamespace, rootType) {
				Kind = TypeKind.Class,
				Accessibility = Accessibility.Public,
				Region = rootRegion
			};
			cu.TopLevelTypeDefinitions.Add (declType);
			
			var initcomp = new DefaultUnresolvedMethod (declType, "InitializeComponent") {
				ReturnType = KnownTypeReference.Void,
				Accessibility = Accessibility.Public
			};
			declType.Members.Add (initcomp);
			
			var _contentLoaded = new DefaultUnresolvedField (declType, "_contentLoaded") {
				ReturnType = KnownTypeReference.Boolean
			};
// was missing in the original code: correct ? 
//			declType.Fields.Add (_contentLoaded);

			if (isApplication)
				return;
			
//			cu.Add (new DomUsing (DomRegion.Empty, "System"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Documents"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Input"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media.Animation"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Shapes"));
//			cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls.Primitives"));
			
	//		Dictionary<string,string> namespaceMap = new Dictionary<string, string> ();
	//		namespaceMap["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
			
			XName nameAtt = new XName ("x", "Name");
			
			foreach (XElement el in doc.XDocument.RootElement.AllDescendentElements) {
				XAttribute name = el.Attributes [nameAtt];
				if (name != null && name.IsComplete) {
					string type = ResolveType (el);
					if (type == null || type.Length == 0)
						cu.Add (new Error (ErrorType.Error, "Could not find namespace for '" + el.Name.FullName + "'.", el.Region.Begin));
					else
						declType.Members.Add (new DefaultUnresolvedField (declType, name.Value) {
							Accessibility = Accessibility.Internal,
							Region = el.Region,
							ReturnType = new DefaultUnresolvedTypeDefinition (type)
						});
				}
			}
		}