com.ximpleware.ModifyException Java Examples

The following examples show how to use com.ximpleware.ModifyException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VTDUtils.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 插入新属性(到最后的位置)。
 * @param xm
 * @param attrName
 * @param attrValue
 * @throws ModifyException
 *             ;
 */
private void insertAttribute(XMLModifier xm, String attrName, String attrValue) throws ModifyException {
	int startTagIndex = vn.getCurrentIndex();
	int type = vn.getTokenType(startTagIndex);
	if (type != VTDNav.TOKEN_STARTING_TAG)
		throw new ModifyException("Token type is not a starting tag");

	String attrFragment = new StringBuffer(" ").append(attrName).append("=\"").append(attrValue).append("\"")
			.toString(); // 构建属性片段,“ attrName="attrValue" ”
	long i = vn.getOffsetAfterHead(); // 得到开始标记的结束位置
	if (vn.getEncoding() < VTDNav.FORMAT_UTF_16BE) {
		xm.insertBytesAt((int) i - 1, attrFragment.getBytes());
	} else {
		xm.insertBytesAt(((int) i - 1) << 1, attrFragment.getBytes());
	}
}
 
Example #2
Source File: VTDUtils.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 插入新属性(到最后的位置)。
 * @param xm
 * @param attrName
 * @param attrValue
 * @throws ModifyException
 *             ;
 */
private void insertAttribute(XMLModifier xm, String attrName, String attrValue) throws ModifyException {
	int startTagIndex = vn.getCurrentIndex();
	int type = vn.getTokenType(startTagIndex);
	if (type != VTDNav.TOKEN_STARTING_TAG)
		throw new ModifyException("Token type is not a starting tag");

	String attrFragment = new StringBuffer(" ").append(attrName).append("=\"").append(attrValue).append("\"")
			.toString(); // 构建属性片段,“ attrName="attrValue" ”
	long i = vn.getOffsetAfterHead(); // 得到开始标记的结束位置
	if (vn.getEncoding() < VTDNav.FORMAT_UTF_16BE) {
		xm.insertBytesAt((int) i - 1, attrFragment.getBytes());
	} else {
		xm.insertBytesAt(((int) i - 1) << 1, attrFragment.getBytes());
	}
}
 
Example #3
Source File: TmxLargeFileDataAccess.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public void updateTuAttribute(String tuIdentifier, TmxTU tu, String name, String newValue) {
	if (tuIdentifier == null || tuIdentifier.length() == 0 || newValue == null || newValue.length() == 0
			|| name == null || name.length() == 0) {
		return;
	}
	String[] strs = TeCoreUtils.parseTuIndentifier(tuIdentifier);
	if (strs == null) {
		return;
	}
	String subFile = strs[0];
	String id = strs[1];
	newValue = TextUtil.cleanSpecialString(newValue);
	String xpath = "/tmx/body/tu[@hsid='" + id + "']";
	VTDUtils vu = container.getVTDUtils(subFile);
	XMLModifier xm = new XMLModifier();
	try {
		xm.bind(vu.getVTDNav());
	} catch (ModifyException e) {
		LOGGER.error("", e);
	}
	if (updateAttrByXpath(xm, xpath, vu, name, newValue) != null) {
		save2SubFile(subFile, vu, xm);// update file and VTDNvn
		updateCacheTuAttr(tu, name, newValue);
		setDirty(true);
	}
}
 
Example #4
Source File: AbstractVersionModifyingMojo.java    From revapi with Apache License 2.0 6 votes vote down vote up
void updateProjectParentVersion(MavenProject project, Version version) throws MojoExecutionException {
    try {
        VTDGen gen = new VTDGen();
        gen.enableIgnoredWhiteSpace(true);
        gen.parseFile(project.getFile().getAbsolutePath(), true);

        VTDNav nav = gen.getNav();
        AutoPilot ap = new AutoPilot(nav);
        ap.selectXPath("namespace-uri(.)");
        String ns = ap.evalXPathToString();

        nav.toElementNS(VTDNav.FIRST_CHILD, ns, "parent");
        nav.toElementNS(VTDNav.FIRST_CHILD, ns, "version");
        int pos = nav.getText();

        XMLModifier mod = new XMLModifier(nav);
        mod.updateToken(pos, version.toString());

        try (OutputStream out = new FileOutputStream(project.getFile())) {
            mod.output(out);
        }
    } catch (IOException | ModifyException | NavException | XPathParseException | TranscodeException e) {
        throw new MojoExecutionException("Failed to update the parent version of project " + project, e);
    }
}
 
Example #5
Source File: PreMachineTranslation.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 将机器翻译结果存入XLIFF
 * @param vu
 * @param tuInfo
 * @param xm
 * @throws XPathParseException
 * @throws XPathEvalException
 * @throws NavException
 * @throws ModifyException
 * @throws UnsupportedEncodingException
 *             ;
 */
private void updateXliffFile(VTDUtils vu, TransUnitInfo2TranslationBean tuInfo, XMLModifier xm)
		throws XPathParseException, XPathEvalException, NavException, ModifyException, UnsupportedEncodingException {

	String targetContent = "";
	String simpleMatchContent = executeSimpleMatch(vu, tuInfo, xm);
	targetContent += simpleMatchContent;
	if (targetContent.length() > 0) {
		currentCounter.countTransTu();
		xm.insertBeforeTail(targetContent);
	}

}
 
Example #6
Source File: XLPHandler.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 保存文件
 * @param xm
 * @throws IOException
 * @throws ParseException
 * @throws TranscodeException
 * @throws ModifyException
 *             ;
 */
private void save(XMLModifier xm) throws IOException, ParseException, TranscodeException, ModifyException {
	vn = xm.outputAndReparse();
	IByteBuffer buffer = vn.getXML();

	// 写隐藏文件,在Windows 平台下不能使用 FileOutputStream。会抛出“拒绝访问”的异常。因此采用 RandomAccessFile
	RandomAccessFile raf = new RandomAccessFile(xlpPath, "rw");
	raf.setLength(0); // 清除文件内容
	raf.seek(0); // 设置文件指针到文件起始位置
	raf.write(buffer.getBytes());
	raf.close();
}
 
