Permalink
Browse files

Refactoring ChunksMakerBuilder and fixing tests

  • Loading branch information...
1 parent 8c43940 commit d0fec687384468b45013594299bdbc5b0041c6e6 @leocwolter leocwolter committed May 14, 2012
Showing with 256 additions and 220 deletions.
  1. +2 −1 src/main/java/br/com/caelum/tubaina/builder/ChapterBuilder.java
  2. +3 −2 src/main/java/br/com/caelum/tubaina/builder/ChunkSplitter.java
  3. +8 −181 src/main/java/br/com/caelum/tubaina/builder/ChunksMakerBuilder.java
  4. +2 −1 src/main/java/br/com/caelum/tubaina/builder/SectionBuilder.java
  5. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/AnswerReplacer.java
  6. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/BoxReplacer.java
  7. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/ExerciseReplacer.java
  8. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/ItemReplacer.java
  9. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/ListReplacer.java
  10. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/NoteReplacer.java
  11. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/QuestionReplacer.java
  12. +199 −0 src/main/java/br/com/caelum/tubaina/builder/replacer/ReplacerType.java
  13. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/TableColumnReplacer.java
  14. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/TableReplacer.java
  15. +1 −1 src/main/java/br/com/caelum/tubaina/builder/replacer/TableRowReplacer.java
  16. +2 −2 src/main/java/br/com/caelum/tubaina/parser/html/kindle/KindleGenerator.java
  17. +14 −13 src/test/java/br/com/caelum/tubaina/builder/ChunksMakerBuilderTest.java
  18. +2 −2 src/test/java/br/com/caelum/tubaina/builder/replacer/ItemReplacerTest.java
  19. +2 −1 src/test/java/br/com/caelum/tubaina/parser/html/desktop/HtmlParserTest.java
  20. +2 −1 src/test/java/br/com/caelum/tubaina/parser/html/desktop/ItemTagTest.java
  21. +2 −1 src/test/java/br/com/caelum/tubaina/parser/html/kindle/ItemTagTest.java
  22. +2 −1 src/test/java/br/com/caelum/tubaina/parser/html/kindle/KindleParserTest.java
  23. +2 −1 src/test/java/br/com/caelum/tubaina/parser/latex/ItemTagTest.java
  24. +4 −3 src/test/java/br/com/caelum/tubaina/parser/latex/LatexParserTest.java
