Search in sources :

Example 1 with VTDNav

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

the class ConverterViewModel method convert.

/**
	 * 正向转换
	 * @param sourceItem
	 * @param monitor
	 * @return ;
	 */
private Map<String, String> convert(final IConversionItem sourceItem, IProgressMonitor monitor) throws ConverterException {
    Map<String, String> result = null;
    boolean convertFlg = false;
    String xliffDir = ConverterUtil.toLocalPath(configBean.getXliffDir());
    String targetFile = ConverterUtil.toLocalPath(configBean.getTarget());
    String skeletonFile = ConverterUtil.toLocalPath(configBean.getSkeleton());
    // 转换前的准备
    ConverterContext converterContext = new ConverterContext(configBean);
    final Map<String, String> configuration = converterContext.getConvertConfiguration();
    // 转换前,生成临时的XLIFF文件,用此文件生成指定目标语言的XLIFF文件
    File targetTempFile = null;
    try {
        targetTempFile = File.createTempFile("tempxlf", "xlf");
    } catch (IOException e) {
        LOGGER.error(Messages.getString("model.ConverterViewModel.msg10"), e);
    }
    configuration.put(Converter.ATTR_XLIFF_FILE, targetTempFile.getAbsolutePath());
    if (configBean.getFileType().equals(FileFormatUtils.MS)) {
        IPreferenceStore ps = net.heartsome.cat.ts.ui.Activator.getDefault().getPreferenceStore();
        String path = ps.getString(ITranslationPreferenceConstants.PATH_OF_OPENOFFICE);
        String port = ps.getString(ITranslationPreferenceConstants.PORT_OF_OPENOFFICE);
        configuration.put("ooPath", path);
        configuration.put("ooPort", port);
    }
    // 创建skeleton文件
    File skeleton = new File(skeletonFile);
    if (!skeleton.exists()) {
        try {
            File parent = skeleton.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            skeleton.createNewFile();
        } catch (IOException e) {
            String message = MessageFormat.format(Messages.getString("model.ConverterViewModel.msg11"), skeletonFile);
            LOGGER.error(message, e);
            IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
            throw new ConverterException(status);
        }
    }
    try {
        // 执行转换
        Converter converter = getConverter();
        if (converter == null) {
            // Build a message
            String message = Messages.getString("model.ConverterViewModel.msg2") + configBean.getFileType();
            // Build a new IStatus
            IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
            throw new ConverterException(status);
        }
        result = converter.convert(configuration, monitor);
        final String alert = result.get("ttx2xlfAlert39238409230481092830");
        if (result.containsKey("ttx2xlfAlert39238409230481092830")) {
            //ttx 转 xlf 时,提示含有未预翻译,不推荐,但没办法。
            Display.getDefault().syncExec(new Runnable() {

                public void run() {
                    MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.getString("handler.PreviewTranslationHandler.msgTitle"), alert);
                }
            });
        }
        // 处理骨架文件,将骨架文件路径修改为项目相对路径,此路径写入external-file节点的href属性
        String projectPath = sourceItem.getProject().getLocation().toOSString();
        String sklPath = skeletonFile.replace(projectPath, "");
        // 处理目标语言, 创建多个目标语言的文件
        List<Language> tgtLang = configBean.getHasSelTgtLangList();
        if (tgtLang != null && tgtLang.size() > 0) {
            // 解析XLIFF文件
            File f = new File(targetTempFile.getAbsolutePath());
            FileInputStream is = null;
            byte[] b = new byte[(int) f.length()];
            try {
                is = new FileInputStream(f);
                is.read(b);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            VTDGen vg = new VTDGen();
            vg.setDoc(b);
            try {
                vg.parse(true);
            } catch (VTDException e) {
                String message = Messages.getString("model.ConverterViewModel.msg12");
                LOGGER.error(message, e);
                IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
                throw new ConverterException(status);
            }
            VTDNav vn = vg.getNav();
            VTDUtils vu = new VTDUtils();
            // 生成多个XLIFF文件,只是修改目标语言和骨架文件路径
            for (Language lang : tgtLang) {
                // 修复 bug 2949 ,当文件名中出现 XLIFF 时,文件名获取失败,下面注释代码为之前的代码。	--robert	2013-04-01
                //					String[] pathArray = targetFile.split(Constant.FOLDER_XLIFF);
                //					StringBuffer xlffPath = new StringBuffer(pathArray[0]);
                //					xlffPath.append(Constant.FOLDER_XLIFF).append(File.separator).append(lang.getCode())
                //							.append(pathArray[1]);
                String fileName = targetFile.substring(xliffDir.length());
                StringBuffer xlfPahtSB = new StringBuffer();
                xlfPahtSB.append(xliffDir);
                xlfPahtSB.append(File.separator);
                xlfPahtSB.append(lang.getCode());
                xlfPahtSB.append(fileName);
                File tmpFile = new File(xlfPahtSB.toString());
                generateTgtFileList.add(tmpFile);
                if (!tmpFile.exists()) {
                    File parent = tmpFile.getParentFile();
                    if (!parent.exists()) {
                        parent.mkdirs();
                    }
                    try {
                        tmpFile.createNewFile();
                    } catch (IOException e) {
                        String message = Messages.getString("model.ConverterViewModel.msg13");
                        LOGGER.error(message, e);
                        IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
                        throw new ConverterException(status);
                    }
                }
                try {
                    vu.bind(vn.duplicateNav());
                } catch (NavException e) {
                    LOGGER.error("", e);
                }
                XMLModifier xm = vu.update("/xliff/file/@target-language", lang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
                xm = vu.update(null, xm, "/xliff/file/header/skl/external-file/@href", TextUtil.cleanString(sklPath));
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(tmpFile);
                    // 写入文件
                    xm.output(fos);
                } catch (Exception e) {
                    String message = Messages.getString("model.ConverterViewModel.msg13");
                    LOGGER.error(message, e);
                    IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
                    throw new ConverterNotFoundException(status);
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            LOGGER.error("", e);
                        }
                    }
                }
            }
            vg.clear();
        }
        convertFlg = true;
    } catch (OperationCanceledException e) {
        LOGGER.info("ConverterViewerModel: 取消转换");
    } finally {
        if (!convertFlg) {
            for (File f : generateTgtFileList) {
                if (f != null && f.exists()) {
                    f.delete();
                }
            }
            if (skeleton != null && skeleton.exists()) {
                skeleton.delete();
            }
        }
        targetTempFile.delete();
        sourceItem.refresh();
    }
    return result;
}
Also used : ConverterException(net.heartsome.cat.converter.ConverterException) XMLModifier(com.ximpleware.XMLModifier) IStatus(org.eclipse.core.runtime.IStatus) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Language(net.heartsome.cat.common.locale.Language) VTDException(com.ximpleware.VTDException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) Converter(net.heartsome.cat.converter.Converter) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NavException(com.ximpleware.NavException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) NavException(com.ximpleware.NavException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ConverterException(net.heartsome.cat.converter.ConverterException) IOException(java.io.IOException) VTDException(com.ximpleware.VTDException) JobRunnable(net.heartsome.cat.convert.ui.job.JobRunnable) FileOutputStream(java.io.FileOutputStream) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 2 with VTDNav

use of com.ximpleware.VTDNav in project opennms by OpenNMS.

the class DefaultVTDXmlCollectionHandler method fillCollectionSet.

@Override
protected void fillCollectionSet(String urlString, Request request, CollectionAgent agent, CollectionSetBuilder builder, XmlSource source) throws Exception {
    VTDNav vn = getVTDXmlDocument(urlString, request);
    LOG.debug("collect: parsed document for source url '{}' collection", source.getUrl());
    fillCollectionSet(agent, builder, source, vn);
}
Also used : VTDNav(com.ximpleware.VTDNav)

Example 3 with VTDNav

use of com.ximpleware.VTDNav in project opennms by OpenNMS.

the class Sftp3gppVTDXmlCollectionHandler method collect.

@Override
public CollectionSet collect(CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
    String status = "finished";
    // Create a new collection set.
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    // TODO We could be careful when handling exceptions because parsing exceptions will be treated different from connection or retrieval exceptions
    DateTime startTime = new DateTime();
    Sftp3gppUrlConnection connection = null;
    try {
        // FIXME: Does not support storeByFS
        ResourcePath resourcePath = ResourcePath.get(Integer.toString(agent.getNodeId()));
        for (XmlSource source : collection.getXmlSources()) {
            if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
                throw new CollectionException("The 3GPP SFTP Collection Handler can only use the protocol " + Sftp3gppUrlHandler.PROTOCOL);
            }
            final String urlStr = source.getUrl();
            final Request request = source.getRequest();
            URL url = UrlFactory.getUrl(source.getUrl(), source.getRequest());
            String lastFile = Sftp3gppUtils.getLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath());
            connection = (Sftp3gppUrlConnection) url.openConnection();
            if (lastFile == null) {
                lastFile = connection.get3gppFileName();
                LOG.debug("collect(single): retrieving file from {}{}{} from {}", url.getPath(), File.separatorChar, lastFile, agent.getHostAddress());
                VTDNav doc = getVTDXmlDocument(urlStr, request);
                fillCollectionSet(agent, builder, source, doc);
                Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), lastFile);
                Sftp3gppUtils.deleteFile(connection, lastFile);
            } else {
                connection.connect();
                List<String> files = connection.getFileList();
                long lastTs = connection.getTimeStampFromFile(lastFile);
                boolean collected = false;
                for (String fileName : files) {
                    if (connection.getTimeStampFromFile(fileName) > lastTs) {
                        LOG.debug("collect(multiple): retrieving file {} from {}", fileName, agent.getHostAddress());
                        InputStream is = connection.getFile(fileName);
                        try {
                            VTDNav doc = getVTDXmlDocument(is, request);
                            fillCollectionSet(agent, builder, source, doc);
                        } finally {
                            IOUtils.closeQuietly(is);
                        }
                        Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), fileName);
                        Sftp3gppUtils.deleteFile(connection, fileName);
                        collected = true;
                    }
                }
                if (!collected) {
                    LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
                }
            }
        }
        return builder.build();
    } catch (Exception e) {
        status = "failed";
        throw new CollectionException(e.getMessage(), e);
    } finally {
        DateTime endTime = new DateTime();
        LOG.debug("collect: {} collection {}: duration: {} ms", status, collection.getName(), endTime.getMillis() - startTime.getMillis());
        UrlFactory.disconnect(connection);
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) CollectionException(org.opennms.netmgt.collection.api.CollectionException) InputStream(java.io.InputStream) Request(org.opennms.protocols.xml.config.Request) DateTime(org.joda.time.DateTime) XmlSource(org.opennms.protocols.xml.config.XmlSource) URL(java.net.URL) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ResourcePath(org.opennms.netmgt.model.ResourcePath) Sftp3gppUrlConnection(org.opennms.protocols.sftp.Sftp3gppUrlConnection) VTDNav(com.ximpleware.VTDNav)

