Search in sources :

Example 16 with AutoPilot

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

the class ImportTbx method saveTermEntryWithOverwrite.

private void saveTermEntryWithOverwrite(Map<String, String> dbTermEntry, AutoPilot langSetAp, AutoPilot termAp) throws XPathParseException, XPathEvalException, NavException, SQLException, EncodingException, EOFException, EntityException, ParseException, TranscodeException, ModifyException, IOException {
    VTDGen vg = new VTDGen();
    VTDUtils vtdUtiles = new VTDUtils();
    String content = dbTermEntry.get("CONTENT");
    String termEntryPk = dbTermEntry.get("BTEPKID");
    if (!content.equals(vu.getElementFragment())) {
        String prelang = "";
        vu.getVTDNav().push();
        while (langSetAp.evalXPath() != -1) {
            String curlang = getElementAttribute("xml:lang");
            String langSetValue = vu.getElementFragment();
            vg.setDoc(content.getBytes());
            vg.parse(true);
            vtdUtiles.bind(vg.getNav());
            if (!prelang.endsWith(curlang)) {
                prelang = curlang;
                dbOperator.deleteTerm(termEntryPk, curlang);
                AutoPilot ap = new AutoPilot(vtdUtiles.getVTDNav());
                ap.declareXPathNameSpace("xml", vtdUtiles.XML_NAMESPACE_URL);
                XMLModifier xm = new XMLModifier(vtdUtiles.getVTDNav());
                vtdUtiles.delete(ap, xm, "/termEntry/langSet[@xml:lang='" + curlang + "']");
                vtdUtiles.bind(xm.outputAndReparse());
            }
            XMLModifier xm = vtdUtiles.insert("/termEntry/langSet", langSetValue);
            vtdUtiles.bind(xm.outputAndReparse());
            vu.getVTDNav().push();
            while (termAp.evalXPath() != -1) {
                String pureText = DocUtils.getTmxTbxPureText(vu);
                String fullText = vu.getElementContent();
                // 当pureText为空字符串时,HASH字段留空
                String hash = null;
                if (pureText != null) {
                    hash = "" + pureText.hashCode();
                }
                dbOperator.insertTextData("B", Integer.parseInt(termEntryPk), hash, pureText, fullText, Utils.convertLangCode(curlang), null, null);
            }
            termAp.resetXPath();
            vu.getVTDNav().pop();
        }
        langSetAp.resetXPath();
        vu.getVTDNav().pop();
        // 处理note
        List<String> existDbNoteId = new ArrayList<String>();
        List<String> existDbNotePk = new ArrayList<String>();
        // 用于记录已经处理的noteId,过滤掉文件重复的内容
        List<String> noteIdRm = new ArrayList<String>();
        StringBuffer noteContentBf = new StringBuffer();
        AutoPilot noteAp = new AutoPilot(vu.getVTDNav());
        vu.getVTDNav().push();
        noteAp.selectXPath("./note");
        while (noteAp.evalXPath() != -1) {
            String noteId = vu.getCurrentElementAttribut("id", "");
            String noteContent = vu.getElementContent();
            if (noteId.equals("") || noteContent == null || noteContent.equals("")) {
                continue;
            }
            String[] ids = noteId.split(",");
            if (ids.length != 2) {
                continue;
            }
            String reNoteId = ids[1] + "," + ids[0];
            if (noteIdRm.contains(noteId) || noteIdRm.contains(reNoteId)) {
                // 重复不重复写入
                continue;
            }
            // 记录已经处理的note
            noteIdRm.add(noteId);
            AutoPilot dbNoteAp = new AutoPilot(vtdUtiles.getVTDNav());
            dbNoteAp.selectXPath("/termEntry/note[@id='" + noteId + "' or @id='" + reNoteId + "']");
            while (dbNoteAp.evalXPath() != -1) {
                String dbNoteId = vtdUtiles.getCurrentElementAttribut("id", "");
                if (!existDbNoteId.contains(dbNoteId)) {
                    existDbNoteId.add(dbNoteId);
                }
                List<Map<String, String>> result = dbOperator.getBNodeByParent(termEntryPk, "termEntry", "E", "note", dbNoteId);
                for (Map<String, String> map : result) {
                    existDbNotePk.add(map.get("NPKID"));
                }
            }
            dbOperator.insertBNode(Integer.parseInt(termEntryPk), "termEntry", "E", "note", noteId, noteContent);
            noteContentBf.append(vu.getElementFragment());
        }
        if (existDbNotePk.size() > 0) {
            dbOperator.deleteBNode(existDbNotePk);
        }
        StringBuffer xpathWhere = new StringBuffer();
        for (String dbNodeId : existDbNoteId) {
            xpathWhere.append("@id='" + dbNodeId + "' or ");
        }
        if (xpathWhere.length() > 0) {
            String xpath = xpathWhere.substring(0, xpathWhere.lastIndexOf("or"));
            // 清除文件中的note
            XMLModifier xm = vtdUtiles.delete("/termEntry/note[" + xpath + "]", VTDUtils.PILOT_TO_END);
            vtdUtiles.bind(xm.outputAndReparse());
        }
        if (noteContentBf.length() != 0) {
            XMLModifier xm = vtdUtiles.insert("/termEntry/text()", noteContentBf.toString());
            vtdUtiles.bind(xm.outputAndReparse());
        }
        content = vtdUtiles.getElementFragment();
        dbOperator.updateTermEntry(content, termEntryPk);
        vu.getVTDNav().pop();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) VTDGen(com.ximpleware.VTDGen) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with AutoPilot

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

the class ImportTbx method saveTermEntryWithMerge.

private void saveTermEntryWithMerge(Map<String, String> dbTermEntry, AutoPilot langSetAp, AutoPilot termAp) throws XPathParseException, XPathEvalException, NavException, SQLException, EncodingException, EOFException, EntityException, ParseException, TranscodeException, ModifyException, IOException {
    String content = dbTermEntry.get("CONTENT");
    String termEntryPk = dbTermEntry.get("BTEPKID");
    // content修改标记
    boolean changFlag = false;
    VTDGen vg = new VTDGen();
    VTDUtils vtdUtiles = new VTDUtils();
    vg.setDoc(content.getBytes());
    vg.parse(true);
    vtdUtiles.bind(vg.getNav());
    AutoPilot _ap = new AutoPilot(vtdUtiles.getVTDNav());
    if (!content.equals(vu.getElementFragment())) {
        vu.getVTDNav().push();
        while (langSetAp.evalXPath() != -1) {
            String curlang = getElementAttribute("xml:lang");
            String tigValue = vu.getChildContent("tig");
            String ntigValue = vu.getChildContent("ntig");
            vtdUtiles.getVTDNav().push();
            _ap.selectXPath("/termEntry/langSet[@xml:lang='" + curlang + "']");
            if (_ap.evalXPath() != -1) {
                // 当语言的langSet在库中是否存在
                vu.getVTDNav().push();
                while (termAp.evalXPath() != -1) {
                    String fullText = vu.getElementContent();
                    String pureText = DocUtils.getTmxTbxPureText(vu);
                    vtdUtiles.getVTDNav().push();
                    _ap.selectXPath("./tig[term='" + fullText + "']|./ntig/termGrp[term='" + fullText + "']");
                    if (_ap.evalXPath() == -1) {
                        XMLModifier xm = null;
                        if (tigValue != null) {
                            xm = vtdUtiles.insert("/termEntry/langSet[@xml:lang='" + curlang + "']/tig", "<tig>" + tigValue + "</tig>");
                        } else if (ntigValue != null) {
                            xm = vtdUtiles.insert("/termEntry/langSet[@xml:lang='" + curlang + "']/tig", "<ntig>" + ntigValue + "</ntig>");
                        }
                        vtdUtiles.bind(xm.outputAndReparse());
                        // 当pureText为空字符串时,HASH字段留空
                        String hash = null;
                        if (pureText != null) {
                            hash = "" + pureText.hashCode();
                        }
                        dbOperator.insertTextData("B", Integer.parseInt(termEntryPk), hash, pureText, fullText, Utils.convertLangCode(curlang), null, null);
                        changFlag = true;
                    }
                    _ap.resetXPath();
                    vtdUtiles.getVTDNav().pop();
                }
                termAp.resetXPath();
                //
                vu.getVTDNav().pop();
            } else {
                // 库中不存在该种语言的langSet,则新增
                XMLModifier xm = vtdUtiles.insert("/termEntry/langSet", vu.getElementFragment());
                vtdUtiles.bind(xm.outputAndReparse());
                changFlag = true;
                vu.getVTDNav().push();
                if (termAp.evalXPath() != -1) {
                    String fullText = vu.getElementContent();
                    String pureText = DocUtils.getTmxTbxPureText(vu);
                    // 当pureText为空字符串时,HASH字段留空
                    String hash = null;
                    if (pureText != null) {
                        hash = "" + pureText.hashCode();
                    }
                    dbOperator.insertTextData("B", Integer.parseInt(termEntryPk), hash, pureText, fullText, Utils.convertLangCode(curlang), null, null);
                }
                termAp.resetXPath();
                vu.getVTDNav().pop();
            }
            vtdUtiles.getVTDNav().pop();
        }
        langSetAp.resetXPath();
        vu.getVTDNav().pop();
        // 处理note
        List<String> existDbNoteId = new ArrayList<String>();
        List<String> existDbNotePk = new ArrayList<String>();
        // 用于记录已经处理的noteId,过滤掉文件重复的内容
        List<String> noteIdRm = new ArrayList<String>();
        StringBuffer noteContentBf = new StringBuffer();
        AutoPilot noteAp = new AutoPilot(vu.getVTDNav());
        vu.getVTDNav().push();
        noteAp.selectXPath("./note");
        while (noteAp.evalXPath() != -1) {
            String noteId = vu.getCurrentElementAttribut("id", "");
            String noteContent = vu.getElementContent();
            if (noteId.equals("") || noteContent == null || noteContent.equals("")) {
                continue;
            }
            String[] ids = noteId.split(",");
            if (ids.length != 2) {
                continue;
            }
            String reNoteId = ids[1] + "," + ids[0];
            if (noteIdRm.contains(noteId) || noteIdRm.contains(reNoteId)) {
                // 重复不重复写入
                continue;
            }
            // 记录已经处理的note
            noteIdRm.add(noteId);
            AutoPilot dbNoteAp = new AutoPilot(vtdUtiles.getVTDNav());
            dbNoteAp.selectXPath("/termEntry/note[@id='" + noteId + "' or @id='" + reNoteId + "']");
            while (dbNoteAp.evalXPath() != -1) {
                String dbNoteId = vtdUtiles.getCurrentElementAttribut("id", "");
                if (!existDbNoteId.contains(dbNoteId)) {
                    existDbNoteId.add(dbNoteId);
                }
                List<Map<String, String>> result = dbOperator.getBNodeByParent(termEntryPk, "termEntry", "E", "note", dbNoteId);
                for (Map<String, String> map : result) {
                    existDbNotePk.add(map.get("NPKID"));
                }
            }
            dbOperator.insertBNode(Integer.parseInt(termEntryPk), "termEntry", "E", "note", noteId, noteContent);
            noteContentBf.append(vu.getElementFragment());
        }
        vu.getVTDNav().pop();
        if (existDbNotePk.size() > 0) {
            dbOperator.deleteBNode(existDbNotePk);
        }
        StringBuffer xpathWhere = new StringBuffer();
        for (String dbNodeId : existDbNoteId) {
            xpathWhere.append("@id='" + dbNodeId + "' or ");
        }
        if (xpathWhere.length() > 0) {
            String xpath = xpathWhere.substring(0, xpathWhere.lastIndexOf("or"));
            // 清除文件中的note
            XMLModifier xm = vtdUtiles.delete("/termEntry/note[" + xpath + "]", VTDUtils.PILOT_TO_END);
            vtdUtiles.bind(xm.outputAndReparse());
            changFlag = true;
        }
        if (noteContentBf.length() != 0) {
            XMLModifier xm = vtdUtiles.insert("/termEntry/text()", noteContentBf.toString());
            vtdUtiles.bind(xm.outputAndReparse());
            changFlag = true;
        }
        if (changFlag) {
            dbOperator.updateTermEntry(vtdUtiles.getElementFragment(), termEntryPk);
        }
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) ArrayList(java.util.ArrayList) VTDGen(com.ximpleware.VTDGen) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with AutoPilot

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

the class ImportTbx method saveTermEntryWithAdd.

/**
	 * 始终增加策略
	 * @param headerId
	 * @throws XPathParseException
	 * @throws XPathEvalException
	 * @throws NavException
	 * @throws SQLException
	 *             ;
	 */
private void saveTermEntryWithAdd(int headerId, AutoPilot langSetAp, AutoPilot termAp) throws XPathParseException, XPathEvalException, NavException, SQLException {
    // 将TermEntry整个节点写入库中
    int termEntryPkId = dbOperator.insertBTermentry(vu.getElementFragment(), getElementAttribute("id"), headerId);
    vu.getVTDNav().push();
    while (langSetAp.evalXPath() != -1) {
        // 获取 LangSet节点下的语言
        String lang = getLang();
        String pureText = "";
        String fullText = "";
        // 取LangSet节点下的Term节点内容
        vu.getVTDNav().push();
        while (termAp.evalXPath() != -1) {
            pureText = DocUtils.getTmxTbxPureText(vu);
            fullText = vu.getElementContent();
            // TODO 将langSet节点中的term节点内容写入Textdata表中
            // 当pureText为空字符串时,HASH字段留空
            String hash = null;
            if (pureText != null) {
                hash = "" + pureText.hashCode();
            }
            dbOperator.insertTextData("B", termEntryPkId, hash, pureText, fullText, lang, null, null);
        }
        termAp.resetXPath();
        vu.getVTDNav().pop();
    }
    langSetAp.resetXPath();
    vu.getVTDNav().pop();
    AutoPilot noteAp = new AutoPilot(vu.getVTDNav());
    noteAp.selectXPath("./note");
    vu.getVTDNav().push();
    while (noteAp.evalXPath() != -1) {
        String content = vu.getElementContent();
        String id = vu.getCurrentElementAttribut("id", "");
        if (id.equals("") || content == null || content.equals("")) {
            continue;
        }
        dbOperator.insertBNode(termEntryPkId, "termEntry", "E", "note", id, content);
    }
    vu.getVTDNav().pop();
}
Also used : AutoPilot(com.ximpleware.AutoPilot)

Example 19 with AutoPilot

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

the class DocUtils method getTmxTbxPureText.

public static String getTmxTbxPureText(VTDUtils vu) throws NavException, XPathParseException, XPathEvalException {
    StringBuilder sb = new StringBuilder();
    VTDNav vn = vu.getVTDNav();
    try {
        vn.push();
        sb.append(vu.getElementContent());
        AutoPilot ap = new AutoPilot(vn);
        // 有子节点,即有内部标记
        if (vu.getChildElementsCount() < 1) {
            return sb.toString();
        }
        ap.resetXPath();
        ap.selectXPath("./*");
        while (ap.evalXPath() != -1) {
            String childNodeName = vu.getCurrentElementName();
            if ("g".equals(childNodeName) || "sub".equals(childNodeName) || "hi".equals(childNodeName) || "mrk".equals(childNodeName) || "foreign".equals(childNodeName)) {
                if (vu.getChildElementsCount() < 1) {
                    String childFrag = vu.getElementFragment();
                    String childContent = vu.getElementContent();
                    childContent = childContent == null ? "" : childContent;
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), childContent);
                } else {
                    String childFrag = vu.getElementFragment();
                    String childContent = getTmxTbxPureText(vu);
                    childContent = childContent == null ? "" : childContent;
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), childContent);
                }
            } else {
                // ph节点的值为code data或者一个sub节点,因此,要考虑到sub节点的情况
                if (vu.getChildElementsCount() < 1) {
                    String childFrag = vu.getElementFragment();
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), "");
                } else {
                    String childFrag = vu.getElementFragment();
                    String childContent = "";
                    AutoPilot _ap = new AutoPilot(vn);
                    _ap.selectXPath("./*");
                    while (_ap.evalXPath() != -1) {
                        if (vu.getChildElementsCount() <= 0) {
                            childContent += vu.getElementContent();
                        } else {
                            childContent += getTmxTbxPureText(vu);
                        }
                    }
                    childContent = childContent == null ? "" : childContent;
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), childContent);
                }
            }
        }
    } finally {
        vn.pop();
    }
    return sb.toString();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

