00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Xml;
00006 using System.IO;
00007 using System.Xml.Linq;
00008 using Foodolini.BusinessLogic;
00009
00010 namespace Foodolini.Tools
00011 {
00012 public class RecipeMLImporter : RecipeImporter
00013 {
00014
00015 public override Recipe ReadData(System.IO.FileStream fs)
00016 {
00017 Recipe result;
00018 string fileName = Path.GetFileName(fs.Name);
00019 XmlDocument sourceXml = new XmlDocument();
00020
00021 try {
00022 sourceXml.Load(fs);
00023 } catch (XmlException ex) {
00024 OnRaiseImportEvent(new RecipeImporterEventArgs(fileName + ": syntax error, line " + ex.LineNumber,
00025 RecipeImporterEventArgs.EventCode.DataSourceError));
00026 return null;
00027 } finally {
00028 fs.Close();
00029 }
00030
00031 result = new Recipe("");
00032
00033 result.Title = sourceXml.SelectSingleNode("recipeml/recipe/head/title").InnerText;
00034
00035
00036 XmlNode yieldNode = sourceXml.SelectSingleNode("recipeml/recipe/head/yield");
00037 if (yieldNode != null) {
00038 string[] servings = yieldNode
00039 .InnerText.ToUpper().Replace(" ", "")
00040 .Split(new string[] { "-", "TO" }, StringSplitOptions.RemoveEmptyEntries);
00041
00042 try {
00043 if (servings.Length > 1) {
00044 result.Servings = ParseQuantity(new string[] { servings[servings.Length - 1] });
00045 } else
00046 result.Servings = ParseQuantity(servings);
00047 } catch {
00048 OnRaiseImportEvent(
00049 new RecipeImporterEventArgs(fileName + ": servings parse error, " + servings[0],
00050 RecipeImporterEventArgs.EventCode.ServingsParseError));
00051 }
00052 }
00053
00054
00055 List<string> categories = new List<string>();
00056 foreach (XmlNode cat in sourceXml.SelectNodes("recipeml/recipe/head/categories/cat"))
00057 categories.Add(cat.InnerText);
00058
00059
00060
00061
00062 foreach (XmlNode ing in sourceXml.SelectNodes("recipeml/recipe/ingredients/ing")) {
00063
00064
00065 XmlNode amtNode = ing.SelectSingleNode("amt");
00066 XmlNode qtyNode = amtNode.SelectSingleNode("qty");
00067 XmlNode unitNode = amtNode.SelectSingleNode("unit");
00068
00069 if (qtyNode != null) {
00070 string[] parts = qtyNode.InnerText.Replace(".", "").Replace("\"", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
00071
00072 if (parts != null) {
00073 if (parts.Length > 0) {
00074 try {
00075
00076 } catch (FormatException ex) {
00077 OnRaiseImportEvent(
00078 new RecipeImporterEventArgs(fileName + ": quantity parse error, " + parts[0],
00079 RecipeImporterEventArgs.EventCode.QuantityParseError));
00080 return null;
00081 }
00082 }
00083 }
00084 }
00085
00086 if (unitNode != null) {
00087
00088 }
00089
00090 }
00091
00092
00093
00094 List<string> directions = new List<string>();
00095 foreach (XmlNode dir in sourceXml.SelectNodes("recipeml/recipe/directions/step")) {
00096 foreach (string step in dir.InnerText.Split(new string[] { "\n \n" }, StringSplitOptions.RemoveEmptyEntries)) {
00097 if (step.Replace(" ", "").Length > 0)
00098 directions.Add(step);
00099 }
00100 }
00101
00102
00103 return result;
00104 }
00105
00106
00107 }
00108 }