Search in sources :

Example 1 with ReaderUtil

use of net.heartsome.cat.converter.mif.common.ReaderUtil in project translationstudio8 by heartsome.

the class XliffReader method readXliffFile.

/**
	 * After parse file, this method read the xliff file from the parse info
	 * @param xlfOs
	 *            the xliff file out put stream
	 * @param iniFile
	 *            the configure file of mif,witch define the ESC
	 * @param isReadMasterPage
	 *            xliff contain the masterpage or not
	 * @param monitor
	 *            the progress monitor
	 * @throws MifParseException
	 * @throws IOException
	 *             ;
	 * @throws UnSuportedFileExcetption
	 */
public void readXliffFile(BufferedWriter xlfOs, String iniFile, boolean isReadMasterPage, IProgressMonitor monitor) throws MifParseException, IOException, UnSuportedFileExcetption {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    rUtil = new ReaderUtil(iniFile);
    File xf = File.createTempFile("xliftemp", ".temp");
    // first read the index
    List<Marker> markers = mpbf.getMarkers();
    if (markers.size() > 0) {
        File f = File.createTempFile("miftemp", ".tmp");
        BufferedWriter tmpWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), mifEncoding));
        try {
            this.xlfOs = new BufferedWriter(new FileWriter(xf));
            // generate the xliff content to temporary file xf
            for (Marker m : markers) {
                outputSegmenet(rUtil.cleanString(m.getContent()), m.getOffset(), m.getEndOffset(), 1);
            }
            // generate temporary file ,the file had extracted index content and contains '%%%index%%%'
            readSkeletonFile(tmpWriter, null);
        } finally {
            if (tmpWriter != null) {
                tmpWriter.close();
            }
            if (this.xlfOs != null) {
                this.xlfOs.close();
            }
        }
        // reload the temporary file
        loadFile(f.getAbsolutePath(), mifEncoding);
        // after reload delete the temporary file
        f.delete();
    }
    // second read the content
    this.xlfOs = xlfOs;
    List<Page> pages = mpbf.getPages();
    monitor.setTaskName(Messages.getString("mif.Mif2Xliff.task3"));
    if (pages.size() == 0) {
        // no pages specified ,direct read the Para statement
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task3"), 1);
        monitor.worked(1);
        readPara();
    } else {
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task3"), pages.size());
        for (Page page : pages) {
            String pageType = page.getPageType().toLowerCase();
            if (pageType.equals("bodypage")) {
                readPage(page);
            } else if (pageType.indexOf("masterpage") != -1 && isReadMasterPage) {
                readPage(page);
            }
            monitor.worked(1);
        }
    }
    BufferedReader r = null;
    try {
        r = new BufferedReader(new FileReader(xf));
        String line = r.readLine();
        while (line != null) {
            this.xlfOs.write(line + "\n");
            line = r.readLine();
        }
    } finally {
        if (r != null) {
            r.close();
        }
        xf.delete();
    }
    monitor.done();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) FileWriter(java.io.FileWriter) ReaderUtil(net.heartsome.cat.converter.mif.common.ReaderUtil) Page(net.heartsome.cat.converter.mif.bean.Page) Marker(net.heartsome.cat.converter.mif.bean.Marker) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Marker (net.heartsome.cat.converter.mif.bean.Marker)1 Page (net.heartsome.cat.converter.mif.bean.Page)1 ReaderUtil (net.heartsome.cat.converter.mif.common.ReaderUtil)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1