Search in sources :

Example 11 with ModifyException

use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.

the class XLFHandler method mergeSegment.

/**
	 * 合并两个文本段(注意:rowId1 和 rowId2 有可能不相邻,中间可能会间隔几个源文为空的文本段)
	 * @param rowId1
	 *            第一个文本段的 rowId
	 * @param rowId2
	 *            第二个文本段的 rowId;
	 */
public String mergeSegment(String rowId1, String rowId2) {
    // if (rowIndex + 1 >= rowIds.size()) {
    // return "";
    // }
    // String rowId1 = getRowId(rowIndex);
    // String rowId2 = getRowId(rowIndex + 1);
    String fileName1 = RowIdUtil.getFileNameByRowId(rowId1);
    String fileName2 = RowIdUtil.getFileNameByRowId(rowId2);
    if (fileName1 == null || fileName2 == null || !fileName1.equals(fileName2)) {
        return "";
    }
    VTDNav vn = vnMap.get(fileName1);
    // TransUnitBean tuTop = getTransUnit(rowIndex);
    // TransUnitBean tuBottom = getTransUnit(rowIndex + 1);
    // String srcContent = tuTop.getSrcContent() + "<ph id=\"hs-merge" +
    // tuTop.getId() + "~" + tuBottom.getId()
    // + "\"/>" + tuBottom.getSrcContent();
    // tuTop.setSrcContent(srcContent);
    // String tgtContent = tuTop.getTgtContent() + "<ph id=\"hs-merge" +
    // tuTop.getId() + "~" + tuBottom.getId()
    // + "\"/>" + tuBottom.getTgtContent();
    // tuTop.setTgtContent(tgtContent);
    // tuTop.setMatches(null);
    //
    // String tuXPath1 = RowIdUtil.parseRowIdToXPath(rowId1);
    // String tuXPath2 = RowIdUtil.parseRowIdToXPath(rowId2);
    // AutoPilot ap = new AutoPilot(vn);
    //
    // XMLModifier xm = new XMLModifier(vn);
    // delete(ap, xm, fileName2, tuXPath2, true);
    // update(ap, xm, fileName1, tuXPath2, tuTop.toXMLString(), true);
    // ap.selectXPath(tuXPath2);
    //
    // saveAndReparse(xm, fileName1);
    String tuXPath1 = RowIdUtil.parseRowIdToXPath(rowId1);
    String tuXPath2 = RowIdUtil.parseRowIdToXPath(rowId2);
    String tuid1 = RowIdUtil.getTUIdByRowId(rowId1);
    String tuid2 = RowIdUtil.getTUIdByRowId(rowId2);
    // 不需加数据库
    boolean isNotSendToTM = false;
    // 是否是疑问行
    boolean isNeeds_review = false;
    // 是否添加属性“不需添加到数据库”
    boolean isAddNotSendToTm = false;
    // 是否添加属性“疑问行”
    boolean isAddNeeds_review = false;
    StringBuffer nodeSB = new StringBuffer();
    AutoPilot ap = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        String oldElementFragment = "";
        ap.selectXPath(tuXPath1);
        String xmlSpace = null;
        if (ap.evalXPath() != -1) {
            xmlSpace = vu.getCurrentElementAttribut("xml:space", "preserve");
            oldElementFragment += vu.getElementFragment();
            // 获取当前tu节点的属性 hs:send-to-tm="no" hs:needs-review="yes"
            isNotSendToTM = "no".equals(vu.getCurrentElementAttribut("hs:send-to-tm", ""));
            isNeeds_review = "yes".equals(vu.getCurrentElementAttribut("hs:needs-review", ""));
        }
        ap.selectXPath(tuXPath2);
        if (ap.evalXPath() != -1) {
            oldElementFragment += vu.getElementFragment();
            if (!isNotSendToTM) {
                isAddNotSendToTm = "no".equals(vu.getCurrentElementAttribut("hs:send-to-tm", ""));
            }
            if (!isNeeds_review) {
                isAddNeeds_review = "yes".equals(vu.getCurrentElementAttribut("hs:needs-review", ""));
            }
            // 开始获取批注
            ap.selectXPath("./note");
            while (ap.evalXPath() != -1) {
                nodeSB.append(vu.getElementFragment());
            }
        }
        XMLModifier xm = new XMLModifier(vn);
        String sourceContent2 = "";
        String targetContent2 = "";
        ap.selectXPath(tuXPath2 + "/source");
        String srcLang = null;
        if (ap.evalXPath() != -1) {
            srcLang = vu.getCurrentElementAttribut("xml:lang", null);
            sourceContent2 = vu.getElementContent();
            sourceContent2 = sourceContent2 == null ? "" : sourceContent2;
        }
        ap.selectXPath(tuXPath2 + "/target");
        if (ap.evalXPath() != -1) {
            targetContent2 = vu.getElementContent();
            targetContent2 = targetContent2 == null ? "" : targetContent2;
        }
        String curTime = "" + System.nanoTime();
        ap.selectXPath(tuXPath1 + "/source");
        if (ap.evalXPath() != -1) {
            String sourceContent1 = vu.getElementContent();
            sourceContent1 = sourceContent1 == null ? "" : sourceContent1;
            String newValue = sourceContent1 + "<ph id=\"hs-merge" + tuid1 + "~" + tuid2 + "\" splitMergeIndex=\"" + curTime + "\"/>" + sourceContent2;
            vu.update(ap, xm, tuXPath1 + "/source/text()", newValue);
        }
        ap.selectXPath(tuXPath1 + "/target");
        if (ap.evalXPath() != -1) {
            String targetContent1 = vu.getElementContent();
            targetContent1 = targetContent1 == null ? "" : targetContent1;
            String newValue = targetContent1 + "<ph id=\"hs-merge" + tuid1 + "~" + tuid2 + "\" splitMergeIndex=\"" + curTime + "\"/>" + targetContent2;
            vu.update(ap, xm, tuXPath1 + "/target/text()", newValue);
        } else {
            String newValue = "<ph id=\"hs-merge" + tuid1 + "~" + tuid2 + "\" splitMergeIndex=\"" + curTime + "\"/>" + targetContent2;
            vu.insert(ap, xm, tuXPath1 + "/target/text()", newValue);
        }
        // 移除alt-trans节点
        ap.selectXPath(tuXPath1 + "/alt-trans");
        while (ap.evalXPath() != -1) {
            xm.remove();
        }
        ap.selectXPath(tuXPath2);
        if (ap.evalXPath() != -1) {
            // xm.remove();
            // Bug #1054:合并文本段时不应丢失第二个文本段的 trans-unit 节点
            String transUnit = "<trans-unit id=\"" + tuid2 + "\" xml:space=\"" + xmlSpace + "\"><target/>" + "<source xml:lang=\"" + srcLang + "\"/></trans-unit>";
            vu.update(ap, xm, tuXPath2, transUnit);
        }
        // 整合并集中的属性以及批注
        ap.selectXPath(tuXPath1);
        if (ap.evalXPath() != -1) {
            // 注意添加属性时必须保持应有的空格
            String insertAttri = isAddNotSendToTm ? " hs:send-to-tm=\"no\" " : "";
            insertAttri += isAddNeeds_review ? " hs:needs-review=\"yes\" " : "";
            if (insertAttri.length() > 0) {
                xm.insertAttribute(insertAttri);
            }
            // 开始添加批注
            if (nodeSB.length() > 0) {
                xm.insertBeforeTail(nodeSB.toString().getBytes("UTF-8"));
            }
            vn.push();
            // 开始处理合并后的状态,保持草稿状态
            ap.selectXPath("./target");
            if (ap.evalXPath() != -1) {
                int index = vn.getAttrVal("state");
                if (index != -1) {
                    xm.updateToken(index, "new");
                } else {
                    xm.insertAttribute(" state=\"new\" ");
                }
            }
            vn.pop();
            // 删除批准状态
            int index = vn.getAttrVal("approved");
            if (index != -1) {
                xm.removeAttribute(index - 1);
            }
        }
        saveAndReparse(xm, fileName1);
        // 删除下面的行
        rowIds.remove(rowId2);
        tuSizeMap.put(fileName1, tuSizeMap.get(fileName1) - 1);
        return oldElementFragment;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return "";
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ModifyException(com.ximpleware.ModifyException) VTDNav(com.ximpleware.VTDNav)

Example 12 with ModifyException

use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.

the class PreMachineTranslation method executeTranslation.

/**
	 * 根据构建参数执行预翻译 ;
	 * @throws InterruptedException
	 */
public List<PreMachineTranslationCounter> executeTranslation(IProgressMonitor monitor) throws InterruptedException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    simpleMatchers = getMacthers();
    if (null == simpleMatchers || simpleMatchers.isEmpty()) {
        return this.transCounters;
    }
    monitor.beginTask("", this.xlfFiles.size());
    monitor.setTaskName(Messages.getString("pretranslation.PreTranslation.task1"));
    try {
        for (String xlfPath : xlfFiles) {
            if (monitor != null && monitor.isCanceled()) {
                throw new InterruptedException();
            }
            currentCounter = new PreMachineTranslationCounter(xlfPath);
            this.transCounters.add(currentCounter);
            VTDNav vn = xlfHandler.getVnMap().get(xlfPath);
            VTDUtils vu = new VTDUtils(vn);
            AutoPilot ap = new AutoPilot(vu.getVTDNav());
            int tuNumber = xlfHandler.getNodeCount(xlfPath, "/xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]");
            currentCounter.setTuNumber(tuNumber);
            ap.selectXPath("/xliff/file");
            String srcLang = "";
            String tgtLang = "";
            XMLModifier xm = new XMLModifier(vn);
            IProgressMonitor monitor2 = new SubProgressMonitor(monitor, 1);
            monitor2.beginTask(Messages.getString("pretranslation.PreTranslation.task2"), tuNumber);
            while (ap.evalXPath() != -1) {
                // 循环 file 节点
                String _srcLang = vu.getCurrentElementAttribut("source-language", "");
                String _tgtLang = vu.getCurrentElementAttribut("target-language", "");
                if (!_srcLang.equals("")) {
                    srcLang = _srcLang;
                }
                if (!_tgtLang.equals("")) {
                    tgtLang = _tgtLang;
                }
                if (srcLang.equals("") || tgtLang.equals("")) {
                    continue;
                }
                keepCurrentMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
            }
            monitor2.done();
            FileOutputStream fos = new FileOutputStream(xlfPath);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            // 写入文件
            xm.output(bos);
            bos.close();
            fos.close();
        }
    } catch (XPathParseException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        logger.error("", e);
        e.printStackTrace();
    }
    monitor.done();
    return this.transCounters;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) PreMachineTranslationCounter(net.heartsome.cat.ts.machinetranslation.bean.PreMachineTranslationCounter) XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TranscodeException(com.ximpleware.TranscodeException) XPathParseException(com.ximpleware.XPathParseException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) VTDNav(com.ximpleware.VTDNav) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with ModifyException

