Search in sources :

Example 11 with VTDNav

use of com.ximpleware.VTDNav 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 12 with VTDNav

use of com.ximpleware.VTDNav 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)

Example 13 with VTDNav

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

the class ApplicationWorkbenchWindowAdvisor method restorEditorHistory.

/**
	 * 重新恢复产品上次退时出所保存的编辑器,此修复针对 mac 下的 bug 2998 启动:某客户安装的软件只能使用一次.		robert	2013-05-21
	 */
private void restorEditorHistory() {
    // 只针对 mac 下的用户
    if (System.getProperty("os.name").indexOf("Mac") == -1) {
        return;
    }
    final WorkbenchPage page = (WorkbenchPage) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            monitor.beginTask("", 10);
            String tempEditorHistoryLC = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(Constant.TEMP_EDITOR_HISTORY).toOSString();
            File tempEditorHistoryFile = new File(tempEditorHistoryLC);
            if (!tempEditorHistoryFile.exists()) {
                return;
            }
            monitor.worked(1);
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            VTDGen vg = new VTDGen();
            try {
                boolean parseResult = vg.parseFile(tempEditorHistoryLC, true);
                if (!parseResult) {
                    return;
                }
                VTDNav vn = vg.getNav();
                AutoPilot ap = new AutoPilot(vn);
                int storeFileSum = 0;
                ap.selectXPath("count(/editors/editor)");
                storeFileSum = (int) ap.evalXPathToNumber();
                IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 9, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
                subMonitor.beginTask("", storeFileSum);
                ap.selectXPath("/editors/editor");
                String editorId = null;
                String localFilePath = null;
                boolean activate = false;
                IEditorReference activateEditorRefe = null;
                IEditorReference firstEditor = null;
                IFile curIFile = null;
                int index = -1;
                while (ap.evalXPath() != -1) {
                    activate = false;
                    editorId = null;
                    localFilePath = null;
                    if ((index = vn.getAttrVal("id")) != -1) {
                        editorId = vn.toRawString(index);
                    }
                    if ((index = vn.getAttrVal("path")) != -1) {
                        localFilePath = vn.toRawString(index);
                    }
                    if (editorId == null || editorId.trim().length() <= 0 || localFilePath == null || localFilePath.trim().length() <= 0) {
                        continue;
                    }
                    if ((index = vn.getAttrVal("active")) != -1) {
                        if ("true".equals(vn.toRawString(index))) {
                            activate = true;
                        }
                    }
                    curIFile = root.getFileForLocation(new Path(localFilePath));
                    if (!curIFile.exists()) {
                        subMonitor.worked(1);
                        continue;
                    }
                    if (activate) {
                        activateEditorRefe = page.getEditorManager().openEditor(editorId, new FileEditorInput(curIFile), false, null);
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().setActivePage(page);
                    } else {
                        if (firstEditor == null) {
                            firstEditor = page.getEditorManager().openEditor(editorId, new FileEditorInput(curIFile), false, null);
                        } else {
                            page.getEditorManager().openEditor(editorId, new FileEditorInput(curIFile), false, null);
                        }
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().setActivePage(page);
                    }
                    subMonitor.worked(1);
                }
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().setActivePage(page);
                if (activateEditorRefe != null) {
                    if (firstEditor != null) {
                        page.activate(firstEditor.getEditor(true));
                    }
                    page.activate(activateEditorRefe.getEditor(true));
                }
                subMonitor.done();
                monitor.done();
            } catch (Exception e) {
                LOGGER.error("restore editor file error", e);
            }
        }
    };
    try {
        new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()).run(true, true, runnable);
    } catch (Exception e) {
        LOGGER.error("restore editor file error", e);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) VTDGen(com.ximpleware.VTDGen) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) IEditorReference(org.eclipse.ui.IEditorReference) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) AutoPilot(com.ximpleware.AutoPilot) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IFile(org.eclipse.core.resources.IFile) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 14 with VTDNav

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

the class Application method deleteErrorMemoryInfo.

/**
	 * 删除错误记录文件,以修改产品第一次运行后,存储错误信息导致第二次打不开的情况	robert	2013-05-15
	 */
