示例#1
0
 protected override void AddProperties(TokenTree parameters)
 {
     base.AddProperties(parameters);
     AddProperty("SelectedValue", "{Binding Selected}");
     AddProperty("SelectedIndex", 0);
     AddProperty("ItemsSource", "{Binding Keys}");
 }
示例#2
0
 /// <summary>
 /// Adds style information.
 /// </summary>
 /// <param name="styles">List of styles to add.</param>
 /// <param name="parameters">Calculation parameters.</param>
 protected void AddStyles(IReadOnlyList<TokenTree> styles, TokenTree parameters)
 {
     _sb.Append("<style>").AppendLine();
     foreach (TokenTree tokenTree in styles)
     {
         IToken targetType = tokenTree.Value;
         if (!string.IsNullOrWhiteSpace(targetType.Text))
         {
             _sb.Append(tokenTree.Value);
         }
         else
         {
             string key = tokenTree["Name"];
             if (!string.IsNullOrWhiteSpace(key))
                 _sb.Append(".").Append(key);
             else
             {
                 key = tokenTree["ID"];
                 _sb.Append("#").Append(key);
             }
         }
         _sb.Append(" {").AppendLine();
         AddProperties(styles, parameters, tokenTree);
         _sb.Append("}").AppendLine();
     }
     _sb.Append("</style>").AppendLine();
 }
示例#3
0
 /// <summary>
 /// Generates the xaml that is to be displayed.
 /// </summary>
 /// <param name="data">The data the xaml is constructed from.</param>
 /// <returns>The xaml representation of the input.</returns>
 public string GenerateXaml(TokenTree data)
 {
     if (_sb.Generated.Length <= 0)
     {
         TokenTree parameters = new TokenTree(data.GetChildren("Parameters"));
         DataConverter.Parameters = parameters;
         foreach (TokenTree child in parameters.Children)
         {
             string name = child.Name;
             TokenTree defaults = parameters.FindFirst("Defaults." + name);
             if (defaults != null)
                 foreach (TokenTree item in child.Children)
                     item.AddMissing(defaults);
         }
         TokenTreeList fields = data.GetAll("Fields");
         TokenTreeList styles = data.GetAll("Styles");
         _sb.Append(
             "<Border HorizontalAlignment=\"Stretch\" VerticalAlignment=\"Stretch\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">")
             .AppendLine();
         if (styles != null && styles.Count > 0)
             AddStyles(styles, parameters);
         foreach (TokenTree field in fields.SelectMany(child => child.Children))
             _sb.AddElement(field, 1, parameters);
         _sb.Append("</Border>").AppendLine();
     }
     return _sb.Generated;
 }
示例#4
0
        /// <summary>
        /// Generates the html that is to be displayed.
        /// </summary>
        /// <param name="data">The data the html is constructed from.</param>
        /// <param name="selected">The selected item for displaying.</param>
        /// <param name="dataName">The name of the data structure that contains item information.</param>
        /// <param name="keys">List of selectable items.</param>
        /// <returns>The html representation of the input.</returns>
        public string GenerateHtml(TokenTree data, TokenTree selected, string dataName, List<string> keys)
        {
            TokenTree parameters = new TokenTree(data.GetChildren("Parameters"));
            foreach (TokenTree child in parameters.Children)
            {
                string name = child.Name;
                TokenTree defaults = parameters.FindFirst("Defaults." + name);
                if (defaults != null)
                    foreach (TokenTree item in child.Children)
                        item.AddMissing(defaults);
            }
            _sb.Clear();
            TokenTree values = selected.Clone();
            TokenTree defaultValues = parameters.FindFirst("Defaults." + dataName);
            if (defaultValues != null)
                values.AddMissing(defaultValues);
            values.SetParameters(parameters);

            TokenTreeList fields = data.GetAll("Fields");
            TokenTreeList styles = data.GetAll("Styles").FindMatches("Style", true);
            AddStyles(styles, parameters);
            foreach (TokenTree field in fields.SelectMany(child => child.Children).Where(x => x.Name == "Field"))
                _sb.AddElement(field, 0, parameters, values, keys);
            return _sb.Generated;
        }
示例#5
0
 protected internal override void AddChildProperties(Field child, TokenTree parameters)
 {
     base.AddChildProperties(child, parameters);
     if (_columns != null)
     {
         Tuple<int, int> rowAndColumn = _positions.GetNextRowAndColumn();
         int column = rowAndColumn.Item2;
         int row = rowAndColumn.Item1;
         child.AddProperty("Grid.Column", column);
         child.AddProperty("Grid.Row", row);
         TokenTreeList tokenTreeList = child.Children.FindMatches("Across");
         _positions.MakeItemUsed(row, column);
         if (tokenTreeList != null && tokenTreeList.Count == 1)
         {
             string across = tokenTreeList[0]?.Value?.Text;
             if (across != null)
             {
                 string[] bits = across.Split(',');
                 int columns = int.Parse(bits[0]);
                 for (int i = 1; i < columns; ++i)
                     _positions.MakeItemUsed(row, column + i);
                 child.AddProperty("Grid.ColumnSpan", columns);
                 if (bits.Length >= 2)
                 {
                     int rows = int.Parse(bits[1]);
                     child.AddProperty("Grid.RowSpan", rows);
                     for (int col = 0; col < columns; ++col)
                         for (int i = 0; i < rows; ++i)
                             _positions.MakeItemUsed(row + i, column + col);
                 }
             }
         }
     }
 }
示例#6
0
 /// <summary>
 /// Converts a value. 
 /// </summary>
 /// <returns>
 /// A converted value. If the method returns null, the valid null value is used.
 /// </returns>
 /// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     string dataType = parameter.ToString();
     TokenTree items = new TokenTree(DataConverter.Parameters?.GetChildren(dataType));
     List<string> result = new List<string>();
     foreach (TokenTree item in items.Children)
         result.Add(item.Name);
     return result;
 }
