private int PerformTemplate(TemplateNode template, XmlDocument xml_doc, ExcelDocument xlsx_doc, int offset) { xlsx_doc.RestartRows(); xlsx_doc.AddRow(); List <string> columns = new List <string>(); var nodes = xml_doc.SelectNodes(template.Path); for (int i = 0; i < nodes.Count; i++) { var node = nodes[i]; var values = template.Apply(node); if (values == null) { continue; } List <string> row = new List <string>(columns.Count); foreach (var value in values) { int index = columns.IndexOf(value.Key); if (index < 0) { index = columns.Count; xlsx_doc.AddColumn(offset + columns.Count); columns.Add(value.Key); } while (row.Count <= index) { row.Add(""); } row[index] = value.Value; } while (row.Count < columns.Count) { row.Add(""); } xlsx_doc.AddRow(offset, row); int progress = (i + 1) * 100 / nodes.Count; if (Progress != progress) { Progress = progress; ProgressChanged?.Invoke(this, EventArgs.Empty); } } for (int i = 0; i < columns.Count; i++) { xlsx_doc[offset + i, 0] = columns[i]; } return(columns.Count); }
private void ConvertButton_Click(object sender, EventArgs e) { try { var xml_path = XMLTextBox.Text; if (!File.Exists(xml_path)) { ErrorMessenger.ShowNoFileError(this, xml_path); return; } var xlsx_path = XLSXTextBox.Text; if (!xlsx_path.ToLower().EndsWith(".xlsx")) { xlsx_path += ".xlsx"; } try { xlsx_path = Path.GetFullPath(xlsx_path); } catch { ErrorMessenger.ShowBadFileNameError(this, xlsx_path); return; } List <TemplateNode> template; try { template = TemplateNode.ParseLines(TemplateTextBox.Lines); } catch (Exception ex) { ErrorMessenger.ShowBadTemplateError(this, ex.Message); return; } var converter = new Converter(xml_path, xlsx_path, template); ConvertButton.Enabled = false; ConverterBackgroundWorker.RunWorkerAsync(converter); } catch (Exception ex) { MessageBox.Show(this, ex.ToString(), "Error: Can not convert XML file.", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static List <TemplateNode> ParseLines(string[] lines) { var result = new List <TemplateNode>(); TemplateNode current = null; for (int i = 0; i < lines.Length; i++) { var line = lines[i].Trim(); if (line.Length <= 0 || line[0] == '#') { continue; } if (line == "===-===") { break; } var pattern = EscapeValues(line); var values = ExtractValues(line); var attribs = ExtractAttribs(line); if (attribs == null) { throw new Exception("[Unclosed quotes]" + i + ": " + line); } if (IsPatternPath(pattern)) { if (values.Length != 1) { throw new Exception("[Values count]" + i + ": " + line); } if (attribs.Length != 1) { throw new Exception("[Attributes count]" + i + ": " + line); } if (attribs[0] != "") { throw new Exception("[Attributes]" + i + ": " + line); } var node = new TemplateNode(); node.Path = values[0]; if (current == null) { result.Add(node); } else { current.SubNodes.Add(node); node.Parrent = current; } current = node; } else if (IsPatternCondition(pattern)) { if (current == null) { throw new Exception("[No current node]" + i + ": " + line); } if (values.Length != 2) { throw new Exception("[Values count]" + i + ": " + line); } if (attribs.Length != 2) { throw new Exception("[Attributes count]" + i + ": " + line); } if (attribs[0] != "") { throw new Exception("[Attributes]" + i + ": " + line); } if (attribs[1].Contains('$')) { throw new Exception("[Attributes]" + i + ": " + line); } if (attribs[1].Contains('.') && attribs[1].Contains('*')) { throw new Exception("[Attributes]" + i + ": " + line); } current.Filters.Add(new KeyValuePair <Unit, Unit>(new Unit(attribs[0], values[0]), new Unit(attribs[1], values[1]))); } else if (IsPatternPrefix(pattern)) { if (current == null) { throw new Exception("[No current node]" + i + ": " + line); } foreach (var attrib in attribs) { if (attrib.Length != 0 && (attrib.Length != 1 || attrib[0] != '!')) { throw new Exception("[Attributes]" + i + ": " + line); } } for (int a = 0; a < attribs.Length; a++) { current.Prefixes.Add(new Unit(attribs[a], values[a])); } } else if (line.Contains(':')) { if (current == null) { throw new Exception("[No current node]" + i + ": " + line); } if (values.Length != 2) { throw new Exception("[Values count]" + i + ": " + line); } if (attribs.Length != 2) { throw new Exception("[Attributes count]" + i + ": " + line); } if (attribs[0].Length != 0 && (attribs[0].Length != 1 || attribs[0][0] != '$')) { throw new Exception("[Attributes]" + i + ": " + line); } if (attribs[1].Length != 0 && (attribs[1].Length != 1 || attribs[1][0] != '!')) { throw new Exception("[Attributes]" + i + ": " + line); } if (current.Values.ContainsKey(new Unit(values[0]))) { throw new Exception("[Duplicated key]" + i + ": " + line); } current.Values.Add(new Unit(attribs[0], values[0]), new Unit(attribs[1], values[1])); } else if (line == "{") { // Nothing to do here } else if (line == "}") { current = current.Parrent; } else { throw new Exception("[Bad format]" + i + ": " + line); } } return(result); }