Search in sources :

Example 51 with NavException

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

the class XLFHandler method getXliffInfo.

/**
	 * 检查所有已读取文件的file节点是否有target-language属性
	 * @return key:filename,value: map(key:file节点的original属性
	 * @throws XPathParseException
	 * @throws NavException
	 * @throws XPathEvalException
	 */
private Map<String, List<XliffBean>> getXliffInfo(boolean checkTgtLang) {
    Map<String, List<XliffBean>> fileNameXliffBeansMap = new HashMap<String, List<XliffBean>>();
    VTDUtils vu = new VTDUtils();
    AutoPilot subAp = new AutoPilot();
    for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
        String fileName = entry.getKey();
        VTDNav vn = entry.getValue();
        try {
            vu.bind(vn);
            subAp.bind(vn);
            subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
            subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"sourcefile\"]");
            AutoPilot ap = new AutoPilot(vn);
            String xpath;
            if (checkTgtLang) {
                xpath = "/xliff/file[not(@target-language)]";
            } else {
                xpath = "/xliff/file";
            }
            // 不存在 target-language 属性。
            ap.selectXPath(xpath);
            while (ap.evalXPath() != -1) {
                vn.push();
                List<XliffBean> xliffBeans = fileNameXliffBeansMap.get(fileName);
                if (xliffBeans == null) {
                    xliffBeans = new ArrayList<XliffBean>();
                    fileNameXliffBeansMap.put(fileName, xliffBeans);
                }
                String sourceLanguage = vu.getCurrentElementAttribut("source-language", "");
                String targetLanguage = vu.getCurrentElementAttribut("target-language", "");
                // 如果是
                String original = vu.getCurrentElementAttribut("original", "");
                // 源文件名
                String sourceFile;
                String datatype = vu.getCurrentElementAttribut("datatype", null);
                if (datatype != null && (datatype.contains("openoffice") || datatype.contains("msoffice2007"))) {
                    // 重置 AutoPilot 对象的储存的信息。
                    subAp.resetXPath();
                    if (subAp.evalXPath() != -1) {
                        sourceFile = vu.getElementContent();
                    } else {
                        sourceFile = original;
                    }
                } else {
                    sourceFile = original;
                }
                XliffBean xliff = null;
                for (XliffBean bean : xliffBeans) {
                    if (bean.getSourceFile().equals(sourceFile)) {
                        xliff = bean;
                    }
                }
                if (xliff == null) {
                    xliff = new XliffBean(datatype, sourceFile, sourceLanguage, targetLanguage, original, fileName);
                    xliffBeans.add(xliff);
                } else {
                    xliff.addOriginal(original);
                }
                vn.pop();
            }
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
    return fileNameXliffBeansMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) VTDNav(com.ximpleware.VTDNav)

Example 52 with NavException

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

the class XLFHandler method addNote.

/**
	 * 添加批注
	 * @param rowId
	 *            行的唯一标识
	 * @param note
	 *            批注内容;
	 */
