Search in sources :

Example 26 with AutoPilot

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

the class PartOperate method loadFile.

/**
	 * 解析文件
	 * @param nameSpaceMap	要申明的命名空间
	 * @throws Exception
	 */
protected void loadFile(Map<String, String> nameSpaceMap) throws Exception {
    VTDGen vg = new VTDGen();
    if (vg.parseFile(partPath, true)) {
        vn = vg.getNav();
        ap = new AutoPilot(vn);
        otherAP = new AutoPilot(vn);
        childAP = new AutoPilot(vn);
        extendAP = new AutoPilot(vn);
        vu = new VTDUtils(vn);
        xm = new XMLModifier(vn);
        // 给 ap 申明命名空间
        for (Entry<String, String> entry : nameSpaceMap.entrySet()) {
            ap.declareXPathNameSpace(entry.getKey(), entry.getValue());
            childAP.declareXPathNameSpace(entry.getKey(), entry.getValue());
            otherAP.declareXPathNameSpace(entry.getKey(), entry.getValue());
            extendAP.declareXPathNameSpace(entry.getKey(), entry.getValue());
        }
    } else {
        throw new DocxConverterException(MessageFormat.format(Messages.getString("docxConvert.msg2"), partPath));
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) DocxConverterException(net.heartsome.cat.converter.word2007.common.DocxConverterException) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen)

Example 27 with AutoPilot

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

the class XliffInputer method restoreGTag.