Example #7
Source File: ImportRTFToXLIFF.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check segments.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws SAXException
 *             the SAX exception
 * @throws XPathEvalException
 * @throws XPathParseException
 * @throws NavException
 * @throws TranscodeException
 * @throws ModifyException
 */
private void checkSegments(IProgressMonitor monitor) throws IOException, SAXException, NavException,
		XPathParseException, XPathEvalException, ModifyException, TranscodeException {
	monitor.beginTask(Messages.getString(Messages.IMPORTRTFTOXLIFF_JOBTITLE_10), segments.size());
	for (int i = 0; i < segments.size(); i++) {
		// 是否取消操作
		if (monitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		String segment = segments.get(i);
		String trimmed = segment.trim();
		if (inExternal && trimmed.startsWith("\\pard")) { //$NON-NLS-1$
			inExternal = false;
		}
		processSegment(segment);
		monitor.worked(1);
	}
	if (xliffEditor != null) {
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				xliffEditor.autoResize();
				xliffEditor.refresh();
			}
		});
	}
	monitor.done();
}
 
Example #8
Source File: PreTranslation.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private void overwriteMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");
	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		String locked = vu.getCurrentElementAttribut("translate", "yes");
		if (locked.equals("no")) {
			continue;
		}

		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		// System.out.println(tuInfo.getSrcFullText());
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);
		getTuContext(vu, contextSize, tuInfo);
		// Vector<Hashtable<String, String>> result = tmMatcher.executeSearch(currentProject, tuInfo);
		List<FuzzySearchResult> result = tmMatcher.executeFuzzySearch(currentProject, tuInfo);

		updateXliffFile(vu, tuInfo, result, xm, true);
		monitor.worked(1);
	}

}
 
Example #9
Source File: PreTranslation.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private void lockTransUnit(VTDNav vn, XMLModifier xm) throws NavException, ModifyException,
		UnsupportedEncodingException, XPathParseException, XPathEvalException {
	vn.push();
	int attrIdx = vn.getAttrVal("translate");
	if (attrIdx != -1) { // 存在translate属性
		String translate = vn.toString(attrIdx);
		if (!translate.equals("no")) { // translate属性值不为指定的translateValue
			xm.updateToken(attrIdx, "no");
		}
	} else {
		xm.insertAttribute(" translate=\"no\" ");
	}
	vn.pop();
}
 
Example #10
Source File: PreTranslation.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private void updateXliffFile(VTDUtils vu, TransUnitInfo2TranslationBean tuInfo,
		List<FuzzySearchResult> fuzzyResult, XMLModifier xm, boolean updateTarget) throws XPathParseException,
		XPathEvalException, NavException, ModifyException, UnsupportedEncodingException {
	String altTransContent = "";
	String targetContent = "";
	vu.delete(new AutoPilot(vu.getVTDNav()), xm, "./alt-trans[@tool-id='" + Constants.TM_TOOLID + "']",
			VTDUtils.PILOT_TO_END);
	if (!fuzzyResult.isEmpty()) {
		altTransContent = generateAltTransUnitNodeXML(fuzzyResult);
	}

	if (updateTarget && !fuzzyResult.isEmpty()) {
		vu.delete(new AutoPilot(vu.getVTDNav()), xm, "./target", VTDUtils.PILOT_TO_END);
		targetContent = generateTargetNodeXML(fuzzyResult.get(0));
		currentCounter.countTransTu();
	}

	targetContent += altTransContent;

	String simpleMatchContent = executeSimpleMatch(vu, tuInfo, xm/* , updateTargetTemp */);
	targetContent += simpleMatchContent;

	if (targetContent.length() > 0) {
		xm.insertBeforeTail(targetContent);
	}

	if (fuzzyResult.size() > 0) {
		String similarity = fuzzyResult.get(0).getSimilarity() + ""; // 取最大匹配率的
		if (parameters.isLockContextMatch() && similarity.equals("101")) {
			lockTransUnit(vu.getVTDNav(), xm);
			currentCounter.countLockedContextmatch();
		} else if (parameters.isLockFullMatch() && similarity.equals("100")) {
			lockTransUnit(vu.getVTDNav(), xm);
			currentCounter.countLockedFullMatch();
		}
	}
}
 
Example #11
Source File: PreTranslation.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private void keepCurrentMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");
	boolean needUpdateTgt = true;
	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		// skip locked segment
		String locked = vu.getCurrentElementAttribut("translate", "yes");
		if (locked.equals("no")) {
			continue;
		}

		String tgtContent = vu.getElementContent("./target");
		if (tgtContent != null && !tgtContent.trim().equals("")) {
			needUpdateTgt = false;
		}
		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);
		getTuContext(vu, contextSize, tuInfo);
		List<FuzzySearchResult> result = tmMatcher.executeFuzzySearch(currentProject, tuInfo);
		updateXliffFile(vu, tuInfo, result, xm, needUpdateTgt);
		needUpdateTgt = true;
		monitor.worked(1);
	}
}
 
