예제 #1
0
        public static Person[] Extract(String input)
        {
            var           type   = typeof(Person);
            RegexStr      reg    = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> people = MyParser.GetEntries(input, reg.Value);

            //Mr. John Smith was born on 2001/06/15
            Person[] result = new Person[people.Count];
            int      i      = 0;

            foreach (String s in people)
            {
                Gender   Gender = Gender.m;
                String   FirstName;
                String   LastName;
                DateTime DateOfBirth;
                String[] parts = s.Split(' ');
                if (parts[0].Equals("Mrs."))
                {
                    Gender = Gender.f;
                }
                FirstName   = parts[1];
                LastName    = parts[2];
                DateOfBirth = DateTime.ParseExact(parts[6], "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture);
                Person res = new Person(Gender, FirstName, LastName, DateOfBirth);
                result[i++] = res;
            }
            return(result);
        }
예제 #2
0
        public static Plant[] Extract(String input)
        {
            var           type   = typeof(Plant);
            RegexStr      reg    = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> plants = MyParser.GetEntries(input, reg.Value);

            Plant[] result = new Plant[plants.Count];
            int     i      = 0;

            foreach (String s in plants)
            {
                String   Name;
                String   Type;
                String   Location;
                int      height;
                String[] parts = s.Split(' ');
                Name     = parts[1];
                Type     = parts[2].Substring(1, parts[2].Length - 2);
                Location = parts[3].Substring(0, parts[3].Length - 1);
                height   = Int32.Parse(parts[4].Substring(0, parts[4].Length - 1));


                Plant res = new Plant(Name, Type, height, Location);
                result[i++] = res;
            }
            return(result);
        }
예제 #3
0
        public static FootballClub[] Extract(String input)
        {
            var      type = typeof(FootballClub);
            RegexStr reg  = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));

            List <String> clubs = MyParser.GetEntries(input, reg.Value);

            FootballClub[] result = new FootballClub[clubs.Count];
            int            i      = 0;

            foreach (String s in clubs)
            {
                String   name;
                String   country;
                String   city;
                DateTime established;
                String[] parts = s.Split(' ');

                name    = parts[1].Substring(0, parts[1].Length);
                city    = parts[2].Substring(1, parts[2].Length - 2);
                country = parts[3].Substring(0, parts[3].Length - 1);

                established = DateTime.ParseExact(parts[5].Substring(0, 4), "yyyy", System.Globalization.CultureInfo.InvariantCulture);
                FootballClub res = new FootballClub(name, country, city, established);
                result[i++] = res;
            }
            return(result);
        }
예제 #4
0
        public static Newspaper[] Extract(String input)
        {
            var           type       = typeof(Newspaper);
            RegexStr      reg        = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> newspapers = MyParser.GetEntries(input, reg.Value);

            Newspaper[] result = new Newspaper[newspapers.Count];
            int         i      = 0;

            foreach (String s in newspapers)
            {
                String   Name;
                String   Country;
                String   Frequency;
                String   Subject;
                String[] parts = s.Split(' ');
                Name      = parts[1].Substring(0, parts[1].Length - 1);
                Country   = parts[2];
                Frequency = parts[3].Substring(1, parts[3].Length - 2);
                Subject   = parts[4].Substring(0, parts[4].Length - 1);
                Newspaper res = new Newspaper(Name, Country, Frequency, Subject);
                result[i++] = res;
            }
            return(result);
        }
예제 #5
0
        public static Car[] Extract(String input)
        {
            var           type = typeof(Car);
            RegexStr      reg  = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> cars = MyParser.GetEntries(input, reg.Value);

            Car[] result = new Car[cars.Count];
            int   i      = 0;

            foreach (String s in cars)
            {
                String   Model;
                String   Manufacturer;
                String   Transmission;
                String   Owner;
                String[] parts = s.Split(' ');
                Manufacturer = parts[1];
                Model        = parts[2];
                Transmission = parts[3].Substring(1, parts[3].Length - 2);
                Owner        = parts[4].Substring(0, parts[4].Length - 1);


                Car res = new Car(Model, Manufacturer, Transmission, Owner);
                result[i++] = res;
            }
            return(result);
        }
예제 #6
0
        public static Animal[] Extract(String input)
        {
            var           type    = typeof(Animal);
            RegexStr      reg     = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> animals = MyParser.GetEntries(input, reg.Value);

            //Animal Zebra (equids, Africa)
            Animal[] result = new Animal[animals.Count];
            int      i      = 0;

            foreach (String s in animals)
            {
                String   Name;
                String   Species;
                String   Location;
                String[] parts = s.Split(' ');
                Name     = parts[1];
                Species  = parts[2].Substring(1, parts[2].Length - 2);
                Location = parts[3].Substring(0, parts[3].Length - 1);
                Animal res = new Animal(Name, Species, Location);
                result[i++] = res;
            }
            return(result);
        }
