Search in sources :

Example 11 with ParseException

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

the class XLPHandler method saveSourceInfo.

/**
	 * 保存 XLIFF 信息
	 * @param sourceInfo
	 *            源文件信息 ;
	 */
public void saveSourceInfo(List<? extends Map<String, String>> sourceInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" {6}=\"{7}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : sourceInfo) {
                vn.push();
                String source = map.get(ATTR_SOURCE);
                String format = map.get(ATTR_FORMAT);
                String srcLang = map.get(ATTR_SRC_LANG);
                String srcEnc = map.get(ATTR_SRC_ENC);
                String xpath = "./item[@" + ATTR_SOURCE + "=\"" + source + "\"]";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_FORMAT, format);
                    updateAttribute(xm, ATTR_SRC_LANG, srcLang);
                    updateAttribute(xm, ATTR_SRC_ENC, srcEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_SOURCE, source, ATTR_FORMAT, format, ATTR_SRC_LANG, srcLang, ATTR_SRC_ENC, srcEnc);
                    ap.selectXPath(xpath);
                    items.append(item).append(LINE_SEPARATOR);
                }
                vn.pop();
            }
            if (items.length() > 1) {
                xm.insertBeforeTail(items.toString());
            }
            save(xm);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException)

Example 12 with ParseException

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

the class LanguageConfiger method parseFile.

private void parseFile() {
    VTDGen vg = new VTDGen();
    try {
        if (vg.parseFile(langConfigFile.getPath(), true)) {
            vu = new VTDUtils(vg.getNav());
            ap = new AutoPilot(vu.getVTDNav());
        } else {
            throw new ParseException();
        }
    } catch (NavException e) {
        LOGGER.error("", e);
    } catch (ParseException e) {
        LOGGER.error("", e);
    }
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) VTDGen(com.ximpleware.VTDGen) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException)

Example 13 with ParseException

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

the class VTDLoader method loadVTDGen.

/**
	 * 使用 VTD 解析 XML 文件,返回 VTDGen 对象<br>
	 * 解析 XML 时忽略掉了 XML 标准中的非法字符
	 * @param file
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 * @throws Exception
	 *             ;
	 */
public static VTDGen loadVTDGen(File f, String fileEncoding) throws ParseException, IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), fileEncoding));
    try {
        char[] cbuf = new char[8192];
        int count = reader.read(cbuf);
        if (count < 20) {
            // Error Empty File
            throw new ParseException("Other Error: XML not starting properly");
        }
        StringBuilder _sb = new StringBuilder();
        // 取前500个字符进行判断
        _sb.append(cbuf, 0, count < 500 ? count : 500);
        int stIdx = _sb.indexOf("<?xml");
        if (stIdx != -1) {
            // is xml
            int endIdx = _sb.indexOf("?>");
            if (endIdx != -1 && _sb.indexOf("encoding") != -1) {
                endIdx += 2;
                String s = "<?xml version=\"1.0\" ?>";
                char[] temp = new char[cbuf.length - endIdx + s.length()];
                char[] schar = s.toCharArray();
                System.arraycopy(schar, 0, temp, 0, schar.length);
                System.arraycopy(cbuf, endIdx, temp, schar.length, cbuf.length - endIdx);
                cbuf = temp;
                count = count - endIdx + s.length();
            }
        } else {
            throw new ParseException("Other Error: XML not starting properly");
        }
        byte[] bArr = new byte[(int) f.length()];
        int i = 0;
        int idx = 0;
        while (count > 0) {
            int tempIdx = 0;
            char[] tempChar = new char[count];
            for (; i < count; i++) {
                char c = cbuf[i];
                if (!XMLChar.isValidChar(c)) {
                    continue;
                } else if (c == '&') {
                    StringBuilder sb = new StringBuilder();
                    int val = 0;
                    int j = i + 1;
                    if (j >= count) {
                        c = (char) reader.read();
                    } else {
                        c = cbuf[j++];
                    }
                    sb.append(c);
                    if (c == '#') {
                        if (j >= count) {
                            c = (char) reader.read();
                        } else {
                            c = cbuf[j++];
                        }
                        sb.append(c);
                        if (c == 'x') {
                            while (true) {
                                if (j >= count) {
                                    c = (char) reader.read();
                                } else {
                                    c = cbuf[j++];
                                }
                                sb.append(c);
                                if (c >= '0' && c <= '9') {
                                    val = (val << 4) + (c - '0');
                                } else if (c >= 'a' && c <= 'f') {
                                    val = (val << 4) + (c - 'a' + 10);
                                } else if (c >= 'A' && c <= 'F') {
                                    val = (val << 4) + (c - 'A' + 10);
                                } else if (c == ';') {
                                    break;
                                } else {
                                    break;
                                }
                            }
                        } else {
                            while (true) {
                                if (c >= '0' && c <= '9') {
                                    val = val * 10 + (c - '0');
                                } else if (c == ';') {
                                    break;
                                } else {
                                    break;
                                }
                                if (j >= count) {
                                    c = (char) reader.read();
                                } else {
                                    c = cbuf[j++];
                                }
                                sb.append(c);
                            }
                        }
                        if (!XMLChar.isValidChar(val)) {
                            if (j <= count) {
                                i = j;
                            }
                            System.out.println((char) val + " " + val + " " + sb);
                            continue;
                        } else {
                            c = cbuf[i];
                            tempChar[tempIdx++] = c;
                            for (int t = 0; t < sb.length(); t++) {
                                if (tempIdx >= tempChar.length) {
                                    tempChar = Arrays.copyOf(tempChar, tempChar.length + 1);
                                    count = tempChar.length;
                                }
                                tempChar[tempIdx++] = sb.charAt(t);
                                i++;
                            }
                        }
                    } else {
                        c = cbuf[i];
                        tempChar[tempIdx++] = c;
                        for (int t = 0; t < sb.length(); t++) {
                            if (tempIdx >= tempChar.length) {
                                tempChar = Arrays.copyOf(tempChar, tempChar.length + 1);
                                count = tempChar.length;
                            }
                            tempChar[tempIdx++] = sb.charAt(t);
                            i++;
                        }
                    }
                } else {
                    tempChar[tempIdx++] = c;
                }
            }
            if (tempChar.length != 0) {
                byte[] temp = getBytes(tempChar, 0, tempIdx);
                System.arraycopy(temp, 0, bArr, idx, temp.length);
                idx += temp.length;
            }
            count = reader.read(cbuf);
            i = 0;
        }
        //			FileOutputStream os = new FileOutputStream("C:\\Users\\Jason\\Desktop\\Trados 2007.tmxsasas");
        //			os.write(bArr, 0, idx);
        //			os.close();
        VTDGen vg = new VTDGen();
        vg.setDoc(Arrays.copyOf(bArr, idx));
        vg.parse(true);
        return vg;
    } finally {
        reader.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) VTDGen(com.ximpleware.VTDGen) ParseException(com.ximpleware.ParseException) FileInputStream(java.io.FileInputStream)

