Search in sources :

Example 46 with XMLModifier

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

the class AbstractDrawing method loadXML.

private void loadXML() {
    VTDGen vg = new VTDGen();
    vg.setDoc(this.xmlContent.getBytes());
    try {
        vg.parse(true);
        vu = new VTDUtils(vg.getNav());
        xm = new XMLModifier(vu.getVTDNav());
    } catch (VTDException e) {
        logger.error("", e);
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) VTDException(com.ximpleware.VTDException) VTDGen(com.ximpleware.VTDGen)

Example 47 with XMLModifier

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

the class XLFHandler method operateMergedXliff.

/**
	 * 针对合并后的xliff文件,会出现有些重复节点的情况,这个方法就是将重复节点进行整合。robert 2012-05-13
	 * 备注:要处理这些重复的节点,首选就是循环所有的占位符,这里是自定义的节点,例如<hs:TuPlaceHolder rowId="this is a rowId" />,之后通过rowId把这些文件
	 * @param xlfPath
	 */
public void operateMergedXliff(String xlfPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    AutoPilot ap = new AutoPilot(vn);
    AutoPilot updateAP = new AutoPilot(vn);
    AutoPilot parentAP = new AutoPilot(vn);
    try {
        XMLModifier xm = new XMLModifier(vn);
        VTDUtils vu = new VTDUtils(vn);
        // 先把把所有的占位符的RowId进行修改成当前文件的rowId
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        ap.selectXPath("/xliff/file/body//hs:TuPlaceHolder");
        while (ap.evalXPath() != -1) {
            int index = vn.getAttrVal("rowId");
            if (index != -1) {
                String rowId = vn.toString(index);
                String newRowId = MessageFormat.format(rowId, new Object[] { xlfPath });
                xm.updateToken(index, newRowId.getBytes());
            }
        }
        saveAndReparse(xm, xlfPath);
        // 现在开始,将所有的rowId取出,再取出rowId所对应的TU节点的frag。
        vn = vnMap.get(xlfPath);
        ap.bind(vn);
        updateAP.bind(vn);
        vu.bind(vn);
        xm.bind(vn);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        ap.selectXPath("/xliff/file/body//hs:TuPlaceHolder");
        Map<String, String> rowIdMap = new LinkedHashMap<String, String>();
        while (ap.evalXPath() != -1) {
            String tuFrag = "";
            String rowId = "";
            int index = vn.getAttrVal("rowId");
            if (index != -1) {
                rowId = vn.toString(index);
                vn.push();
                updateAP.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
                if (updateAP.evalXPath() != -1) {
                    tuFrag = vu.getElementFragment();
                }
                vn.pop();
            }
            // 开始存值
            if (!"".equals(tuFrag)) {
                rowIdMap.put(rowId, tuFrag);
            }
        }
        // 开始根据所有的rowId进行删除父节点
        parentAP.bind(vn);
        for (String rowId : rowIdMap.keySet()) {
            ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
            while (ap.evalXPath() != -1) {
                updateAP.resetXPath();
                updateAP.selectXPath("ancestor::node()");
                while (updateAP.evalXPath() != -1) {
                    boolean isBodyParent = false;
                    vn.push();
                    parentAP.selectXPath("parent::node()");
                    if (parentAP.evalXPath() != -1) {
                        int index = vn.getCurrentIndex();
                        if (index != -1 && "body".equals(vn.toString(index))) {
                            isBodyParent = true;
                        }
                    }
                    vn.pop();
                    if (isBodyParent) {
                        xm.remove(vn.getElementFragment());
                        saveAndReparse(xm, xlfPath);
                        vn = vnMap.get(xlfPath);
                        ap.bind(vn);
                        updateAP.bind(vn);
                        parentAP.bind(vn);
                        vu.bind(vn);
                        xm.bind(vn);
                        ap.resetXPath();
                        break;
                    }
                }
            }
        }
        // 最后,开始将每个占位符进行替换
        vn = vnMap.get(xlfPath);
        ap.bind(vn);
        updateAP.bind(vn);
        vu.bind(vn);
        xm.bind(vn);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        ap.selectXPath("/xliff/file/body//hs:TuPlaceHolder");
        while (ap.evalXPath() != -1) {
            String tuFrag = "";
            String rowId = "";
            int index = vn.getAttrVal("rowId");
            if (index != -1) {
                rowId = vn.toString(index);
                tuFrag = rowIdMap.get(rowId);
                // 先删除,再添加
                xm.remove(vn.getElementFragment());
                xm.insertAfterElement(tuFrag.getBytes());
            }
        }
        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) LinkedHashMap(java.util.LinkedHashMap)

Example 48 with XMLModifier

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

the class XLFHandler method handleSomeSegment.

/**
	 * 处理多个文本段
	 * @param rowIds
	 *            行的唯一标识
	 * @param handler
	 *            单个文本段的处理实现 ;
	 */
private Map<String, Object> handleSomeSegment(List<String> rowIds, PerSegmentHandler handler) {
    if (rowIds == null || rowIds.isEmpty()) {
        return getSuccessResult();
    }
    VTDNav vn = null;
    VTDUtils vu = new VTDUtils();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIds);
    String errorMsg = null;
    Throwable error = null;
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIdList = entry.getValue();
        vn = vnMap.get(fileName);
        vn.push();
        try {
            vu.bind(vn);
            ap.bind(vn);
            xm.bind(vn);
            for (String rowId : rowIdList) {
                handler.handle(rowId, vu, ap, xm);
            }
            // 保存并更新VTDNav对象
            saveAndReparse(xm, fileName);
        // return getSuccessResult(); //robert注释
        } catch (XPathParseException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger11");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (XPathEvalException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger12");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (NavException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger12");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (ModifyException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger13");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (UnsupportedEncodingException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger14");
            error = e;
            LOGGER.error(errorMsg, e);
        } finally {
            vn.pop();
        }
    }
    vu = null;
    ap = null;
    xm = null;
    if (errorMsg != null) {
        return getErrorResult(Messages.getString("file.XLFHandler.msg3"), error);
    } else {
        return getSuccessResult();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XPathParseException(com.ximpleware.XPathParseException) 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) VTDNav(com.ximpleware.VTDNav)

Example 49 with XMLModifier

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

the class XLPHandler method saveXliffInfo.

/**
	 * 保存 XLIFF 信息
	 * @param xliffInfo
	 *            XLIFF 信息 ;
	 */
public void saveXliffInfo(List<? extends Map<String, String>> xliffInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : xliffInfo) {
                vn.push();
                String xliff = map.get(ATTR_XLIFF);
                String tgtEnc = map.get(ATTR_TGT_ENC);
                String xpath = "./item[@" + ATTR_XLIFF + "='" + xliff + "']";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_TGT_ENC, tgtEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_XLIFF, xliff, ATTR_TGT_ENC, tgtEnc);
                    items.append(item).append(LINE_SEPARATOR);
                }
                vn.pop();
            }
            if (items.length() > 1) {
                xm.insertBeforeTail(items.toString());
            }
            save(xm);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException)