private void restoreGTag(String interTagPath) throws Exception {
    if (!new File(interTagPath).exists()) {
        return;
    }
    // 先解析 interTag.xml
    VTDGen vg = new VTDGen();
    if (!vg.parseFile(interTagPath, true)) {
        throw new Exception();
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    VTDUtils vu = new VTDUtils(vn);
    String xpath = "/docxTags/tag";
    ap.selectXPath(xpath);
    Map<String, String> tagMap = new HashMap<String, String>();
    String tuId = null;
    int index = -1;
    while (ap.evalXPath() != -1) {
        tuId = null;
        if ((index = vn.getAttrVal("tuId")) != -1) {
            tuId = vn.toString(index);
        }
        if (tuId == null) {
            continue;
        }
        String content = vu.getElementContent().replace("</g>", "");
        if (content.indexOf("<g") != 0) {
            continue;
        }
        tagMap.put(tuId, content);
    }
    // 再将结果传至 xliff 文件
    vg = new VTDGen();
    if (!vg.parseFile(xliffFile, true)) {
        throw new Exception();
    }
    vn = vg.getNav();
    vu.bind(vn);
    ap.bind(vn);
    XMLModifier xm = new XMLModifier(vn);
    for (Entry<String, String> entry : tagMap.entrySet()) {
        String thisTuId = entry.getKey();
        String tagContent = entry.getValue();
        // docx 转换器里面是没有 多个file节点 的情况
        // 先处理源文
        xpath = "/xliff/file/body//trans-unit[@id='" + thisTuId + "']/source";
        ap.selectXPath(xpath);
        if (ap.evalXPath() != -1) {
            String srcHeader = vu.getElementHead();
            String oldContent = vu.getElementContent();
            xm.remove();
            StringBuffer newFragSB = new StringBuffer();
            newFragSB.append(srcHeader);
            newFragSB.append(tagContent);
            newFragSB.append(oldContent);
            newFragSB.append("</g></source>");
            xm.insertAfterElement(newFragSB.toString());
        }
        // 处理译文
        xpath = "/xliff/file/body//trans-unit[@id='" + thisTuId + "']/target";
        ap.selectXPath(xpath);
        if (ap.evalXPath() != -1) {
            String srcHeader = vu.getElementHead();
            String oldContent = vu.getElementContent();
            xm.remove();
            StringBuffer newFragSB = new StringBuffer();
            newFragSB.append(srcHeader);
            newFragSB.append(tagContent);
            newFragSB.append(oldContent);
            newFragSB.append("</g></target>");
            xm.insertAfterElement(newFragSB.toString());
        }
    }
    xm.output(xliffFile);
    // 删除 interTag.xml
    File interTagFile = new File(interTagPath);
    interTagFile.delete();
}
Also used : XMLModifier(com.ximpleware.XMLModifier) HashMap(java.util.HashMap) VTDGen(com.ximpleware.VTDGen) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 28 with AutoPilot

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

the class XliffInputer method anaysisTgtOrSrcNode.

/**
	 * 分析 source 或 target 节点,获取其内容
	 * @throws Exception
	 */
private void anaysisTgtOrSrcNode(StringBuffer textSB, VTDNav vn, boolean isText) throws Exception {
    vn.push();
    AutoPilot otherAP = new AutoPilot(vn);
    String childXpath = "./text()|node()";
    otherAP.selectXPath(childXpath);
    int tokenId = -1;
    int index = -1;
    Map<Integer, SectionSegBean> targetMap = new TreeMap<Integer, SectionSegBean>();
    while (otherAP.evalXPath() != -1) {
        index = vn.getCurrentIndex();
        tokenId = vn.getTokenType(index);
        if (tokenId == 0) {
            //节点子节点
            ananysisTag(vn, targetMap);
        } else if (tokenId == 5) {
            // 文本子节点
            //				if ("+1 845-536-1416".equals(vn.toString(index))) {
            //					System.out.println("问题开始了。。。。");
            //				}
            targetMap.put(index, new SectionSegBean(null, vn.toRawString(index), null, null, null));
        }
    }
    vn.pop();
    SectionSegBean bean;
    for (Entry<Integer, SectionSegBean> entry : targetMap.entrySet()) {
        bean = entry.getValue();
        if (isText) {
            if (bean.getText() != null) {
                textSB.append(bean.getText());
            }
        } else {
            // 这个要组装 w:r 等节点
            String ctype = bean.getCtype() == null ? "" : bean.getCtype();
            String style = bean.getStyle() == null ? "" : bean.getStyle();
            String extendNodes = bean.getExtendNodesStr() == null ? "" : bean.getExtendNodesStr();
            if (bean.getPhTagStr() != null) {
                textSB.append(bean.getPhTagStr());
            } else {
                if ("".equals(ctype)) {
                    textSB.append("<w:r>" + style + extendNodes);
                    textSB.append("<w:t xml:space=\"preserve\">" + bean.getText() + "</w:t></w:r>");
                } else {
                    // <w:hyperlink r:id="rId8" w:history="1">
                    int endIdx = ctype.indexOf(" ") == -1 ? ctype.indexOf(">") : ctype.indexOf(" ");
                    String nodeName = ctype.substring(ctype.indexOf("<") + 1, endIdx);
                    textSB.append(ctype);
                    textSB.append("<w:r>" + style + extendNodes);
                    textSB.append("<w:t xml:space=\"preserve\">" + bean.getText() + "</w:t></w:r>");
                    textSB.append("</" + nodeName + ">");
                }
            }
        }
    }
}
Also used : SectionSegBean(net.heartsome.cat.converter.word2007.common.SectionSegBean) AutoPilot(com.ximpleware.AutoPilot) TreeMap(java.util.TreeMap)

Example 29 with AutoPilot

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

the class DocumentRelation method loadFile.

private void loadFile() throws Exception {
    VTDGen vg = new VTDGen();
    if (vg.parseFile(xmlPath, true)) {
        vn = vg.getNav();
        ap = new AutoPilot(vn);
    } else {
        throw new Exception(MessageFormat.format(Messages.getString("docxConvert.msg2"), xmlPath));
    }
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException)

Example 30 with AutoPilot

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

the class Xliff2Ttx method main.

public static void main(String[] args) {
    //		String content = "<g id='2'>this is a test</g>asdf as sad ";
    //		content = content.substring(0, content.lastIndexOf("</g>")) + content.substring(content.lastIndexOf("</g>") + 4);
    //		System.out.println(content);
    //		String content = "<g id='2'>this is a test</g>asdf as sad</g> ";
    //		int index = content.indexOf("</g>");
    //		content = "<cf" + content.substring(content.indexOf("<g") + 2, content.indexOf(">")) + ">";
    //		System.out.println(content);
    int index = -1;
    //		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt;</ph></g>";
    //		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;field/&gt;</ph></g>";
    //		String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;/cf&gt;</ph></g><ph>&lt;field/&gt;</ph>";
    String content = "<g id='2' size=\"11\" complexscriptssize=\"11\" complexscriptsbold=\"on\" bold=\"on\" superscript=\"on\"><ph>&lt;/cf&gt;</ph></g>这后面是个cf标记了哦。<ph type='cf'>&lt;/cf&gt;</ph>";
    index = content.indexOf("<ph");
    while (index != -1) {
        String phFrag = content.substring(index, content.indexOf("</ph>", index) + 5);
        System.out.println(phFrag);
        try {
            VTDGen vg = new VTDGen();
            vg.setDoc(phFrag.getBytes());
            vg.parse(true);
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            VTDUtils vu = new VTDUtils(vn);
            ap.selectXPath("/ph");
            String replaceText = "";
            String phContent = "";
            int attrIdx = -1;
            if (ap.evalXPath() != -1) {
                phContent = vu.getElementContent();
                // 有type属性的,一般是cf标记
                if ((attrIdx = vn.getAttrVal("type")) != -1) {
                    if ("cf".equals(vn.toString(attrIdx))) {
                        String utType = "";
                        // 这个cf是开始还是结束&lt;cf size=&quot;11&quot; complexscriptssize=&quot;11&quot;&gt;
                        if (phContent.indexOf("&lt;cf") != -1) {
                            utType = "start";
                        } else {
                            utType = "end";
                        }
                        replaceText = "<ut Type=\"" + utType + "\" RightEdge=\"angle\" DisplayText=\"cf\">" + phContent + "</ut>";
                    }
                } else {
                    //没有type的,是其他标记,如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
                    String tagName = "";
                    int startIdx = -1;
                    int endIdx = -1;
                    // 针对起始标记如&lt;symbol font=&quot;Symbol&quot; character=&quot;F0E2&quot;/&gt
                    if ((startIdx = phContent.trim().indexOf("&lt;")) != -1) {
                        //针对结束标记如&lt;/null?&gt;
                        if ("/".equals(phContent.trim().substring(startIdx + 4, startIdx + 5))) {
                            tagName = phContent.trim().substring(startIdx + 5, phContent.trim().indexOf("&gt;"));
                        } else {
                            if ((endIdx = phContent.trim().indexOf(" ")) != -1) {
                                tagName = phContent.trim().substring(startIdx + 4, endIdx);
                            } else {
                                //针对没有空格的如<ph>&lt;field/&gt;</ph>
                                tagName = phContent.trim().substring(startIdx + 4, phContent.trim().indexOf("/"));
                            }
                        }
                    //							System.out.println("tagName = '" + tagName + "'");
                    }
                    //						System.out.println( "tagName = " + tagName);
                    replaceText = "<ut DisplayText=\"" + tagName + "\">" + phContent + "</ut>";
                }
                content = content.replace(phFrag, replaceText);
            //					System.out.println(content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        index = content.indexOf("<ph", index + 1);
    }
    //		System.out.println(content);
    String tagStr = "<g id='1' size='12'/>";
    String newTagStr = "<cf" + tagStr.substring(tagStr.indexOf("<g") + 2, tagStr.indexOf("/>")) + ">";
    newTagStr = "<ut Type=\"start\" RightEdge=\"angle\" DisplayText=\"cf\">" + TextUtil.cleanSpecialString(newTagStr) + "</ut>";
//		System.out.println("newTagStr = " + newTagStr);
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) VTDNav(com.ximpleware.VTDNav) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ConverterException(net.heartsome.cat.converter.ConverterException)

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