Search in sources :

Example 91 with VTDUtils

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

the class XLFHandler method updateAltTrans.

/**
	 * 更新alt-trans节点,删除指定类型的alt-trans(以toolid区分类型),重新写入新的alt-trans 内容
	 * @param rowId
	 *            当前标识
	 * @param newAltTrans
	 *            新的alt-trans内容
	 * @param oldAltTransToolId
	 *            旧的alt-trans toolId,会删除此toolid的所有alt-trans;
	 */
public void updateAltTrans(String rowId, List<AltTransBean> newAltTrans, List<String> oldAltTransToolId) {
    String fileName = RowIdUtil.getFileNameByRowId(rowId);
    VTDUtils vu;
    XMLModifier xm = null;
    try {
        vu = new VTDUtils(vnMap.get(fileName));
        xm = new XMLModifier(vu.getVTDNav());
        updateAltTrans(vu, xm, rowId, newAltTrans, oldAltTransToolId);
    } catch (NavException e) {
        LOGGER.error("", e);
    } catch (ModifyException e) {
        LOGGER.error("", e);
    }
    saveAndReparse(xm, fileName);
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException)

Example 92 with VTDUtils

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

the class XLFHandler method saveAsText.

/**
	 * 存为纯文本
	 * @param fileName
	 * @param out
	 * @param elementName
	 * @return ;
	 */
public boolean saveAsText(String fileName, String out, String elementName) {
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(out));
        VTDNav vn = vnMap.get(fileName);
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath("/xliff/file");
        while (ap.evalXPath() != -1) {
            AutoPilot subAp = new AutoPilot(vn);
            subAp.selectXPath("descendant::trans-unit[source/text()!='']");
            while (subAp.evalXPath() != -1) {
                if ("target".equals(elementName)) {
                    String approvedValue = vu.getValue("./@approved");
                    approvedValue = approvedValue == null ? "no" : approvedValue;
                    if (!"yes".equalsIgnoreCase(approvedValue)) {
                        // 未批准
                        continue;
                    }
                }
                String value = vu.getValue("./" + elementName + "/text()");
                if (value != null) {
                    bos.write((value + "\n").getBytes());
                }
            }
        }
        return true;
    } catch (FileNotFoundException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) VTDNav(com.ximpleware.VTDNav)

Example 93 with VTDUtils

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

the class XLFHandler method deleteNote.

/**
	 * 删除批注
	 * @param rowId
	 *            行的唯一标识
	 * @param note
	 *            批注;
	 */