Example #12
Source File: TmxLargeFileDataAccess.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public void updateTuProp(String tuIdentifier, TmxTU tu, TmxProp prop, String propType, String newContent) {
	if (newContent == null || newContent.length() == 0 || propType == null || propType.length() == 0 || tu == null
			|| prop == null) {
		return;
	}
	
	propType = TextUtil.cleanSpecialString(propType);
	newContent = TextUtil.cleanSpecialString(newContent);
	
	String[] strs = TeCoreUtils.parseTuIndentifier(tuIdentifier);
	if (strs == null) {
		return;
	}
	String subFile = strs[0];
	String id = strs[1];
	List<TmxProp> props = tu.getProps();
	if (props == null || !props.contains(prop)) {
		return;
	}
	int propid = prop.getDbPk();
	String xpath = "/tmx/body/tu[@hsid='" + id + "']/prop[" + propid + "]";
	String nodeFragment = "<prop type=\"" + propType + "\">" + newContent + "</prop>";
	VTDUtils vu = container.getVTDUtils(subFile);
	if (vu == null) {
		return;
	}
	XMLModifier xm = new XMLModifier();
	try {
		xm.bind(vu.getVTDNav());
	} catch (ModifyException e) {
		LOGGER.error("", e);
	}
	if (updateNode(xm, vu, xpath, nodeFragment)) {
		save2SubFile(subFile, vu, xm); // update file and VTDNav
		// update TmxProp
		prop.setName(propType);
		prop.setValue(newContent);
		setDirty(true);
	}
}
 
Example #13
Source File: TmxLargeFileDataAccess.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public void updateTuvAttribute(String tuIdentifier, TmxSegement tuv, String name, String newValue) {
	if (tuIdentifier == null || tuIdentifier.length() == 0 || newValue == null || newValue.length() == 0
			|| name == null || name.length() == 0) {
		return;
	}
	String[] strs = TeCoreUtils.parseTuIndentifier(tuIdentifier);
	if (strs == null) {
		return;
	}
	String subFile = strs[0];
	String id = strs[1];
	int tuvId = tuv.getDbPk();
	newValue = TextUtil.cleanSpecialString(newValue);
	String xpath = "/tmx/body/tu[@hsid='" + id + "']/tuv[" + tuvId + "]";
	VTDUtils vu = container.getVTDUtils(subFile);
	XMLModifier xm = new XMLModifier();
	try {
		xm.bind(vu.getVTDNav());
	} catch (ModifyException e) {
		LOGGER.error("", e);
	}
	if (updateAttrByXpath(xm, xpath, vu, name, newValue) != null) {
		save2SubFile(subFile, vu, xm);
		updateCacheTuvAttr(tuv, name, newValue);
		setDirty(true);
	}
}
 
Example #14
Source File: TmxLargeFileDataAccess.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public void addTuvAttribute(String tuIdentifier, TmxSegement tuv, String name, String value) {
	if (tuIdentifier == null || tuIdentifier.length() == 0 || value == null || value.length() == 0 || name == null
			|| name.length() == 0 || tuv == null) {
		return;
	}
	String[] strs = TeCoreUtils.parseTuIndentifier(tuIdentifier);
	if (strs == null) {
		return;
	}
	String subFile = strs[0];
	String id = strs[1];
	int tuvId = tuv.getDbPk();

	VTDUtils vu = container.getVTDUtils(subFile);
	if (vu == null) {
		return;
	}

	Map<String, String> attrs = tuv.getAttributes();
	if (attrs != null && attrs.containsKey(name)) {
		return;
	}

	value = TextUtil.cleanSpecialString(value);
	String attributeStr = " " + name + "=\"" + value + "\"";
	String xpath = "/tmx/body/tu[@hsid='" + id + "']/tuv[" + tuvId + "]";
	XMLModifier xm = new XMLModifier();
	try {
		xm.bind(vu.getVTDNav());
	} catch (ModifyException e) {
		LOGGER.error("", e);
	}
	addNodeAttribute(xm, vu, xpath, attributeStr);

	save2SubFile(subFile, vu, xm); // update file and VTDNav
	setDirty(true);
	tuv.appendAttribute(name, value); // update tuv
}
 
Example #15
Source File: TmxLargeFileDataAccess.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public void addTuAttribute(String tuIdentifier, TmxTU tu, String name, String value) {
	if (tuIdentifier == null || tuIdentifier.length() == 0 || value == null || value.length() == 0 || name == null
			|| name.length() == 0) {
		return;
	}
	Map<String, String> attrs = tu.getAttributes();
	if (attrs != null && attrs.containsKey(name)) {
		return;
	}

	String[] strs = TeCoreUtils.parseTuIndentifier(tuIdentifier);
	if (strs == null) {
		return;
	}
	String subFile = strs[0];
	String id = strs[1];
	value = TextUtil.cleanSpecialString(value);
	String attrStr = " " + name + "=\"" + value + "\"";
	String xpath = "//tu[@hsid='" + id + "']";
	VTDUtils vu = container.getVTDUtils(subFile);
	XMLModifier xm = new XMLModifier();
	try {
		xm.bind(vu.getVTDNav());
	} catch (ModifyException e) {
		LOGGER.error("", e);
	}
	addNodeAttribute(xm, vu, xpath, attrStr);
	save2SubFile(subFile, vu, xm);// update file and VTDNav
	setDirty(true);
	tu.appendAttribute(name, value);// update TU
}
 
Example #16
Source File: PreTranslation.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void keepCurrentMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");
	boolean needUpdateTgt = true;
	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		// skip locked segment
		String locked = vu.getCurrentElementAttribut("translate", "yes");
		if (locked.equals("no")) {
			continue;
		}

		String tgtContent = vu.getElementContent("./target");
		if (tgtContent != null && !tgtContent.trim().equals("")) {
			needUpdateTgt = false;
		}
		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);
		getTuContext(vu, contextSize, tuInfo);
		List<FuzzySearchResult> result = tmMatcher.executeFuzzySearch(currentProject, tuInfo);
		updateXliffFile(vu, tuInfo, result, xm, needUpdateTgt);
		needUpdateTgt = true;
		monitor.worked(1);
	}
}
 
Example #17
Source File: XLPHandler.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 保存文件
 * @param xm
 * @throws IOException
 * @throws ParseException
 * @throws TranscodeException
 * @throws ModifyException
 *             ;
 */
