Search in sources :

Example 21 with ModifyException

use of com.ximpleware.ModifyException 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 22 with ModifyException

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

the class QAXmlHandler method save.

/**
	 * 保存文件
	 * @param xm
	 *            XMLModifier对象
	 * @param fileName
	 *            文件名
	 * @return 是否保存成功;
	 */
private boolean save(XMLModifier xm, File file) {
    try {
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 写入文件
        xm.output(bos);
        bos.close();
        fos.close();
        IPath path = URIUtil.toPath(file.toURI());
        IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
        if (iFile != null) {
            // 同步导航视图和文件系统
            iFile.refreshLocal(IResource.DEPTH_ZERO, null);
        }
        return true;
    } catch (ModifyException e) {
        e.printStackTrace();
        logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger4"), file.getAbsolutePath()), e);
    } catch (TranscodeException e) {
        e.printStackTrace();
        logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger4"), file.getAbsolutePath()), e);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger5"), file.getAbsolutePath()), e);
    } catch (CoreException e) {
        e.printStackTrace();
        logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger6"), file.getAbsolutePath()), e);
    }
    return false;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) TranscodeException(com.ximpleware.TranscodeException)

Example 23 with ModifyException

use of com.ximpleware.ModifyException 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)

Example 24 with ModifyException

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

the class LanguageConfiger 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);
    } catch (TranscodeException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    }
    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 25 with ModifyException

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

the class ExportFilterStoreConfiger 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)

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