Search in sources :

Example 76 with AutoPilot

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

the class ReverseConversionValidateWithLibrary3 method reBuildXlf.

/**
	 * <div style='color:red;'>代替上面的 reBuildXlf 方法</div>
	 * 将要逆转换的 hsxliff 文件生成临时文件,再将这个临时文件进行处理,比如将分割的文本段拆分开来	robert	2012-11-28
	 * @param tmpXLFFile
	 */
private void reBuildXlf(String xliffPath, File tmpXLFFile) {
    //先将所有合并的文本段进行恢复成原来的样子
    try {
        ResourceUtils.copyFile(new File(xliffPath), tmpXLFFile);
        VTDGen vg = new VTDGen();
        if (!vg.parseFile(xliffPath, true)) {
            LOGGER.error(MessageFormat.format("{0} parse error!", xliffPath));
            return;
        }
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        AutoPilot childAP = new AutoPilot(vn);
        ap.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
        childAP.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = new XMLModifier(vn);
        // 先找出所有的分割与合并信息,再依序列号从高到低依次分解,合并信息是<ph id="hs-merge0~1" splitMergeIndex="0"> 这种标记
        NavigableMap<Long, SegMergeInfoBean> infoMap = new TreeMap<Long, SegMergeInfoBean>();
        ap.selectXPath("/xliff/file/body/descendant::node()" + "[(name()='group' and @ts='hs-split') or (name()='ph' and contains(@id, 'hs-merge'))]");
        int idx = -1;
        while (ap.evalXPath() != -1) {
            String nodeName = vn.toString(vn.getCurrentIndex());
            long index = -1;
            if ((idx = vn.getAttrVal("splitMergeIndex")) != -1) {
                index = Long.parseLong(vn.toString(idx));
            }
            boolean isMerge = false;
            // 如果是 ph 节点,那么这个就是合并信息
            if ("ph".equals(nodeName)) {
                isMerge = true;
                String phFrag = vu.getElementFragment();
                String phID = vn.toString(vn.getAttrVal("id"));
                String[] tuIds = vn.toString(vn.getAttrVal("id")).replace("hs-merge", "").split("~");
                String mergeFirstId = tuIds[0].trim();
                String mergeSecondId = tuIds[1].trim();
                System.out.println("mergeFirstId = " + mergeFirstId);
                System.out.println("mergeSecondId = " + mergeSecondId);
                infoMap.put(index, new SegMergeInfoBean(isMerge, phFrag, phID, mergeFirstId, mergeSecondId));
            } else {
                infoMap.put(index, new SegMergeInfoBean(isMerge));
            }
        }
        for (Entry<Long, SegMergeInfoBean> entry : infoMap.descendingMap().entrySet()) {
            Long index = entry.getKey();
            SegMergeInfoBean bean = entry.getValue();
            if (bean.isMerge()) {
                resetMerge(ap, vn, vu, xm, index, bean);
            } else {
                resetSplit(ap, childAP, vn, vu, xm, index);
            }
            vn = xm.outputAndReparse();
            xm.bind(vn);
            ap.bind(vn);
            childAP.bind(vn);
            vu.bind(vn);
        }
        xm.output(tmpXLFFile.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDGen(com.ximpleware.VTDGen) TreeMap(java.util.TreeMap) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) IFile(org.eclipse.core.resources.IFile) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 77 with AutoPilot

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

the class TSFileHandler method getNotes.

// 获取当前节点的所有批注。
private Vector<NoteBean> getNotes(VTDUtils vu) throws XPathEvalException, NavException, XPathParseException {
    Vector<NoteBean> notes = new Vector<NoteBean>();
    VTDNav vn = vu.getVTDNav();
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("./note");
    while (ap.evalXPath() != -1) {
        NoteBean note = new NoteBean(vu.getElementContent());
        int attInx = vn.getAttrVal("xml:lang");
        if (attInx != -1) {
            note.setLang(vn.toString(attInx));
        }
        attInx = vn.getAttrVal("from");
        if (attInx != -1) {
            note.setFrom(vn.toString(attInx));
        }
        attInx = vn.getAttrVal("priority");
        if (attInx != -1) {
            note.setPriority(vn.toString(attInx));
        }
        attInx = vn.getAttrVal("annotates");
        if (attInx != -1) {
            note.setAnnotates(vn.toString(attInx));
        }
        notes.add(note);
    }
    if (notes.isEmpty()) {
        notes = null;
    }
    return notes;
}
Also used : NoteBean(net.heartsome.cat.ts.core.bean.NoteBean) AutoPilot(com.ximpleware.AutoPilot) Vector(java.util.Vector) VTDNav(com.ximpleware.VTDNav)

Example 78 with AutoPilot

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

the class ProjectConfiger method getAttrMap.

/**
	 * 获取配置文件中的项目属性字段信息
	 * @return
	 * @throws XPathParseException
	 * @throws XPathEvalException
	 * @throws NavException ;
	 */
private LinkedHashMap<String, Object[]> getAttrMap() throws XPathParseException, XPathEvalException, NavException {
    AutoPilot ap = new AutoPilot(vu.getVTDNav());
    Map<String, Object[]> mapAttr = new HashMap<String, Object[]>();
    ap.selectXPath("/projectDescription/hs//attributeList/attribute");
    AutoPilot ap2 = new AutoPilot(vu.getVTDNav());
    final Collator collatorChinese = Collator.getInstance(java.util.Locale.CHINA);
    while (ap.evalXPath() != -1) {
        String attrName = TextUtil.stringToXML(vu.getCurrentElementAttribut("name", ""));
        String attrSelVal = TextUtil.stringToXML(vu.getCurrentElementAttribut("selection", ""));
        vu.getVTDNav().push();
        ap2.selectXPath("./item");
        ArrayList<String> lstAttrVal = new ArrayList<String>();
        while (ap2.evalXPath() != -1) {
            lstAttrVal.add(vu.getElementContent());
        }
        vu.getVTDNav().pop();
        Collections.sort(lstAttrVal, collatorChinese);
        mapAttr.put(attrName, new Object[] { attrSelVal, lstAttrVal });
    }
    List<Entry<String, Object[]>> lstAttr = new ArrayList<Entry<String, Object[]>>(mapAttr.entrySet());
    Collections.sort(lstAttr, new Comparator<Entry<String, Object[]>>() {

        public int compare(Entry<String, Object[]> arg0, Entry<String, Object[]> arg1) {
            return collatorChinese.compare(arg0.getKey(), arg1.getKey());
        }
    });
    LinkedHashMap<String, Object[]> linkedMapAttr = new LinkedHashMap<String, Object[]>();
    for (Entry<String, Object[]> entry : lstAttr) {
        linkedMapAttr.put(entry.getKey(), entry.getValue());
    }
    return linkedMapAttr;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Collator(java.text.Collator) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) AutoPilot(com.ximpleware.AutoPilot)

Example 79 with AutoPilot

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

the class ProjectConfiger method getProjectName.

/**
	 * 获取项目名称,如果在设置项目信息时,未填写该值返回将为空串
	 * @return 如果返回null表示为获取到项目名称;
	 * @throws XPathParseException
	 * @throws XPathEvalException
	 * @throws NavException
	 */
public String getProjectName() throws XPathParseException, XPathEvalException, NavException {
    AutoPilot ap = new AutoPilot(vu.getVTDNav());
    ap.selectXPath("/projectDescription/hs");
    if (ap.evalXPath() != -1) {
        return TextUtil.resetSpecialString(vu.getChildContent("projectname"));
    }
    return null;
}
Also used : AutoPilot(com.ximpleware.AutoPilot)

Example 80 with AutoPilot

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

the class ProjectConfiger method getTargetlanguages.

/**
	 * 获取项目的所有目标语言
	 * @return 如果返回空的list表示没有获取到项目的目标语言;
	 * @throws XPathParseException
	 * @throws NavException
	 * @throws XPathEvalException
	 */
public List<Language> getTargetlanguages() throws XPathParseException, NavException, XPathEvalException {
    AutoPilot ap = new AutoPilot(vu.getVTDNav());
    ap.selectXPath("/projectDescription/hs/language/target");
    List<Language> targetLangs = new ArrayList<Language>();
    while (ap.evalXPath() != -1) {
        String code = vu.getCurrentElementAttribut("code", "");
        String name = vu.getElementContent();
        String image = vu.getCurrentElementAttribut("image", "");
        String isBidi = vu.getCurrentElementAttribut("isbidi", "false");
        targetLangs.add(new Language(code, name, image, isBidi.equals("false") ? false : true));
    }
    return targetLangs;
}
Also used : Language(net.heartsome.cat.common.locale.Language) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList)

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