private void save(XMLModifier xm) throws IOException, ParseException, TranscodeException, ModifyException {
	vn = xm.outputAndReparse();
	IByteBuffer buffer = vn.getXML();

	// 写隐藏文件,在Windows 平台下不能使用 FileOutputStream。会抛出“拒绝访问”的异常。因此采用 RandomAccessFile
	RandomAccessFile raf = new RandomAccessFile(xlpPath, "rw");
	raf.setLength(0); // 清除文件内容
	raf.seek(0); // 设置文件指针到文件起始位置
	raf.write(buffer.getBytes());
	raf.close();
}
 
Example #18
Source File: PreTranslation.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void overwriteMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");
	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		String locked = vu.getCurrentElementAttribut("translate", "yes");
		if (locked.equals("no")) {
			continue;
		}

		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		// System.out.println(tuInfo.getSrcFullText());
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);
		getTuContext(vu, contextSize, tuInfo);
		// Vector<Hashtable<String, String>> result = tmMatcher.executeSearch(currentProject, tuInfo);
		List<FuzzySearchResult> result = tmMatcher.executeFuzzySearch(currentProject, tuInfo);

		updateXliffFile(vu, tuInfo, result, xm, true);
		monitor.worked(1);
	}

}
 
Example #19
Source File: PreTranslation.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void updateXliffFile(VTDUtils vu, TransUnitInfo2TranslationBean tuInfo,
		List<FuzzySearchResult> fuzzyResult, XMLModifier xm, boolean updateTarget) throws XPathParseException,
		XPathEvalException, NavException, ModifyException, UnsupportedEncodingException {
	String altTransContent = "";
	String targetContent = "";
	vu.delete(new AutoPilot(vu.getVTDNav()), xm, "./alt-trans[@tool-id='" + Constants.TM_TOOLID + "']",
			VTDUtils.PILOT_TO_END);
	if (!fuzzyResult.isEmpty()) {
		altTransContent = generateAltTransUnitNodeXML(fuzzyResult);
	}

	if (updateTarget && !fuzzyResult.isEmpty()) {
		vu.delete(new AutoPilot(vu.getVTDNav()), xm, "./target", VTDUtils.PILOT_TO_END);
		targetContent = generateTargetNodeXML(fuzzyResult.get(0), tuInfo);
		currentCounter.countTransTu();
	}

	targetContent += altTransContent;

	String simpleMatchContent = ""; // executeSimpleMatch(vu, tuInfo, xm/* , updateTargetTemp */);
	targetContent += simpleMatchContent;

	if (targetContent.length() > 0) {
		xm.insertBeforeTail(targetContent);
	}

	if (fuzzyResult.size() > 0) {
		String similarity = fuzzyResult.get(0).getSimilarity() + ""; // 取最大匹配率的
		if (parameters.isLockContextMatch() && similarity.equals("101")) {
			lockTransUnit(vu.getVTDNav(), xm);
			currentCounter.countLockedContextmatch();
		} else if (parameters.isLockFullMatch() && similarity.equals("100")) {
			lockTransUnit(vu.getVTDNav(), xm);
			currentCounter.countLockedFullMatch();
		}
	}
}
 
Example #20
Source File: Tmx2Docx.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public StyleXml(String template) throws Exception {
	VTDGen vg = new VTDGen();
	vg.parseZIPFile(template, "word/styles.xml", true);
	vn = vg.getNav();
	ap = new AutoPilot(vn);
	ap.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
	try {
		xm = new XMLModifier(vn);
	} catch (ModifyException e) {
		Exception e1 = new Exception(Messages.getString("converter.tmx2docx.template.notfound"));
		throw e1;
	}
}
 
Example #21
Source File: PreTranslation.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private void lockTransUnit(VTDNav vn, XMLModifier xm) throws NavException, ModifyException,
		UnsupportedEncodingException, XPathParseException, XPathEvalException {
	vn.push();
	int attrIdx = vn.getAttrVal("translate");
	if (attrIdx != -1) { // 存在translate属性
		String translate = vn.toString(attrIdx);
		if (!translate.equals("no")) { // translate属性值不为指定的translateValue
			xm.updateToken(attrIdx, "no");
		}
	} else {
		xm.insertAttribute(" translate=\"no\" ");
	}
	vn.pop();
}
 
Example #22
Source File: PreTranslation.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
private void keepHigherMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");
	boolean needUpdateTarget = false;
	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		String locked = vu.getCurrentElementAttribut("translate", "yes");
		if (locked.equals("no")) {
			continue;
		}

		//  ===从库中查询匹配===
		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);
		getTuContext(vu, contextSize, tuInfo);

		List<FuzzySearchResult> result = tmMatcher.executeFuzzySearch(currentProject, tuInfo);
		// Vector<Hashtable<String, String>> tmMatches = tmMatcher.executeSearch(currentProject, tuInfo);
		//  ====查询结束===

		if (!result.isEmpty()) {
			int matchMaxSimiInt = result.get(0).getSimilarity();
			// ===获取当前目标的匹配率===
			int currMaxSimInt = 0;
			vu.getVTDNav().push();
			AutoPilot targetAp = new AutoPilot(vu.getVTDNav());
			targetAp.selectXPath("./target");
			if (targetAp.evalXPath() != -1) {
				String targetContent = vu.getElementContent();
				if (targetContent != null && !targetContent.equals("")) {
					Hashtable<String, String> attrs = vu.getCurrentElementAttributs();
					if (attrs != null) {
						String type = attrs.get("hs:matchType");
						String quality = attrs.get("hs:quality");
						if (type != null && type.equals("TM") && quality != null && !quality.equals("")) {
							currMaxSimInt = Integer.parseInt(quality);
						} 
					}
				} else { // target内容为空
					needUpdateTarget = true;
				}
			} else { // 无target内容
				needUpdateTarget = true;
			}
			vu.getVTDNav().pop();
			if (currMaxSimInt != 0 && matchMaxSimiInt > currMaxSimInt) {
				needUpdateTarget = true;
			}
		}
		//  ===获取当前最大匹配结束===
		updateXliffFile(vu, tuInfo, result, xm, needUpdateTarget);
		needUpdateTarget = false;
		monitor.worked(1);
	}

}
 
