Search in sources :

Example 1 with Page

use of net.heartsome.cat.converter.mif.bean.Page in project translationstudio8 by heartsome.

the class MifParser method parse.

public void parse() throws EOFException, MifParseException {
    ch = r.getCharAfterIgnore();
    // the top level element start offset
    int start = 0;
    // the embed element start offset
    int markerFlg = 0;
    while (true) {
        switch(ch) {
            case '<':
                // the statement start offset
                tos = sos - 1;
                String name = getStatmentName();
                nameStack.push(name);
                // System.out.println(name);
                if (name.equals("aframes")) {
                    start = tos;
                } else if (name.equals("tbl")) {
                    start = tos;
                } else if (name.equals("page")) {
                    start = tos;
                } else if (name.equals("textflow")) {
                    start = tos;
                } else if (name.equals("importobject")) {
                    // the ImportObject statement individual parse
                    // the ImportObject statement contained specification of object data
                    parseImportObejct();
                } else if (name.equals("marker")) {
                    // parse the marker element and get the index and cross-ref info
                    // This implement at 2012-08-15 for new requirement of extract the index text
                    markerFlg = tos;
                } else if (name.equals("mtype") && markerFlg != 0) {
                    // check marker type
                    String v = getContentEndBy('>').trim();
                    if (!v.equals("2")) {
                        // 9: cross-ref , 2 : index
                        markerFlg = 0;
                    }
                } else if (name.equals("mtext") && markerFlg != 0) {
                    // extract marker text
                    ch = r.getCharAfterIgnore();
                    if (ch == '`') {
                        int strsos = sos;
                        String text = getValue();
                        int streos = sos;
                        if (text.length() != 0) {
                            Marker m = new Marker(strsos, streos, text);
                            mpbf.appendMarker(m);
                            markerFlg = 0;
                        }
                    }
                }
                break;
            case '`':
                // skip the value
                getValue();
                break;
            case '>':
                if (doc[sos - 2] == '\\') {
                    break;
                }
                if (nameStack.isEmpty()) {
                    throw new MifParseException(Messages.getString("mif.Mif2Xliff.mismatchStartOrEndTag") + formatLineNumber());
                }
                name = nameStack.pop();
                if (name.equals("aframes")) {
                    // System.out.println(new String(doc,start,sos - start));
                    int tempEos = eos;
                    eos = sos;
                    sos = start;
                    parseAframes();
                    eos = tempEos;
                    // rest the record
                    start = 0;
                } else if (name.equals("tbl")) {
                    int tempEos = eos;
                    eos = sos;
                    sos = start;
                    Table tbl = parseTables();
                    if (tbl.validate()) {
                        mpbf.appendTbale(tbl);
                    }
                    eos = tempEos;
                    // rest the record
                    start = 0;
                } else if (name.equals("page")) {
                    int tempEos = eos;
                    eos = sos;
                    sos = start;
                    Page page = parsePage();
                    if (page.validate()) {
                        mpbf.appendPage(page);
                    }
                    eos = tempEos;
                    // rest the record
                    start = 0;
                } else if (name.equals("textflow")) {
                    int tempEos = eos;
                    eos = sos;
                    sos = start;
                    TextFlow tf = parseTextFlow();
                    if (tf.validate()) {
                        mpbf.appendTextFlow(tf);
                    }
                    eos = tempEos;
                    // rest the record
                    start = 0;
                }
                break;
            default:
                break;
        }
        if (sos == eos) {
            return;
        }
        ch = r.getChar();
    }
}
Also used : Table(net.heartsome.cat.converter.mif.bean.Table) MifParseException(net.heartsome.cat.converter.mif.common.MifParseException) Page(net.heartsome.cat.converter.mif.bean.Page) TextFlow(net.heartsome.cat.converter.mif.bean.TextFlow) Marker(net.heartsome.cat.converter.mif.bean.Marker)

Example 2 with Page

use of net.heartsome.cat.converter.mif.bean.Page 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)

Example 3 with Page

use of net.heartsome.cat.converter.mif.bean.Page in project translationstudio8 by heartsome.

the class MifParser method parsePage.

private Page parsePage() throws EOFException, MifParseException {
    Page page = new Page();
    page.setOffset(sos);
    page.setEndOffset(eos);
    int start = 0;
    ch = r.getCharAfterIgnore();
    while (true) {
        switch(ch) {
            case '<':
                tos = sos - 1;
                String name = getStatmentName();
                nameStack.push(name);
                if (name.equals("pagetype")) {
                    String content = getContentEndBy('>');
                    content = content.trim();
                    page.setPageType(content);
                }
                if (name.equals("pagetag")) {
                    ch = r.getCharAfterIgnore();
                    String pageTag = getValue();
                    pageTag = pageTag.trim();
                    page.setPageTag(pageTag);
                }
                if (name.equals("textrect")) {
                    start = tos;
                }
                if (name.equals("importobject")) {
                    // the ImportObject statement individual parse
                    // the ImportObject statement contained specification of object data
                    parseImportObejct();
                }
                break;
            case '`':
                getValue();
                break;
            case '>':
                if (doc[sos - 2] == '\\') {
                    break;
                }
                if (nameStack.isEmpty()) {
                    throw new MifParseException(Messages.getString("mif.Mif2Xliff.mismatchStartOrEndTag") + formatLineNumber());
                }
                name = nameStack.pop();
                if (name.equals("textrect")) {
                    // System.out.println(new String(doc,start,sos - start));
                    int tempEos = eos;
                    eos = sos;
                    sos = start;
                    TextRect tr = parseTextRect();
                    if (tr.validate()) {
                        page.appendTextRect(tr);
                    }
                    eos = tempEos;
                    start = 0;
                }
                break;
            default:
                break;
        }
        if (sos == eos) {
            return page;
        }
        ch = r.getChar();
    }
}
Also used : MifParseException(net.heartsome.cat.converter.mif.common.MifParseException) TextRect(net.heartsome.cat.converter.mif.bean.TextRect) Page(net.heartsome.cat.converter.mif.bean.Page)

Aggregations

Page (net.heartsome.cat.converter.mif.bean.Page)3 Marker (net.heartsome.cat.converter.mif.bean.Marker)2 MifParseException (net.heartsome.cat.converter.mif.common.MifParseException)2 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 Table (net.heartsome.cat.converter.mif.bean.Table)1 TextFlow (net.heartsome.cat.converter.mif.bean.TextFlow)1 TextRect (net.heartsome.cat.converter.mif.bean.TextRect)1 ReaderUtil (net.heartsome.cat.converter.mif.common.ReaderUtil)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1