private static string GenerateText(string text, bool replace, ITextVariables variables)
 {
     if (replace) {
         var replacer = new TextReplacer();
         return replacer.ReplaceVariables(text, variables);
     } else {
         return text;
     }
 }
        private XNode GenerateNode(XNode node, string environment, HashSet<string> allEnvironments, ITextVariables variables, bool replace)
        {
            var element = node as XElement;
            if (element != null) {
                if (IsElementForEnvironment(element, environment, allEnvironments)) {
                    if (IsVarElement(element)) {
                        SetVariable(element, replace, variables);
                        return null;
                    } else {
                        return BuildConfigForEnvironment(element, environment, allEnvironments, replace, variables);
                    }
                } else {
                    return null;
                }
            }

            var text = node as XText;
            if (text != null) {
                if (replace) {
                    var replacer = new TextReplacer();
                    return new XText(replacer.ReplaceVariables(text.Value, variables));
                } else {
                    return node;
                }
            }

            return node;
        }
 private void SetVariable(XElement element, bool replace, ITextVariables variables)
 {
     var name = element.Attributes().First(a => a.Name.LocalName == "name");
     if (name != null)
     {
         var withVars = ReplaceForThisConfig(element, replace);
         var value = GenerateText(element.Value, withVars, variables);
         variables[name.Value] = value;
     }
 }
 private void CopyAttributes(XElement config, ITextVariables variables, bool replace, XElement output)
 {
     foreach (XAttribute attr in config.Attributes()) {
         if (!IsSpecialAttribute(attr)) {
             string value = GenerateText(attr.Value, replace, variables);
             output.Add(new XAttribute(attr.Name, value));
         }
     }
 }
        private void CopyElements(XElement config, string environment, HashSet<string> allEnvironments, bool replace, ITextVariables variables, XElement output)
        {
            foreach (XNode node in config.Nodes()) {
                XNode nodeToAdd = GenerateNode(node, environment, allEnvironments, variables, replace);

                if (nodeToAdd != null) {
                    output.Add(nodeToAdd);
                }
            }
        }
        private XElement BuildConfigForEnvironment(XElement config, string environment, HashSet<string> allEnvironments, bool replace, ITextVariables outerVariables)
        {
            replace = ReplaceForThisConfig(config, replace);

            var output = new XElement(config.Name);
            var variables = outerVariables.InnerFrame;

            CopyAttributes(config, variables, replace, output);
            CopyElements(config, environment, allEnvironments, replace, variables, output);

            return output;
        }
Пример #7
0
 public ParserReplacer(char[] text, ITextVariables variables, TextWriter output)
 {
     this.text = text;
     this.variables = variables;
     this.output = output;
 }
Пример #8
0
 public string ReplaceVariables(string text, ITextVariables variables)
 {
     var output = new StringWriter();
     new ParserReplacer(text.ToCharArray(), variables, output).ParseText(0);
     return output.ToString();
 }
Пример #9
0
 public TextVariablesFrame(IDictionary<string, string> variables, ITextVariables outerFrame)
 {
     this.variables = variables;
     this.outerFrame = outerFrame;
 }
Пример #10
0
 public TextVariablesFrame(ITextVariables outerFrame)
     : this(new Dictionary<string, string>(), outerFrame)
 {
 }