示例#7
0
 public Data()
 {
     string dataFile = ConfigurationManager.AppSettings.Get("DataFile");
     _dataName = ConfigurationManager.AppSettings.Get("DataName");
     if (s_allValues == null)
         s_allValues = Parser.ParseFile(dataFile);
     Keys = new List<string>();
     foreach (TokenTree child in s_allValues.Children)
         Keys.Add(child.Name);
 }
示例#8
0
 protected override void AddProperty(string name, IToken value, TokenTree parameters)
 {
     switch (name)
     {
         case "Content":
             name = "Text";
             break;
     }
     base.AddProperty(name, value, parameters);
 }
示例#9
0
        protected override void AddChildren(TokenTree parameters, string endOfLine)
        {
            IEnumerable<TokenTree> fields = GetSubFields();
            _columns = GetColumnData();
            _rows = GetRowData();
            if (_rows != null && _columns == null)
                _columns = new List<string> {_columnWidth};

            if (_columns != null)
                _positions = new GridData(_columns.Count);
            foreach (TokenTree child in fields)
                AddChild(child, Level + 1, parameters, Builder, Offset, endOfLine, this);

            if (_columns != null)
                AddColumnsAndRows();
        }
示例#10
0
 public string GenerateXaml(TokenTree data)
 {
     TokenTree parameters = new TokenTree(data.GetChildren("Parameters"));
     DataConverter.Parameters = parameters;
     foreach (TokenTree child in parameters.Children)
     {
         string name = child.Name;
         TokenTree defaults = DataConverter.Parameters.FindFirst("Defaults." + name);
         if (defaults != null)
             foreach (TokenTree item in child.Children)
                 item.AddMissing(defaults);
     }
     TokenTreeList fields = data.Children.FindMatches("Fields");
     foreach (TokenTree field in fields.SelectMany(child => child.Children))
         Field.AddChild(field, 0, parameters, _sb, _offset, _endOfLine);
     return _sb.ToString();
 }
