Search in sources :

Example 6 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class ExportConfig method saveAsChecker.

private void saveAsChecker() {
    setSaveAs = new LinkedHashSet<String>();
    mapSaveAs = new LinkedHashMap<XliffBean, String>();
    Map<String, XliffBean> tmpMap = new HashMap<String, XliffBean>();
    Set<String> tmpSet = new LinkedHashSet<String>();
    boolean overwrite = false;
    boolean noMoreAlert = false;
    for (Entry<IProject, List<XliffBean>> entry : mapXlfBeans.entrySet()) {
        for (XliffBean bean : entry.getValue()) {
            String projectName = entry.getKey().getLocation().toOSString();
            projectName = projectName.substring(projectName.lastIndexOf(File.separator) + 1);
            String saveAsName = new StringBuilder().append(projectName).append("_").append(bean.getSourceLanguage()).append("_").append(bean.getTargetLanguage()).append(".tmx").toString();
            String saveAsFile = saveAsFolder + File.separator + saveAsName;
            mapSaveAs.put(bean, saveAsFile);
            setSaveAs.add(saveAsFile);
            tmpSet.add(saveAsFile);
            tmpMap.put(saveAsFile, bean);
        }
    }
    for (String saveAsFile : tmpSet) {
        File file = new File(saveAsFile);
        if (file.exists()) {
            if (!noMoreAlert) {
                // 已存在名为 xx 的文件,是否覆盖?
                // 是,全部是,否,全部否。
                int i = ExportExternal.openConfirmDialog(getShell(), MessageFormat.format(Messages.getString("ExportDocxDialog.ok.msg5"), "TMX", saveAsFile));
                switch(i) {
                    case 0:
                        noMoreAlert = false;
                        overwrite = true;
                        break;
                    case 1:
                        noMoreAlert = true;
                        overwrite = true;
                        break;
                    case 2:
                        noMoreAlert = false;
                        overwrite = false;
                        break;
                    case 3:
                        noMoreAlert = true;
                        overwrite = false;
                }
            }
            if (!overwrite) {
                mapSaveAs.remove(tmpMap.get(saveAsFile));
                setSaveAs.remove(saveAsFile);
            } else {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    mapSaveAs.remove(tmpMap.get(saveAsFile));
                    setSaveAs.remove(saveAsFile);
                    openErrorDialog(new ExportCanceledException(MessageFormat.format(Messages.getString("ExportExternal.error.cannotExport"), saveAsFile), e));
                } finally {
                    if (fos != null) {
                        try {
                            fos.flush();
                            fos.close();
                        }// already closed
                         catch (Exception e) {
                        }
                    }
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileNotFoundException(java.io.FileNotFoundException) IProject(org.eclipse.core.resources.IProject) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) File(java.io.File)

Example 7 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class ExportConfig method saveAsCheckerUnclean.

private void saveAsCheckerUnclean() {
    setSaveAs = new LinkedHashSet<String>();
    mapSaveAs = new LinkedHashMap<XliffBean, String>();
    Map<String, XliffBean> tmpMap = new HashMap<String, XliffBean>();
    Set<String> tmpSet = new LinkedHashSet<String>();
    boolean overwrite = false;
    boolean noMoreAlert = false;
    for (Entry<IProject, List<XliffBean>> entry : mapXlfBeans.entrySet()) {
        for (XliffBean bean : entry.getValue()) {
            String strippedName = bean.getXliffFile().substring(bean.getXliffFile().lastIndexOf(File.separator) + 1);
            String saveAsName = "";
            if (exportType == ExportExternal.EXPORT_HSPROOF) {
                saveAsName = new File(bean.getXliffFile()).getName() + "_" + bean.getTargetLanguage() + ".docx";
            } else {
                saveAsName = strippedName(strippedName, "_" + bean.getTargetLanguage() + "_unclean.docx");
            }
            String saveAsFile = new StringBuilder(saveAsFolder).append(File.separator).append(saveAsName).toString();
            tmpMap.put(saveAsFile, bean);
            tmpSet.add(saveAsFile);
            mapSaveAs.put(bean, saveAsFile);
            setSaveAs.add(saveAsFile);
        }
    }
    for (String saveAsFile : tmpSet) {
        File file = new File(saveAsFile);
        if (file.exists()) {
            if (!noMoreAlert) {
                // 已存在名为 xx 的文件,是否覆盖?
                // 是,全部是,否,全部否。
                int i = ExportExternal.openConfirmDialog(getShell(), MessageFormat.format(Messages.getString("ExportDocxDialog.ok.msg5"), "docx", saveAsFile));
                switch(i) {
                    case ExportExternal.CONFIRM_YES:
                        noMoreAlert = false;
                        overwrite = true;
                        break;
                    case ExportExternal.CONFIRM_YESTOALL:
                        noMoreAlert = true;
                        overwrite = true;
                        break;
                    case ExportExternal.CONFIRM_NO:
                        noMoreAlert = false;
                        overwrite = false;
                        break;
                    case ExportExternal.CONFIRM_NOTOALL:
                        noMoreAlert = true;
                        overwrite = false;
                }
            }
            if (!overwrite) {
                setSaveAs.remove(saveAsFile);
                mapSaveAs.remove(tmpMap.get(saveAsFile));
            //remove
            } else {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file, true);
                } catch (FileNotFoundException e) {
                    setSaveAs.remove(saveAsFile);
                    mapSaveAs.remove(tmpMap.get(saveAsFile));
                    //remove
                    openErrorDialog(new ExportCanceledException(MessageFormat.format(Messages.getString("ExportExternal.error.cannotExport"), saveAsFile), e));
                } finally {
                    if (fos != null) {
                        try {
                            fos.flush();
                            fos.close();
                        }// already closed
                         catch (Exception e) {
                        }
                    }
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileNotFoundException(java.io.FileNotFoundException) IProject(org.eclipse.core.resources.IProject) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) File(java.io.File)

Example 8 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class AbstractExportHandler method getXlfBean.

private XliffBean getXlfBean(IFile file) {
    XliffBean bean = null;
    XLFHandler xlfHandler = new XLFHandler();
    Map<String, Object> result = xlfHandler.openFile(file.getLocation().toOSString());
    Object obj = result.get(Constant.RETURNVALUE_RESULT);
    if (obj != null && (Integer) obj == Constant.RETURNVALUE_RESULT_SUCCESSFUL) {
        bean = xlfHandler.getXliffInfo().get(file.getLocation().toOSString()).get(0);
    }
    return bean;
}
Also used : XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 9 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean 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)

Aggregations

XliffBean (net.heartsome.cat.ts.core.bean.XliffBean)9 ArrayList (java.util.ArrayList)5 List (java.util.List)5 XPathParseException (com.ximpleware.XPathParseException)4 AutoPilot (com.ximpleware.AutoPilot)3 VTDNav (com.ximpleware.VTDNav)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)3 IProject (org.eclipse.core.resources.IProject)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 NavException (com.ximpleware.NavException)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 LinkedHashSet (java.util.LinkedHashSet)2 Entry (java.util.Map.Entry)2 Point (org.eclipse.swt.graphics.Point)2