Exemplo n.º 1
0
 /// <summary>
 ///   Raises the <see cref="Include" /> event.
 /// </summary>
 /// <param name="e">The <see cref="NetCodeT.IncludeEventArgs" /> instance containing the event data.</param>
 protected virtual void OnInclude(IncludeEventArgs e)
 {
     Include?.Invoke(this, e);
 }
Exemplo n.º 2
0
        /// <summary>
        ///   Pre-processes the template code to handle include-directives.
        /// </summary>
        /// <param name="content">
        ///   The content to pre-process.
        /// </param>
        /// <param name="inclusionStack">
        ///   This structure is used to detect cycles in included content, where one included
        ///   piece of content includes another, which again includes the first.
        /// </param>
        /// <returns>
        ///   The preprocessed code.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <para><paramref name="content" /> is <c>null</c>.</para>
        /// </exception>
        private string PreProcessCode(string content, ISet <string> inclusionStack)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var parser = new TemplateParser(content);

            parser.Parse();

            var sb = new StringBuilder();

            if (inclusionStack.Count == 0)
            {
                sb.AppendFormat("<%@ language {0} %>{1}", parser.Language, Environment.NewLine);
            }

            foreach (var reference in parser.AssemblyReferences)
            {
                sb.AppendFormat("<%@ references {0} %>{1}", reference, Environment.NewLine);
            }
            foreach (var import in parser.NamespaceImports)
            {
                sb.AppendFormat("<%@ using {0} %>{1}", import, Environment.NewLine);
            }

            foreach (var part in parser.CodeParts)
            {
                if (part.StartsWith("<%@", StringComparison.Ordinal))
                {
                    var subContent = part.Substring(3, part.Length - 5).Trim();
                    if (subContent.StartsWith("include ", StringComparison.OrdinalIgnoreCase))
                    {
                        var name = subContent.Substring(7).Trim();
                        if (inclusionStack.Contains(name))
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                              "A cycle is detected in included content, was triggered by {0}, stack already contains {1}",
                                                                              name, string.Join(", ", inclusionStack.ToArray())));
                        }
                        var includeEventArgs = new IncludeEventArgs(name);
                        OnInclude(includeEventArgs);
                        if (!includeEventArgs.Handled)
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                              "Unable to include content named '{0}'", name));
                        }

                        inclusionStack.Add(name);
                        sb.Append(PreProcessCode(includeEventArgs.Content, inclusionStack));
                        inclusionStack.Remove(name);
                    }
                    else
                    {
                        sb.Append(part);
                    }
                }
                else
                {
                    sb.Append(part);
                }
            }

            return(sb.ToString());
        }