Package org.livesub.input.parsers

Source Code of org.livesub.input.parsers.SrtParser

package org.livesub.input.parsers;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.livesub.utils.doublelinkedlist.DoubleLinkedList;
import org.livesub.input.Caption;
import org.livesub.utils.Utils;

/**
* Read and parse the .srt file
* to the double linked list
*
* @author vasilis
*/
public class SrtParser implements Parser {
   
    @Override
    public DoubleLinkedList<Caption> read(BufferedReader br) {
       
        DoubleLinkedList<Caption> subs = new DoubleLinkedList<Caption>();
       
        String line = null;
        try {
            line = br.readLine();
        } catch (IOException ex) {
            Logger.getLogger(SrtParser.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        Caption caption = null;
        int lineNumber = 0;
               
        while(line != null){
            if (line.matches("[0-9]+:[0-9]+:[0-9]+,[0-9]+\\s-->\\s[0-9]+:[0-9]+:[0-9]+,[0-9]+")) {
                long startTime = Utils.dateParseRegExp(line.split("\\s-->\\s")[0]);
                long endTime = Utils.dateParseRegExp(line.split("\\s-->\\s")[1]);
                caption = new Caption(startTime, endTime);
                caption.setNumericCounter(lineNumber);
            } else if (line.matches("[0-9]+") && caption == null) {
                lineNumber = Integer.parseInt(line);
            } else if (line.matches(".+")) {
                if (caption != null) {
                    caption.setContents(line);
                }
            } else {
                if (caption != null) {
                    subs.put(caption, caption.getNumericCounter());
                    caption = null;
                }
            }
            try {
                line = br.readLine();
            } catch (IOException ex) {
                Logger.getLogger(SrtParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
        if (caption != null) {
            subs.put(caption, caption.getNumericCounter());
        }
       
        return subs;
    }
   
}
TOP

Related Classes of org.livesub.input.parsers.SrtParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.