Example #23
Source File: VTDUtils.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 根据 vn 的 encoding 获取字符集编码(此方法与 XMLModifier 的 bind 方法中获取字符集的方式一致)
 * @return
 * @throws ModifyException
 *             ;
 */
public String getCharsetByEncoding() throws ModifyException {
	int encoding = vn.getEncoding();
	switch (encoding) {
	case VTDNav.FORMAT_ASCII:
		return "ASCII";
	case VTDNav.FORMAT_ISO_8859_1:
		return "ISO8859_1";
	case VTDNav.FORMAT_UTF8:
		return "UTF8";
	case VTDNav.FORMAT_UTF_16BE:
		return "UnicodeBigUnmarked";
	case VTDNav.FORMAT_UTF_16LE:
		return "UnicodeLittleUnmarked";
	case VTDNav.FORMAT_ISO_8859_2:
		return "ISO8859_2";
	case VTDNav.FORMAT_ISO_8859_3:
		return "ISO8859_3";
	case VTDNav.FORMAT_ISO_8859_4:
		return "ISO8859_4";
	case VTDNav.FORMAT_ISO_8859_5:
		return "ISO8859_5";
	case VTDNav.FORMAT_ISO_8859_6:
		return "ISO8859_6";
	case VTDNav.FORMAT_ISO_8859_7:
		return "ISO8859_7";
	case VTDNav.FORMAT_ISO_8859_8:
		return "ISO8859_8";
	case VTDNav.FORMAT_ISO_8859_9:
		return "ISO8859_9";
	case VTDNav.FORMAT_ISO_8859_10:
		return "ISO8859_10";
	case VTDNav.FORMAT_ISO_8859_11:
		return "x-iso-8859-11";
	case VTDNav.FORMAT_ISO_8859_12:
		return "ISO8859_12";
	case VTDNav.FORMAT_ISO_8859_13:
		return "ISO8859_13";
	case VTDNav.FORMAT_ISO_8859_14:
		return "ISO8859_14";
	case VTDNav.FORMAT_ISO_8859_15:
		return "ISO8859_15";

	case VTDNav.FORMAT_WIN_1250:
		return "Cp1250";
	case VTDNav.FORMAT_WIN_1251:
		return "Cp1251";
	case VTDNav.FORMAT_WIN_1252:
		return "Cp1252";
	case VTDNav.FORMAT_WIN_1253:
		return "Cp1253";
	case VTDNav.FORMAT_WIN_1254:
		return "Cp1254";
	case VTDNav.FORMAT_WIN_1255:
		return "Cp1255";
	case VTDNav.FORMAT_WIN_1256:
		return "Cp1256";
	case VTDNav.FORMAT_WIN_1257:
		return "Cp1257";
	case VTDNav.FORMAT_WIN_1258:
		return "Cp1258";
	default:
		throw new ModifyException(Messages.getString("vtdimpl.VTDUtils.logger2"));
	}
}
 
Example #24
Source File: PreMachineTranslation.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
private void keepCurrentMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");

	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		
		if (parameters.isIgnoreLock()) {// 1、如果忽略锁定文本
			String locked = vu.getCurrentElementAttribut("translate", "yes");
			if (locked.equals("no")) {
				currentCounter.countLockedContextmatch();
				continue;
			}
		}
		
		
		if(parameters.isIgnoreExactMatch()){// 2、如果忽略匹配率				
			String qualityValue = vu.getElementAttribute("./target", "hs:quality");
			if(null != qualityValue && !qualityValue.isEmpty()){
				qualityValue = qualityValue.trim();
				if (qualityValue.equals("100") || qualityValue.equals("101")) {
					currentCounter.countLockedFullMatch();
					continue;
				}	
			}
			
		}
		

		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);

		updateXliffFile(vu, tuInfo, xm);

		monitor.worked(1);
	}
}
 
Example #25
Source File: ImportTbx.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * (non-Javadoc)
 * @throws ParseException
 * @throws EntityException
 * @throws EOFException
 * @throws EncodingException
 * @throws IOException
 * @throws ModifyException
 * @throws TranscodeException
 * @see net.heartsome.cat.document.ImportAbstract#executeImport(java.lang.String)
 */
