Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Node;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static long getEndTime(Node p) {
        long time = 0;
        Node current = p;
        while ((current = current.getParentNode()) != null) {
            if (current.getAttributes() != null && current.getAttributes().getNamedItem("begin") != null) {
                time += toTime(current.getAttributes().getNamedItem("begin").getNodeValue());
            }
        }

        if (p.getAttributes() != null && p.getAttributes().getNamedItem("end") != null) {
            return time + toTime(p.getAttributes().getNamedItem("end").getNodeValue());
        }
        return time;
    }

    public static long toTime(String expr) {
        Pattern p = Pattern.compile("(-?)([0-9][0-9]):([0-9][0-9]):([0-9][0-9])([\\.:][0-9][0-9]?[0-9]?)?");
        Matcher m = p.matcher(expr);
        if (m.matches()) {
            String minus = m.group(1);
            String hours = m.group(2);
            String minutes = m.group(3);
            String seconds = m.group(4);
            String fraction = m.group(5);
            if (fraction == null) {
                fraction = ".000";
            }

            fraction = fraction.replace(":", ".");
            long ms = Long.parseLong(hours) * 60 * 60 * 1000;
            ms += Long.parseLong(minutes) * 60 * 1000;
            ms += Long.parseLong(seconds) * 1000;
            if (fraction.contains(":")) {
                ms += Double.parseDouble("0" + fraction.replace(":", ".")) * 40 * 1000; // 40ms == 25fps - simplifying assumption should be ok for here
            } else {
                ms += Double.parseDouble("0" + fraction) * 1000;
            }

            return ms * ("-".equals(minus) ? -1 : 1);
        } else {
            throw new RuntimeException("Cannot match '" + expr + "' to time expression");
        }
    }
}