use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.

the class PreTranslation method executeTranslation.

/**
	 * 根据构建参数执行预翻译 ;
	 * @throws InterruptedException
	 */
public List<PreTranslationCounter> executeTranslation(IProgressMonitor monitor) throws InterruptedException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask("", this.xlfFiles.size());
    monitor.setTaskName(Messages.getString("pretranslation.PreTranslation.task1"));
    try {
        for (String xlfPath : xlfFiles) {
            if (monitor != null && monitor.isCanceled()) {
                throw new InterruptedException();
            }
            currentCounter = new PreTranslationCounter(xlfPath);
            this.transCounters.add(currentCounter);
            VTDNav vn = xlfHandler.getVnMap().get(xlfPath);
            VTDUtils vu = new VTDUtils(vn);
            AutoPilot ap = new AutoPilot(vu.getVTDNav());
            int tuNumber = xlfHandler.getNodeCount(xlfPath, "/xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]");
            currentCounter.setTuNumber(tuNumber);
            ap.selectXPath("/xliff/file");
            String srcLang = "";
            String tgtLang = "";
            XMLModifier xm = new XMLModifier(vn);
            IProgressMonitor monitor2 = new SubProgressMonitor(monitor, 1);
            monitor2.beginTask(Messages.getString("pretranslation.PreTranslation.task2"), tuNumber);
            while (ap.evalXPath() != -1) {
                // 循环 file 节点
                String _srcLang = vu.getCurrentElementAttribut("source-language", "");
                String _tgtLang = vu.getCurrentElementAttribut("target-language", "");
                if (!_srcLang.equals("")) {
                    srcLang = _srcLang;
                }
                if (!_tgtLang.equals("")) {
                    tgtLang = _tgtLang;
                }
                if (srcLang.equals("") || tgtLang.equals("")) {
                    continue;
                }
                if (updateStrategy == PreTransParameters.KEEP_OLD_TARGET) {
                    keepCurrentMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
                } else if (updateStrategy == PreTransParameters.KEEP_BEST_MATCH_TARGET) {
                    keepHigherMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
                } else if (updateStrategy == PreTransParameters.KEEP_NEW_TARGET) {
                    overwriteMatchs(vu, srcLang, tgtLang, xm, monitor2);
                }
            }
            monitor2.done();
            FileOutputStream fos = new FileOutputStream(xlfPath);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            // 写入文件
            xm.output(bos);
            bos.close();
            fos.close();
        }
    } catch (XPathParseException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        logger.error("", e);
        e.printStackTrace();
    }
    monitor.done();
    return this.transCounters;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TranscodeException(com.ximpleware.TranscodeException) XPathParseException(com.ximpleware.XPathParseException) PreTranslationCounter(net.heartsome.cat.ts.pretranslation.bean.PreTranslationCounter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) VTDNav(com.ximpleware.VTDNav) BufferedOutputStream(java.io.BufferedOutputStream)

