Example #1
0
 /// <summary>
 /// This is the method called from the other AppDomain to compile and prepare the transformation code for running
 /// </summary>
 /// <remarks>
 /// Note that this method uses an implicit contract with the transformation object.
 /// See the DocComment on TextTransformation for details before changing the dynamic calls from this method.
 /// </remarks>
 /// <returns>True if the transformation run has been properly prepared.</returns>
 public bool PrepareTransformation(TemplateProcessingSession session, string source, ITextTemplatingEngineHost host)
 {
     this.session = session;
     this.host    = host;
     ToStringHelper.FormatProvider = session.FormatProvider;
     assembly = null;
     try
     {
         this.session.AssemblyDirectives.Add(GetType().Assembly.Location);
         this.session.AssemblyDirectives.Add(typeof(ITextTemplatingEngineHost).Assembly.Location);
         assembly = LocateAssembly(session.CacheAssemblies, session.ClassFullName, source, session.TemplateFile, session.Debug, session.Language, session.AssemblyDirectives, session.CompilerOptions);
     }
     catch (Exception ex)
     {
         if (Engine.IsCriticalException(ex))
         {
             throw;
         }
         LogError(Resources.ExceptionWhileRunningCode + string.Format(CultureInfo.CurrentCulture, Resources.Exception, ex), false);
     }
     return(assembly != null);
 }
Example #2
0
        /// <summary>
        /// This is the method called from the other AppDomain to run the transformation
        /// </summary>
        public string PerformTransformation()
        {
            string result = Resources.ErrorOutput;

            if (assembly == null)
            {
                LogError(Resources.ErrorInitializingTransformationObject, false);
                return(result);
            }
            dynamic val = null;

            try
            {
                val = CreateTextTransformation(session.ClassFullName, host, assembly, session.UserTransformationSession, session.BaseClassName);
                if (val != null)
                {
                    try
                    {
                        val.Initialize();
                    }
                    catch (RuntimeBinderException arg)
                    {
                        LogError(Resources.InvalidBaseClass + string.Format(CultureInfo.CurrentCulture, Resources.Exception, arg), false);
                    }
                    catch (Exception ex)
                    {
                        if (Engine.IsCriticalException(ex))
                        {
                            throw;
                        }
                        LogError(Resources.ErrorInitializingTransformationObject + string.Format(CultureInfo.CurrentCulture, Resources.Exception, ex), false);
                    }
                    try
                    {
                        if (!val.Errors.HasErrors && !Errors.HasErrors)
                        {
                            try
                            {
                                result = val.TransformText();
                            }
                            catch (RuntimeBinderException arg2)
                            {
                                LogError(Resources.InvalidBaseClass + string.Format(CultureInfo.CurrentCulture, Resources.Exception, arg2), false);
                            }
                            catch (Exception innerException)
                            {
                                if (Engine.IsCriticalException(innerException))
                                {
                                    throw;
                                }
                                if (innerException.InnerException != null)
                                {
                                    innerException = innerException.InnerException;
                                }
                                if (innerException.Data["TextTemplatingProgress"] != null)
                                {
                                    result = innerException.Data["TextTemplatingProgress"].ToString();
                                }
                                ArgumentNullException ex2 = innerException as ArgumentNullException;
                                if (ex2 != null && StringComparer.OrdinalIgnoreCase.Compare(ex2.ParamName, "objectToConvert") == 0)
                                {
                                    int    lineNum  = 0;
                                    string filename = session.TemplateFile;
                                    if (session.Debug && !TryParseStackTrace(ex2.StackTrace, out lineNum, out filename))
                                    {
                                        filename = session.TemplateFile;
                                    }
                                    LogError(Resources.ExpressionBlockNull + Environment.NewLine + ex2.ToString(), false, filename, lineNum, 0);
                                }
                                else
                                {
                                    int    lineNum2  = 0;
                                    string filename2 = session.TemplateFile;
                                    if (session.Debug && !TryParseStackTrace(innerException.StackTrace, out lineNum2, out filename2))
                                    {
                                        filename2 = session.TemplateFile;
                                    }
                                    LogError(Resources.TransformationErrorPrepend + innerException.ToString(), false, filename2, lineNum2, 0);
                                }
                            }
                        }
                        foreach (dynamic item in val.Errors)
                        {
                            item.ErrorText = Resources.TransformationErrorPrepend + item.ErrorText;
                            Type type = item.GetType();
                            if (type.IsEquivalentTo(typeof(CompilerError)))
                            {
                                Errors.Add(item);
                            }
                            else
                            {
                                Errors.Add(new CompilerError(item.FileName, item.Line, item.Column, item.ErrorNumber, item.ErrorText));
                            }
                        }
                        return(result);
                    }
                    catch (RuntimeBinderException arg3)
                    {
                        LogError(Resources.InvalidBaseClass + string.Format(CultureInfo.CurrentCulture, Resources.Exception, arg3), false);
                        return(result);
                    }
                }
                return(result);
            }
            catch (Exception ex3)
            {
                if (Engine.IsCriticalException(ex3))
                {
                    throw;
                }
                LogError(Resources.ExceptionWhileRunningCode + string.Format(CultureInfo.CurrentCulture, Resources.Exception, ex3), false);
                return(result);
            }
            finally
            {
                (val as IDisposable)?.Dispose();
                assembly = null;
                host     = null;
                session  = null;
            }
        }