@Override
protected void executeImport(String srcLang) throws SQLException, NavException, XPathParseException,
		XPathEvalException, EncodingException, EOFException, EntityException, ParseException, TranscodeException,
		ModifyException, IOException {
	srcLang = Utils.convertLangCode(srcLang);
	if (monitor != null) {
		int task = vu.getChildElementsCount("/martif/text/body") + vu.getChildElementsCount("/martif/text/back")
				+ 10;
		monitor.beginTask(Messages.getString("document.ImportTbx.task1"), task);
	}
	int headerPkId = 0;
	String sourceLang = null;
	AutoPilot ap = new AutoPilot(vu.getVTDNav());
	ap.selectXPath("/martif");
	Map<String, String> martifAttr = new HashMap<String, String>();
	if (ap.evalXPath() != -1) {
		martifAttr = vu.getCurrentElementAttributs();
		sourceLang = martifAttr.get("xml:lang");
		if (sourceLang == null) {
			sourceLang = martifAttr.get("lang");
		}
	}
	if (sourceLang == null || sourceLang.equals("")) {
		sourceLang = srcLang;
	} else {
		sourceLang = Utils.convertLangCode(sourceLang);
	}

	if (sourceLang == null || sourceLang.equals("*all*") || sourceLang.equals("")) {
		if (LocaleService.getLanguage(sourceLang).equals("")) {
			throw new NavException(Messages.getString("document.ImportTbx.msg1"));
		}
	}

	if (monitor != null) {
		monitor.worked(5);
	}
	// 导入Header
	ap.selectXPath("/martif/martifHeader");
	if (ap.evalXPath() != -1) {
		String hContent = vu.getElementFragment();
		if (hContent != null) {
			headerPkId = dbOperator.insertBMartifHeader(hContent, getElementAttribute("id"));
		}
	}
	// TOTO 保存martifAttr到BATTRIBUTE表
	dbOperator.insertBAttribute(martifAttr, "martif", headerPkId);

	ap.selectXPath("/martif/text");
	if (ap.evalXPath() != -1) {
		Map<String, String> textAttr = vu.getCurrentElementAttributs();
		dbOperator.insertBAttribute(textAttr, "text", headerPkId);
	}

	ap.selectXPath("/martif/body");
	if (ap.evalXPath() != -1) {
		Map<String, String> bodyAttr = vu.getCurrentElementAttributs();
		dbOperator.insertBAttribute(bodyAttr, "body", headerPkId);
	}
	if (monitor != null) {
		monitor.worked(5);
	}

	this.saveTermEntry(sourceLang, headerPkId);

	ap.selectXPath("/martif/text/back");
	if (ap.evalXPath() != -1) {
		Map<String, String> backAttr = vu.getCurrentElementAttributs();
		// TODO 保存back节点的属性
		dbOperator.insertBAttribute(backAttr, "back", headerPkId);
		ap.selectXPath("./refObjectList");
		while (ap.evalXPath() != -1) {
			String roblId = getElementAttribute("id");
			String roblContent = vu.getElementFragment();
			// 保存refObjectList内容
			dbOperator.insertBRefobjectlist(roblContent, roblId, headerPkId);
			if (monitor != null) {
				monitor.worked(1);
			}
		}
	}
	if (monitor != null) {
		monitor.done();
	}

}
 
Example #26
Source File: ImportRTFToXLIFF.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 更新 XLIFF 文件,如果 XLIFF 编辑器在活动状态则刷新界面
 * @throws NavException
 * @throws XPathParseException
 * @throws XPathEvalException
 * @throws ModifyException
 * @throws TranscodeException
 * @throws IOException
 *             ;
 */
private void updateXLIFF() throws NavException, XPathParseException, XPathEvalException, ModifyException,
		TranscodeException, IOException {
	VTDGen vg = new VTDGen();
	if (vg.parseFile(xliffPath, true)) {
		VTDNav vn = vg.getNav();
		AutoPilot ap = new AutoPilot(vn);
		ap.declareXPathNameSpace(XLFHandler.hsNSPrefix, XLFHandler.hsR7NSUrl);
		String xpath = "/xliff/file[@source-language='" + lstHeader.get(1) + "' and @target-language='"
				+ lstHeader.get(2) + "']/body/descendant::trans-unit[@id='" + lstPerRowValue.get(0).trim() + "']";

		VTDUtils vu = new VTDUtils(vn);
		ap.selectXPath(xpath);
		XMLModifier xm = new XMLModifier(vn);
		boolean isUpdate = false;
		// 一个 XLIFF 文件有多个file节点,并且 id 可能会相同,因此此处用 while 循环
		while (ap.evalXPath() != -1) {
			String transunit = vu.getElementFragment();
			String srcText = vu.getValue("./source/text()");
			if (lstPerRowValue.size() < 2 || lstPerRowValue.get(1).equals("")) {
				continue;
			}
			String srcRtfText = net.heartsome.cat.common.util.TextUtil.xmlToString(lstPerRowValue.get(1));
			// 比较源文时要去掉标记-->将 xliff 的源文也加上 rtf 的标记,然后比较
			if (srcText == null || srcText.equals("")) {
				continue;
			}
			StringBuffer sbSrc = new StringBuffer(srcText);
			InnerTagUtil.parseXmlToDisplayValue(sbSrc);
			srcText = net.heartsome.cat.common.util.TextUtil.xmlToString(sbSrc.toString());
			String srcHexString = net.heartsome.cat.common.util.TextUtil.encodeHexString(srcText);
			// 替换不可见字符
			srcHexString = Rtf.replaceInvisibleChar(srcHexString);
			srcText = net.heartsome.cat.common.util.TextUtil.decodeHexString(srcHexString);

			// 由于解析时,标记前后可能丢失空格,因此先将源文本标记前后的空格去掉再比较
			srcText = srcText.replaceAll("\n", "");
			srcText = srcText.replaceAll("\\\\n", "\\\\n");
			srcText = srcText.replaceAll("&amp;", "&");
			srcText = srcText.replaceAll(" " + RTFConstants.TAG_RTF, RTFConstants.TAG_RTF);
			srcText = srcText.replaceAll(RTFConstants.TAG_RTF + " ", RTFConstants.TAG_RTF);
			String srcRtfHexString = net.heartsome.cat.common.util.TextUtil.encodeHexString(srcRtfText);
			// 替换不可见字符
			srcRtfHexString = Rtf.replaceInvisibleChar(srcRtfHexString);
			srcRtfText = net.heartsome.cat.common.util.TextUtil.decodeHexString(srcRtfHexString);
			srcRtfText = srcRtfText.replaceAll("\n", "");
			srcRtfText = srcRtfText.replaceAll("\\\\n", "\\n");
			srcRtfText = srcRtfText.replaceAll(" " + RTFConstants.TAG_RTF, RTFConstants.TAG_RTF);
			srcRtfText = srcRtfText.replaceAll(RTFConstants.TAG_RTF + " ", RTFConstants.TAG_RTF);

			// 源文相同时,再以 rtf 的译文替换 xliff 的译文
			if (srcText.trim().equals(srcRtfText.trim())) {
				xm.remove(vn.getElementFragment());
				transunit = handleTransUnit(transunit);
				xm.insertAfterElement(transunit.getBytes());
				isUpdate = true;
			}
		}
		if (xliffEditor != null && isUpdate) {
			xliffEditor.getXLFHandler().saveAndReparse(xm, xliffPath);
		}
		// saveToFile(xm, xliffPath);
	}
}
 