@@ -7,6 +7,7 @@
import br.com.caelum.tubaina.Chapter;
import br.com.caelum.tubaina.Section;
+import br.com.caelum.tubaina.builder.replacer.ReplacerType;
import br.com.caelum.tubaina.chunk.IntroductionChunk;
import br.com.caelum.tubaina.resources.Resource;
@@ -50,7 +51,7 @@ public Chapter build() {
}
}
- IntroductionChunk intro = new IntroductionChunk(new ChunkSplitter(resources, "all").splitChunks(introduction));
+ IntroductionChunk intro = new IntroductionChunk(new ChunkSplitter(resources, ReplacerType.ALL).splitChunks(introduction));
return new Chapter(title, intro, sections, resources);
}
@@ -3,14 +3,15 @@
import java.util.List;
import br.com.caelum.tubaina.Chunk;
+import br.com.caelum.tubaina.builder.replacer.ReplacerType;
import br.com.caelum.tubaina.resources.Resource;
public class ChunkSplitter {
private List<Resource> resources;
- private String replacerType;
+ private ReplacerType replacerType;
- public ChunkSplitter(List<Resource> resources, String replacerType) {
+ public ChunkSplitter(List<Resource> resources, ReplacerType replacerType) {
this.resources = resources;
this.replacerType = replacerType;
}
@@ -6,32 +6,12 @@
import java.util.List;
import java.util.Map;
-import br.com.caelum.tubaina.builder.replacer.AnswerReplacer;
-import br.com.caelum.tubaina.builder.replacer.BoxReplacer;
-import br.com.caelum.tubaina.builder.replacer.CenteredParagraphReplacer;
-import br.com.caelum.tubaina.builder.replacer.CodeReplacer;
-import br.com.caelum.tubaina.builder.replacer.ExerciseReplacer;
-import br.com.caelum.tubaina.builder.replacer.GistReplacer;
-import br.com.caelum.tubaina.builder.replacer.ImageReplacer;
-import br.com.caelum.tubaina.builder.replacer.IndexReplacer;
-import br.com.caelum.tubaina.builder.replacer.ItemReplacer;
-import br.com.caelum.tubaina.builder.replacer.JavaReplacer;
-import br.com.caelum.tubaina.builder.replacer.ListReplacer;
-import br.com.caelum.tubaina.builder.replacer.NoteReplacer;
-import br.com.caelum.tubaina.builder.replacer.ParagraphInsideItemReplacer;
-import br.com.caelum.tubaina.builder.replacer.ParagraphReplacer;
-import br.com.caelum.tubaina.builder.replacer.QuestionReplacer;
import br.com.caelum.tubaina.builder.replacer.Replacer;
-import br.com.caelum.tubaina.builder.replacer.RubyReplacer;
-import br.com.caelum.tubaina.builder.replacer.TableColumnReplacer;
-import br.com.caelum.tubaina.builder.replacer.TableReplacer;
-import br.com.caelum.tubaina.builder.replacer.TableRowReplacer;
-import br.com.caelum.tubaina.builder.replacer.TodoReplacer;
-import br.com.caelum.tubaina.builder.replacer.XmlReplacer;
+import br.com.caelum.tubaina.builder.replacer.ReplacerType;
import br.com.caelum.tubaina.resources.Resource;
public class ChunksMakerBuilder {
- private Map<String, List<Replacer>> replacerMap = new HashMap<String, List<Replacer>>();
+ private Map<ReplacerType, List<Replacer>> replacerMap = new HashMap<ReplacerType, List<Replacer>>();
private String paragraphTerminator = "java|box|code|gist|img|list|xml|exercise|note|answer|item|question|todo|index|ruby|table|row|center";
@@ -48,168 +28,15 @@ public static boolean isClosableTag(String tag) {
}
public ChunksMakerBuilder(List<Resource> resources) {
- List<Replacer> replacers;
-
- // Answer tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new BoxReplacer(resources));
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new ListReplacer(resources));
- replacers.add(new NoteReplacer(resources));
- replacers.add(new XmlReplacer());
- replacers.add(new IndexReplacer(resources));
- replacers.add(new TodoReplacer());
- replacers.add(new RubyReplacer());
- replacers.add(new TableReplacer(resources));
-
- replacers.add(new CenteredParagraphReplacer());
- replacers.add(new ParagraphReplacer(paragraphTerminator));
- replacerMap.put("answer", replacers);
-
- // Box tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new ListReplacer(resources));
- replacers.add(new XmlReplacer());
- replacers.add(new NoteReplacer(resources));
- replacers.add(new IndexReplacer(resources));
- replacers.add(new TodoReplacer());
- replacers.add(new RubyReplacer());
- replacers.add(new TableReplacer(resources));
- replacers.add(new CenteredParagraphReplacer());
- replacers.add(new ParagraphReplacer(paragraphTerminator));
- replacerMap.put("box", replacers);
-
- // Exercise tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new QuestionReplacer(resources));
- replacers.add(new TodoReplacer());
- replacerMap.put("exercise", replacers);
-
- // Item tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new BoxReplacer(resources));
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ExerciseReplacer(resources));
- replacers.add(new ListReplacer(resources));
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new NoteReplacer(resources));
- replacers.add(new XmlReplacer());
- replacers.add(new IndexReplacer(resources));
- replacers.add(new TodoReplacer());
-
- replacers.add(new RubyReplacer());
- replacers.add(new CenteredParagraphReplacer());
- replacers.add(new ParagraphInsideItemReplacer(paragraphTerminator));
- replacerMap.put("item", replacers);
-
- // List tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new ItemReplacer(resources));
- replacers.add(new NoteReplacer(resources));
- replacers.add(new TodoReplacer());
- replacerMap.put("list", replacers);
-
- // Note tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new ListReplacer(resources));
- replacers.add(new XmlReplacer());
- replacers.add(new IndexReplacer(resources));
- replacers.add(new TodoReplacer());
-
- replacers.add(new RubyReplacer());
- replacers.add(new TableReplacer(resources));
- replacers.add(new CenteredParagraphReplacer());
- replacers.add(new ParagraphReplacer(paragraphTerminator));
- replacerMap.put("note", replacers);
-
- // Question tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new AnswerReplacer(resources));
- replacers.add(new BoxReplacer(resources));
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new ListReplacer(resources));
- replacers.add(new NoteReplacer(resources));
- replacers.add(new XmlReplacer());
- replacers.add(new IndexReplacer(resources));
- replacers.add(new TodoReplacer());
- replacers.add(new RubyReplacer());
-
- replacers.add(new TableReplacer(resources));
- replacers.add(new CenteredParagraphReplacer());
- replacers.add(new ParagraphReplacer(paragraphTerminator));
- replacerMap.put("question", replacers);
-
- // Table tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new TableRowReplacer(resources));
- replacers.add(new TodoReplacer());
- replacerMap.put("table", replacers);
-
- // Row tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new TableColumnReplacer(resources));
- replacers.add(new TodoReplacer());
- replacerMap.put("row", replacers);
-
- // Column tag
- replacers = new ArrayList<Replacer>();
- replacers.add(new BoxReplacer(resources));
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ExerciseReplacer(resources));
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new ListReplacer(resources));
- replacers.add(new NoteReplacer(resources));
- replacers.add(new XmlReplacer());
- replacers.add(new TodoReplacer());
-
- replacers.add(new RubyReplacer());
- replacers.add(new ParagraphReplacer(paragraphTerminator));
- replacerMap.put("col", replacers);
-
- // All tags not restricted to others, like ItemTag
- replacers = new ArrayList<Replacer>();
- replacers.add(new BoxReplacer(resources));
- replacers.add(new CodeReplacer());
- replacers.add(new GistReplacer());
- replacers.add(new ExerciseReplacer(resources));
- replacers.add(new ImageReplacer(resources));
- replacers.add(new JavaReplacer());
- replacers.add(new ListReplacer(resources));
- replacers.add(new NoteReplacer(resources));
- replacers.add(new XmlReplacer());
-
- replacers.add(new IndexReplacer(resources));
- replacers.add(new TodoReplacer());
- replacers.add(new RubyReplacer());
- replacers.add(new TableReplacer(resources));
- replacers.add(new CenteredParagraphReplacer());
- replacers.add(new ParagraphReplacer(paragraphTerminator));
- replacerMap.put("all", replacers);
-
+ for (ReplacerType replacerType : ReplacerType.values()) {
+ replacerMap.put(replacerType, replacerType.getReplacers(resources, paragraphTerminator));
+ }
}
- public ChunksMaker build(String tag) {
+ public ChunksMaker build(ReplacerType type) {
ChunksMaker maker = new ChunksMaker();
- if (replacerMap.containsKey(tag)) {
- for (Replacer replacer : replacerMap.get(tag)) {
+ if (replacerMap.containsKey(type)) {
+ for (Replacer replacer : replacerMap.get(type)) {
maker.register(replacer);
}
}
@@ -3,6 +3,7 @@
import java.util.List;
import br.com.caelum.tubaina.Section;
+import br.com.caelum.tubaina.builder.replacer.ReplacerType;
import br.com.caelum.tubaina.resources.Resource;
public class SectionBuilder {
@@ -20,6 +21,6 @@ public SectionBuilder(String sectionTitle, String sectionContent, List<Resource>
}
public Section build() {
- return new Section(sectionTitle, new ChunkSplitter(resources, "all").splitChunks(sectionContent));
+ return new Section(sectionTitle, new ChunkSplitter(resources, ReplacerType.ALL).splitChunks(sectionContent));
}
}
@@ -19,7 +19,7 @@ public AnswerReplacer(List<Resource> resources) {
@Override
public Chunk createChunk(String options, String content) {
- AnswerChunk chunk = new AnswerChunk(new ChunkSplitter(resources, "answer").splitChunks(content));
+ AnswerChunk chunk = new AnswerChunk(new ChunkSplitter(resources, ReplacerType.ANSWER).splitChunks(content));
resources.add(new AnswerResource(chunk));
return chunk;
}
@@ -19,7 +19,7 @@ public BoxReplacer(List<Resource> resources) {
@Override
public Chunk createChunk(String options, String content) {
String title = options.trim();
- List<Chunk> body = new ChunkSplitter(resources, "box").splitChunks(content);
+ List<Chunk> body = new ChunkSplitter(resources, ReplacerType.BOX).splitChunks(content);
return new BoxChunk(title, body);
}
@@ -23,7 +23,7 @@ public ExerciseReplacer(List<Resource> resources) {
@Override
public Chunk createChunk(String options, String content) {
ArrayList<Resource> subResources = new ArrayList<Resource>();
- ChunkSplitter splitter = new ChunkSplitter(subResources, "exercise");
+ ChunkSplitter splitter = new ChunkSplitter(subResources, ReplacerType.EXERCISE);
List<Chunk> chunks = splitter.splitChunks(content);
if (hasAnswer(subResources))
resources.add(new ExerciseResource(ExerciseChunk.getExerciseCount()));
@@ -25,7 +25,7 @@ public ItemReplacer(final List<Resource> resources) {
}
public Chunk createChunk(final String content) {
- return new ItemChunk(new ChunkSplitter(resources, "item").splitChunks(content));
+ return new ItemChunk(new ChunkSplitter(resources, ReplacerType.ITEM).splitChunks(content));
}
public String execute(final String text, final List<Chunk> chunks) {
@@ -18,7 +18,7 @@ public ListReplacer(List<Resource> resources) {
@Override
public Chunk createChunk(String options, String content) {
- ChunkSplitter splitter = new ChunkSplitter(resources, "list");
+ ChunkSplitter splitter = new ChunkSplitter(resources, ReplacerType.LIST);
return new ListChunk(options, splitter.splitChunks(content));
}
@@ -21,7 +21,7 @@ public NoteReplacer(List<Resource> resources) {
@Override
public Chunk createChunk(String options, String content) {
- ChunkSplitter splitter = new ChunkSplitter(resources, "note");
+ ChunkSplitter splitter = new ChunkSplitter(resources, ReplacerType.NOTE);
List<Chunk> list = new ArrayList<Chunk>();
list.add(new MockedChunk(INSTRUCTOR_NOTE));
return new NoteChunk(list, splitter.splitChunks(content));
@@ -18,7 +18,7 @@ public QuestionReplacer(List<Resource> resources) {
@Override
public Chunk createChunk(String options, String content) {
- ChunkSplitter splitter = new ChunkSplitter(resources, "question");
+ ChunkSplitter splitter = new ChunkSplitter(resources, ReplacerType.QUESTION);
List<Chunk> chunks = splitter.splitChunks(content);
return new QuestionChunk(chunks);
}
Oops, something went wrong.

0 comments on commit d0fec68

Please sign in to comment.