示例#11
0
 public void TestMatching()
 {
     TokenTreeList children = new TokenTreeList
     {
         new TokenTree(new ListToken {new StringToken("C"), new StringToken("D")}, new StringToken("E")),
         new TokenTree(new StringToken("A"), new StringToken("B")),
         new TokenTree(new RegExToken(".*F", RegExToken.RegexType.Regex), new StringToken("G")),
         new TokenTree(new RegExToken("*H", RegExToken.RegexType.Wildcard), new StringToken("I")),
         new TokenTree(new RegExToken("%J", RegExToken.RegexType.Sql), new StringToken("K"))
     };
     TokenTree toSearch = new TokenTree(children);
     Assert.AreEqual("B", toSearch["A"]);
     Assert.AreEqual("E", toSearch["C"]);
     Assert.AreEqual("E", toSearch["D"]);
     Assert.AreEqual("G", toSearch["IF"]);
     Assert.AreEqual("I", toSearch["EACH"]);
     Assert.AreEqual("K", toSearch["RAJ"]);
 }
示例#12
0
 protected override void AddProperty(string name, IToken value, TokenTree parameters)
 {
     switch (name)
     {
         case "Content":
             base.AddProperty("SelectedValue", value, parameters);
             break;
         case "SelectedItem":
             base.AddProperty("SelectedValue", new StringToken("{Binding " + value.Text + "}"), parameters);
             break;
         case "SelectionOptions":
             AddProperty("ItemsSource", "{Binding Values, Converter={StaticResource ListConverter}, ConverterParameter=" + value.Text + "}");
             break;
         default:
             base.AddProperty(name, value, parameters);
             break;
     }
 }
示例#13
0
 public static Field CreateField(string fieldName, TokenTree data, int level, TokenTree parameters, Field parent)
 {
     Field field;
     switch (fieldName)
     {
         case "ComboBox":
             field = new ComboBox(parent, data, level);
             break;
         case "Selector":
             field = new Selector(parent, data, level);
             break;
         case "Grid":
             field = new Grid(parent, data, level);
             break;
         case "":
         case null:
             field = new Continuation(parent, data, level);
             break;
         case "TextBox":
             field = new TextBox(parent, data, level);
             break;
         default:
             TokenTreeList fields = parameters?.GetAll("Field");
             TokenTree replacement = fields?.FindValue(fieldName);
             if (replacement != null)
             {
                 fieldName = replacement["BasedOn"];
                 foreach (TokenTree child in replacement.Children.Where(x => x.Name != "BasedOn"))
                 {
                     if (child.Name == "Field")
                         data.Children.Add(child);
                     else
                         data.Children.AddIfMissing(child);
                 }
                 field = CreateField(fieldName, data, level, parameters, parent);
             }
             else
             {
                 field = new Field(parent, fieldName, data, level);
             }
             break;
     }
     return field;
 }
示例#14
0
 public TokenTree AddLine(Line line, bool ignoreErrors = false)
 {
     TokenTree tokenTree = Splitter.Split(line.Content, ignoreErrors);
     StringToken key = tokenTree.Key as StringToken;
     if (key != null && key.Text.Contains("."))
     {
         string[] parts = key.Text.Split('.');
         TokenTree tree = new TokenTree(parts[0]);
         TokenTree top = tree;
         for (int i = 1; i < parts.Length - 1; ++i)
         {
             TokenTree child = new TokenTree(parts[i]);
             tree.Children.Add(child);
             tree = child;
         }
         tree.Children.Add(new TokenTree(parts[parts.Length - 1], tokenTree.Value, tokenTree.Children));
         tokenTree = top;
     }
     TokenTree parent = LastAtLevel[line.Offset - 1];
     LastAtLevel[line.Offset] = tokenTree;
     parent.Children.Add(tokenTree);
     return tokenTree;
 }