Example 20 with AutoPilot

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

the class DocUtils method isTBX.

/**
	 * 判断是否是正确的 TBX 文件
	 * @param fileName
	 *            TBX 文件的全路径
	 * @return 反回null,验证失败
	 * @throws ParseException
	 * @throws EntityException
	 * @throws EOFException
	 * @throws EncodingException
	 * @throws FileNotFoundException
	 */
public static VTDUtils isTBX(String fileName) throws EncodingException, ParseException, FileNotFoundException {
    VTDGen vg = new VTDGen();
    FileInputStream fis = null;
    File f = null;
    try {
        f = new File(fileName);
        fis = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        int offset = 0;
        int numRead = 0;
        // I choose this value randomally,
        int numOfBytes = 1048576;
        // any other (not too big) value also can be here.
        if (b.length - offset < numOfBytes) {
            numOfBytes = b.length - offset;
        }
        while (offset < b.length && (numRead = fis.read(b, offset, numOfBytes)) >= 0) {
            offset += numRead;
            if (b.length - offset < numOfBytes) {
                numOfBytes = b.length - offset;
            }
        }
        vg.setDoc(b);
        vg.parse(true);
    } catch (IOException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger1"), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    String rootPath = "/martif";
    VTDUtils vtdUtils = new VTDUtils();
    try {
        vtdUtils.bind(vn);
        ap.selectXPath(rootPath);
        if (ap.evalXPath() == -1) {
            // } else {
            return null;
        }
    } catch (NavException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathEvalException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathParseException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } finally {
        vg.clear();
    }
    return vtdUtils;
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) NavException(com.ximpleware.NavException) IOException(java.io.IOException) EncodingException(com.ximpleware.EncodingException) FileNotFoundException(java.io.FileNotFoundException) ParseException(com.ximpleware.ParseException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Aggregations

AutoPilot (com.ximpleware.AutoPilot)308 VTDNav (com.ximpleware.VTDNav)173 NavException (com.ximpleware.NavException)150 XPathParseException (com.ximpleware.XPathParseException)145 XPathEvalException (com.ximpleware.XPathEvalException)137 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)112 IOException (java.io.IOException)103 ModifyException (com.ximpleware.ModifyException)95 TranscodeException (com.ximpleware.TranscodeException)82 CoreException (org.eclipse.core.runtime.CoreException)76 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 VTDGen (com.ximpleware.VTDGen)50 FileNotFoundException (java.io.FileNotFoundException)49 XMLModifier (com.ximpleware.XMLModifier)46 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)44 ArrayList (java.util.ArrayList)42 HashMap (java.util.HashMap)39 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)34 LinkedList (java.util.LinkedList)25