Permalink
Browse files

Top-level translator is collecting and combining slides and relations…

…hip data
  • Loading branch information...
1 parent 79c6a9f commit c0883c4d6bdc990c22e79abe2a1e70966bb5a978 @rdclark committed May 19, 2011
View
30 src/main/java/net/nextquestion/pptx2html/adaptors/StaxTokenSource.java
@@ -26,18 +26,38 @@
/* Use a XMLStreamReader for speed */
protected XMLStreamReader reader;
+ /* Creates the stream reader */
+ final private XMLInputFactory factory;
+
/* Used to look up the token type numbers for each tag and attribute */
- protected Map<String, Integer> string2type = new HashMap<String, Integer>();
+ private Map<String, Integer> string2type = new HashMap<String, Integer>();
/* Buffers multiple tokens (start, attributes..., stop) for a tag */
- protected Queue<Token> tokens = new LinkedList<Token>();
+ private Queue<Token> tokens = new LinkedList<Token>();
- public StaxTokenSource(Reader tokenDefinitionsReader, Reader xmlReader) throws IOException, XMLStreamException {
- XMLInputFactory factory = XMLInputFactory.newInstance();
- this.reader = factory.createXMLStreamReader(xmlReader);
+
+ public StaxTokenSource(Reader tokenDefinitionsReader) throws IOException {
+ factory = XMLInputFactory.newInstance();
initMapping(tokenDefinitionsReader);
}
+ /**
+ * @deprecated create the token source and then set the input file
+ * @param tokenDefinitionsReader
+ * @param xmlReader
+ * @throws IOException
+ * @throws XMLStreamException
+ */
+ public StaxTokenSource(Reader tokenDefinitionsReader, Reader xmlReader) throws IOException, XMLStreamException {
+ this(tokenDefinitionsReader);
+ useReader(xmlReader);
+ }
+
+
+ public void useReader(Reader xmlReader) throws XMLStreamException {
+ this.reader = factory.createXMLStreamReader(xmlReader);
+ tokens.clear();
+ }
public Token nextToken() {
try {
View
4 src/main/java/net/nextquestion/pptx2html/model/Slide.java
@@ -2,6 +2,7 @@
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
/**
* Created by IntelliJ IDEA.
@@ -48,4 +49,7 @@ public void addImageRef(String refText) {
return imageRefs;
}
+ public void addRelationships(Map<String, Relationship> relationships) {
+ // TODO implement
+ }
}
View
80 src/main/java/net/nextquestion/pptx2html/translator/PowerpointTranslator.java
@@ -0,0 +1,80 @@
+package net.nextquestion.pptx2html.translator;
+
+import net.nextquestion.pptx2html.adaptors.StaxTokenSource;
+import net.nextquestion.pptx2html.model.Relationship;
+import net.nextquestion.pptx2html.model.Slide;
+import net.nextquestion.pptx2html.parser.RELSParser;
+import net.nextquestion.pptx2html.parser.SlideParser;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.TokenStream;
+import sun.misc.Regexp;
+
+import javax.xml.stream.XMLStreamException;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rdclark
+ * Date: 5/19/11
+ * Time: 11:12 AM
+ * To change this template use File | Settings | File Templates.
+ */
+public class PowerpointTranslator {
+
+ final private File explodedPresentation;
+ private List<Slide> slides;
+
+ final private StaxTokenSource slideTokenSource;
+ final private StaxTokenSource relsTokenSource;
+
+ public PowerpointTranslator(File explodedPresentation) throws IOException {
+ this.explodedPresentation = explodedPresentation;
+ slideTokenSource = new StaxTokenSource(new FileReader("target/generated-sources/antlr3/Slide.tokens"));
+ relsTokenSource = new StaxTokenSource(new FileReader("target/generated-sources/antlr3/RELS.tokens"));
+ }
+
+ public List<Slide> getSlides() throws FileNotFoundException, XMLStreamException, RecognitionException {
+ if (slides == null) {
+ slides = new ArrayList<Slide>();
+ // Get a list of the slide files (in order)
+ final Pattern namePattern = Pattern.compile("slide\\d+\\.xml");
+ File slideFolder = new File(explodedPresentation, "ppt/slides");
+ File[] slideFiles = slideFolder.listFiles(new FilenameFilter() {
+ public boolean accept(File file, String name) {
+ return namePattern.matcher(name).matches();
+ }
+ });
+ // parse relationships and slides
+ File relsFolder = new File(slideFolder, "_rels");
+ for (File slideFile: slideFiles) {
+ // Extract relationships, extract the Slide, merge
+ File relsFile = new File(relsFolder, slideFile.getName() + ".rels");
+ relsTokenSource.useReader(new FileReader(relsFile));
+ RELSParser relsParser = new RELSParser(new CommonTokenStream(relsTokenSource));
+ Map<String, Relationship> relationships = relsParser.relationships();
+
+ slideTokenSource.useReader(new FileReader(slideFile));
+ SlideParser slideParser = new SlideParser(new CommonTokenStream(slideTokenSource));
+ Slide slide = slideParser.slide();
+ slide.addRelationships(relationships);
+ slides.add(slide);
+
+ }
+ }
+ return slides;
+ }
+
+
+ protected TokenStream getTokenStream(String fileName, String tokenFileName) throws IOException, XMLStreamException {
+ Reader tokenReader = new FileReader(tokenFileName);
+ StaxTokenSource tokenSource = new StaxTokenSource(tokenReader);
+ tokenSource.useReader(new FileReader(fileName));
+ return new CommonTokenStream(tokenSource);
+ }
+
+}
View
3 src/test/java/net/nextquestion/pptx2html/parser/ParserTestUtilities.java
@@ -24,7 +24,8 @@
protected TokenStream getTokenStream(String fileName, String tokenFileName) throws IOException, XMLStreamException {
Reader tokenReader = new FileReader(tokenFileName);
- StaxTokenSource tokenSource = new StaxTokenSource(tokenReader, new FileReader(fileName));
+ StaxTokenSource tokenSource = new StaxTokenSource(tokenReader);
+ tokenSource.useReader(new FileReader(fileName));
return new CommonTokenStream(tokenSource);
}
}
View
43 src/test/java/net/nextquestion/pptx2html/translator/PowerpointTranslatorTest.java
@@ -0,0 +1,43 @@
+package net.nextquestion.pptx2html.translator;
+
+import net.nextquestion.pptx2html.model.Slide;
+import org.antlr.runtime.RecognitionException;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.xml.stream.XMLStreamException;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rdclark
+ * Date: 5/19/11
+ * Time: 11:11 AM
+ * To change this template use File | Settings | File Templates.
+ */
+public class PowerpointTranslatorTest {
+
+ private PowerpointTranslator translator;
+
+ @Before
+ public void prepareTranslator() throws IOException {
+ File explodedPresentation = new File("src/test/resources/TestPresentation");
+ assert(explodedPresentation.isDirectory());
+ translator = new PowerpointTranslator(explodedPresentation);
+ }
+
+ @Test
+ public void translatorCollectsAllSlides() throws XMLStreamException, RecognitionException, FileNotFoundException {
+ List<Slide> slides = translator.getSlides();
+ assertThat(slides, notNullValue());
+ assertThat(slides.size(), equalTo(3));
+ }
+
+}

0 comments on commit c0883c4

Please sign in to comment.