Example #27
Source File: PreTranslation.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
private void keepHigherMatchs(VTDUtils vu, String srcLang, String tgtLang, XMLModifier xm, IProgressMonitor monitor)
		throws NavException, XPathParseException, XPathEvalException, ModifyException,
		UnsupportedEncodingException, InterruptedException {
	AutoPilot tuAp = new AutoPilot(vu.getVTDNav());
	tuAp.selectXPath("./body//trans-unit");
	boolean needUpdateTarget = false;
	while (tuAp.evalXPath() != -1) { // 循环 Trans-unit
		if (monitor != null && monitor.isCanceled()) {
			throw new InterruptedException();
		}

		String locked = vu.getCurrentElementAttribut("translate", "yes");
		if (locked.equals("no")) {
			continue;
		}

		//  ===从库中查询匹配===
		TransUnitInfo2TranslationBean tuInfo = getTransUnitInfo(vu);
		if (tuInfo == null) {
			continue;
		}
		tuInfo.setSrcLanguage(srcLang);
		tuInfo.setTgtLangugage(tgtLang);
		getTuContext(vu, contextSize, tuInfo);

		List<FuzzySearchResult> result = tmMatcher.executeFuzzySearch(currentProject, tuInfo);
		// Vector<Hashtable<String, String>> tmMatches = tmMatcher.executeSearch(currentProject, tuInfo);
		//  ====查询结束===

		if (!result.isEmpty()) {
			int matchMaxSimiInt = result.get(0).getSimilarity();
			// ===获取当前目标的匹配率===
			int currMaxSimInt = 0;
			vu.getVTDNav().push();
			AutoPilot targetAp = new AutoPilot(vu.getVTDNav());
			targetAp.selectXPath("./target");
			if (targetAp.evalXPath() != -1) {
				String targetContent = vu.getElementContent();
				if (targetContent != null && !targetContent.equals("")) {
					Hashtable<String, String> attrs = vu.getCurrentElementAttributs();
					if (attrs != null) {
						String type = attrs.get("hs:matchType");
						String quality = attrs.get("hs:quality");
						if (type != null && type.equals("TM") && quality != null && !quality.equals("")) {
							currMaxSimInt = Integer.parseInt(quality);
						} else {
							//对于这种没有匹配率的 segment(手动翻译了,但是没入库,没锁定的情况。),需要覆盖2013-09-02, Austen。
							currMaxSimInt = 1;
						}
					}
				} else { // target内容为空
					needUpdateTarget = true;
				}
			} else { // 无target内容
				needUpdateTarget = true;
			}
			vu.getVTDNav().pop();
			if (currMaxSimInt != 0 && matchMaxSimiInt > currMaxSimInt) {
				needUpdateTarget = true;
			}
		}
		//  ===获取当前最大匹配结束===
		updateXliffFile(vu, tuInfo, result, xm, needUpdateTarget);
		needUpdateTarget = false;
		monitor.worked(1);
	}

}
 
Example #28
Source File: ImportTbx.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * (non-Javadoc)
 * @throws ParseException
 * @throws EntityException
 * @throws EOFException
 * @throws EncodingException
 * @throws IOException
 * @throws ModifyException
 * @throws TranscodeException
 * @see net.heartsome.cat.document.ImportAbstract#executeImport(java.lang.String)
 */
@Override
protected void executeImport(String srcLang) throws SQLException, NavException, XPathParseException,
		XPathEvalException, EncodingException, EOFException, EntityException, ParseException, TranscodeException,
		ModifyException, IOException {
	srcLang = Utils.convertLangCode(srcLang);
	if (monitor != null) {
		int task = vu.getChildElementsCount("/martif/text/body") + vu.getChildElementsCount("/martif/text/back")
				+ 10;
		monitor.beginTask(Messages.getString("document.ImportTbx.task1"), task);
	}
	int headerPkId = 0;
	String sourceLang = null;
	AutoPilot ap = new AutoPilot(vu.getVTDNav());
	ap.selectXPath("/martif");
	Map<String, String> martifAttr = new HashMap<String, String>();
	if (ap.evalXPath() != -1) {
		martifAttr = vu.getCurrentElementAttributs();
		sourceLang = martifAttr.get("xml:lang");
		if (sourceLang == null) {
			sourceLang = martifAttr.get("lang");
		}
	}
	if (sourceLang == null || sourceLang.equals("")) {
		sourceLang = srcLang;
	} else {
		sourceLang = Utils.convertLangCode(sourceLang);
	}

	if (sourceLang == null || sourceLang.equals("*all*") || sourceLang.equals("")) {
		if (LocaleService.getLanguage(sourceLang).equals("")) {
			throw new NavException(Messages.getString("document.ImportTbx.msg1"));
		}
	}

	if (monitor != null) {
		monitor.worked(5);
	}
	// 导入Header
	ap.selectXPath("/martif/martifHeader");
	if (ap.evalXPath() != -1) {
		String hContent = vu.getElementFragment();
		if (hContent != null) {
			headerPkId = dbOperator.insertBMartifHeader(hContent, getElementAttribute("id"));
		}
	}
	// TOTO 保存martifAttr到BATTRIBUTE表
	dbOperator.insertBAttribute(martifAttr, "martif", headerPkId);

	ap.selectXPath("/martif/text");
	if (ap.evalXPath() != -1) {
		Map<String, String> textAttr = vu.getCurrentElementAttributs();
		dbOperator.insertBAttribute(textAttr, "text", headerPkId);
	}

	ap.selectXPath("/martif/body");
	if (ap.evalXPath() != -1) {
		Map<String, String> bodyAttr = vu.getCurrentElementAttributs();
		dbOperator.insertBAttribute(bodyAttr, "body", headerPkId);
	}
	if (monitor != null) {
		monitor.worked(5);
	}

	this.saveTermEntry(sourceLang, headerPkId);

	ap.selectXPath("/martif/text/back");
	if (ap.evalXPath() != -1) {
		Map<String, String> backAttr = vu.getCurrentElementAttributs();
		// TODO 保存back节点的属性
		dbOperator.insertBAttribute(backAttr, "back", headerPkId);
		ap.selectXPath("./refObjectList");
		while (ap.evalXPath() != -1) {
			String roblId = getElementAttribute("id");
			String roblContent = vu.getElementFragment();
			// 保存refObjectList内容
			dbOperator.insertBRefobjectlist(roblContent, roblId, headerPkId);
			if (monitor != null) {
				monitor.worked(1);
			}
		}
	}
	if (monitor != null) {
		monitor.done();
	}

}
 