Example 14 with ModifyException

use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.

the class DatabaseConfiger method saveToFile.

/**
	 * 保存文件
	 * @param xm
	 *            XMLModifier对象
	 * @param fileName
	 *            文件名
	 * @return 是否保存成功;
	 */
private boolean saveToFile(XMLModifier xm, File file) {
    try {
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 写入文件
        xm.output(bos);
        bos.close();
        fos.close();
        return true;
    } catch (ModifyException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        logger.error("", e);
        e.printStackTrace();
    }
    return false;
}
Also used : FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) TranscodeException(com.ximpleware.TranscodeException)

Example 15 with ModifyException

use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.

the class WebSearchPreferencStore method storeConfig.

/**
	 * 保存设置值
	 * @param searchEntry
	 *            ;
	 */
public void storeConfig(List<SearchEntry> searchEntry) {
    FileOutputStream out = null;
    boolean hasError = false;
    try {
        out = new FileOutputStream(customConfigPath);
        writeContent(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        writeContent(out, "<WebSearchInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instancexmlns:xsd=http://www.w3.org/2001/XMLSchema\">\r\n");
        writeContent(out, "	<Providers>\r\n");
        if (null != searchEntry) {
            for (SearchEntry s : searchEntry) {
                writeContent(out, convert2Xml(s));
            }
        }
        writeContent(out, "	</Providers>\r\n </WebSearchInfo>");
        out.flush();
        support.firePropertyChange("URL", null, getUseredConfig(searchEntry));
    } catch (FileNotFoundException e) {
        hasError = true;
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        hasError = true;
        e.printStackTrace();
    } catch (IOException e) {
        hasError = true;
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (hasError) {
            XMLModifier modifier;
            try {
                modifier = new XMLModifier(customVu.getVTDNav());
                modifier.output(customConfigPath);
            } catch (ModifyException e) {
                e.printStackTrace();
                logger.error("", e);
            } catch (TranscodeException e) {
                e.printStackTrace();
                logger.error("", e);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("", e);
            }
        } else {
            try {
                customVu.parseFile(customConfigPath, false);
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error("", e);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("", e);
            }
        }
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ModifyException(com.ximpleware.ModifyException) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) SearchEntry(net.heartsome.cat.ts.websearch.bean.SearchEntry) TranscodeException(com.ximpleware.TranscodeException)

Aggregations

ModifyException (com.ximpleware.ModifyException)35 NavException (com.ximpleware.NavException)27 XMLModifier (com.ximpleware.XMLModifier)25 AutoPilot (com.ximpleware.AutoPilot)20 XPathParseException (com.ximpleware.XPathParseException)20 XPathEvalException (com.ximpleware.XPathEvalException)18 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)18 TranscodeException (com.ximpleware.TranscodeException)17 IOException (java.io.IOException)17 VTDNav (com.ximpleware.VTDNav)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 FileOutputStream (java.io.FileOutputStream)12 BufferedOutputStream (java.io.BufferedOutputStream)10 ArrayList (java.util.ArrayList)7 ParseException (com.ximpleware.ParseException)6 VTDGen (com.ximpleware.VTDGen)6 List (java.util.List)6 LinkedList (java.util.LinkedList)5 FileNotFoundException (java.io.FileNotFoundException)4 HashMap (java.util.HashMap)4