Example 14 with ParseException

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

the class DocUtils method isTMX.

/**
	 * 判断是否是正确的 TMX 文件
	 * @param fileName
	 * @return ;
	 * @throws FileNotFoundException
	 * @throws ParseException
	 * @throws EntityException
	 * @throws EOFException
	 * @throws EncodingException
	 */
public static VTDUtils isTMX(String fileName) throws FileNotFoundException, EncodingException, ParseException {
    VTDGen vg = new VTDGen();
    FileInputStream fis = null;
    File f = null;
    try {
        f = new File(fileName);
        fis = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        int offset = 0;
        int numRead = 0;
        // I choose this value randomally,
        int numOfBytes = 1048576;
        // any other (not too big) value also can be here.
        if (b.length - offset < numOfBytes) {
            numOfBytes = b.length - offset;
        }
        while (offset < b.length && (numRead = fis.read(b, offset, numOfBytes)) >= 0) {
            offset += numRead;
            if (b.length - offset < numOfBytes) {
                numOfBytes = b.length - offset;
            }
        }
        vg.setDoc(b);
        vg.parse(true);
    } catch (IOException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger1"), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    String rootPath = "/tmx";
    VTDUtils vu = new VTDUtils();
    try {
        vu.bind(vn);
        ap.selectXPath(rootPath);
        if (ap.evalXPath() == -1) {
            return null;
        }
    } catch (NavException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathEvalException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathParseException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } finally {
        vg.clear();
    }
    return vu;
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) NavException(com.ximpleware.NavException) IOException(java.io.IOException) EncodingException(com.ximpleware.EncodingException) FileNotFoundException(java.io.FileNotFoundException) ParseException(com.ximpleware.ParseException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 15 with ParseException

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

the class ProjectConfiger method save.

/**
	 * 保存文件
	 * @param xm
	 *            XMLModifier对象
	 * @param fileName
	 *            文件名
	 * @return 是否保存成功;
	 */
private boolean save(XMLModifier xm, File file) {
    try {
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 写入文件
        xm.output(bos);
        bos.close();
        fos.close();
        VTDGen vg = new VTDGen();
        try {
            if (vg.parseFile(projCfgFile.getPath(), true)) {
                vu.bind(vg.getNav());
            } else {
                throw new ParseException();
            }
        } catch (NavException e) {
            logger.error("", e);
        } catch (ParseException e) {
            logger.error("", e);
        }
        return true;
    } catch (ModifyException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        logger.error("", e);
        e.printStackTrace();
    }
    return false;
}
Also used : FileOutputStream(java.io.FileOutputStream) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) VTDGen(com.ximpleware.VTDGen) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) TranscodeException(com.ximpleware.TranscodeException)

Aggregations

ParseException (com.ximpleware.ParseException)15 XPathParseException (com.ximpleware.XPathParseException)14 NavException (com.ximpleware.NavException)13 IOException (java.io.IOException)13 XPathEvalException (com.ximpleware.XPathEvalException)11 VTDGen (com.ximpleware.VTDGen)10 AutoPilot (com.ximpleware.AutoPilot)9 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)8 EncodingException (com.ximpleware.EncodingException)7 ModifyException (com.ximpleware.ModifyException)7 TranscodeException (com.ximpleware.TranscodeException)7 EOFException (com.ximpleware.EOFException)6 EntityException (com.ximpleware.EntityException)6 VTDNav (com.ximpleware.VTDNav)5 XMLModifier (com.ximpleware.XMLModifier)5 FileInputStream (java.io.FileInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 MessageFormat (java.text.MessageFormat)3 VTDException (com.ximpleware.VTDException)2 File (java.io.File)2