@Override public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs) throws IOException { DataInputStream dataInputStream = new DataInputStream(inputStream); String cueText = dataInputStream.readUTF(); return new Tx3gSubtitle(startTimeUs, new Cue(cueText)); }
/** * Asserts that the Subtitle's cues (which are all part of the event at t=0) are equal to the * expected Cues. * * @param sub The parsed {@link Subtitle} to check. * @param expectedCues Expected {@link Cue}s in order of appearance. */ private static void assertMp4WebvttSubtitleEquals(Subtitle sub, Cue... expectedCues) { assertEquals(1, sub.getEventTimeCount()); assertEquals(0, sub.getEventTime(0)); List<Cue> subtitleCues = sub.getCues(0); assertEquals(expectedCues.length, subtitleCues.size()); for (int i = 0; i < subtitleCues.size(); i++) { List<String> differences = getCueDifferences(subtitleCues.get(i), expectedCues[i]); assertTrue("Cues at position " + i + " are not equal. Different fields are " + Arrays.toString(differences.toArray()), differences.isEmpty()); } }
@Override public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs) throws IOException { try { XmlPullParser xmlParser = xmlParserFactory.newPullParser(); xmlParser.setInput(inputStream, inputEncoding); TtmlSubtitle ttmlSubtitle = null; LinkedList<TtmlNode> nodeStack = new LinkedList<>(); int unsupportedNodeDepth = 0; int eventType = xmlParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { TtmlNode parent = nodeStack.peekLast(); if (unsupportedNodeDepth == 0) { String name = xmlParser.getName(); if (eventType == XmlPullParser.START_TAG) { if (!isSupportedTag(name)) { Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName()); unsupportedNodeDepth++; } else { try { TtmlNode node = parseNode(xmlParser, parent); nodeStack.addLast(node); if (parent != null) { parent.addChild(node); } } catch (ParserException e) { if (strictParsing) { throw e; } else { Log.e(TAG, "Suppressing parser error", e); // Treat the node (and by extension, all of its children) as unsupported. unsupportedNodeDepth++; } } } } else if (eventType == XmlPullParser.TEXT) { parent.addChild(TtmlNode.buildTextNode(xmlParser.getText())); } else if (eventType == XmlPullParser.END_TAG) { if (xmlParser.getName().equals(TtmlNode.TAG_TT)) { ttmlSubtitle = new TtmlSubtitle(nodeStack.getLast(), startTimeUs); } nodeStack.removeLast(); } } else { if (eventType == XmlPullParser.START_TAG) { unsupportedNodeDepth++; } else if (eventType == XmlPullParser.END_TAG) { unsupportedNodeDepth--; } } xmlParser.next(); eventType = xmlParser.getEventType(); } return ttmlSubtitle; } catch (XmlPullParserException xppe) { throw new ParserException("Unable to parse source", xppe); } }
@Override public Subtitle parse(byte[] bytes, int offset, int length) { String cueText = new String(bytes, offset, length); return new Tx3gSubtitle(new Cue(cueText)); }
public void testSingleCueSample() throws ParserException { Subtitle result = parser.parse(SINGLE_CUE_SAMPLE, 0, SINGLE_CUE_SAMPLE.length); Cue expectedCue = new Cue("Hello World"); // Line feed must be trimmed by the parser assertMp4WebvttSubtitleEquals(result, expectedCue); }
public void testTwoCuesSample() throws ParserException { Subtitle result = parser.parse(DOUBLE_CUE_SAMPLE, 0, DOUBLE_CUE_SAMPLE.length); Cue firstExpectedCue = new Cue("Hello World"); Cue secondExpectedCue = new Cue("Bye Bye"); assertMp4WebvttSubtitleEquals(result, firstExpectedCue, secondExpectedCue); }
public void testNoCueSample() throws IOException { Subtitle result = parser.parse(NO_CUE_SAMPLE, 0, NO_CUE_SAMPLE.length); assertMp4WebvttSubtitleEquals(result, new Cue[] {}); }
@Override public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs) throws IOException { try { XmlPullParser xmlParser = xmlParserFactory.newPullParser(); xmlParser.setInput(inputStream, inputEncoding); TtmlSubtitle ttmlSubtitle = null; LinkedList<TtmlNode> nodeStack = new LinkedList<TtmlNode>(); int unsupportedNodeDepth = 0; int eventType = xmlParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { TtmlNode parent = nodeStack.peekLast(); if (unsupportedNodeDepth == 0) { String name = xmlParser.getName(); if (eventType == XmlPullParser.START_TAG) { if (!isSupportedTag(name)) { Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName()); unsupportedNodeDepth++; } else { try { TtmlNode node = parseNode(xmlParser, parent); nodeStack.addLast(node); if (parent != null) { parent.addChild(node); } } catch (ParserException e) { if (strictParsing) { throw e; } else { Log.e(TAG, "Suppressing parser error", e); // Treat the node (and by extension, all of its children) as unsupported. unsupportedNodeDepth++; } } } } else if (eventType == XmlPullParser.TEXT) { parent.addChild(TtmlNode.buildTextNode(xmlParser.getText())); } else if (eventType == XmlPullParser.END_TAG) { if (xmlParser.getName().equals(TtmlNode.TAG_TT)) { ttmlSubtitle = new TtmlSubtitle(nodeStack.getLast(), startTimeUs); } nodeStack.removeLast(); } } else { if (eventType == XmlPullParser.START_TAG) { unsupportedNodeDepth++; } else if (eventType == XmlPullParser.END_TAG) { unsupportedNodeDepth--; } } xmlParser.next(); eventType = xmlParser.getEventType(); } return ttmlSubtitle; } catch (XmlPullParserException xppe) { throw new ParserException("Unable to parse source", xppe); } }
@Override public Subtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs) throws IOException { try { XmlPullParser xmlParser = xmlParserFactory.newPullParser(); xmlParser.setInput(inputStream, inputEncoding); TtmlSubtitle ttmlSubtitle = null; LinkedList<TtmlNode> nodeStack = new LinkedList<TtmlNode>(); int unsupportedTagDepth = 0; int eventType = xmlParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { TtmlNode parent = nodeStack.peekLast(); if (unsupportedTagDepth == 0) { String name = xmlParser.getName(); if (eventType == XmlPullParser.START_TAG) { if (!isSupportedTag(name)) { Log.w(TAG, "Ignoring unsupported tag: " + xmlParser.getName()); unsupportedTagDepth++; } else { TtmlNode node = parseNode(xmlParser, parent); nodeStack.addLast(node); if (parent != null) { parent.addChild(node); } } } else if (eventType == XmlPullParser.TEXT) { parent.addChild(TtmlNode.buildTextNode(xmlParser.getText())); } else if (eventType == XmlPullParser.END_TAG) { if (xmlParser.getName().equals(TtmlNode.TAG_TT)) { ttmlSubtitle = new TtmlSubtitle(nodeStack.getLast(), startTimeUs); } nodeStack.removeLast(); } } else { if (eventType == XmlPullParser.START_TAG) { unsupportedTagDepth++; } else if (eventType == XmlPullParser.END_TAG) { unsupportedTagDepth--; } } xmlParser.next(); eventType = xmlParser.getEventType(); } return ttmlSubtitle; } catch (XmlPullParserException xppe) { throw new IOException("Unable to parse source", xppe); } }