Search in sources :

Example 61 with VTDNav

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

the class XLFHandler method getTMXFileContent.

/**
	 * 根据同一个 XLIFF 文件中的 rowId 生成 TMX 文件
	 * @param list
	 *            rowId
	 * @param creationTool
	 *            创建工具
	 * @param creationToolVersion
	 *            创建工具版本
	 * @param srcName
	 *            源语言
	 * @param monitor
	 *            进度条
	 * @return TMX 文件
	 */
public StringBuffer getTMXFileContent(List<String> list, String creationTool, String creationToolVersion, String srcName, IProgressMonitor monitor) {
    if (list == null || list.size() == 0) {
        return new StringBuffer();
    }
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask(Messages.getString("file.XLFHandler.task3"), list.size());
    if (monitor.isCanceled())
        return new StringBuffer();
    StringBuffer re = new StringBuffer();
    re.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    re.append("<tmx version=\"1.4b\">");
    re.append("<header creationtool=\"" + creationTool + "\" creationtoolversion=\"" + creationToolVersion + "\" srclang=\"" + srcName + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
    re.append("</header>");
    re.append("<body>");
    VTDNav vn = getVTDNavByRowId(list.get(0));
    AutoPilot ap = new AutoPilot(vn);
    VTDUtils vu = null;
    try {
        vu = new VTDUtils(vn);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        monitor.done();
    }
    String defaultSrcLang = "";
    String defaultTgtLang = "";
    String fileXPath = RowIdUtil.getFileXpathByRowId(list.get(0));
    if (vu.pilot(ap, fileXPath) != -1) {
        defaultSrcLang = vu.getValue(ap, "./@source-language");
        defaultTgtLang = vu.getValue(ap, "./@target-language");
    }
    if (defaultTgtLang == null) {
        defaultTgtLang = "";
    }
    for (String rowId : list) {
        if (monitor.isCanceled())
            return new StringBuffer();
        String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
        try {
            ap.selectXPath(tuXPath);
            ap.evalXPath();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
            monitor.done();
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
            monitor.done();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
            monitor.done();
        }
        re.append("<tu ");
        // 添加tu的属性
        boolean hasTuId = false;
        String id = null;
        String attrNameXp = "./@*[name]";
        String attrValueXp = "./@";
        List<String> tuPropNames = vu.getValues(ap, attrNameXp);
        for (String name : tuPropNames) {
            String value = vu.getValue(ap, attrValueXp + name);
            re.append(name + "=\"" + value + "\" ");
            if (name.equals("id")) {
                id = value;
            }
            if (!hasTuId) {
                if (name.equals("tuid")) {
                    hasTuId = true;
                }
            }
        }
        if (!hasTuId) {
            re.append("tuid=\"" + genTuId(rowId, id) + "\"");
        }
        re.append(">");
        String typeXp = "./prop-group/prop/@prop-type";
        List<String> typeValues = vu.getValues(ap, typeXp);
        if (typeValues != null) {
            for (String typeValue : typeValues) {
                re.append("<prop type=\"" + typeValue + "\">");
                re.append(vu.getValue(ap, "./prop-group/prop[@prop-type='" + typeValue + "']/text()"));
                re.append("</prop>");
            }
        }
        // HS的自定义属性组
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        String hsPropXp = "./hs:prop-group/prop/@prop-type";
        typeValues = vu.getValues(ap, hsPropXp);
        if (typeValues != null) {
            for (String typeValue : typeValues) {
                re.append("<prop type=\"" + typeValue + "\">");
                re.append(vu.getValue(ap, "./hs:prop-group/prop[@prop-type='" + typeValue + "']/text()"));
                re.append("</prop>");
            }
        }
        // 添加tuv(source)
        re.append("<tuv ");
        // 添加tuv(source)属性
        attrNameXp = "./source/@*[name]";
        attrValueXp = "./source/@";
        List<String> sourcePropNames = vu.getValues(ap, attrNameXp);
        if (sourcePropNames != null) {
            for (String name : sourcePropNames) {
                String value = vu.getValue(ap, attrValueXp + name);
                re.append(name + "=\"" + value + "\" ");
            }
        }
        /*
			 * source节点可能没有xml:lang属性,如果没有,则要去file节点去取source-language(Required)属性的值
			 */
        if (sourcePropNames == null || !sourcePropNames.contains("xml:lang")) {
            re.append("xml:lang=\"" + defaultSrcLang + "\"");
        }
        re.append(">");
        // 添加上下文prop
        re.append("<prop type=\"x-preContext\">");
        re.append(getContext(vu, ap, contextNum, true));
        re.append("</prop>");
        re.append("<prop type=\"x-nextContext\">");
        re.append(getContext(vu, ap, contextNum, false));
        re.append("</prop>");
        String sourceTextXp = "./source/text()";
        re.append("<seg>").append(vu.getValue(ap, sourceTextXp)).append("</seg>");
        re.append("</tuv>");
        // 添加tuv(target)
        re.append("<tuv ");
        // 添加tuv(target)属性
        attrNameXp = "./target/@*[name]";
        attrValueXp = "./target/@";
        List<String> targetPropNames = vu.getValues(ap, attrNameXp);
        if (targetPropNames != null) {
            for (String name : targetPropNames) {
                String value = vu.getValue(ap, attrValueXp + name);
                re.append(name + "=\"" + value + "\" ");
            }
        }
        /*
			 * target节点可能没有xml:lang属性,如果没有,则要去file节点去取target-language(Optional)属性的值
			 */
        if (targetPropNames == null || !targetPropNames.contains("xml:lang")) {
            re.append("xml:lang=\"" + defaultTgtLang + "\"");
        }
        re.append(">");
        String targetTextXp = "./target/text()";
        re.append("<seg>").append(vu.getValue(ap, targetTextXp)).append("</seg>");
        re.append("</tuv>");
        re.append("</tu>");
        monitor.worked(1);
    }
    re.append("</body>");
    re.append("</tmx>");
    monitor.done();
    return re;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav)

Example 62 with VTDNav

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

the class XLFHandler method getNodeContent.

/**
	 * 获取指定文件的指定节点的内容(除节点头与尾之外)
	 * @param xlfPath
	 * @param nodeXpath
	 * @return robert 2011-10-26
	 */
public String getNodeContent(String xlfPath, String nodeXpath) {
    String nodeContent = "";
    VTDNav vn = vnMap.get(xlfPath);
    // Add by Jason
    vn.push();
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    try {
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            nodeContent = vUtils.getElementContent();
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    // Add by Jason
    vn.pop();
    return nodeContent;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 63 with VTDNav

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

the class XLFHandler method deleteLastSplitInfo.

/**
	 * 删除目标文件的最后一条切割信息
	 * @param xlfPath
	 *            robert 2011-10-26
	 */
public void deleteLastSplitInfo(String xlfPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    try {
        // --robert split
        String xPath = "/xliff/file/header/hs:splitInfos/hs:splitInfo[last()]";
        // String xPath = "/xliff/file/header/splitInfos/splitInfo[last()]";
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        XMLModifier xm = vu.delete(ap, null, xPath, VTDUtils.PILOT_TO_END);
        saveAndReparse(xm, xlfPath);
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 64 with VTDNav

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

the class XLFHandler method getDocumentInfo.

/**
	 * 得到文档属性的信息
	 * @param fileName
	 *            文件名
	 * @return 多个文件的文档属性集合(一个 file 节点文档的属性为一个 HashMap);
	 */
public List<HashMap<String, String>> getDocumentInfo(String fileName) {
    ArrayList<HashMap<String, String>> fileList = new ArrayList<HashMap<String, String>>();
    VTDNav vn = vnMap.get(fileName);
    AutoPilot apFile = new AutoPilot(vn);
    try {
        apFile.selectXPath("/xliff/file");
        String[] fileAttrNames = { DocumentPropertiesKeys.ORIGINAL, DocumentPropertiesKeys.DATA_TYPE, DocumentPropertiesKeys.SOURCE_LANGUAGE, DocumentPropertiesKeys.TARGET_LANGUAGE };
        String[] propTypes = new String[] { DocumentPropertiesKeys.PROJECT_REF, DocumentPropertiesKeys.JOB_REF, DocumentPropertiesKeys.JOB_DATE, DocumentPropertiesKeys.JOB_OWNER, DocumentPropertiesKeys.CLIENT };
        VTDUtils vu = new VTDUtils(vn);
        while (apFile.evalXPath() != -1) {
            String value = "";
            HashMap<String, String> fileAttrs = new HashMap<String, String>();
            for (String attrName : fileAttrNames) {
                value = vu.getCurrentElementAttribut(attrName, "");
                fileAttrs.put(attrName, value);
            }
            AutoPilot ap = new AutoPilot(vn);
            vn.push();
            value = "";
            ap.selectXPath("./header/skl");
            if (ap.evalXPath() != -1) {
                ap.selectXPath("./external-file");
                if (ap.evalXPath() != -1) {
                    value = vu.getCurrentElementAttribut("href", "");
                } else {
                    ap.selectXPath("./internal-file");
                    if (ap.evalXPath() != -1) {
                        value = Constant.SKL_INTERNAL_FILE;
                    }
                }
            }
            // vn.push();
            // ap.selectXPath("./header/skl/external-file");
            // value = "";
            // if (ap.evalXPath() != -1) {
            // int attrIdx = vn.getAttrVal("href");
            // value = attrIdx != -1 ? vn.toString(attrIdx) : "";
            // }
            fileAttrs.put(DocumentPropertiesKeys.SKL, value);
            vn.pop();
            ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
            vn.push();
            ap.selectXPath("./header/hs:prop-group[@name='encoding']/hs:prop[@prop-type='encoding']");
            value = "";
            if (ap.evalXPath() != -1) {
                value = vn.toString(vn.getText());
            }
            fileAttrs.put(DocumentPropertiesKeys.ENCODING, value);
            vn.pop();
            for (String attrName : propTypes) {
                vn.push();
                ap.selectXPath("./header/hs:prop-group[@name=\"project\"]/hs:prop[@prop-type=\"" + attrName + "\"]");
                value = "";
                if (ap.evalXPath() != -1) {
                    value = vn.toString(vn.getText());
                }
                if ("".equals(value) && DocumentPropertiesKeys.JOB_DATE.equals(attrName)) {
                    value = sdf.format(new Date());
                }
                fileAttrs.put(attrName, value);
                vn.pop();
            }
            fileList.add(fileAttrs);
        }
    } 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 fileList;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) Date(java.util.Date) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

Example 65 with VTDNav

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

the class XLFHandler method resetMergeSegment.

/**
	 * 合并文本段的撤销功能 robert 2012-11-06
	 */
public void resetMergeSegment(Map<String, String> oldSegFragMap) {
    String xlfPath = "";
    VTDNav vn = null;
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    try {
        // 由于合并的文本段都是同一个文件,因此此处不做 文件名 的判断
        for (Entry<String, String> entry : oldSegFragMap.entrySet()) {
            String rowId = entry.getKey();
            if ("".equals(xlfPath) || xlfPath == null || vn == null) {
                xlfPath = RowIdUtil.getFileNameByRowId(rowId);
                vn = vnMap.get(xlfPath);
                ap.bind(vn);
                xm.bind(vn);
            }
            ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
            if (ap.evalXPath() != -1) {
                xm.remove();
                xm.insertAfterElement(entry.getValue());
            }
        }
        saveAndReparse(xm, xlfPath);
        resetRowIdsToUnsorted();
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Aggregations

VTDNav (com.ximpleware.VTDNav)206 AutoPilot (com.ximpleware.AutoPilot)177 NavException (com.ximpleware.NavException)128 XPathParseException (com.ximpleware.XPathParseException)115 XPathEvalException (com.ximpleware.XPathEvalException)110 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)99 IOException (java.io.IOException)85 ModifyException (com.ximpleware.ModifyException)81 TranscodeException (com.ximpleware.TranscodeException)69 CoreException (org.eclipse.core.runtime.CoreException)69 XMLModifier (com.ximpleware.XMLModifier)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)49 FileNotFoundException (java.io.FileNotFoundException)46 VTDGen (com.ximpleware.VTDGen)45 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)43 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)27 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)25 LinkedList (java.util.LinkedList)23