Example 4 with VTDNav

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

the class XliffReader method ananysisTag.

private void ananysisTag(Map<Integer, String[]> targetMap) throws VTDException {
    VTDNav vn = vu.getVTDNav();
    vn.push();
    int idex = vn.getCurrentIndex();
    String tagName = vn.toString(idex);
    if ("g".equals(tagName)) {
        String ctype = vu.getCurrentElementAttribut("ctype", null);
        String rpr = vu.getCurrentElementAttribut("rPr", "");
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("./node() | text()");
        while (ap.evalXPath() != -1) {
            idex = vn.getCurrentIndex();
            int tokenType = vn.getTokenType(idex);
            if (tokenType == 0) {
                String name = vu.getCurrentElementName();
                if ("ph".equals(name)) {
                    targetMap.put(idex, new String[] { vu.getElementContent(), rpr, ctype });
                } else if ("g".equals(name)) {
                    ananysisTag(targetMap);
                }
            } else if (tokenType == 5) {
                targetMap.put(idex, new String[] { vn.toRawString(idex), rpr, ctype });
            }
        }
    } else if ("ph".equals(tagName)) {
        targetMap.put(idex, new String[] { vu.getElementContent(), null, null });
    } else {
        // 其他节点,一律当做字符串处理
        targetMap.put(idex, new String[] { vu.getElementFragment(), null, null });
    }
    vn.pop();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

Example 5 with VTDNav

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

the class DocUtils method getTmxTbxPureText.

public static String getTmxTbxPureText(VTDUtils vu) throws NavException, XPathParseException, XPathEvalException {
    StringBuilder sb = new StringBuilder();
    VTDNav vn = vu.getVTDNav();
    try {
        vn.push();
        sb.append(vu.getElementContent());
        AutoPilot ap = new AutoPilot(vn);
        // 有子节点,即有内部标记
        if (vu.getChildElementsCount() < 1) {
            return sb.toString();
        }
        ap.resetXPath();
        ap.selectXPath("./*");
        while (ap.evalXPath() != -1) {
            String childNodeName = vu.getCurrentElementName();
            if ("g".equals(childNodeName) || "sub".equals(childNodeName) || "hi".equals(childNodeName) || "mrk".equals(childNodeName) || "foreign".equals(childNodeName)) {
                if (vu.getChildElementsCount() < 1) {
                    String childFrag = vu.getElementFragment();
                    String childContent = vu.getElementContent();
                    childContent = childContent == null ? "" : childContent;
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), childContent);
                } else {
                    String childFrag = vu.getElementFragment();
                    String childContent = getTmxTbxPureText(vu);
                    childContent = childContent == null ? "" : childContent;
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), childContent);
                }
            } else {
                // ph节点的值为code data或者一个sub节点,因此,要考虑到sub节点的情况
                if (vu.getChildElementsCount() < 1) {
                    String childFrag = vu.getElementFragment();
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), "");
                } else {
                    String childFrag = vu.getElementFragment();
                    String childContent = "";
                    AutoPilot _ap = new AutoPilot(vn);
                    _ap.selectXPath("./*");
                    while (_ap.evalXPath() != -1) {
                        if (vu.getChildElementsCount() <= 0) {
                            childContent += vu.getElementContent();
                        } else {
                            childContent += getTmxTbxPureText(vu);
                        }
                    }
                    childContent = childContent == null ? "" : childContent;
                    int start = sb.indexOf(childFrag);
                    sb.replace(start, start + childFrag.length(), childContent);
                }
            }
        }
    } finally {
        vn.pop();
    }
    return sb.toString();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

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