Search in sources :

Example 31 with VTDNav

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

the class ExportDocx method addTagRIdToRels.

/**
	 * 给标记的 r:id 属性添加关系申明
	 */
private void addTagRIdToRels() throws Exception {
    VTDGen vg = new VTDGen();
    String docRelsPath = docxFolderPath + File.separator + "word" + File.separator + "_rels" + File.separator + "document.xml.rels";
    boolean parseResult = vg.parseFile(docRelsPath, true);
    if (!parseResult) {
        throw new Exception(MessageFormat.format("文件{0}解析失败!", new Object[] { docRelsPath }));
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    XMLModifier xm = new XMLModifier(vn);
    ap.selectXPath("/Relationships");
    if (ap.evalXPath() != -1) {
        String tagRelationShip = "<Relationship Id=\"" + tagRId + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"http://null\" TargetMode=\"External\"/>";
        xm.insertBeforeTail(tagRelationShip);
        xm.output(docRelsPath);
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) VTDNav(com.ximpleware.VTDNav)

Example 32 with VTDNav

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

the class ImportExternalDialog method testUncelan.

private int testUncelan(String path) {
    VTDGen vg = new VTDGen();
    if (!vg.parseZIPFile(path, "word/styles.xml", true)) {
        alertParseError(path);
        return -2;
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    ap.declareXPathNameSpace("w", ExportExternal.NAMESPACE_W);
    try {
        ap.selectXPath("/w:styles/w:style[@w:styleId=\"tw4winMark\"]");
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        return -2;
    }
    return ap.evalXPathToBoolean() ? ExportExternal.EXPORT_SDLUNCLEAN : -1;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) VTDNav(com.ximpleware.VTDNav)

Example 33 with VTDNav

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

the class ExportConfig method transunitLooper.

private void transunitLooper(ExportWriter exper) {
    if (projects == null) {
        return;
    }
    final int steps = 100;
    int totalFile = mapSaveAs.size();
    String baseXpath1 = "/xliff/file";
    String baseXpath = "./body/descendant::trans-unit";
    int index = 0;
    for (IProject project : projects) {
        inner: for (XliffBean bean : mapXlfBeans.get(project)) {
            String saveAs = mapSaveAs.get(bean);
            if (!setSaveAs.contains(saveAs)) {
                return;
            }
            String xlfFileName = bean.getXliffFile().substring(bean.getXliffFile().lastIndexOf(File.separator) + 1);
            IProgressMonitor subMonitor = null;
            // 进度条信息
            if (monitor != null) {
                monitor.beginTask("[" + ++index + "/" + totalFile + "]", 1);
            }
            try {
                exper.init(project, bean);
            } catch (OperationCanceledException e) {
                continue;
            } catch (ExportExternalException e) {
                LOGGER.error("", e);
                openErrorDialog(e);
                return;
            } catch (ExportCanceledException e) {
                LOGGER.error("", e);
                openErrorDialog(e);
                continue;
            }
            VTDUtils vu = new VTDUtils();
            try {
                vu.parseFile(bean.getXliffFile(), true);
            } catch (Exception e) {
                if (openErrorDialog(MessageFormat.format(Messages.getString("ExportDocxDialog.ok.parseError"), xlfFileName))) {
                    continue;
                }
            }
            try {
                VTDNav vn = vu.getVTDNav();
                AutoPilot apFile = new AutoPilot(vn);
                // 总控制
                AutoPilot ap = new AutoPilot(vn);
                // 临时用
                AutoPilot _ap = new AutoPilot(vn);
                apFile.declareXPathNameSpace("hs", ExportExternal.NAMESPACE_HS);
                ap.declareXPathNameSpace("hs", ExportExternal.NAMESPACE_HS);
                _ap.declareXPathNameSpace("hs", ExportExternal.NAMESPACE_HS);
                int page = 0;
                double total = 0;
                int worked = 0;
                int count = 0;
                int tmp = 0;
                if (monitor != null) {
                    _ap.selectXPath("count(/xliff/file/body/descendant::trans-unit)");
                    total = _ap.evalXPathToNumber();
                    if (total == 0) {
                        exper.canceled();
                        continue;
                    }
                    subMonitor = new SubProgressMonitor(monitor, 1);
                    subMonitor.setTaskName("[" + index + "/" + totalFile + "]" + Messages.getString("ExportDocxDialog.ok.monitor.msg0") + xlfFileName);
                    subMonitor.beginTask("subtask", (total > steps ? steps : (int) total) + 1);
                }
                apFile.selectXPath(baseXpath1);
                while (apFile.evalXPath() != -1) {
                    page++;
                    ap.selectXPath(baseXpath);
                    while (ap.evalXPath() != -1) {
                        // monitor
                        if (subMonitor != null) {
                            if (subMonitor.isCanceled()) {
                                exper.canceled();
                                return;
                            }
                            tmp = (int) ((count++ / total) * steps);
                            if (tmp > worked) {
                                subMonitor.worked(tmp - worked);
                                worked = tmp;
                            }
                        }
                        if (filterAllTrans) {
                            // continue if bingo (排除此项)
                            if (exceptLocked && testXpath(_ap, "./@translate='no'")) {
                                continue;
                            }
                            // should we use machine translation??
                            if (exceptFullMatch && testXpath(_ap, "./target/@hs:matchType and ./target/@hs:quality='100'")) {
                                continue;
                            }
                            // should we except machine translation ??
                            if (exceptContextMatch && testXpath(_ap, "./target/@hs:matchType and ./target/@hs:quality='101'")) {
                                continue;
                            }
                        } else if (filterSpecial) {
                            // just contains <note> or state='need-review'
                            boolean bingo = false;
                            if (withNote) {
                                bingo = testXpath(_ap, "./note");
                            }
                            if (!bingo && withNeedReview) {
                                bingo = testXpath(_ap, "./@hs:needs-review='yes'");
                            }
                            if (!bingo) {
                                continue;
                            }
                        } else if (filterHasState) {
                            // when tu not/new/has translated, or approved
                            boolean bingo = false;
                            if (!bingo && noTrans) {
                                bingo = testXpath(_ap, "string-length(./target/text()) < 1") && !testXpath(_ap, "./target/*");
                            }
                            if (!bingo && newTrans) {
                                bingo = testXpath(_ap, "./target/@state='new'");
                            }
                            if (!bingo && transed) {
                                bingo = testXpath(_ap, "./target/@state='translated'") && !testXpath(_ap, "./@approved='yes'");
                            }
                            if (!bingo && approved) {
                                bingo = testXpath(_ap, "./@approved='yes'");
                            }
                            if (!bingo) {
                                continue;
                            }
                        }
                        try {
                            exper.append(vu, count, page);
                        } catch (ExportExternalException e) {
                            openErrorDialog(e);
                            continue inner;
                        }
                    }
                }
                try {
                    exper.flush();
                } catch (ExportExternalException e) {
                    openErrorDialog(e);
                    continue;
                }
                if (subMonitor != null) {
                    subMonitor.done();
                }
            } catch (Exception e) {
                e.printStackTrace();
                openErrorDialog(e);
                return;
            }
        }
    }
    exper.close();
    if (monitor != null) {
        monitor.done();
    }
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IProject(org.eclipse.core.resources.IProject) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) VTDNav(com.ximpleware.VTDNav)

Example 34 with VTDNav

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

the class ColumnTypeDialog method initListener.

/**
	 * 初始化加载配置和保存配置按钮的监听 ;
	 */
private void initListener() {
    btnLoadConfiguration.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent event) {
            FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
            fd.setText(Messages.getString("dialog.ColumnTypeDialog.fdTitle"));
            //$NON-NLS-1$ //$NON-NLS-2$
            String[] extensions = { "*.ctc", "*" };
            String[] names = { Messages.getString("dialog.ColumnTypeDialog.filterName1"), //$NON-NLS-1$ //$NON-NLS-2$
            Messages.getString("dialog.ColumnTypeDialog.filterName2") };
            fd.setFilterExtensions(extensions);
            fd.setFilterNames(names);
            fd.setFilterPath(System.getProperty("user.home"));
            String name = fd.open();
            if (name == null) {
                return;
            }
            try {
                VTDGen vg = new VTDGen();
                if (vg.parseFile(name, true)) {
                    VTDNav vn = vg.getNav();
                    VTDUtils vu = new VTDUtils(vn);
                    AutoPilot ap = new AutoPilot(vn);
                    ap.selectXPath("/CSV2TBX-configuration");
                    ap.evalXPath();
                    int count = vu.getChildElementsCount();
                    if (count != arrCmbLangs.length) {
                        MessageDialog.openInformation(getShell(), Messages.getString("dialog.ColumnTypeDialog.msgTitle"), Messages.getString("dialog.ColumnTypeDialog.msg1"));
                        return;
                    }
                    String xpath = "/CSV2TBX-configuration/item";
                    ap.selectXPath(xpath);
                    int i = 0;
                    while (ap.evalXPath() != -1) {
                        String propLevel = vu.getCurrentElementAttribut("propLevel", "");
                        //$NON-NLS-1$ //$NON-NLS-2$
                        String propType = vu.getCurrentElementAttribut("propType", "");
                        //$NON-NLS-1$ //$NON-NLS-2$
                        String lang = vu.getCurrentElementAttribut("propLang", "");
                        String propName = vu.getCurrentElementAttribut("propName", "");
                        arrCmbPropsLevel[i].setItems(levelValues);
                        arrCmbPropsLevel[i].select(0);
                        if (!propLevel.equals("")) {
                            //$NON-NLS-1$
                            arrCmbPropsLevel[i].setText(propLevel);
                            if (propLevel.equals(ColProperties.conceptLevel)) {
                                arrCmbLangs[i].setEnabled(false);
                                arrCmbPropsName[i].setItems(conceptPropValues);
                                arrCmbPropsName[i].select(0);
                                arrCmbPropsType[i].setItems(conceptPropTypeValues);
                                arrCmbPropsType[i].select(0);
                            }
                            if (propLevel.equals(ColProperties.langLevel)) {
                                arrCmbLangs[i].setEnabled(true);
                                arrCmbPropsName[i].setItems(TranslationPropValues);
                                arrCmbPropsName[i].select(0);
                                arrCmbPropsType[i].setItems(termDescripPropTypeValues);
                                arrCmbPropsType[i].select(0);
                            }
                        }
                        // Update content for Prop Name combo
                        if (!propName.equals("")) {
                            //$NON-NLS-1$
                            arrCmbPropsName[i].setText(propName);
                        }
                        if (!propLevel.equals("")) {
                            //$NON-NLS-1$
                            if (propLevel.equals(ColProperties.conceptLevel)) {
                                arrCmbPropsType[i].setEnabled(propName.equals(ColProperties.descripName));
                                arrCmbPropsType[i].setItems(conceptPropTypeValues);
                                arrCmbPropsType[i].select(0);
                            }
                            if (propLevel.equals(ColProperties.langLevel)) {
                                arrCmbPropsType[i].setEnabled(!propName.equals(ColProperties.termName));
                                if (propName.equals(ColProperties.descripName)) {
                                    arrCmbPropsType[i].setItems(termDescripPropTypeValues);
                                } else {
                                    arrCmbPropsType[i].setItems(termTermNotePropTypeValues);
                                }
                                arrCmbPropsType[i].select(0);
                            }
                        }
                        // Update content for Prop Type combo
                        if (!propType.equals("")) {
                            //$NON-NLS-1$
                            arrCmbPropsType[i].setText(propType);
                        }
                        // Update content for Language Combo
                        arrCmbLangs[i].setItems(LocaleService.getLanguages());
                        arrCmbLangs[i].select(0);
                        if (!lang.equals("")) {
                            //$NON-NLS-1$
                            arrCmbLangs[i].setText(lang);
                        }
                        i++;
                    }
                }
            } catch (XPathParseException e) {
                LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger1"), e);
            } catch (NavException e) {
                LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger1"), e);
            } catch (XPathEvalException e) {
                LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger1"), e);
            }
        }

        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    btnSaveConfiguration.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
            fd.setText(Messages.getString("dialog.ColumnTypeDialog.savefdTitle"));
            //$NON-NLS-1$ //$NON-NLS-2$
            String[] extensions = { "*.ctc", "*.*" };
            String[] names = { Messages.getString("dialog.ColumnTypeDialog.filterName1"), //$NON-NLS-1$ //$NON-NLS-2$
            Messages.getString("dialog.ColumnTypeDialog.filterName2") };
            fd.setFilterExtensions(extensions);
            fd.setFilterNames(names);
            fd.setFilterPath(System.getProperty("user.home"));
            String name = fd.open();
            if (name == null) {
                return;
            }
            try {
                FileOutputStream output = new FileOutputStream(name);
                //$NON-NLS-1$ //$NON-NLS-2$
                output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes("UTF-8"));
                //$NON-NLS-1$ //$NON-NLS-2$
                output.write("<CSV2TBX-configuration>\n".getBytes("UTF-8"));
                for (int i = 0; i < arrCmbLangs.length; i++) {
                    String strItem = //$NON-NLS-1$
                    "<item propLang=\"" + arrCmbLangs[i].getText() + "\" propName=\"" + //$NON-NLS-1$
                    arrCmbPropsName[i].getText() + "\" propType=\"" + //$NON-NLS-1$
                    arrCmbPropsType[i].getText() + "\" propLevel=\"" + //$NON-NLS-1$
                    arrCmbPropsLevel[i].getText() + //$NON-NLS-1$
                    "\"/>\n";
                    //$NON-NLS-1$
                    output.write(strItem.getBytes("UTF-8"));
                }
                //$NON-NLS-1$ //$NON-NLS-2$
                output.write("</CSV2TBX-configuration>\n".getBytes("UTF-8"));
                output.close();
            } catch (FileNotFoundException e1) {
                LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger2"), e);
            } catch (UnsupportedEncodingException e1) {
                LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger2"), e);
            } catch (IOException e1) {
                LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger2"), e);
            }
        }

        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileDialog(org.eclipse.swt.widgets.FileDialog) VTDNav(com.ximpleware.VTDNav) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 35 with VTDNav

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

the class PluginConfigManage method getPluginCofigData.

/**
	 * 从插件配置文件中获取插件配置的相关信息
	 * @return ;
	 */
public List<PluginConfigBean> getPluginCofigData() {
    System.out.println(pluginXmlLocation);
    List<PluginConfigBean> dataList = new LinkedList<PluginConfigBean>();
    File pluginXMl = new File(pluginXmlLocation);
    if (!pluginXMl.exists()) {
        return dataList;
    }
    VTDGen vg = new VTDGen();
    vg.parseFile(pluginXmlLocation, true);
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    try {
        ap.selectXPath("/shortcuts/plugin");
        PluginConfigBean bean;
        while (ap.evalXPath() != -1) {
            String id = "";
            String name = "";
            String commandLine = "";
            String output = "";
            String input = "";
            String shortcutKey = "";
            String outputPath = "";
            int index = -1;
            if ((index = vn.getAttrVal("id")) != -1) {
                id = vn.toString(index);
            }
            if ((index = vn.getText()) != -1) {
                name = vn.toString(index);
            }
            if ((index = vn.getAttrVal("command")) != -1) {
                commandLine = vn.toString(index);
            }
            if ((index = vn.getAttrVal("output")) != -1) {
                output = vn.toString(index);
            }
            if ((index = vn.getAttrVal("input")) != -1) {
                input = vn.toString(index);
            }
            if ((index = vn.getAttrVal("shortcut-key")) != -1) {
                shortcutKey = vn.toString(index);
            }
            if ((index = vn.getAttrVal("outputpath")) != -1) {
                outputPath = vn.toString(index);
            }
            bean = new PluginConfigBean(id, name, commandLine, input, output, outputPath, shortcutKey);
            dataList.add(bean);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    return dataList;
}
Also used : PluginConfigBean(net.heartsome.cat.ts.ui.plugin.bean.PluginConfigBean) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) IFile(org.eclipse.core.resources.IFile) File(java.io.File) VTDNav(com.ximpleware.VTDNav) LinkedList(java.util.LinkedList)

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