Search in sources :

Example 41 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class XLFHandler method getMatchOfSegments.

/**
	 * 获取翻译匹配率达到 <code>minMatchQuality</code> 的匹配
	 * @param minMatchQuality
	 *            最低匹配率
	 */
public Map<String, String> getMatchOfSegments(final int minMatchQuality) {
    final HashMap<String, String> map = new HashMap<String, String>();
    handleAllSegment(new PerFileHandler() {

        public void handle(String fileName, VTDUtils vu, AutoPilot ap, XMLModifier xm) throws ModifyException, XPathParseException, XPathEvalException, NavException, UnsupportedEncodingException {
            ap.selectXPath("/xliff/file/body//trans-unit[translate(alt-trans/@match-quality, '%', '')>=" + minMatchQuality + "]");
            AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
            while (ap.evalXPath() != -1) {
                String rowId = RowIdUtil.getRowId(vu.getVTDNav(), fileName);
                if (isApproved(rowId) || isLocked(rowId)) {
                    // 已经批准或者锁定的,跳过。
                    continue;
                }
                String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
                if (vu.pilot(tempAp, tuXPath) != -1) {
                    String tgt = vu.getValue(tempAp, "./alt-trans[translate(@match-quality, '%', '')>=" + minMatchQuality + "]/target/text()");
                    if (tgt != null) {
                        // 当前 Target 的值
                        String currentTgt = vu.getValue(tempAp, "./target/text()");
                        if (!tgt.equals(currentTgt)) {
                            map.put(rowId, tgt);
                        }
                    }
                }
            }
            saveAndReparse(xm, fileName);
        }
    });
    return map;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 42 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 43 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 44 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 45 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class XLFHandler method getTagLocation.

/**
	 * <div style='color:red'>此方法有复制到 convert.ui 插件的 ReverseConversionValidateWithLibrary3 类中。故若修改,注意保持同步 --robert 2012-11-29</div>
	 * 获取每个标记 header 与 tail 在文本中的 index,此方法主要针对文本段分割,分割点在g、mrk标记里面。robert 2012-11-15
	 * @param vn
	 */
private List<Map<String, String>> getTagLocation(VTDNav vn, String srcContent) {
    List<Map<String, String>> tagLoctionList = new LinkedList<Map<String, String>>();
    vn.push();
    AutoPilot ap = new AutoPilot(vn);
    String xpath = "./descendant::node()";
    try {
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath(xpath);
        int lastIdx = 0;
        while (ap.evalXPath() != -1) {
            Map<String, String> tagLocationMap = new HashMap<String, String>();
            String tagName = vn.toString(vn.getCurrentIndex());
            if (!("g".equals(tagName) || "mrk".equals(tagName) || "sub".equals(tagName))) {
                continue;
            }
            String tagHeader = vu.getElementHead();
            String tagTail = vu.getElementFragment().replace(tagHeader, "").replace(vu.getElementContent(), "");
            int headerIdx = srcContent.indexOf(tagHeader, lastIdx);
            int tailIdx = headerIdx + tagHeader.length() + vu.getElementContent().length();
            lastIdx = headerIdx;
            tagLocationMap.put("tagHeader", tagHeader);
            tagLocationMap.put("tagTail", tagTail);
            tagLocationMap.put("headerIdx", "" + headerIdx);
            tagLocationMap.put("tailIdx", "" + tailIdx);
            tagLoctionList.add(tagLocationMap);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    vn.pop();
    return tagLoctionList;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AutoPilot(com.ximpleware.AutoPilot) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) 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

VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)137 AutoPilot (com.ximpleware.AutoPilot)112 NavException (com.ximpleware.NavException)103 VTDNav (com.ximpleware.VTDNav)99 XPathParseException (com.ximpleware.XPathParseException)83 XPathEvalException (com.ximpleware.XPathEvalException)81 IOException (java.io.IOException)64 ModifyException (com.ximpleware.ModifyException)62 TranscodeException (com.ximpleware.TranscodeException)49 CoreException (org.eclipse.core.runtime.CoreException)45 XMLModifier (com.ximpleware.XMLModifier)41 VTDGen (com.ximpleware.VTDGen)33 FileNotFoundException (java.io.FileNotFoundException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 HashMap (java.util.HashMap)27 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)27 LinkedHashMap (java.util.LinkedHashMap)25 ArrayList (java.util.ArrayList)23 XQException (javax.xml.xquery.XQException)20 LinkedList (java.util.LinkedList)14