";
Exception ie = ex;
while (ie != null)
{
ret.Text += "
" + ie.Message + "
";
ret.Text += ie.StackTrace + "
";
ie = ie.InnerException;
}
ret.Text += "
";
}
return ret;
}
public Control loadMacroDLR(macro macro, Hashtable attributes, Hashtable pageElements)
{
LiteralControl ret = new LiteralControl();
Hashtable args = new Hashtable();
foreach (DictionaryEntry macroDef in macro.properties)
{
try
{
args.Add(macroDef.Key.ToString(), helper.FindAttribute(pageElements, attributes, macroDef.Key.ToString()));
}
catch (Exception e)
{
HttpContext.Current.Trace.Warn("umbracoMacro", "Could not add global variable (" + macroDef.Key + ") to DLR enviroment", e);
}
}
foreach (DictionaryEntry pageVal in pageElements)
{
try
{
args.Add(pageVal.Key.ToString(), pageVal.Value);
}
catch (Exception e)
{
HttpContext.Current.Trace.Warn("umbracoMacro", "Could not add page value (" + pageVal.Key + ") to DLR enviroment", e);
}
}
args.Add("currentPage", umbraco.presentation.nodeFactory.Node.GetCurrent());
string path = IOHelper.MapPath(SystemDirectories.Python + "/" + macro.scriptFile);
ret.Text = MacroScript.ExecuteFile(path, args);
return ret;
}
/// (macrosAddedKey));
TraceInfo(loadUserControlKey, string.Format("Usercontrol added with id '{0}'", oControl.ID));
Type type = oControl.GetType();
if (type == null)
{
TraceWarn(loadUserControlKey, "Unable to retrieve control type: " + fileName);
return oControl;
}
foreach (string propertyAlias in properties.Keys)
{
PropertyInfo prop = type.GetProperty(propertyAlias);
if (prop == null)
{
TraceWarn(loadUserControlKey, "Unable to retrieve type from propertyAlias: " + propertyAlias);
continue;
}
object propValue =
helper.FindAttribute(pageElements, attributes, propertyAlias).Replace("&", "&").Replace(
""", "\"").Replace("<", "<").Replace(">", ">");
if (string.IsNullOrEmpty(propValue as string))
continue;
// Special case for types of webControls.unit
try
{
if (prop.PropertyType == typeof(Unit))
propValue = Unit.Parse(propValue.ToString());
else
{
try
{
object o = propertyDefinitions[propertyAlias];
if (o == null)
continue;
TypeCode st = (TypeCode)Enum.Parse(typeof(TypeCode), o.ToString(), true);
// Special case for booleans
if (prop.PropertyType == typeof(bool))
{
bool parseResult;
if (
Boolean.TryParse(
propValue.ToString().Replace("1", "true").Replace("0", "false"),
out parseResult))
propValue = parseResult;
else
propValue = false;
}
else
propValue = Convert.ChangeType(propValue, st);
Trace.Write("macro.loadControlProperties",
string.Format("Property added '{0}' with value '{1}'", propertyAlias,
propValue));
}
catch (Exception PropException)
{
HttpContext.Current.Trace.Warn("macro.loadControlProperties",
string.Format(
"Error adding property '{0}' with value '{1}'",
propertyAlias, propValue), PropException);
}
}
prop.SetValue(oControl, Convert.ChangeType(propValue, prop.PropertyType), null);
}
catch (Exception propException)
{
HttpContext.Current.Trace.Warn("macro.loadControlProperties",
string.Format(
"Error adding property '{0}' with value '{1}', maybe it doesn't exists or maybe casing is wrong!",
propertyAlias, propValue), propException);
}
}
return oControl;
}
catch (Exception e)
{
HttpContext.Current.Trace.Warn("macro", string.Format("Error creating usercontrol ({0})", fileName), e);
return new LiteralControl(
string.Format(
"Error creating control ({0}).
Maybe file doesn't exists or the usercontrol has a cache directive, which is not allowed! See the tracestack for more information!
",
fileName));
}
}
private void TraceInfo(string category, string message)
{
if (HttpContext.Current != null)
HttpContext.Current.Trace.Write(category, message);
}
private void TraceWarn(string category, string message)
{
if (HttpContext.Current != null)
HttpContext.Current.Trace.Warn(category, message);
}
///
/// For debug purposes only - should be deleted or made private
///
/// The type of object (control) to show properties from
public void macroProperties(Type type)
{
PropertyInfo[] myProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
HttpContext.Current.Response.Write("" + type.Name + "
");
foreach (PropertyInfo propertyItem in myProperties)
{
// if (propertyItem.CanWrite)
HttpContext.Current.Response.Write(propertyItem.Name + " (" + propertyItem.PropertyType +
")
");
}
HttpContext.Current.Response.Write("
");
}
public static string renderMacroStartTag(Hashtable attributes, int pageId, Guid versionId)
{
string div = "";
return div;
}
private static string encodeMacroAttribute(string attributeContents)
{
// Replace linebreaks
attributeContents = attributeContents.Replace("\n", "\\n").Replace("\r", "\\r");
// Replace quotes
attributeContents =
attributeContents.Replace("\"", """);
// Replace tag start/ends
attributeContents =
attributeContents.Replace("<", "<").Replace(">", ">");
return attributeContents;
}
public static string renderMacroEndTag()
{
return "
";
}
public static string GetRenderedMacro(int MacroId, page umbPage, Hashtable attributes, int pageId)
{
macro m = new macro(MacroId);
Control c = m.renderMacro(attributes, umbPage.Elements, pageId);
TextWriter writer = new StringWriter();
HtmlTextWriter ht = new HtmlTextWriter(writer);
c.RenderControl(ht);
string result = writer.ToString();
// remove hrefs
string pattern = "href=\"([^\"]*)\"";
MatchCollection hrefs =
Regex.Matches(result, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (Match href in hrefs)
result = result.Replace(href.Value, "href=\"javascript:void(0)\"");
return result;
}
public static string MacroContentByHttp(int PageID, Guid PageVersion, Hashtable attributes)
{
string tempAlias = (attributes["macroalias"] != null) ? attributes["macroalias"].ToString() : attributes["macroAlias"].ToString();
if (!ReturnFromAlias(tempAlias).DontRenderInEditor)
{
string querystring = "umbPageId=" + PageID + "&umbVersionId=" + PageVersion;
IDictionaryEnumerator ide = attributes.GetEnumerator();
while (ide.MoveNext())
querystring += "&umb_" + ide.Key + "=" + HttpContext.Current.Server.UrlEncode(ide.Value.ToString());
// Create a new 'HttpWebRequest' Object to the mentioned URL.
string retVal = string.Empty;
string url = "http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ":" +
HttpContext.Current.Request.ServerVariables["SERVER_PORT"] + IOHelper.ResolveUrl(SystemDirectories.Umbraco) +
"/macroResultWrapper.aspx?" +
querystring;
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = null;
try
{
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
Stream streamResponse = myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
Char[] readBuff = new Char[256];
int count = streamRead.Read(readBuff, 0, 256);
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
retVal += outputData;
count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Find the content of a form
string grabStart = "";
string grabEnd = "";
int grabStartPos = retVal.IndexOf(grabStart) + grabStart.Length;
int grabEndPos = retVal.IndexOf(grabEnd) - grabStartPos;
retVal = retVal.Substring(grabStartPos, grabEndPos);
}
else
retVal = "No macro content available for WYSIWYG editing";
// Release the HttpWebResponse Resource.
myHttpWebResponse.Close();
}
catch
{
retVal = "No macro content available for WYSIWYG editing";
}
finally
{
// Release the HttpWebResponse Resource.
if (myHttpWebResponse != null)
myHttpWebResponse.Close();
}
return retVal.Replace("\n", string.Empty).Replace("\r", string.Empty);
}
else
return "This macro does not provides rendering in WYSIWYG editor";
}
///
/// Adds the XSLT extension namespaces to the XSLT header using
/// {0} as the container for the namespace references and
/// {1} as the container for the exclude-result-prefixes
///
/// The XSLT
///
public static string AddXsltExtensionsToHeader(string xslt)
{
StringBuilder namespaceList = new StringBuilder();
StringBuilder namespaceDeclaractions = new StringBuilder();
foreach (KeyValuePair extension in macro.GetXsltExtensions())
{
namespaceList.Append(extension.Key).Append(' ');
namespaceDeclaractions.AppendFormat("xmlns:{0}=\"urn:{0}\" ", extension.Key);
}
// parse xslt
xslt = xslt.Replace("{0}", namespaceDeclaractions.ToString());
xslt = xslt.Replace("{1}", namespaceList.ToString());
return xslt;
}
}
public class MacroCacheContent
{
private Control _control;
private string _id;
public string ID
{
get { return _id; }
}
public Control Content
{
get { return _control; }
}
public MacroCacheContent(Control control, string ID)
{
_control = control;
_id = ID;
}
}
public class macroCacheRefresh : ICacheRefresher
{
#region ICacheRefresher Members
public string Name
{
get
{
// TODO: Add templateCacheRefresh.Name getter implementation
return "Macro cache refresher";
}
}
public Guid UniqueIdentifier
{
get
{
// TODO: Add templateCacheRefresh.UniqueIdentifier getter implementation
return new Guid("7B1E683C-5F34-43dd-803D-9699EA1E98CA");
}
}
public void RefreshAll()
{
macro.ClearAliasCache();
}
public void Refresh(Guid Id)
{
// Doesn't do anything
}
void ICacheRefresher.Refresh(int Id)
{
new macro(Id).removeFromCache();
}
void ICacheRefresher.Remove(int Id)
{
new macro(Id).removeFromCache();
}
#endregion
}
///
/// Allows App_Code XSLT extensions to be declared using the [XsltExtension] class attribute.
///
///
/// An optional XML namespace can be specified using [XsltExtension("MyNamespace")].
///
[AttributeUsage(AttributeTargets.Class)]
[AspNetHostingPermission(System.Security.Permissions.SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium, Unrestricted = false)]
public class XsltExtensionAttribute : Attribute
{
public XsltExtensionAttribute()
{
this.Namespace = String.Empty;
}
public XsltExtensionAttribute(string ns)
{
this.Namespace = ns;
}
public string Namespace { get; set; }
public override string ToString() { return this.Namespace; }
}
}