public void deleteNote(final HashMap<String, Vector<NoteBean>> mapNote) {
    HashMap<String, List<String>> mapRowIdAndXpath = new HashMap<String, List<String>>();
    Iterator<Entry<String, Vector<NoteBean>>> it = mapNote.entrySet().iterator();
    List<String> lstRowId = new ArrayList<String>();
    while (it.hasNext()) {
        Entry<String, Vector<NoteBean>> entry = (Entry<String, Vector<NoteBean>>) it.next();
        String rowId = entry.getKey();
        String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
        for (NoteBean bean : entry.getValue()) {
            String from = bean.getFrom();
            String applyCurrent = bean.getApplyCurrent();
            StringBuffer subXPath = new StringBuffer();
            if (from != null && !from.equals("")) {
                subXPath.append("@from='").append(from).append("' and ");
            }
            // 只有是所有句段时才加该属性
            if (applyCurrent != null && applyCurrent.equalsIgnoreCase("No")) {
                subXPath.append("@hs:apply-current='").append(applyCurrent).append("' and ");
            }
            subXPath.insert(0, "/note[");
            subXPath.append("text()='");
            if (bean.getApplyCurrent().equals("Yes")) {
                List<String> lstXpath = mapRowIdAndXpath.get(rowId);
                if (lstXpath == null) {
                    lstXpath = new ArrayList<String>();
                    mapRowIdAndXpath.put(rowId, lstXpath);
                }
                subXPath.insert(0, tuXPath);
                subXPath.append(bean.getNoteText()).append("']");
                lstXpath.add(subXPath.toString());
                if (!lstRowId.contains(rowId)) {
                    lstRowId.add(rowId);
                }
            } else {
                // 删除所有句段的批注
                List<String> rowIds = getRowIds();
                if (!lstRowId.containsAll(rowIds)) {
                    lstRowId.clear();
                    lstRowId.addAll(rowIds);
                }
                for (String strRowId : rowIds) {
                    StringBuffer strTuXPath = new StringBuffer(RowIdUtil.parseRowIdToXPath(strRowId));
                    List<String> lstXpath = mapRowIdAndXpath.get(strRowId);
                    if (lstXpath == null) {
                        lstXpath = new ArrayList<String>();
                        mapRowIdAndXpath.put(strRowId, lstXpath);
                    }
                    lstXpath.add(strTuXPath.append(subXPath).append(bean.getNoteText()).append("']").toString());
                }
            }
        }
    }
    XMLModifier xm = new XMLModifier();
    VTDUtils vu = new VTDUtils();
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(lstRowId);
    Iterator<Entry<String, List<String>>> iterator = map.entrySet().iterator();
    try {
        while (iterator.hasNext()) {
            Entry<String, List<String>> entry = (Entry<String, List<String>>) iterator.next();
            String fileName = entry.getKey();
            VTDNav vn = vnMap.get(fileName);
            xm.bind(vn);
            vu.bind(vn);
            List<String> rowIdList = entry.getValue();
            for (String rowId : rowIdList) {
                List<String> lstXpath = mapRowIdAndXpath.get(rowId);
                if (lstXpath != null) {
                    AutoPilot ap = new AutoPilot(vu.getVTDNav());
                    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
                    for (String xpath : lstXpath) {
                        xm = vu.delete(ap, xm, xpath);
                    }
                }
            }
            saveAndReparse(xm, fileName);
        }
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) NavException(com.ximpleware.NavException) Entry(java.util.Map.Entry) NoteBean(net.heartsome.cat.ts.core.bean.NoteBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Vector(java.util.Vector) VTDNav(com.ximpleware.VTDNav)

Example 94 with VTDUtils

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

the class XLFHandler method getTransUnit.

/**
	 * 获取指定索引的翻译单元对象。
	 * @param rowId
	 *            行的唯一标识,由三部分拼成
	 */
public TransUnitBean getTransUnit(String rowId) {
    if (tuSizeMap == null || rowId == null) {
        return null;
    }
    String tuNode = RowIdUtil.parseRowIdToXPath(rowId);
    if (tuNode == null) {
        return null;
    }
    VTDUtils vu = null;
    VTDNav vn = getVTDNavByRowId(rowId);
    try {
        vu = new VTDUtils(vn);
    } catch (NavException e1) {
        String errorMsg = Messages.getString("file.XLFHandler.logger4");
        LOGGER.error(errorMsg, e1);
        return null;
    }
    try {
        if (vu.pilot(tuNode) != -1) {
            // 导航到 trans-unit 节点
            String tuid = "";
            String srcText = "";
            String srcContent = "";
            String tgtText = "";
            String tgtContent = "";
            Hashtable<String, String> srcProps = new Hashtable<String, String>();
            Hashtable<String, String> tgtProps = new Hashtable<String, String>();
            // 取翻译单元所有属性
            vn.push();
            Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
            vn.pop();
            if (tuProps != null) {
                tuid = tuProps.get("id");
            }
            AutoPilot ap = new AutoPilot(vn);
            ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
            ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
            // 取翻译单元源节点完整文本,含内部标记。
            vn.push();
            if (vu.pilot(ap, "./source") != -1) {
                // 导航到 Source 子节点
                // 源节点完整内容。
                srcContent = vu.getElementContent();
                // 源节点纯文本内容。
                srcText = getTUPureText(vu.getVTDNav());
                // 源节点属性集合。
                srcProps = vu.getCurrentElementAttributs();
            }
            ap.resetXPath();
            vn.pop();
            // 取翻译单元目标节点完整文本,含内部标记。
            vn.push();
            if (vu.pilot(ap, "./target") != -1) {
                // 导航到 Target 子节点
                // 目标节点完整内容。
                tgtContent = vu.getElementContent();
                // 目标节点纯文本内容。
                tgtText = getTUPureText(vu.getVTDNav());
                // 目标节点属性集合。
                tgtProps = vu.getCurrentElementAttributs();
            }
            vn.pop();
            // 获取所有的 alttrans 匹配节点。
            vn.push();
            Vector<AltTransBean> matches = getAltTrans(vu);
            vn.pop();
            // 构建翻译单元对象,存储节点信息
            TransUnitBean tub = new TransUnitBean(tuid, srcContent, srcText);
            tub.setTuProps(tuProps);
            tub.setSrcProps(srcProps);
            tub.setTgtContent(tgtContent);
            tub.setTgtText(tgtText);
            tub.setTgtProps(tgtProps);
            tub.setMatches(matches);
            vn.push();
            tub.setNotes(getNotes(vu));
            vn.pop();
            vn.push();
            tub.setPropgroups(getPrpoGroups(vu));
            vn.pop();
            return tub;
        }
    } catch (XPathEvalException e) {
        String errorMsg = Messages.getString("file.XLFHandler.logger5");
        LOGGER.error(errorMsg, e);
        return null;
    } catch (NavException e) {
        String errorMsg = Messages.getString("file.XLFHandler.logger6");
        LOGGER.error(errorMsg, e);
        return null;
    } catch (XPathParseException e) {
        String errorMsg = Messages.getString("file.XLFHandler.logger7");
        LOGGER.error(errorMsg, e);
        return null;
    }
    return null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) Hashtable(java.util.Hashtable) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav)

Example 95 with VTDUtils

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

the class XLFHandler method saveAsHtml.

/**
	 * 存为HTML
	 * @param fileName
	 * @param out
	 * @param elementName
	 * @return ;
	 */
public boolean saveAsHtml(String fileName, String out, String elementName) {
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(out));
        //$NON-NLS-1$
        bos.write("<html>\n".getBytes());
        //$NON-NLS-1$
        bos.write("  <head>\n".getBytes());
        //$NON-NLS-1$
        bos.write("    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n".getBytes());
        //$NON-NLS-1$ //$NON-NLS-2$
        bos.write(("    <title>" + fileName + "</title>\n").getBytes());
        //$NON-NLS-1$
        bos.write("  </head>\n".getBytes());
        //$NON-NLS-1$
        bos.write("<body>\n".getBytes());
        VTDNav vn = vnMap.get(fileName);
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath("/xliff/file");
        while (ap.evalXPath() != -1) {
            AutoPilot subAp = new AutoPilot(vn);
            subAp.selectXPath("descendant::trans-unit[source/text()!='']");
            while (subAp.evalXPath() != -1) {
                if ("target".equals(elementName)) {
                    String approvedValue = vu.getValue("./@approved");
                    approvedValue = approvedValue == null ? "no" : approvedValue;
                    if (!"yes".equalsIgnoreCase(approvedValue)) {
                        // 未批准
                        continue;
                    }
                }
                String value = vu.getValue("./" + elementName + "/text()");
                if (value != null) {
                    bos.write(("<p>" + value + "</p>\n").getBytes());
                }
            }
        }
        bos.write("</body>\n</html>\n".getBytes());
        return true;
    } catch (FileNotFoundException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) VTDNav(com.ximpleware.VTDNav)

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