88
99namespace FreeFrame . Components
1010{
11- static class Importer
11+ public static class Importer
1212 {
1313 static public void Import ( )
1414 {
@@ -17,32 +17,50 @@ static public void Import()
1717 //{
1818 //}
1919 }
20- static public List < Shape > Import ( string filename )
20+ static public List < Shape > ImportFromStream ( Stream pStream )
2121 {
22- if ( ! File . Exists ( filename ) )
23- throw new ArgumentException ( $ "'{ nameof ( filename ) } ' file cannot be found.", nameof ( filename ) ) ; //TODO: replace by a simple alert window
24-
2522 List < Shape > shapes = new List < Shape > ( ) ;
2623
27- using ( XmlReader reader = XmlReader . Create ( filename ) )
24+ using ( XmlReader reader = XmlReader . Create ( pStream ) )
2825 {
29- while ( reader . Read ( ) )
26+ while ( reader . Read ( ) )
3027 {
3128 if ( reader . HasAttributes )
3229 {
3330 Console . WriteLine ( "Attributes of <" + reader . Name + ">" ) ;
3431 switch ( reader . Name )
3532 {
33+ case "xml" :
34+ case "svg" :
35+ case "rect" :
36+ case "path" :
37+ break ;
3638 case "circle" :
3739 shapes . Add ( new SVGCircle ( reader ) ) ;
3840 break ;
3941 default :
42+ // TODO: show all the elemnt are not valid, be careful
4043 break ;
4144 }
4245 }
4346 }
4447 }
4548 return ( shapes ) ;
4649 }
50+ static public List < Shape > ImportFromFile ( string pFilename )
51+ {
52+ if ( ! File . Exists ( pFilename ) )
53+ throw new ArgumentException ( $ "'{ nameof ( pFilename ) } ' file cannot be found.", nameof ( pFilename ) ) ; //TODO: replace by a simple alert window
54+
55+ byte [ ] byteArray = Encoding . UTF8 . GetBytes ( File . ReadAllText ( pFilename ) ) ;
56+
57+ return ImportFromStream ( new MemoryStream ( byteArray ) ) ;
58+ }
59+ static public List < Shape > ImportFromString ( string pString )
60+ {
61+ byte [ ] byteArray = Encoding . UTF8 . GetBytes ( pString ) ;
62+
63+ return ImportFromStream ( new MemoryStream ( byteArray ) ) ;
64+ }
4765 }
4866}
0 commit comments