Example #29
Source File: VTDUtils.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 根据 vn 的 encoding 获取字符集编码(此方法与 XMLModifier 的 bind 方法中获取字符集的方式一致)
 * @return
 * @throws ModifyException
 *             ;
 */
public String getCharsetByEncoding() throws ModifyException {
	int encoding = vn.getEncoding();
	switch (encoding) {
	case VTDNav.FORMAT_ASCII:
		return "ASCII";
	case VTDNav.FORMAT_ISO_8859_1:
		return "ISO8859_1";
	case VTDNav.FORMAT_UTF8:
		return "UTF8";
	case VTDNav.FORMAT_UTF_16BE:
		return "UnicodeBigUnmarked";
	case VTDNav.FORMAT_UTF_16LE:
		return "UnicodeLittleUnmarked";
	case VTDNav.FORMAT_ISO_8859_2:
		return "ISO8859_2";
	case VTDNav.FORMAT_ISO_8859_3:
		return "ISO8859_3";
	case VTDNav.FORMAT_ISO_8859_4:
		return "ISO8859_4";
	case VTDNav.FORMAT_ISO_8859_5:
		return "ISO8859_5";
	case VTDNav.FORMAT_ISO_8859_6:
		return "ISO8859_6";
	case VTDNav.FORMAT_ISO_8859_7:
		return "ISO8859_7";
	case VTDNav.FORMAT_ISO_8859_8:
		return "ISO8859_8";
	case VTDNav.FORMAT_ISO_8859_9:
		return "ISO8859_9";
	case VTDNav.FORMAT_ISO_8859_10:
		return "ISO8859_10";
	case VTDNav.FORMAT_ISO_8859_11:
		return "x-iso-8859-11";
	case VTDNav.FORMAT_ISO_8859_12:
		return "ISO8859_12";
	case VTDNav.FORMAT_ISO_8859_13:
		return "ISO8859_13";
	case VTDNav.FORMAT_ISO_8859_14:
		return "ISO8859_14";
	case VTDNav.FORMAT_ISO_8859_15:
		return "ISO8859_15";

	case VTDNav.FORMAT_WIN_1250:
		return "Cp1250";
	case VTDNav.FORMAT_WIN_1251:
		return "Cp1251";
	case VTDNav.FORMAT_WIN_1252:
		return "Cp1252";
	case VTDNav.FORMAT_WIN_1253:
		return "Cp1253";
	case VTDNav.FORMAT_WIN_1254:
		return "Cp1254";
	case VTDNav.FORMAT_WIN_1255:
		return "Cp1255";
	case VTDNav.FORMAT_WIN_1256:
		return "Cp1256";
	case VTDNav.FORMAT_WIN_1257:
		return "Cp1257";
	case VTDNav.FORMAT_WIN_1258:
		return "Cp1258";
	default:
		throw new ModifyException(Messages.getString("vtdimpl.VTDUtils.logger2"));
	}
}
 
Example #30
Source File: AbstractVersionModifyingMojo.java    From revapi with Apache License 2.0 4 votes vote down vote up
void updateProjectVersion(MavenProject project, Version version) throws MojoExecutionException {
    try {

        int indentationSize;
        try (BufferedReader rdr = new BufferedReader(new FileReader(project.getFile()))) {
            indentationSize = XmlUtil.estimateIndentationSize(rdr);
        }

        VTDGen gen = new VTDGen();
        gen.enableIgnoredWhiteSpace(true);
        gen.parseFile(project.getFile().getAbsolutePath(), true);

        VTDNav nav = gen.getNav();
        AutoPilot ap = new AutoPilot(nav);
        XMLModifier mod = new XMLModifier(nav);

        ap.selectXPath("/project/version");
        if (ap.evalXPath() != -1) {
            //found the version
            int textPos = nav.getText();
            mod.updateToken(textPos, version.toString());
        } else {
            if (!forceVersionUpdate && !project.getParent().getVersion().equals(version.toString())) {
                throw new MojoExecutionException("Project " + project.getArtifactId() + " inherits the version" +
                        " from the parent project (" + project.getParent().getVersion() + ") but should have a" +
                        " different version according to the configured rules (" + version.toString() + "). If" +
                        " you wish to insert an explicit version instead of inheriting it, set the " +
                        Props.forceVersionUpdate.NAME + " property to true.");
            }

            //place the version after the artifactId
            ap.selectXPath("/project/artifactId");
            if (ap.evalXPath() == -1) {
                throw new MojoExecutionException("Failed to find artifactId element in the pom.xml of project "
                        + project.getArtifact().getId() + " when trying to insert a version tag after it.");
            } else {
                StringBuilder versionLine = new StringBuilder();
                versionLine.append('\n');
                for (int i = 0; i < indentationSize; ++i) {
                    versionLine.append(' ');
                }
                versionLine.append("<version>").append(version.toString()).append("</version>");
                mod.insertAfterElement(versionLine.toString());
            }
        }

        try (OutputStream out = new FileOutputStream(project.getFile())) {
            mod.output(out);
        }
    } catch (IOException | ModifyException | NavException | XPathParseException | XPathEvalException | TranscodeException e) {
        throw new MojoExecutionException("Failed to update the version of project " + project, e);
    }
}