Monday, 27 August 2012

XPathNodeIterator MoveNext() issue, and more Razor

Upon trying to save a file under Scripting Files, you get 'Scripting file could not be saved'
But the file is saved,
And, there is an extra temporary script file left behind.
This does not happen when 'skip file testing' is checked.
Error occurs in SaveDLRScript(codeEditorSave.asmx.cs 291) and new Node(id) yields null, and breaks things.


This highlights another mono subtlety: in mono, usage for an XPathNodeIterator is:
('MoveNext' issue)


			XPathNodeIterator iter = Select (expression);
if (iter.MoveNext ())
return iter.Current;
else
return null;

Where the return type is 'XPathNavigator'. We need to update this for all relevant cases in umbraco.
This is because initially the XpathNodeIterator's Position is at '0', and the 'Current' property is always at null.


And, therefore any immediate reference to iter.Current is always null.


First add a function to .../umbraco/businesslogic/xmlHelper.cs:


		//this is very mono specific at the moment
public static XmlNode GetCurrentNodeFromIterator(XPathNodeIterator xpi)
{
if (xpi != null)
{
if (xpi.MoveNext())
return ((IHasXmlNode)xpi.Current).GetNode();
}

return null;
}

Next use the following examples for the replacement.
** Loading razor script into rich text editor yields (insert macro):
'Error loading MacroEngine script (file: TestRazorSiteMap.cshtml)'
So, in .../umbraco/presentation/umbraco/nodeFactory/Page.cs replace, line 593,


 
XmlNode n = ((IHasXmlNode)library.GetXmlNodeCurrent().Current).GetNode();
with
XmlNode n = xmlHelper.GetCurrentNodeFromIterator(library.GetXmlNodeCurrent());

** Above saveDLRScript error. So, in .../umbraco/presentation/umbraco/nodeFactory/Page.cs replace, line 298,


 _pageXmlNode = ((IHasXmlNode)library.GetXmlNodeById(NodeId.ToString()).Current).GetNode();		
with
_pageXmlNode = xmlHelper.GetCurrentNodeFromIterator(library.GetXmlNodeById(NodeId.ToString()));

** Total Changes: 14
p.aspx.cs, 42, 46
library.cs, 96, 1207, 1266, 1340
baseHttpModule.cs, 182
UmbracoPage.cs, 22
Page_Legacy.cs, 219, 269, 510
Page.cs, 247, 298, 591


** RAZOR RELATED: **


ExamineBackedMedia.cs
48:51, from,
var media = umbraco.library.GetMedia(id, true);
if (media != null && media.Current != null)
{
media.MoveNext();
return new ExamineBackedMedia(media.Current);
}
to
XPathNodeIterator media = umbraco.library.GetMedia(id, true);
if (media != null)
{
if (media.MoveNext())
return new ExamineBackedMedia(media.Current);
}
Note: we need to type media, otherwise 'MoveNext()' will return false.

62:76, from,
XPathNodeIterator result = xpath.SelectChildren(XPathNodeType.Element);
//add the attributes e.g. id, parentId etc
if (result.Current.HasAttributes)
{
if (result.Current.MoveToFirstAttribute())
{
Values.Add(result.Current.Name, result.Current.Value);
while (result.Current.MoveToNextAttribute())
{
Values.Add(result.Current.Name, result.Current.Value);
}
result.Current.MoveToParent();
}
}
while (result.MoveNext())
to
XPathNodeIterator xpi = xpath.Select(".");
//add the attributes e.g. id, parentId etc
if (xpi.MoveNext())
if (xpi.Current.HasAttributes)
{
if (xpi.Current.MoveToFirstAttribute())
{
Values.Add(xpi.Current.Name, xpi.Current.Value);
while (xpi.Current.MoveToNextAttribute())
{
Values.Add(xpi.Current.Name, xpi.Current.Value);
}
}
}
XPathNodeIterator result = xpath.SelectChildren(XPathNodeType.Element);
while (result.MoveNext())

108:110, from, 
if (media != null && media.Current != null)
{
media.MoveNext();
XPathNavigator xpath = media.Current;
to
if (media != null && media.MoveNext())
{
XPathNavigator xpath = media.Current;

370:373, from,
if (media != null && media.Current != null)
{
media.MoveNext();
var children = media.Current.SelectChildren(XPathNodeType.Element);
to
if (media != null && media.MoveNext())
{
var children = media.Current.SelectChildren(XPathNodeType.Element);


                               
DynamicXml.cs, 31:33
from,
if (xpni.Current != null)
{
var xml = xpni.Current.OuterXml;
to
if (xpni.MoveNext())
{
var xml = xpni.Current.OuterXml;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.