示例#15
0
 public void TestSimpleGeneration()
 {
     TokenTree toGenerate = new TokenTree
     {
         Key = new StringToken("Field"),
         Value = new StringToken("Grid"),
         Children =
         {
             new TokenTree
             {
                 Key = new StringToken("Field"),
                 Value = new StringToken("Label"),
                 Children =
                 {
                     new TokenTree {Key = new StringToken("Content"), Value = new StringToken("Hi")},
                     new TokenTree {Key = new StringToken("Width"), Value = new StringToken("200")}
                 }
             }
         }
     };
     string generated = new XamlGenerator("").GenerateXaml(toGenerate);
     Assert.AreEqual(XAML, generated);
 }
示例#16
0
        /// <summary>
        /// Adds style information.
        /// </summary>
        /// <param name="styles">List of styles to add.</param>
        /// <param name="parameters">Calculation parameters.</param>
        private void AddStyles(IReadOnlyList<TokenTree> styles, TokenTree parameters)
        {
            _sb.Append("  <Border.Resources>").AppendLine();
            foreach (TokenTree tokenTree in styles[0].Children)
            {
                _sb.Append("    <Style ");

                IToken targetType = tokenTree.Value;
                bool hasType = false;
                if (!string.IsNullOrWhiteSpace(targetType.Text))
                {
                    _sb.Append("TargetType=\"{x:Type ").Append(targetType.Text).Append("}\" ");
                    hasType = true;
                }

                IToken key = tokenTree.Key;
                if (!string.IsNullOrWhiteSpace(key.Text))
                    _sb.Append("x:Key=\"").Append(key.Text).Append("\" ");

                string basedOn = tokenTree["BasedOn"];
                if (!string.IsNullOrWhiteSpace(basedOn))
                    _sb.Append("BasedOn=\"{StaticResource ").Append(basedOn).Append("}\" ");

                _sb.Append(">").AppendLine();

                foreach (TokenTree child in tokenTree.Children.Where(x => x.Name != "Name" && x.Name != "BasedOn"))
                {
                    _sb.Append("      <Setter Property=\"");
                    if (!hasType)
                        _sb.Append("Control.");
                    _sb.Append(child.Name).Append("\" Value=\"")
                        .Append(ProcessTokens(child.Value, new TokenTreeList(parameters))).Append("\"/>").AppendLine();
                }
                _sb.Append("    </Style>").AppendLine();
            }
            _sb.Append("  </Border.Resources>").AppendLine();
        }
 public void TestSimpleGeneration()
 {
     TokenTree toGenerate = new TokenTree
     {
         Key = new StringToken(""),
         Children =
         {
             new TokenTree
             {
                 Key = new StringToken("Fields"),
                 Children =
                 {
                     new TokenTree
                     {
                         Key = new StringToken("Field"),
                         Value = new StringToken("Grid"),
                         Children =
                         {
                             new TokenTree
                             {
                                 Key = new StringToken("Field"),
                                 Value = new StringToken("Label"),
                                 Children =
                                 {
                                     new TokenTree {Key = new StringToken("Content"), Value = new StringToken("Hi")},
                                     new TokenTree {Key = new StringToken("Width"), Value = new StringToken("200")}
                                 }
                             }
                         }
                     }
                 }
             }
         }
     };
     string generated = new XamlGenerator().GenerateXaml(toGenerate);
     Assert.AreEqual(XAML.Replace(" ", ""), generated.Replace(" ", "").Replace(Environment.NewLine, ""));
 }
示例#18
0
 protected override void AddStart(string endOfLine, TokenTree parameters)
 {
     foreach (var child in Children.Where(child => child.Name == "Inputs"))
         parameters.Replace(child);
 }
示例#19
0
 protected internal override void AddChildProperties(Field child, TokenTree parameters)
 {
     Parent.AddChildProperties(child, parameters);
 }
示例#20
0
 public Continuation(Field parent, TokenTree data = null, int level = -1)
     : base(parent, "", data, level)
 {
 }
示例#21
0
 public void SetParameters(TokenTree values)
 {
     _parameters = new TokenTreeList(this) {values};
 }
示例#22
0
 public TokenTree SubstituteParameters(TokenTree tree)
 {
     return new TokenTree(Key.SubstituteParameters(tree), Value.SubstituteParameters(tree)) {Children = Children.SubstituteParameters(tree)};
 }
