Search in sources :

Example 81 with NavException

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

the class XLFHandler method getCustomerInfo.

/**
	 * 得到用户定制的信息(customer prop-group 的信息)
	 * @param fileName
	 * @return 以{ {key1, value1}, {key2, value2} }二维数组的形式返回;
	 */
public String[][] getCustomerInfo(String fileName) {
    ArrayList<String[]> customerInfo = new ArrayList<String[]>();
    VTDNav vn = vnMap.get(fileName);
    AutoPilot ap = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        ap.selectXPath("/xliff/file/header/hs:prop-group[@name='customer']/hs:prop");
        while (ap.evalXPath() != -1) {
            String key = vu.getCurrentElementAttribut("prop-type", "");
            String value = vn.toString(vn.getText());
            String[] entry = { key, value };
            customerInfo.add(entry);
        }
    } 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 customerInfo.toArray(new String[][] {});
}
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) VTDNav(com.ximpleware.VTDNav)

Example 82 with NavException

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

the class XLFHandler method changeSomeTranslateProp.

/**
	 * 若当前目标文本段(target)内容不为空,则自动将其 state 属性值设为“translated”
	 * @param rowIdList
	 *            要修改的翻译单元Id的集合;
	 */
public void changeSomeTranslateProp(List<String> rowIdList) {
    if (rowIdList == null) {
        return;
    }
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIdList);
    VTDNav vn = null;
    AutoPilot ap = new AutoPilot();
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIds = entry.getValue();
        vn = vnMap.get(fileName);
        ap.bind(vn);
        boolean isNew;
        for (int i = 0; i < rowIds.size(); i++) {
            ap.resetXPath();
            String rowId = rowIds.get(i);
            String tuXPath = RowIdUtil.parseRowIdToXPathWithCondition(rowId, "(not(source='')) and (not(target=''))");
            try {
                isNew = false;
                ap.selectXPath(tuXPath + "/target/@state");
                int stateIdx = -1;
                if ((stateIdx = ap.evalXPath()) != -1) {
                    String state = vn.toNormalizedString(stateIdx + 1);
                    if ("new".equalsIgnoreCase(state)) {
                        isNew = true;
                    }
                }
                if (!isNew && rowIdList.size() > i) {
                    // state不为“new”
                    rowIdList.remove(i);
                }
            // else { // state为“new”,则继续找下一个
            //
            // }
            } catch (XPathParseException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            } catch (XPathEvalException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            } catch (NavException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
    }
    if (rowIdList.size() > 0) {
        changeTgtPropValue(rowIdList, "state", "translated", "new");
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Example 83 with NavException

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

the class XLFHandler method batchUpdateAltTrans.

public void batchUpdateAltTrans(int[] rowIndexs, Map<Integer, List<AltTransBean>> newAltTransList, Map<Integer, List<String>> oldAltTransToolIdList) {
    String preFileName = "";
    XMLModifier xm = null;
    VTDUtils vu = new VTDUtils();
    for (int rowIndex : rowIndexs) {
        String rowId = getRowId(rowIndex);
        String fileName = RowIdUtil.getFileNameByRowId(rowId);
        try {
            if (preFileName.equals("")) {
                preFileName = fileName;
                vu.bind(vnMap.get(fileName));
                xm = new XMLModifier(vu.getVTDNav());
                preFileName = fileName;
            } else if (!preFileName.equals(fileName)) {
                saveAndReparse(xm, fileName);
                vu.bind(vnMap.get(fileName));
                xm = new XMLModifier(vu.getVTDNav());
                preFileName = fileName;
            }
            updateAltTrans(vu, xm, rowId, newAltTransList.get(rowIndex), oldAltTransToolIdList.get(rowIndex));
        } catch (NavException e) {
            LOGGER.error("", e);
        } catch (ModifyException e) {
            LOGGER.error("", e);
        }
    }
    saveAndReparse(xm, preFileName);
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException)

Example 84 with NavException

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

the class QAXmlHandler method getNodeFrag.

/**
	 * 获取整个节点,包括其头部,其子节点,其文本
	 * @param xlfPath
	 * @param nodeXPath	节点的xpath
	 * @return
	 * robert	2011-10-21
	 */
public String getNodeFrag(String xlfPath, String nodeXPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    String xliffNodeContent = "";
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(nodeXPath);
        VTDUtils vu = new VTDUtils(vn);
        if (ap.evalXPath() != -1) {
            xliffNodeContent = vu.getElementFragment();
        }
    } catch (XPathParseException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
    } catch (NavException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
    } catch (XPathEvalException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
    }
    return xliffNodeContent;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav)

Example 85 with NavException

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

the class XLFValidator method validateXliffFile.

/**
	 * 验证 XLIFF 文件的语言对是否与项目的语言对一致。
	 * @param iFile
	 *            ;
	 * @throws XPathEvalException
	 * @throws XPathParseException
	 * @throws NavException
	 */
public static boolean validateXliffFile(IFile iFile) {
    shell = Display.getDefault().getActiveShell();
    try {
        Object[] arrObj = getProjectLang(iFile);
        if (arrObj == null) {
            return false;
        }
        String xlfFolderPath = iFile.getProject().getFullPath().append(Constant.FOLDER_XLIFF).toOSString();
        String xlfFullPath = iFile.getFullPath().toOSString();
        if (!xlfFullPath.startsWith(xlfFolderPath) || iFile.getParent().getFullPath().toOSString().equals(xlfFolderPath)) {
            // 该 XLIFF 文件是 XLIFF 目录的直接子文件或者不在 XLIFF 的目录下
            if (!blnMsg1) {
                MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg1"), xlfFullPath), Messages.getString("file.XLFValidator.toggleStateMsg"), false, null, null);
                blnMsg1 = dialog.getToggleState();
            }
            return false;
        }
        Language projectSrcLang = (Language) arrObj[0];
        @SuppressWarnings("unchecked") List<Language> lstProjectTgtLang = (List<Language>) arrObj[1];
        // /test/XLIFF/zh-CN/split/test.xlf (zh-CN 在第三级目录下)
        String parentName = xlfFullPath.split(System.getProperty("file.separator").replaceAll("\\\\", "\\\\\\\\"))[3];
        Vector<String> languageVector = new Vector<String>();
        languageVector.add(parentName);
        if (LocaleService.verifyLanguages(languageVector)) {
            boolean flag = false;
            for (Language lang : lstProjectTgtLang) {
                if (lang.getCode().equalsIgnoreCase(parentName)) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                if (!blnMsg1) {
                    MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg2"), xlfFullPath), Messages.getString("file.XLFValidator.toggleStateMsg"), false, null, null);
                    blnMsg1 = dialog.getToggleState();
                }
                return false;
            }
        } else {
            if (!blnMsg1) {
                MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg3"), xlfFullPath), Messages.getString("file.XLFValidator.toggleStateMsg"), false, null, null);
                blnMsg1 = dialog.getToggleState();
            }
            return false;
        }
        String xlfSrcLang = null;
        String xlfTgtLang = null;
        VTDGen vg = new VTDGen();
        XMLModifier xm = null;
        boolean isConfirmSrc = false;
        boolean isConfirmTgt = false;
        String fileOsPath = ResourceUtils.iFileToOSPath(iFile);
        boolean result = false;
        try {
            result = vg.parseFile(fileOsPath, true);
        } catch (Exception e) {
        }
        if (!result) {
            MessageDialog.openError(shell, Messages.getString("file.XLFValidator.errorTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.parseError"), fileOsPath));
            return false;
        }
        VTDNav vn = vg.getNav();
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
        ap.selectXPath("/xliff/file");
        String original = null;
        int tempi = ap.evalXPath();
        if (tempi == -1) {
            MessageDialog.openError(shell, Messages.getString("file.XLFValidator.errorTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.parseError"), fileOsPath));
            return false;
        }
        do {
            xlfSrcLang = vu.getCurrentElementAttribut("source-language", null);
            xlfTgtLang = vu.getCurrentElementAttribut("target-language", null);
            original = vu.getCurrentElementAttribut("original", null);
            if (original == null || original.trim().isEmpty()) {
                MessageDialog.openWarning(shell, Messages.getString("file.XLFValidator.warningTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg10"), xlfFullPath));
                return false;
            }
            String msg = null;
            // XLIFF 源语言为空或与项目源语言不一致;
            if (xlfSrcLang == null || !xlfSrcLang.equalsIgnoreCase(projectSrcLang.getCode())) {
                if (!blnIsOpenConfirmSrc && !isConfirmSrc) {
                    if (xlfSrcLang == null) {
                        msg = MessageFormat.format(Messages.getString("file.XLFValidator.msg4"), xlfFullPath, projectSrcLang.getCode());
                    } else {
                        msg = MessageFormat.format(Messages.getString("file.XLFValidator.msg5"), xlfFullPath, xlfSrcLang.toLowerCase(), projectSrcLang.getCode());
                    }
                    MessageDialogWithToggle dialog = new MessageDialogWithToggle(shell, Messages.getString("file.XLFValidator.msgTitle2"), null, msg, MessageDialog.CONFIRM, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, Messages.getString("file.XLFValidator.toggleStateMsg"), false);
                    int returnCode = dialog.open();
                    if (returnCode == IDialogConstants.YES_ID) {
                        isConfirmSrc = true;
                        blnIsOpenConfirmSrcY = true;
                    } else if (returnCode == IDialogConstants.NO_ID) {
                        isConfirmSrc = false;
                        blnIsOpenConfirmSrcY = false;
                    }
                    blnIsOpenConfirmSrc = dialog.getToggleState();
                }
                if ((isConfirmSrc || (blnIsOpenConfirmSrc && blnIsOpenConfirmSrcY)) && xlfSrcLang != null) {
                    xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@source-language", projectSrcLang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
                } else if (!isConfirmSrc && !(blnIsOpenConfirmSrc && blnIsOpenConfirmSrcY)) {
                    return false;
                }
            }
            // XLIFF 目标语言为空,(且其所在的 XLIFF 一级子目录名称是项目的目标语言代码之一,已在上面验证)直接设置
            if (xlfTgtLang == null || xlfSrcLang == null) {
                // XLIFF 文件中源与目标都为空时,由于 VTD 要求不能在一个位置修改两次,因此使用下面的方式插入源与目标到 file 节点中
                if (xlfTgtLang == null && xlfSrcLang == null) {
                    String attrFragment = new StringBuffer(" source-language=\"").append(projectSrcLang.getCode()).append("\" target-language=\"").append(parentName).append("\"").toString();
                    // attrName="attrValue"
                    // ”
                    // 得到开始标记的结束位置
                    long i = vn.getOffsetAfterHead();
                    if (xm == null) {
                        xm = new XMLModifier(vn);
                    }
                    if (vn.getEncoding() < VTDNav.FORMAT_UTF_16BE) {
                        xm.insertBytesAt((int) i - 1, attrFragment.getBytes());
                    } else {
                        xm.insertBytesAt(((int) i - 1) << 1, attrFragment.getBytes());
                    }
                } else if (xlfTgtLang == null) {
                    xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@target-language", parentName, VTDUtils.CREATE_IF_NOT_EXIST);
                } else if (xlfSrcLang == null) {
                    xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@source-language", projectSrcLang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
                }
            }
            if (xlfTgtLang != null) {
                // XLIFF 目标语言非空,但未放在对应的目录下。
                boolean flag = false;
                for (Language lang : lstProjectTgtLang) {
                    if (lang.getCode().equalsIgnoreCase(xlfTgtLang)) {
                        flag = true;
                        break;
                    }
                }
                String message = null;
                if (!flag) {
                    message = MessageFormat.format(Messages.getString("file.XLFValidator.msg6"), xlfFullPath, xlfTgtLang, parentName);
                } else if (!xlfTgtLang.equalsIgnoreCase(parentName)) {
                    message = MessageFormat.format(Messages.getString("file.XLFValidator.msg7"), xlfFullPath, xlfTgtLang, parentName);
                }
                if (!blnIsOpenConfirmTgt && !isConfirmTgt && message != null) {
                    MessageDialogWithToggle dialog = new MessageDialogWithToggle(shell, Messages.getString("file.XLFValidator.msgTitle2"), null, message, MessageDialog.CONFIRM, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, Messages.getString("file.XLFValidator.toggleStateMsg"), false);
                    int returnCode = dialog.open();
                    if (returnCode == IDialogConstants.YES_ID) {
                        isConfirmTgt = true;
                        blnIsOpenConfirmTgtY = true;
                    } else if (returnCode == IDialogConstants.NO_ID) {
                        isConfirmTgt = false;
                        blnIsOpenConfirmTgtY = false;
                    }
                    blnIsOpenConfirmTgt = dialog.getToggleState();
                }
                if ((blnIsOpenConfirmTgt && blnIsOpenConfirmTgtY) || isConfirmTgt) {
                    xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@target-language", parentName, VTDUtils.CREATE_IF_NOT_EXIST | VTDUtils.PILOT_TO_END);
                // vu.bind(xm.outputAndReparse());
                } else if (message != null) {
                    return false;
                }
            }
            // Bug #2329:文件语言更改成项目语言时应同时更改 source 节点的 xml:lang 属性值,如果 target 节点有 xml:lang 属性,也要修改成项目语言
            AutoPilot tempAp = new AutoPilot(vn);
            tempAp.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
            xm = vu.update(tempAp, xm, "/xliff/file[@original=\"" + original + "\"]/body//trans-unit/source/@xml:lang", projectSrcLang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST | VTDUtils.PILOT_TO_END);
            xm = vu.update(tempAp, xm, "/xliff/file[@original=\"" + original + "\"]/body//trans-unit/target/@xml:lang", parentName, VTDUtils.PILOT_TO_END);
        } while (ap.evalXPath() != -1);
        if (xm != null) {
            // vu.bind(xm.outputAndReparse());
            vu.bind(vu.updateVTDNav(xm, ResourceUtils.iFileToOSPath(iFile)));
        }
        vg.clear();
        iFile.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
    } catch (XPathParseException e) {
        e.printStackTrace();
        logger.error(Messages.getString("file.XLFValidator.logger1"), e);
        MessageDialog.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), Messages.getString("file.XLFValidator.msg8"));
        return false;
    } catch (XPathEvalException e) {
        e.printStackTrace();
        logger.error(Messages.getString("file.XLFValidator.logger1"), e);
        MessageDialog.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), Messages.getString("file.XLFValidator.msg8"));
        return false;
    } catch (NavException e) {
        e.printStackTrace();
        logger.error(Messages.getString("file.XLFValidator.logger1"), e);
        MessageDialog.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), Messages.getString("file.XLFValidator.msg8"));
        return false;
    } catch (CoreException e) {
        logger.error("", e);
    } catch (ModifyException e) {
        logger.error("", e);
    }
    return true;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathParseException(com.ximpleware.XPathParseException) Language(net.heartsome.cat.common.locale.Language) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) ModifyException(com.ximpleware.ModifyException) ArrayList(java.util.ArrayList) List(java.util.List) Vector(java.util.Vector) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDGen(com.ximpleware.VTDGen) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) ModifyException(com.ximpleware.ModifyException) CoreException(org.eclipse.core.runtime.CoreException) AutoPilot(com.ximpleware.AutoPilot) 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