public void addNote(String rowId, String note) {
    String fileName = RowIdUtil.getFileNameByRowId(rowId);
    String xpath = RowIdUtil.parseRowIdToXPath(rowId);
    VTDNav vn = vnMap.get(fileName);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = vu.insert(xpath + "/text()", "<note>" + note + "</note>");
        saveAndReparse(xm, fileName);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 53 with NavException

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

the class XLFHandler method getLanguages.

/**
	 * 得到所有语言对
	 * @return 语言对的Map<br/>
	 *         key: 源语言;value: 对应的目标语言(可以是多个)
	 */
public Map<String, ArrayList<String>> getLanguages() {
    TreeMap<String, ArrayList<String>> languages = new TreeMap<String, ArrayList<String>>();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    VTDUtils vu = new VTDUtils();
    for (VTDNav vn : vnMap.values()) {
        ap.bind(vn);
        try {
            vu.bind(vn);
            ap.selectXPath("/xliff/file");
            while (ap.evalXPath() != -1) {
                String srcLanguage = vu.getCurrentElementAttribut("source-language", null);
                String tgtLanguage = vu.getCurrentElementAttribut("target-language", null);
                if (srcLanguage == null) {
                    // TODO 该file节点不存在“source-language”属性,提醒添加
                    continue;
                }
                if (tgtLanguage == null) {
                    // TODO 该file节点不存在“target-language”属性,提醒添加
                    continue;
                }
                ArrayList<String> tgtLanguages = languages.get(srcLanguage);
                if (tgtLanguages == null) {
                    tgtLanguages = new ArrayList<String>();
                    languages.put(srcLanguage, tgtLanguages);
                }
                if (!tgtLanguages.contains(tgtLanguage)) {
                    // 未包含,就添加进去
                    tgtLanguages.add(tgtLanguage);
                }
            }
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
    return languages;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) TreeMap(java.util.TreeMap) VTDNav(com.ximpleware.VTDNav)

Example 54 with NavException

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

the class XLFHandler method generateTMXFileContent.

/**
	 * 根据同一个 XLIFF 文件中的 rowId 生成 TMX 文件, 对于不添加到库中的文本段将被忽略
	 * @param list
	 *            rowId
	 * @param srcLang
	 *            源语言
	 * @param monitor
	 *            进度条
	 * @return TMX 文件
	 */
public StringBuffer generateTMXFileContent(String systemUser, List<String> list, String srcLang, String tgtLang, IProgressMonitor monitor, int contextSize, IProject project) {
    if (list == null || list.size() == 0) {
        return null;
    }
    // long start = System.currentTimeMillis();
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.setTaskName(Messages.getString("file.XLFHandler.task4"));
    monitor.beginTask(Messages.getString("file.XLFHandler.task4"), list.size());
    if (monitor.isCanceled()) {
        monitor.setTaskName(Messages.getString("file.XLFHandler.task5"));
        throw new OperationCanceledException();
    }
    StringBuffer re = new StringBuffer();
    re.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    re.append("<tmx version=\"1.4b\">");
    re.append("<header creationtool=\"" + Constants.TMX_CREATIONTOOL + "\" creationtoolversion=\"" + Constants.TMX_CREATIONTOOLVERSION + "\" srclang=\"" + srcLang + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
    re.append("</header>");
    re.append("<body>");
    VTDNav vn = getVTDNavByRowId(list.get(0)).duplicateNav();
    AutoPilot ap = new AutoPilot(vn);
    VTDUtils vu = null;
    try {
        vu = new VTDUtils(vn);
    } catch (NavException e) {
        LOGGER.error(Messages.getString("file.XLFHandler.logger15"), e);
    }
    String strChangeBy = systemUser;
    String strProp = getProjectProp(project);
    boolean isNull = true;
    AutoPilot ap2 = new AutoPilot(vn);
    ap2.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    for (String rowId : list) {
        if (monitor.isCanceled()) {
            re = null;
            monitor.setTaskName(Messages.getString("file.XLFHandler.task5"));
            throw new OperationCanceledException();
        }
        String tgtText = getTgtContent(rowId);
        String srcText = getSrcContent(rowId);
        if (srcText == null || srcText.trim().equals("") || tgtText == null || tgtText.trim().equals("")) {
            monitor.worked(1);
            continue;
        }
        if (isSendToTM(rowId)) {
            monitor.worked(1);
            continue;
        }
        String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
        try {
            ap.selectXPath(tuXPath);
            ap.evalXPath();
        } catch (XPathParseException e) {
            LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
            monitor.done();
        } catch (XPathEvalException e) {
            LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
            monitor.done();
        } catch (NavException e) {
            LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
            monitor.done();
        }
        re.append("<tu ");
        // 添加tu的属性
        String id = vu.getValue("./@id");
        String creationDate = CommonFunction.retTMXDate();
        String changeDate = creationDate;
        re.append("tuid=\"" + genTuId(rowId, id) + "\" ");
        re.append("changedate=\"" + changeDate + "\" changeid=\"" + strChangeBy + "\" ");
        re.append("creationdate=\"" + creationDate + "\" creationid=\"" + strChangeBy + "\" >\n");
        try {
            // Fixed Bug #2290 入库时未添加批注信息 by Jason
            Vector<NoteBean> notes = getNotes(rowId);
            if (notes != null) {
                for (NoteBean bean : notes) {
                    String lang = bean.getLang();
                    if (lang != null && !lang.equals("")) {
                        re.append("<note xml:lang='" + lang + "'>" + bean.getNoteText() + "</note>");
                    } else {
                        re.append("<note>" + bean.getNoteText() + "</note>");
                    }
                }
            }
        } catch (NavException e1) {
            LOGGER.error("", e1);
            e1.printStackTrace();
        } catch (XPathParseException e1) {
            LOGGER.error("", e1);
            e1.printStackTrace();
        } catch (XPathEvalException e1) {
            LOGGER.error("", e1);
            e1.printStackTrace();
        }
        // 添加上下文prop
        re.append("<prop type=\"x-preContext\">");
        vn.push();
        try {
            ap2.selectXPath(tuXPath);
        } catch (XPathParseException e) {
            LOGGER.error("XPathParseException", e);
            e.printStackTrace();
        }
        re.append(getContext(vu, ap2, contextSize, true));
        re.append("</prop>");
        vn.pop();
        vn.push();
        re.append("<prop type=\"x-nextContext\">");
        ap2.resetXPath();
        re.append(getContext(vu, ap2, contextSize, false));
        re.append("</prop>");
        vn.pop();
        // 添加自定义属性
        re.append(strProp);
        re.append("<tuv xml:lang=\"" + srcLang + "\">\n");
        // String sourceTextXp = "./source/text()";
        // re.append("<seg>").append(vu.getValue(ap, sourceTextXp)).append("</seg>");
        re.append("<seg>").append(srcText.trim()).append("</seg>\n");
        re.append("</tuv>\n");
        // 添加tuv(target)
        re.append("<tuv xml:lang=\"" + tgtLang + "\">\n");
        // String targetTextXp = "./target/text()";
        // re.append("<seg>").append(vu.getValue(ap, targetTextXp)).append("</seg>");
        re.append("<seg>").append(tgtText.trim()).append("</seg>\n");
        re.append("</tuv>\n");
        re.append("</tu>\n");
        monitor.worked(1);
        isNull = false;
    }
    re.append("</body>");
    re.append("</tmx>");
    monitor.done();
    if (isNull) {
        return null;
    } else {
        return re;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) NoteBean(net.heartsome.cat.ts.core.bean.NoteBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

Example 55 with NavException

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

the class XLFHandler method updateAndSave.

/**
	 * 修改翻译单元内容并保存
	 * @param rowId
	 * @param subXPath
	 * @param newValue
	 *            ;
	 */
public void updateAndSave(String rowId, String subXPath, String newValue) {
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    String fileName = RowIdUtil.getFileNameByRowId(rowId);
    VTDNav vn = vnMap.get(fileName);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = vu.update(tuXPath + subXPath, newValue, VTDUtils.CREATE_IF_NOT_EXIST);
        saveAndReparse(xm, fileName);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Aggregations

NavException (com.ximpleware.NavException)93 AutoPilot (com.ximpleware.AutoPilot)70 XPathParseException (com.ximpleware.XPathParseException)70 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)63 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)58 XMLModifier (com.ximpleware.XMLModifier)31 ModifyException (com.ximpleware.ModifyException)29 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)21 VTDGen (com.ximpleware.VTDGen)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)13 TranscodeException (com.ximpleware.TranscodeException)12 ParseException (com.ximpleware.ParseException)11 FileOutputStream (java.io.FileOutputStream)11 List (java.util.List)10 LinkedList (java.util.LinkedList)9 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8