Example 50 with XMLModifier

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

the class XLPHandler method saveSourceInfo.

/**
	 * 保存 XLIFF 信息
	 * @param sourceInfo
	 *            源文件信息 ;
	 */
public void saveSourceInfo(List<? extends Map<String, String>> sourceInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" {6}=\"{7}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : sourceInfo) {
                vn.push();
                String source = map.get(ATTR_SOURCE);
                String format = map.get(ATTR_FORMAT);
                String srcLang = map.get(ATTR_SRC_LANG);
                String srcEnc = map.get(ATTR_SRC_ENC);
                String xpath = "./item[@" + ATTR_SOURCE + "=\"" + source + "\"]";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_FORMAT, format);
                    updateAttribute(xm, ATTR_SRC_LANG, srcLang);
                    updateAttribute(xm, ATTR_SRC_ENC, srcEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_SOURCE, source, ATTR_FORMAT, format, ATTR_SRC_LANG, srcLang, ATTR_SRC_ENC, srcEnc);
                    ap.selectXPath(xpath);
                    items.append(item).append(LINE_SEPARATOR);
                }
                vn.pop();
            }
            if (items.length() > 1) {
                xm.insertBeforeTail(items.toString());
            }
            save(xm);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException)

Aggregations

XMLModifier (com.ximpleware.XMLModifier)76 NavException (com.ximpleware.NavException)52 VTDNav (com.ximpleware.VTDNav)49 ModifyException (com.ximpleware.ModifyException)48 AutoPilot (com.ximpleware.AutoPilot)46 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)41 XPathParseException (com.ximpleware.XPathParseException)40 XPathEvalException (com.ximpleware.XPathEvalException)39 IOException (java.io.IOException)35 TranscodeException (com.ximpleware.TranscodeException)32 UnsupportedEncodingException (java.io.UnsupportedEncodingException)22 VTDGen (com.ximpleware.VTDGen)19 CoreException (org.eclipse.core.runtime.CoreException)17 FileNotFoundException (java.io.FileNotFoundException)15 ParseException (com.ximpleware.ParseException)13 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)13 ArrayList (java.util.ArrayList)12 FileOutputStream (java.io.FileOutputStream)11 XQException (javax.xml.xquery.XQException)11 File (java.io.File)10