Browse Source

Xaml Parser should use the current node on Exceptions, so we have a better Line/Column Number

pull/586/head
jogibear9988 11 years ago
parent
commit
c2d558543e
  1. 26
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlParser.cs

26
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlParser.cs

@ -80,6 +80,8 @@ namespace ICSharpCode.WpfDesign.XamlDom
throw new ArgumentNullException("reader"); throw new ArgumentNullException("reader");
return Parse(XmlReader.Create(reader), settings); return Parse(XmlReader.Create(reader), settings);
} }
private XmlNode currentParsedNode;
/// <summary> /// <summary>
/// Parses a XAML document using an XmlReader. /// Parses a XAML document using an XmlReader.
@ -128,7 +130,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
var root = p.ParseObject(document.DocumentElement); var root = p.ParseObject(document.DocumentElement);
p.document.ParseComplete(root); p.document.ParseComplete(root);
} catch (Exception x) { } catch (Exception x) {
p.ReportException(x, document.DocumentElement); p.ReportException(x, p.currentParsedNode);
} }
return p.document; return p.document;
@ -220,12 +222,15 @@ namespace ICSharpCode.WpfDesign.XamlDom
if (onlyTextNodes && numberOfTextNodes == 1) { if (onlyTextNodes && numberOfTextNodes == 1) {
foreach (XmlNode childNode in element.ChildNodes) { foreach (XmlNode childNode in element.ChildNodes) {
if (childNode.NodeType == XmlNodeType.Text) { if (childNode.NodeType == XmlNodeType.Text) {
currentParsedNode = childNode;
initializeFromTextValueInsteadOfConstructor = (XamlTextValue)ParseValue(childNode); initializeFromTextValueInsteadOfConstructor = (XamlTextValue)ParseValue(childNode);
} }
} }
} }
} }
currentParsedNode = element;
object instance; object instance;
if (initializeFromTextValueInsteadOfConstructor != null) { if (initializeFromTextValueInsteadOfConstructor != null) {
instance = TypeDescriptor.GetConverter(elementType).ConvertFromString( instance = TypeDescriptor.GetConverter(elementType).ConvertFromString(
@ -318,6 +323,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
collectionPropertyElement = element; collectionPropertyElement = element;
} else if (defaultProperty != null && defaultProperty.IsCollection && !element.IsEmpty) { } else if (defaultProperty != null && defaultProperty.IsCollection && !element.IsEmpty) {
foreach (XmlNode childNode in elementChildNodes) { foreach (XmlNode childNode in elementChildNodes) {
currentParsedNode = childNode;
XmlElement childElement = childNode as XmlElement; XmlElement childElement = childNode as XmlElement;
if (childElement == null || !ObjectChildElementIsPropertyElement(childElement)) { if (childElement == null || !ObjectChildElementIsPropertyElement(childElement)) {
obj.AddProperty(collectionProperty = new XamlProperty(obj, defaultProperty)); obj.AddProperty(collectionProperty = new XamlProperty(obj, defaultProperty));
@ -328,6 +334,8 @@ namespace ICSharpCode.WpfDesign.XamlDom
} }
} }
currentParsedNode = element;
if (collectionType != null && collectionInstance == null && elementChildNodes.Count() == 1) if (collectionType != null && collectionInstance == null && elementChildNodes.Count() == 1)
{ {
var firstChild = elementChildNodes.First() as XmlElement; var firstChild = elementChildNodes.First() as XmlElement;
@ -344,6 +352,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
else else
{ {
foreach (XmlNode childNode in elementChildNodes) { foreach (XmlNode childNode in elementChildNodes) {
currentParsedNode = childNode;
XmlElement childElement = childNode as XmlElement; XmlElement childElement = childNode as XmlElement;
if (childElement != null) { if (childElement != null) {
if (childElement.NamespaceURI == XamlConstants.XamlNamespace) if (childElement.NamespaceURI == XamlConstants.XamlNamespace)
@ -374,6 +383,8 @@ namespace ICSharpCode.WpfDesign.XamlDom
} }
} }
} }
currentParsedNode = element;
} }
IEnumerable<XmlNode> GetNormalizedChildNodes(XmlElement element) IEnumerable<XmlNode> GetNormalizedChildNodes(XmlElement element)
@ -412,10 +423,12 @@ namespace ICSharpCode.WpfDesign.XamlDom
Justification="We need to continue parsing, and the error is reported to the user.")] Justification="We need to continue parsing, and the error is reported to the user.")]
XamlPropertyValue ParseValue(XmlNode childNode) XamlPropertyValue ParseValue(XmlNode childNode)
{ {
currentParsedNode = childNode;
try { try {
return ParseValueCore(childNode); return ParseValueCore(currentParsedNode);
} catch (Exception x) { } catch (Exception x) {
ReportException(x, childNode); ReportException(x, currentParsedNode);
} }
return null; return null;
} }
@ -647,6 +660,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
} }
foreach (XmlNode childNode in element.ChildNodes) { foreach (XmlNode childNode in element.ChildNodes) {
currentParsedNode = childNode;
XamlPropertyValue childValue = ParseValue(childNode); XamlPropertyValue childValue = ParseValue(childNode);
if (childValue != null) { if (childValue != null) {
if (propertyInfo.IsCollection) { if (propertyInfo.IsCollection) {
@ -667,7 +681,9 @@ namespace ICSharpCode.WpfDesign.XamlDom
} }
} }
} }
currentParsedNode = element;
currentXmlSpace = oldXmlSpace; currentXmlSpace = oldXmlSpace;
} }

Loading…
Cancel
Save