Initial commit.
This commit is contained in:
12
src/net/woodyfolsom/cs6601/p2/Ingredient.java
Normal file
12
src/net/woodyfolsom/cs6601/p2/Ingredient.java
Normal 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;
|
||||
}
|
||||
}
|
||||
24
src/net/woodyfolsom/cs6601/p2/Recipe.java
Normal file
24
src/net/woodyfolsom/cs6601/p2/Recipe.java
Normal 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();
|
||||
}
|
||||
}
|
||||
21
src/net/woodyfolsom/cs6601/p2/RecipeBook.java
Normal file
21
src/net/woodyfolsom/cs6601/p2/RecipeBook.java
Normal 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();
|
||||
}
|
||||
}
|
||||
13
src/net/woodyfolsom/cs6601/p2/RecipeBookReader.java
Normal file
13
src/net/woodyfolsom/cs6601/p2/RecipeBookReader.java
Normal 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);
|
||||
}
|
||||
}
|
||||
19
src/net/woodyfolsom/cs6601/p2/RecipeBookStub.java
Normal file
19
src/net/woodyfolsom/cs6601/p2/RecipeBookStub.java
Normal 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
26
src/net/woodyfolsom/cs6601/p2/RecipeHead.java
Normal file
26
src/net/woodyfolsom/cs6601/p2/RecipeHead.java
Normal 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);
|
||||
}
|
||||
}
|
||||
37
src/net/woodyfolsom/cs6601/p2/XStreamFactory.java
Normal file
37
src/net/woodyfolsom/cs6601/p2/XStreamFactory.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user