private static void deleteErrorMemoryInfo() {
    // 只针对 mac 下的用户
    if (System.getProperty("os.name").indexOf("Mac") == -1) {
        return;
    }
    String workbenchXmlPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.ui.workbench/workbench.xml").toOSString();
    VTDGen vg = new VTDGen();
    boolean result = vg.parseFile(workbenchXmlPath, true);
    if (!result) {
        new File(workbenchXmlPath).delete();
        return;
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    FileOutputStream stream = null;
    try {
        XMLModifier xm = new XMLModifier(vn);
        // 删除节点 editor
        ap.selectXPath("/workbench/window/page/editors/editor");
        while (ap.evalXPath() != -1) {
            xm.remove();
        }
        // 删除其他记录
        ap.selectXPath("/workbench/window/page/navigationHistory");
        if (ap.evalXPath() != -1) {
            xm.remove();
        }
        xm.output(workbenchXmlPath);
    } catch (Exception e) {
    // do nothing
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e2) {
            }
        }
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) VTDGen(com.ximpleware.VTDGen) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 15 with VTDNav

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

the class OpenFileWithValidAction method validIsopened.

/**
	 * 验证是否已经被打开
	 * @param iFile
	 * @return 如果当前要打开的文件未被打开,那么验证通过,返回true,否则返回false。
	 */
private boolean validIsopened(IFile iFile) throws Exception {
    String extention = iFile.getFileExtension();
    if (IDE.getDefaultEditor(iFile, true) != null && XLIFF_EDITOR_ID.equals(IDE.getDefaultEditor(iFile, true).getId()) && CommonFunction.validXlfExtension(extention)) {
        IEditorReference[] editorRes = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
        IEditorPart editor = null;
        IFile openIFile = null;
        String needOpenIfileLc = iFile.getLocation().toOSString();
        for (int i = 0; i < editorRes.length; i++) {
            if (editorRes[i].getId().equals(XLIFF_EDITOR_ID)) {
                // 备注:这里的两行代码已经被注释了,原因是因为 getEditor 方法会让每一个 editor 都初始一次,这并不是本次需求所要的。注意。	2013-01-04
                //editor = editorRes[i].getEditor(true);
                //openIFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
                openIFile = ((IFileEditorInput) editorRes[i].getEditorInput()).getFile();
                // 判断是否是合并打开,后缀名为xlp为合并打开,后缀名为xlf为单一打开
                if ("xlp".equals(openIFile.getFileExtension())) {
                    // 开始解析这个合并打开临时文件,获取合并打开的文件。
                    VTDGen vg = new VTDGen();
                    if (vg.parseFile(openIFile.getLocation().toOSString(), true)) {
                        VTDNav vn = vg.getNav();
                        AutoPilot ap = new AutoPilot(vn);
                        try {
                            ap.selectXPath("/mergerFiles/mergerFile/@filePath");
                            int index = -1;
                            while ((index = ap.evalXPath()) != -1) {
                                String fileLC = vn.toString(index + 1);
                                if (fileLC != null && !"".equals(fileLC)) {
                                    if (fileLC.equals(needOpenIfileLc)) {
                                        editor = editorRes[i].getEditor(true);
                                        page.activate(editor);
                                        return false;
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : IEditorReference(org.eclipse.ui.IEditorReference) IFile(org.eclipse.core.resources.IFile) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) IEditorPart(org.eclipse.ui.IEditorPart) VTDNav(com.ximpleware.VTDNav) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

VTDNav (com.ximpleware.VTDNav)206 AutoPilot (com.ximpleware.AutoPilot)177 NavException (com.ximpleware.NavException)128 XPathParseException (com.ximpleware.XPathParseException)115 XPathEvalException (com.ximpleware.XPathEvalException)110 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)99 IOException (java.io.IOException)85 ModifyException (com.ximpleware.ModifyException)81 TranscodeException (com.ximpleware.TranscodeException)69 CoreException (org.eclipse.core.runtime.CoreException)69 XMLModifier (com.ximpleware.XMLModifier)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)49 FileNotFoundException (java.io.FileNotFoundException)46 VTDGen (com.ximpleware.VTDGen)45 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)43 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)27 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)25 LinkedList (java.util.LinkedList)23