예제 #7
0
        public static Company[] Extract(String input)
        {
            var           type      = typeof(Company);
            RegexStr      reg       = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> companies = MyParser.GetEntries(input, reg.Value);

            //Company Microsoft (IT, USA)
            Company[] result = new Company[companies.Count];
            int       i      = 0;

            foreach (String s in companies)
            {
                String   Name;
                String   Segment;
                String   Location;
                String[] parts = s.Split(' ');
                Name     = parts[1];
                Segment  = parts[2].Substring(1, parts[2].Length - 2);
                Location = parts[3].Substring(0, parts[3].Length - 1);
                Company res = new Company(Name, Segment, Location);
                result[i++] = res;
            }
            return(result);
        }
예제 #8
0
        public static Bicycle[] Extract(String input)
        {
            var           type     = typeof(Bicycle);
            RegexStr      reg      = (RegexStr)type.GetTypeInfo().GetCustomAttribute(typeof(RegexStr));
            List <String> bicycles = MyParser.GetEntries(input, reg.Value);

            Bicycle[] result = new Bicycle[bicycles.Count];
            int       i      = 0;

            foreach (String s in bicycles)
            {
                String   Model;
                String   Manufacturer;
                String   Owner;
                String[] parts = s.Split(' ');
                Manufacturer = parts[1];
                Model        = parts[2];
                Owner        = parts[3].Substring(1, parts[3].Length - 2);

                Bicycle res = new Bicycle(Model, Manufacturer, Owner);
                result[i++] = res;
            }
            return(result);
        }
예제 #9
0
        // main methods: MyParser.SaveToXml(XmlWriteable), puts object into its directory in xml
        // PersonFactory/FootballClubFactory/CompanyFactory.GetFromXml(path), get objects from xml,
        //implement generic interface MyXmlReader (only writeable objects can be read from xml)
        //xmlwriteable objects need to provide xml representation and file name
        public static void Main()
        {
            //   [RegexStr(@"Car \D+ \D+ \((\D+,\D+)\)")]
            //  [RegexStr(@"Bicycle \D+ \D+ \((\D+)\)")]
            String s = " Car Ford GT (mechanic, Joe) 123 Bicycle HTX Pro (Lewis) abc Mr. John Smith was born on 2001/06/15 abc123 Company Microsoft (IT, USA) gdf Animal Zebra (horse, Africa) gdf Mrs. Jane Smith was born on 1999/11/03 32 asd" +
                       "abc 123 FC Dynamo (Kyiv, Ukraine, est. 1927) gf Plant Appletree (fruit, Europe, 2000 sm) dgd Newspaper Times, USA (daily, general) asdad FC Dynamo (Kyiv, Ukraine, est. 1927)";

            Extract[]      ex  = { Person.Extract, FootballClub.Extract, Company.Extract, Animal.Extract, Plant.Extract, Car.Extract, Bicycle.Extract, Newspaper.Extract };
            List <IParsee> res = MyParser.ParseAll(s, ex);

            foreach (IParsee t in res)
            {
                Console.WriteLine(t.ToString());
            }


            Console.WriteLine();
            SaveToXml((Person)res[0]);
            SaveToXml((FootballClub)res[2]);
            SaveToXml((Company)res[4]);
            SaveToXml((Animal)res[5]);
            SaveToXml((Plant)res[6]);
            SaveToXml((Car)res[7]);
            SaveToXml((Bicycle)res[8]);
            SaveToXml((Newspaper)res[9]);
            Console.WriteLine("Saved to xml");
            var folders = new DirectoryInfo(xmlloc).EnumerateDirectories();

            var deserializedEntities = new List <IParsee>();

            foreach (var fldr in folders)
            {
                var files = fldr.EnumerateFiles();

                if (!serializerTypes.ContainsKey(fldr.Name))
                {
                    throw new Exception("Missing serializer or bad folder name");
                }

                Type serializerType = serializerTypes[fldr.Name];


                var serializer = new XmlSerializer(serializerType);

                foreach (var fl in files)
                {
                    using (var stream = new FileStream(fl.FullName, FileMode.Open))
                    {
                        try
                        {
                            deserializedEntities.Add((IParsee)serializer.Deserialize(stream));
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine($"Cannot deserialize file {fl.Name} in {fldr.Name} folder: {exc}");
                        }
                    }
                }
            }

            foreach (var en in deserializedEntities)
            {
                Console.WriteLine(en.ToString());
            }
        }