Initial commit.

This commit is contained in:
Woody Folsom
2012-03-06 11:45:35 -05:00
commit fc3709dda7
13 changed files with 404 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package net.woodyfolsom.cs6601.p2;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("ing")
public class Ingredient {
private String item;
public String getItem() {
return item;
}
}

View File

@@ -0,0 +1,24 @@
package net.woodyfolsom.cs6601.p2;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("recipe")
public class Recipe {
private List<Ingredient> ingredients;
private RecipeHead head;
public RecipeHead getHead() {
return head;
}
public Ingredient getIngredient(int index) {
return ingredients.get(index);
}
public int getIngredientsSize() {
return ingredients.size();
}
}

View File

@@ -0,0 +1,21 @@
package net.woodyfolsom.cs6601.p2;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("recipeml")
public class RecipeBook {
@XStreamImplicit(itemFieldName="recipe")
private List<Recipe> recipes;
public Recipe getRecipe(int index) {
return recipes.get(index);
}
public int getSize() {
return recipes.size();
}
}

View File

@@ -0,0 +1,13 @@
package net.woodyfolsom.cs6601.p2;
import java.io.File;
import com.thoughtworks.xstream.XStream;
public class RecipeBookReader {
private static XStream xstream = XStreamFactory.getInstance();
public static RecipeBook readRecipeBook(File recipeBookFile) {
return (RecipeBook) xstream.fromXML(recipeBookFile);
}
}

View File

@@ -0,0 +1,19 @@
package net.woodyfolsom.cs6601.p2;
import java.io.File;
public class RecipeBookStub {
/**
* @param args
*/
public static void main(String[] args) {
RecipeBook recipeBook = RecipeBookReader.readRecipeBook(new File("data/PastelVasco.xml"));
System.out.println("Read " + recipeBook.getSize() + " recipes.");
for (int i = 0 ; i < recipeBook.getSize(); i++) {
System.out.println("Read recipe: " + recipeBook.getRecipe(i).getHead().getTitle());
}
}
}

View File

@@ -0,0 +1,26 @@
package net.woodyfolsom.cs6601.p2;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("head")
public class RecipeHead {
private String title;
private String source;
private List<String> categories;
public String getSource() {
return source;
}
public String getTitle() {
return title;
}
public boolean isCategoryMatch(String catName) {
return categories.contains(catName);
}
}

View File

@@ -0,0 +1,37 @@
package net.woodyfolsom.cs6601.p2;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.ClassAliasingMapper;
public class XStreamFactory {
public static XStream getInstance() {
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(RecipeBook.class);
xstream.processAnnotations(Recipe.class);
xstream.omitField(Recipe.class, "directions");
xstream.processAnnotations(RecipeHead.class);
ClassAliasingMapper mapper = new ClassAliasingMapper(xstream.getMapper());
mapper.addClassAlias("cat", String.class);
xstream.registerLocalConverter(
RecipeHead.class,
"categories",
new CollectionConverter(mapper)
);
xstream.omitField(RecipeHead.class, "yield");
xstream.omitField(RecipeHead.class, "preptime");
xstream.omitField(Ingredient.class, "amt");
xstream.omitField(Ingredient.class, "prep");
return xstream;
}
}