Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…hip data
  • Loading branch information...
commit c0883c4d6bdc990c22e79abe2a1e70966bb5a978 1 parent 79c6a9f
Richard Clark authored
30 src/main/java/net/nextquestion/pptx2html/adaptors/StaxTokenSource.java
@@ -26,18 +26,38 @@
26 26 /* Use a XMLStreamReader for speed */
27 27 protected XMLStreamReader reader;
28 28
  29 + /* Creates the stream reader */
  30 + final private XMLInputFactory factory;
  31 +
29 32 /* Used to look up the token type numbers for each tag and attribute */
30   - protected Map<String, Integer> string2type = new HashMap<String, Integer>();
  33 + private Map<String, Integer> string2type = new HashMap<String, Integer>();
31 34
32 35 /* Buffers multiple tokens (start, attributes..., stop) for a tag */
33   - protected Queue<Token> tokens = new LinkedList<Token>();
  36 + private Queue<Token> tokens = new LinkedList<Token>();
34 37
35   - public StaxTokenSource(Reader tokenDefinitionsReader, Reader xmlReader) throws IOException, XMLStreamException {
36   - XMLInputFactory factory = XMLInputFactory.newInstance();
37   - this.reader = factory.createXMLStreamReader(xmlReader);
  38 +
  39 + public StaxTokenSource(Reader tokenDefinitionsReader) throws IOException {
  40 + factory = XMLInputFactory.newInstance();
38 41 initMapping(tokenDefinitionsReader);
39 42 }
40 43
  44 + /**
  45 + * @deprecated create the token source and then set the input file
  46 + * @param tokenDefinitionsReader
  47 + * @param xmlReader
  48 + * @throws IOException
  49 + * @throws XMLStreamException
  50 + */
  51 + public StaxTokenSource(Reader tokenDefinitionsReader, Reader xmlReader) throws IOException, XMLStreamException {
  52 + this(tokenDefinitionsReader);
  53 + useReader(xmlReader);
  54 + }
  55 +
  56 +
  57 + public void useReader(Reader xmlReader) throws XMLStreamException {
  58 + this.reader = factory.createXMLStreamReader(xmlReader);
  59 + tokens.clear();
  60 + }
41 61
42 62 public Token nextToken() {
43 63 try {
4 src/main/java/net/nextquestion/pptx2html/model/Slide.java
@@ -2,6 +2,7 @@
2 2
3 3 import java.util.ArrayList;
4 4 import java.util.List;
  5 +import java.util.Map;
5 6
6 7 /**
7 8 * Created by IntelliJ IDEA.
@@ -48,4 +49,7 @@ public void addImageRef(String refText) {
48 49 return imageRefs;
49 50 }
50 51
  52 + public void addRelationships(Map<String, Relationship> relationships) {
  53 + // TODO implement
  54 + }
51 55 }
80 src/main/java/net/nextquestion/pptx2html/translator/PowerpointTranslator.java
... ... @@ -0,0 +1,80 @@
  1 +package net.nextquestion.pptx2html.translator;
  2 +
  3 +import net.nextquestion.pptx2html.adaptors.StaxTokenSource;
  4 +import net.nextquestion.pptx2html.model.Relationship;
  5 +import net.nextquestion.pptx2html.model.Slide;
  6 +import net.nextquestion.pptx2html.parser.RELSParser;
  7 +import net.nextquestion.pptx2html.parser.SlideParser;
  8 +import org.antlr.runtime.CommonTokenStream;
  9 +import org.antlr.runtime.RecognitionException;
  10 +import org.antlr.runtime.TokenStream;
  11 +import sun.misc.Regexp;
  12 +
  13 +import javax.xml.stream.XMLStreamException;
  14 +import java.io.*;
  15 +import java.util.ArrayList;
  16 +import java.util.List;
  17 +import java.util.Map;
  18 +import java.util.regex.Pattern;
  19 +
  20 +/**
  21 + * Created by IntelliJ IDEA.
  22 + * User: rdclark
  23 + * Date: 5/19/11
  24 + * Time: 11:12 AM
  25 + * To change this template use File | Settings | File Templates.
  26 + */
  27 +public class PowerpointTranslator {
  28 +
  29 + final private File explodedPresentation;
  30 + private List<Slide> slides;
  31 +
  32 + final private StaxTokenSource slideTokenSource;
  33 + final private StaxTokenSource relsTokenSource;
  34 +
  35 + public PowerpointTranslator(File explodedPresentation) throws IOException {
  36 + this.explodedPresentation = explodedPresentation;
  37 + slideTokenSource = new StaxTokenSource(new FileReader("target/generated-sources/antlr3/Slide.tokens"));
  38 + relsTokenSource = new StaxTokenSource(new FileReader("target/generated-sources/antlr3/RELS.tokens"));
  39 + }
  40 +
  41 + public List<Slide> getSlides() throws FileNotFoundException, XMLStreamException, RecognitionException {
  42 + if (slides == null) {
  43 + slides = new ArrayList<Slide>();
  44 + // Get a list of the slide files (in order)
  45 + final Pattern namePattern = Pattern.compile("slide\\d+\\.xml");
  46 + File slideFolder = new File(explodedPresentation, "ppt/slides");
  47 + File[] slideFiles = slideFolder.listFiles(new FilenameFilter() {
  48 + public boolean accept(File file, String name) {
  49 + return namePattern.matcher(name).matches();
  50 + }
  51 + });
  52 + // parse relationships and slides
  53 + File relsFolder = new File(slideFolder, "_rels");
  54 + for (File slideFile: slideFiles) {
  55 + // Extract relationships, extract the Slide, merge
  56 + File relsFile = new File(relsFolder, slideFile.getName() + ".rels");
  57 + relsTokenSource.useReader(new FileReader(relsFile));
  58 + RELSParser relsParser = new RELSParser(new CommonTokenStream(relsTokenSource));
  59 + Map<String, Relationship> relationships = relsParser.relationships();
  60 +
  61 + slideTokenSource.useReader(new FileReader(slideFile));
  62 + SlideParser slideParser = new SlideParser(new CommonTokenStream(slideTokenSource));
  63 + Slide slide = slideParser.slide();
  64 + slide.addRelationships(relationships);
  65 + slides.add(slide);
  66 +
  67 + }
  68 + }
  69 + return slides;
  70 + }
  71 +
  72 +
  73 + protected TokenStream getTokenStream(String fileName, String tokenFileName) throws IOException, XMLStreamException {
  74 + Reader tokenReader = new FileReader(tokenFileName);
  75 + StaxTokenSource tokenSource = new StaxTokenSource(tokenReader);
  76 + tokenSource.useReader(new FileReader(fileName));
  77 + return new CommonTokenStream(tokenSource);
  78 + }
  79 +
  80 +}
3  src/test/java/net/nextquestion/pptx2html/parser/ParserTestUtilities.java
@@ -24,7 +24,8 @@
24 24
25 25 protected TokenStream getTokenStream(String fileName, String tokenFileName) throws IOException, XMLStreamException {
26 26 Reader tokenReader = new FileReader(tokenFileName);
27   - StaxTokenSource tokenSource = new StaxTokenSource(tokenReader, new FileReader(fileName));
  27 + StaxTokenSource tokenSource = new StaxTokenSource(tokenReader);
  28 + tokenSource.useReader(new FileReader(fileName));
28 29 return new CommonTokenStream(tokenSource);
29 30 }
30 31 }
43 src/test/java/net/nextquestion/pptx2html/translator/PowerpointTranslatorTest.java
... ... @@ -0,0 +1,43 @@
  1 +package net.nextquestion.pptx2html.translator;
  2 +
  3 +import net.nextquestion.pptx2html.model.Slide;
  4 +import org.antlr.runtime.RecognitionException;
  5 +import org.junit.Before;
  6 +import org.junit.Test;
  7 +
  8 +import javax.xml.stream.XMLStreamException;
  9 +import java.io.File;
  10 +import java.io.FileNotFoundException;
  11 +import java.io.IOException;
  12 +import java.util.List;
  13 +
  14 +import static org.hamcrest.MatcherAssert.assertThat;
  15 +import static org.hamcrest.Matchers.equalTo;
  16 +import static org.hamcrest.Matchers.notNullValue;
  17 +
  18 +/**
  19 + * Created by IntelliJ IDEA.
  20 + * User: rdclark
  21 + * Date: 5/19/11
  22 + * Time: 11:11 AM
  23 + * To change this template use File | Settings | File Templates.
  24 + */
  25 +public class PowerpointTranslatorTest {
  26 +
  27 + private PowerpointTranslator translator;
  28 +
  29 + @Before
  30 + public void prepareTranslator() throws IOException {
  31 + File explodedPresentation = new File("src/test/resources/TestPresentation");
  32 + assert(explodedPresentation.isDirectory());
  33 + translator = new PowerpointTranslator(explodedPresentation);
  34 + }
  35 +
  36 + @Test
  37 + public void translatorCollectsAllSlides() throws XMLStreamException, RecognitionException, FileNotFoundException {
  38 + List<Slide> slides = translator.getSlides();
  39 + assertThat(slides, notNullValue());
  40 + assertThat(slides.size(), equalTo(3));
  41 + }
  42 +
  43 +}

0 comments on commit c0883c4

Please sign in to comment.
Something went wrong with that request. Please try again.