示例#23
0
 public void Replace(TokenTree inputs)
 {
     Remove(inputs.Name);
     Children.Add(inputs);
 }
示例#24
0
 public void AddMissing(TokenTree defaults)
 {
     foreach (TokenTree child in defaults.Children)
     {
         if (child.Value.Text == "ALL")
         {
             Children.Add(child.Clone());
         }
         else
         {
             bool found = Children.Any(item => item.Name == child.Name);
             if (!found)
                 Children.Add(child.Clone());
         }
     }
 }
示例#25
0
 public Selector(Field parent, TokenTree data, int level)
     : base(parent, data, level)
 {
 }
示例#26
0
        public void TestSubstitution()
        {
            Dictionary<string, string> tests = new Dictionary<string, string>
            {
                ["$($e + '.c')"] = "d",
                ["$$d"] = "4",
                ["{b}"] = "4",
                ["${d}"] = "4",
                ["$($d)"] = "4",
                ["'a' $b + $d 'd'"] = "a4bd",
                ["{$e.$f}"] = "d",
                ["$a"] = "3",
                ["$a + $b"] = "7",
                ["$($e + '.c') + $b"] = "d4",
            //                ["$($e).c + $b"] = "d4", These to be added later possibly
            //                ["${e}.c + $b"] = "d4",
                ["$a.c + $b"] = "d4",
                ["{'a' + 'b'}"] = "5",
                ["{$d + $e}"] = "12"
            };

            TokenTree parameters = new TokenTree
            {
                Children =
                {
                    new TokenTree("a", "3")
                    {
                        Children = {new TokenTree("c", "d")}
                    },
                    new TokenTree("b", "4"),
                    new TokenTree("ab", "5"),
                    new TokenTree("ba", "12"),
                    new TokenTree("d", "b"),
                    new TokenTree("e", "a"),
                    new TokenTree("f", "c")
                }
            };

            TokenGenerator tokenGenerator = new TokenGenerator();
            int passedCount = 0;
            foreach (var test in tests)
            {
                IToken token = tokenGenerator.Parse(test.Key);
                token = token.Evaluate(new TokenTreeList(parameters))[0];
                bool passed = test.Value == token.Text;
                string result = passed ? "passed" : "failed";
                if (passed)
                    ++passedCount;
                Console.WriteLine($"'{test.Key}' {result}.");
            }
            Assert.AreEqual(tests.Count, passedCount);
        }
示例#27
0
 public void Replace(TokenTree inputs)
 {
     TokenTree result = Children.FirstOrDefault(child => child.Name == inputs.Name);
     if (result != null)
         Children.Remove(result);
     Children.Add(inputs);
 }
示例#28
0
 public void UpdateFirstLeaf(TokenTree tree)
 {
     if (tree.Children.Count == 0)
     {
         Children.SetValue(tree.Name, tree.Value);
     }
     else
     {
         TokenTree found = Children.FirstOrDefault(child => child.Name == tree.Name);
         if (found == null)
             Children.Add(tree);
         else
             found.UpdateFirstLeaf(tree.Children[0]);
     }
 }
示例#29
0
 public void AddMissing(TokenTree defaults)
 {
     foreach (TokenTree child in defaults.Children)
     {
         bool found = Children.Any(item => item.Name == child.Name);
         if (!found)
             Children.Add(child.Clone());
     }
 }
示例#30
0
 private static void Initialise()
 {
     if (s_replacements == null)
     {
         s_initialising = true;
         s_replacements = new TokenTree();
         IInputData inputData = IOCContainer.Instance.Resolve<IInputData>();
         if (inputData != null)
         {
             string optionsFile = FileUtils.GetFullFileName(inputData.OptionsFile, inputData.DefaultDirectory);
             if (File.Exists(optionsFile))
                 s_replacements = Parser.Parse(new StreamReader(optionsFile)).FindFirst("Replacements") ?? new TokenTree();
         }
         s_initialising = false;
     }
 }