Search in sources :

Example 51 with XPathParseException

use of com.ximpleware.XPathParseException 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 52 with XPathParseException

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

Example 53 with XPathParseException

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

the class XLFHandler method getCaseTgtContent.

/** burke 为修改锁定行目标文本不能修改其大小写添加 getCaseTgtContent方法 */
/**
	 * 获取要改变大小写的target中的文本内容
	 * @param rowId
	 * @return
	 */
public String getCaseTgtContent(String rowId) {
    AutoPilot ap = new AutoPilot();
    VTDNav vn = null;
    vn = getVTDNavByRowId(rowId);
    ap.bind(vn);
    try {
        String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
        ap.selectXPath(tuXPath);
        if (ap.evalXPath() != -1) {
            int attrIndex = vn.getAttrVal("translate");
            if (attrIndex != -1) {
                String attrValue = vn.toString(attrIndex);
                if (attrValue.equals("no")) {
                    return "no";
                }
            }
        }
        ap.resetXPath();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    String tgt = getTgtContent(rowId);
    return tgt;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 54 with XPathParseException

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

the class ProjectConfiger method getCurrentProjectConfig.

/**
	 * 获取项目配置信息
	 * @return 指定项目配置文件中的所有信息;
	 */
public ProjectInfoBean getCurrentProjectConfig() {
    ProjectInfoBean bean = new ProjectInfoBean();
    vu.getVTDNav().push();
    AutoPilot hsAp = new AutoPilot(vu.getVTDNav());
    try {
        hsAp.selectXPath("/projectDescription/hs");
        if (hsAp.evalXPath() != -1) {
            bean.setProjectName(getProjectName());
            bean.setMapField(getFieldMap());
            bean.setMapAttr(getAttrMap());
            bean.setSourceLang(getSourceLanguage());
            bean.setTargetLang(getTargetlanguages());
            bean.setTmDb(getAllTmDbs());
            bean.setTbDb(getTermBaseDbs(false));
        }
    } catch (XPathParseException e) {
        logger.error("", e);
    } catch (XPathEvalException e) {
        logger.error("", e);
    } catch (NavException e) {
        logger.error("", e);
    }
    vu.getVTDNav().pop();
    return bean;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) ProjectInfoBean(net.heartsome.cat.common.bean.ProjectInfoBean) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 55 with XPathParseException

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

the class DeleteSelectionSegmentNotesHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (!(editor instanceof XLIFFEditorImplWithNatTable)) {
        return null;
    }
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    boolean res = MessageDialog.openConfirm(window.getShell(), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msgTitle1"), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msg1"));
    if (res) {
        XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
        XLFHandler handler = xliffEditor.getXLFHandler();
        List<String> lstRowId = xliffEditor.getSelectedRowIds();
        // 先将应用范围为当前文本段的批注删除
        handler.deleteEditableSegmentNote(lstRowId);
        try {
            HashMap<String, Vector<NoteBean>> mapNote = new HashMap<String, Vector<NoteBean>>();
            for (String rowId : lstRowId) {
                // 删除应用范围为所有文本段的批注
                Vector<NoteBean> noteBeans = xliffEditor.getXLFHandler().getNotes(rowId);
                if (noteBeans != null && noteBeans.size() > 0) {
                    mapNote.put(rowId, noteBeans);
                }
            }
            xliffEditor.getXLFHandler().deleteNote(mapNote);
        } catch (NavException e) {
            LOGGER.error("", e);
            MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msgTitle2"), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msg2"));
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msgTitle2"), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msg2"));
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msgTitle2"), Messages.getString("handler.DeleteSelectionSegmentNotesHandler.msg2"));
        } finally {
            xliffEditor.refresh();
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) HashMap(java.util.HashMap) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) IEditorPart(org.eclipse.ui.IEditorPart) XPathParseException(com.ximpleware.XPathParseException) NoteBean(net.heartsome.cat.ts.core.bean.NoteBean) Vector(java.util.Vector) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Aggregations

XPathParseException (com.ximpleware.XPathParseException)71 NavException (com.ximpleware.NavException)66 XPathEvalException (com.ximpleware.XPathEvalException)66 AutoPilot (com.ximpleware.AutoPilot)65 VTDNav (com.ximpleware.VTDNav)46 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)36 ModifyException (com.ximpleware.ModifyException)17 ArrayList (java.util.ArrayList)17 XMLModifier (com.ximpleware.XMLModifier)15 IOException (java.io.IOException)15 VTDGen (com.ximpleware.VTDGen)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)9 ParseException (com.ximpleware.ParseException)8 TranscodeException (com.ximpleware.TranscodeException)7 FileNotFoundException (java.io.FileNotFoundException)7 FileOutputStream (java.io.FileOutputStream)6 EOFException (com.ximpleware.EOFException)5 EncodingException (com.ximpleware.EncodingException)5 EntityException (com.ximpleware.EntityException)5