Search in sources :

Example 26 with NavException

use of com.ximpleware.NavException 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 27 with NavException

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

the class XLFHandler method validateMultiFileNodes.

/**
	 * 检验多个 file 节点是否存在 document 属性组,以及该属性组下是否存在 original 属性(用于转换 XLIFF 的源文件为 OpenOffice 和 MSOffice2007 的情况)
	 * @param fileName
	 * @return ;
	 */
public boolean validateMultiFileNodes(String fileName) {
    VTDNav vn = vnMap.get(fileName);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + fileName);
    try {
        AutoPilot subAp = new AutoPilot(vn);
        subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"original\"]");
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/xliff/file");
        while (ap.evalXPath() != -1) {
            vn.push();
            subAp.resetXPath();
            if (subAp.evalXPath() != -1) {
                String documentOriginal = vu.getElementContent();
                if (documentOriginal == null || documentOriginal.equals("")) {
                    return false;
                }
            } else {
                return false;
            }
            vn.pop();
        }
        return true;
    } 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 false;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 28 with NavException

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

the class XLFHandler method getNodeFrag.

/**
	 * 获取整个节点,包括其头部,其子节点,其文本
	 * @param xlfPath
	 * @param nodeXPath
	 *            节点的xpath
	 * @return robert 2011-10-21
	 */
public String getNodeFrag(String xlfPath, String nodeXPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    String xliffNodeContent = "";
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(nodeXPath);
        VTDUtils vu = new VTDUtils(vn);
        if (ap.evalXPath() != -1) {
            xliffNodeContent = vu.getElementFragment();
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return xliffNodeContent;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav)

Example 29 with NavException

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

the class XLFHandler method getSegmentIndex.

/**
	 * 得到文本段的索引
	 * @param currentRowIndex
	 * @param relativeXPath
	 * @param isNext
	 *            是否是找下一个。<br/>
	 *            <code>true</code>: 下一个;<code>false</code>: 上一个。
	 * @return 文本段的索引;
	 */
private int getSegmentIndex(int currentRowIndex, String relativeXPath, boolean isNext) {
    int step = isNext ? 1 : -1;
    currentRowIndex += step;
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    while (currentRowIndex >= 0 && currentRowIndex < rowIds.size()) {
        String rowId = getRowId(currentRowIndex);
        VTDNav vn = getVTDNavByRowId(rowId);
        ap.bind(vn);
        String xpath = RowIdUtil.parseRowIdToXPath(rowId);
        try {
            ap.selectXPath(xpath + relativeXPath);
            if (ap.evalXPath() != -1) {
                return currentRowIndex;
            }
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
        currentRowIndex += step;
    }
    return -1;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 30 with NavException

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

the class XLFHandler method addNote.

/**
	 * 添加批注
	 * @param rowId
	 *            行的唯一标识
	 * @param note
	 *            批注内容;
	 */
public void addNote(Map<String, List<String>> mapRowIdByFileName, String note, String from, boolean isApplyCurrent) {
    try {
        StringBuffer insertValue = new StringBuffer("<note");
        if (from != null) {
            insertValue.append(" from='").append(from).append("'");
        }
        if (!isApplyCurrent) {
            insertValue.append(" hs:apply-current='No'");
        }
        insertValue.append(">").append(StringUtilsBasic.checkNullStr(note)).append("</note>");
        VTDUtils vu = new VTDUtils();
        XMLModifier xm = new XMLModifier();
        Iterator<Entry<String, List<String>>> it = mapRowIdByFileName.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> entry = it.next();
            String fileName = entry.getKey();
            VTDNav vn = vnMap.get(fileName);
            vu.bind(vn);
            xm.bind(vn);
            for (String rowId : entry.getValue()) {
                StringBuffer xpath = new StringBuffer(RowIdUtil.parseRowIdToXPath(rowId));
                xm = vu.insert(null, xm, xpath.append("/text()").toString(), insertValue.toString());
            }
            saveAndReparse(xm, fileName);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) Entry(java.util.Map.Entry) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Aggregations

NavException (com.ximpleware.NavException)93 AutoPilot (com.ximpleware.AutoPilot)70 XPathParseException (com.ximpleware.XPathParseException)70 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)63 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)58 XMLModifier (com.ximpleware.XMLModifier)31 ModifyException (com.ximpleware.ModifyException)29 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)21 VTDGen (com.ximpleware.VTDGen)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)13 TranscodeException (com.ximpleware.TranscodeException)12 ParseException (com.ximpleware.ParseException)11 FileOutputStream (java.io.FileOutputStream)11 List (java.util.List)10 LinkedList (java.util.LinkedList)9 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8