|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Xml; |
| 7 | +using FreeFrame.Components.Shapes; |
| 8 | + |
| 9 | +namespace FreeFrame.Components |
| 10 | +{ |
| 11 | + public static class Importer |
| 12 | + { |
| 13 | + static public void Import() |
| 14 | + { |
| 15 | + //TODO: find a solution for opening a crossplatform file dialog. Do I need a crossplatform dialog? |
| 16 | + //using (OpenFileDialog openFileDialog = new OpenFileDialog()) |
| 17 | + //{ |
| 18 | + //} |
| 19 | + } |
| 20 | + static public List<Shape> ImportFromStream(Stream pStream) |
| 21 | + { |
| 22 | + List<Shape> shapes = new List<Shape>(); |
| 23 | + |
| 24 | + using (XmlReader reader = XmlReader.Create(pStream)) |
| 25 | + { |
| 26 | + while (reader.Read()) |
| 27 | + { |
| 28 | + if (reader.HasAttributes) |
| 29 | + { |
| 30 | + //Console.WriteLine("Attributes of <" + reader.Name + ">"); |
| 31 | + switch (reader.Name) |
| 32 | + { |
| 33 | + case "xml": |
| 34 | + case "svg": |
| 35 | + break; // Skip knowned elements |
| 36 | + case "polygon": |
| 37 | + shapes.Add(new SVGPolygon(reader)); |
| 38 | + break; |
| 39 | + case "path": |
| 40 | + shapes.Add(new SVGPath(reader)); |
| 41 | + break; |
| 42 | + case "rect": |
| 43 | + shapes.Add(new SVGRectangle(reader)); |
| 44 | + break; |
| 45 | + case "circle": |
| 46 | + shapes.Add(new SVGCircle(reader)); |
| 47 | + break; |
| 48 | + default: |
| 49 | + // TODO: show all the elemnt are not valid, be careful |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return (shapes); |
| 56 | + } |
| 57 | + static public List<Shape> ImportFromFile(string pFilename) |
| 58 | + { |
| 59 | + if (!File.Exists(pFilename)) |
| 60 | + throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window |
| 61 | + |
| 62 | + byte[] byteArray = Encoding.UTF8.GetBytes(File.ReadAllText(pFilename)); |
| 63 | + |
| 64 | + return ImportFromStream(new MemoryStream(byteArray)); |
| 65 | + } |
| 66 | + static public List<Shape> ImportFromString(string pString) |
| 67 | + { |
| 68 | + byte[] byteArray = Encoding.UTF8.GetBytes(pString); |
| 69 | + |
| 70 | + return ImportFromStream(new MemoryStream(byteArray)); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments