use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method splitSegment.
/**
* 分割文本段
* @param rowId
* 要分割的行的RowId
* @param spltOffset
* 分割的位置;
* @return ;
*/
public String splitSegment(String rowId, int spltOffset) {
String fileName = RowIdUtil.getFileNameByRowId(rowId);
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
String xpath = RowIdUtil.parseRowIdToXPath(rowId);
try {
VTDUtils vu = new VTDUtils(vn);
ap.selectXPath(xpath);
if (ap.evalXPath() == -1) {
return "";
}
String tuid = vu.getCurrentElementAttribut("id", "");
String xmlSpace = vu.getCurrentElementAttribut("xml:space", "preserve");
String approved = vu.getCurrentElementAttribut("approved", null);
String sendToTM = vu.getCurrentElementAttribut("hs:send-to-tm", null);
String needReview = vu.getCurrentElementAttribut("hs:needs-review", null);
String tuHead = vu.getElementHead();
// 删除 approved 属性
if (approved != null) {
tuHead = tuHead.replace(" approved=\"" + approved + "\"", "");
tuHead = tuHead.replace(" approved='" + approved + "'", "");
}
// tuHead2 删除 hs:send-to-tm, hs:needs-review 属性
String tuHead2 = tuHead;
if (sendToTM != null) {
tuHead2 = tuHead2.replace(" hs:send-to-tm=\"" + sendToTM + "\"", "");
tuHead2 = tuHead2.replace(" hs:send-to-tm='" + sendToTM + "'", "");
}
if (needReview != null) {
tuHead2 = tuHead2.replace(" hs:needs-review=\"" + needReview + "\"", "");
tuHead2 = tuHead2.replace(" hs:needs-review='" + needReview + "'", "");
}
StringBuffer tu1;
StringBuffer tu2;
if (tuHead.contains("id=\"" + tuid + "\"")) {
tu1 = new StringBuffer(tuHead.replace("id=\"" + tuid + "\"", "id=\"" + tuid + "-1\""));
tu2 = new StringBuffer(tuHead2.replace("id=\"" + tuid + "\"", "id=\"" + tuid + "-2\""));
} else if (tuHead.contains("id='" + tuid + "'")) {
tu1 = new StringBuffer(tuHead.replace("id='" + tuid + "'", "id=\"" + tuid + "-1\""));
tu2 = new StringBuffer(tuHead2.replace("id='" + tuid + "'", "id=\"" + tuid + "-2\""));
} else {
// 不存在 id 属性
return "";
}
String sourceFragment1 = null;
String sourceFragment2 = null;
String targetFragment1 = null;
String targetFragment2 = null;
ap.selectXPath(xpath + "/source");
if (ap.evalXPath() != -1) {
String sourceHead = vu.getElementHead();
if (sourceHead != null) {
// source节点的内容
String sourceContent = vu.getElementContent();
// 处理光标在 g 标记内的情况 --robert 2012-11-15
List<Map<String, String>> tagLocationList = getTagLocation(vn, sourceContent);
String srcAddStr1 = "";
String srcAddStr2 = "";
for (Map<String, String> map : tagLocationList) {
String tagHeader = map.get("tagHeader");
String tagTail = map.get("tagTail");
int headerIdx = Integer.parseInt(map.get("headerIdx"));
int tailIdx = Integer.parseInt(map.get("tailIdx"));
if (headerIdx < spltOffset && spltOffset <= tailIdx) {
srcAddStr1 = tagTail + srcAddStr1;
srcAddStr2 += tagHeader;
}
}
sourceFragment1 = sourceHead + sourceContent.substring(0, spltOffset) + srcAddStr1 + "</source>";
sourceFragment2 = sourceHead + srcAddStr2 + sourceContent.substring(spltOffset) + "</source>";
}
}
ap.selectXPath(xpath + "/target");
if (ap.evalXPath() != -1) {
String state = vu.getCurrentElementAttribut("state", null);
String targetHead = vu.getElementHead();
if (targetHead != null) {
if (state != null && !state.equalsIgnoreCase("new")) {
targetHead = targetHead.replace(" state=\"" + state + "\"", " state=\"new\"");
targetHead = targetHead.replace(" state='" + state + "'", " state=\"new\"");
targetFragment1 = targetHead + vu.getElementContent() + "</target>";
} else {
// target节点的段落
targetFragment1 = vu.getElementFragment();
}
// targetFragment2 = targetHead + "</target>";
// modify by peason---- Bug #1048
targetFragment2 = "<target></target>";
}
}
if (sourceFragment1 != null) {
tu1.append(sourceFragment1);
}
if (targetFragment1 != null) {
tu1.append(targetFragment1);
}
if (sourceFragment2 != null) {
tu2.append(sourceFragment2);
}
if (targetFragment2 != null) {
tu2.append(targetFragment2);
}
// 批注信息添加到分割后的第一个文本段中
ap.selectXPath(xpath + "/note");
while (ap.evalXPath() != -1) {
tu1.append(vu.getElementFragment());
}
tu1.append("</trans-unit>");
tu2.append("</trans-unit>");
StringBuffer group = new StringBuffer("<group ");
group.append("id=\"" + tuid + "\" ");
group.append("ts=\"hs-split\" ");
group.append("splitMergeIndex=\"" + System.nanoTime() + "\" ");
group.append("xml:space=\"" + xmlSpace + "\">");
group.append(tu1).append(tu2);
group.append("</group>");
String tuFragment = "";
ap.selectXPath(xpath);
if (ap.evalXPath() != -1) {
XMLModifier xm = new XMLModifier(vn);
xm.insertBeforeElement(group.toString());
// 保存修改前的内容
tuFragment = vu.getElementFragment();
xm.remove();
// 保存并更新VTDNav对象
saveAndReparse(xm, fileName);
int index = rowIds.indexOf(rowId);
// 移除分割前的RowId
rowIds.remove(index);
// 添加分割后的RowId
rowIds.add(index, rowId + "-2");
rowIds.add(index, rowId + "-1");
int tuSize = tuSizeMap.get(fileName);
// 翻译单元总数加1
tuSizeMap.put(fileName, tuSize + 1);
}
return tuFragment;
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return "";
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method getXliffInfo.
/**
* 检查所有已读取文件的file节点是否有target-language属性
* @return key:filename,value: map(key:file节点的original属性
* @throws XPathParseException
* @throws NavException
* @throws XPathEvalException
*/
private Map<String, List<XliffBean>> getXliffInfo(boolean checkTgtLang) {
Map<String, List<XliffBean>> fileNameXliffBeansMap = new HashMap<String, List<XliffBean>>();
VTDUtils vu = new VTDUtils();
AutoPilot subAp = new AutoPilot();
for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
String fileName = entry.getKey();
VTDNav vn = entry.getValue();
try {
vu.bind(vn);
subAp.bind(vn);
subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"sourcefile\"]");
AutoPilot ap = new AutoPilot(vn);
String xpath;
if (checkTgtLang) {
xpath = "/xliff/file[not(@target-language)]";
} else {
xpath = "/xliff/file";
}
// 不存在 target-language 属性。
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
vn.push();
List<XliffBean> xliffBeans = fileNameXliffBeansMap.get(fileName);
if (xliffBeans == null) {
xliffBeans = new ArrayList<XliffBean>();
fileNameXliffBeansMap.put(fileName, xliffBeans);
}
String sourceLanguage = vu.getCurrentElementAttribut("source-language", "");
String targetLanguage = vu.getCurrentElementAttribut("target-language", "");
// 如果是
String original = vu.getCurrentElementAttribut("original", "");
// 源文件名
String sourceFile;
String datatype = vu.getCurrentElementAttribut("datatype", null);
if (datatype != null && (datatype.contains("openoffice") || datatype.contains("msoffice2007"))) {
// 重置 AutoPilot 对象的储存的信息。
subAp.resetXPath();
if (subAp.evalXPath() != -1) {
sourceFile = vu.getElementContent();
} else {
sourceFile = original;
}
} else {
sourceFile = original;
}
XliffBean xliff = null;
for (XliffBean bean : xliffBeans) {
if (bean.getSourceFile().equals(sourceFile)) {
xliff = bean;
}
}
if (xliff == null) {
xliff = new XliffBean(datatype, sourceFile, sourceLanguage, targetLanguage, original, fileName);
xliffBeans.add(xliff);
} else {
xliff.addOriginal(original);
}
vn.pop();
}
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
return fileNameXliffBeansMap;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method addNote.
/**
* 添加批注
* @param rowId
* 行的唯一标识
* @param note
* 批注内容;
*/
public void addNote(String rowId, String note) {
String fileName = RowIdUtil.getFileNameByRowId(rowId);
String xpath = RowIdUtil.parseRowIdToXPath(rowId);
VTDNav vn = vnMap.get(fileName);
try {
VTDUtils vu = new VTDUtils(vn);
XMLModifier xm = vu.insert(xpath + "/text()", "<note>" + note + "</note>");
saveAndReparse(xm, fileName);
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method getLanguages.
/**
* 得到所有语言对
* @return 语言对的Map<br/>
* key: 源语言;value: 对应的目标语言(可以是多个)
*/
public Map<String, ArrayList<String>> getLanguages() {
TreeMap<String, ArrayList<String>> languages = new TreeMap<String, ArrayList<String>>();
AutoPilot ap = new AutoPilot();
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
VTDUtils vu = new VTDUtils();
for (VTDNav vn : vnMap.values()) {
ap.bind(vn);
try {
vu.bind(vn);
ap.selectXPath("/xliff/file");
while (ap.evalXPath() != -1) {
String srcLanguage = vu.getCurrentElementAttribut("source-language", null);
String tgtLanguage = vu.getCurrentElementAttribut("target-language", null);
if (srcLanguage == null) {
// TODO 该file节点不存在“source-language”属性,提醒添加
continue;
}
if (tgtLanguage == null) {
// TODO 该file节点不存在“target-language”属性,提醒添加
continue;
}
ArrayList<String> tgtLanguages = languages.get(srcLanguage);
if (tgtLanguages == null) {
tgtLanguages = new ArrayList<String>();
languages.put(srcLanguage, tgtLanguages);
}
if (!tgtLanguages.contains(tgtLanguage)) {
// 未包含,就添加进去
tgtLanguages.add(tgtLanguage);
}
}
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
return languages;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method generateTMXFileContent.
/**
* 根据同一个 XLIFF 文件中的 rowId 生成 TMX 文件, 对于不添加到库中的文本段将被忽略
* @param list
* rowId
* @param srcLang
* 源语言
* @param monitor
* 进度条
* @return TMX 文件
*/
public StringBuffer generateTMXFileContent(String systemUser, List<String> list, String srcLang, String tgtLang, IProgressMonitor monitor, int contextSize, IProject project) {
if (list == null || list.size() == 0) {
return null;
}
// long start = System.currentTimeMillis();
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.setTaskName(Messages.getString("file.XLFHandler.task4"));
monitor.beginTask(Messages.getString("file.XLFHandler.task4"), list.size());
if (monitor.isCanceled()) {
monitor.setTaskName(Messages.getString("file.XLFHandler.task5"));
throw new OperationCanceledException();
}
StringBuffer re = new StringBuffer();
re.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
re.append("<tmx version=\"1.4b\">");
re.append("<header creationtool=\"" + Constants.TMX_CREATIONTOOL + "\" creationtoolversion=\"" + Constants.TMX_CREATIONTOOLVERSION + "\" srclang=\"" + srcLang + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
re.append("</header>");
re.append("<body>");
VTDNav vn = getVTDNavByRowId(list.get(0)).duplicateNav();
AutoPilot ap = new AutoPilot(vn);
VTDUtils vu = null;
try {
vu = new VTDUtils(vn);
} catch (NavException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger15"), e);
}
String strChangeBy = systemUser;
String strProp = getProjectProp(project);
boolean isNull = true;
AutoPilot ap2 = new AutoPilot(vn);
ap2.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
for (String rowId : list) {
if (monitor.isCanceled()) {
re = null;
monitor.setTaskName(Messages.getString("file.XLFHandler.task5"));
throw new OperationCanceledException();
}
String tgtText = getTgtContent(rowId);
String srcText = getSrcContent(rowId);
if (srcText == null || srcText.trim().equals("") || tgtText == null || tgtText.trim().equals("")) {
monitor.worked(1);
continue;
}
if (isSendToTM(rowId)) {
monitor.worked(1);
continue;
}
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
try {
ap.selectXPath(tuXPath);
ap.evalXPath();
} catch (XPathParseException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
monitor.done();
} catch (XPathEvalException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
monitor.done();
} catch (NavException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
monitor.done();
}
re.append("<tu ");
// 添加tu的属性
String id = vu.getValue("./@id");
String creationDate = CommonFunction.retTMXDate();
String changeDate = creationDate;
re.append("tuid=\"" + genTuId(rowId, id) + "\" ");
re.append("changedate=\"" + changeDate + "\" changeid=\"" + strChangeBy + "\" ");
re.append("creationdate=\"" + creationDate + "\" creationid=\"" + strChangeBy + "\" >\n");
try {
// Fixed Bug #2290 入库时未添加批注信息 by Jason
Vector<NoteBean> notes = getNotes(rowId);
if (notes != null) {
for (NoteBean bean : notes) {
String lang = bean.getLang();
if (lang != null && !lang.equals("")) {
re.append("<note xml:lang='" + lang + "'>" + bean.getNoteText() + "</note>");
} else {
re.append("<note>" + bean.getNoteText() + "</note>");
}
}
}
} catch (NavException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
} catch (XPathParseException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
} catch (XPathEvalException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
}
// 添加上下文prop
re.append("<prop type=\"x-preContext\">");
vn.push();
try {
ap2.selectXPath(tuXPath);
} catch (XPathParseException e) {
LOGGER.error("XPathParseException", e);
e.printStackTrace();
}
re.append(getContext(vu, ap2, contextSize, true));
re.append("</prop>");
vn.pop();
vn.push();
re.append("<prop type=\"x-nextContext\">");
ap2.resetXPath();
re.append(getContext(vu, ap2, contextSize, false));
re.append("</prop>");
vn.pop();
// 添加自定义属性
re.append(strProp);
re.append("<tuv xml:lang=\"" + srcLang + "\">\n");
// String sourceTextXp = "./source/text()";
// re.append("<seg>").append(vu.getValue(ap, sourceTextXp)).append("</seg>");
re.append("<seg>").append(srcText.trim()).append("</seg>\n");
re.append("</tuv>\n");
// 添加tuv(target)
re.append("<tuv xml:lang=\"" + tgtLang + "\">\n");
// String targetTextXp = "./target/text()";
// re.append("<seg>").append(vu.getValue(ap, targetTextXp)).append("</seg>");
re.append("<seg>").append(tgtText.trim()).append("</seg>\n");
re.append("</tuv>\n");
re.append("</tu>\n");
monitor.worked(1);
isNull = false;
}
re.append("</body>");
re.append("</tmx>");
monitor.done();
if (isNull) {
return null;
} else {
return re;
}
}
Aggregations