Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**
     * Converts a TimeML 1.2 file into a non-tagged TE3 TimeML input
     * get TE3-input, TE3input from tml
     *
     * @param tmlfile
     * @return
     */
    public static String TML2TE3(String tmlfile) {
        String outputfile = null;
        try {
            String line;
            boolean textfound = false;
            String header = "";
            String footer = "";
            String text = "";

            //process header (and dct)/text/footer
            outputfile = tmlfile + ".TE3input";
            BufferedWriter te3writer = new BufferedWriter(new FileWriter(new File(outputfile)));
            BufferedReader TE3inputReader = new BufferedReader(new FileReader(new File(tmlfile)));

            try {

                // read out header
                while ((line = TE3inputReader.readLine()) != null) {
                    if (line.length() > 0) {
                        // break on TEXT
                        if (line.matches(".*<TEXT>.*")) {
                            textfound = true;
                            break;
                        }
                    }
                    header += line + "\n";
                }

                if (!textfound) {
                    throw new Exception("Premature end of file (" + tmlfile + ")");
                }

                // read out text
                while ((line = TE3inputReader.readLine()) != null) {
                    if (line.length() > 0) {
                        // break on TEXT
                        if (line.matches(".*</TEXT>.*")) {
                            textfound = false;
                            break;
                        }
                    }
                    text += line.replaceAll("<[^>]*>", "") + "\n";
                }

                if (textfound) {
                    throw new Exception("Premature end of file (" + tmlfile + ")");
                }

                // read out footer
                while ((line = TE3inputReader.readLine()) != null) {
                    line = line.replaceAll("<(!--|[TSA]LINK|MAKEINSTANCE)[^>]*>", "").trim();
                    if (line.length() > 0) {
                        footer += line + "\n";
                    }
                }

                te3writer.write(header + "\n");
                te3writer.write("\n<TEXT>\n" + text + "</TEXT>\n");
                te3writer.write(footer + "\n");

                System.err.println("Processing file: " + tmlfile);

            } finally {
                if (TE3inputReader != null) {
                    TE3inputReader.close();
                }
                if (te3writer != null) {
                    te3writer.close();
                }
            }
        } catch (Exception e) {
            System.err.println("Errors found (TML_file_utils):\n\t" + e.toString() + "\n");
            if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) {
                e.printStackTrace(System.err);
            }
            return null;
        }
        return outputfile;
    }
}