use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class VTDUtils method getValues.
/**
* 根据 XPath 取一个集合的值。<br/>
* <b>注意</b>在XPath基础上,添加关键字“[name]”、“puretext()”的支持: <li>[name]:用于取得指定属性名称,例如,/a/@*[name] 取得 a 节点下所有属性的名字</li> <li>
* puretext():用于取得指定节点的纯文本内容(排除子节点),例如,/a/puretext() 取得 a 节点的纯文本内容</li>
* @param ap
* AutoPilot 对象
* @param xpath
* XPath 表达式
* @param isAllowRepeat
* 是否允许取值重复。
* @return XPath 得到的值,无匹配的值则为 null;
*/
public List<String> getValues(AutoPilot ap, String xpath, boolean isAllowRepeat) {
if (ap == null) {
ap = new AutoPilot(vn);
}
List<String> values = new Vector<String>();
try {
vn.push();
if (xpath.endsWith("/puretext()")) {
xpath = xpath.substring(0, xpath.length() - "/puretext()".length());
ap.selectXPath(xpath);
if (ap.evalXPath() != -1) {
String strTmpValue = getElementPureText();
if (isAllowRepeat) {
values.add(strTmpValue);
} else {
if (!values.contains(strTmpValue)) {
values.add(strTmpValue);
}
}
}
} else if (xpath.endsWith("/text()")) {
xpath = xpath.substring(0, xpath.length() - "/text()".length());
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
String strTmpValue = getElementContent();
if (isAllowRepeat) {
values.add(strTmpValue);
} else {
if (!values.contains(strTmpValue)) {
values.add(strTmpValue);
}
}
}
} else {
boolean isAttrName = false;
if (xpath.endsWith("[name]")) {
xpath = xpath.substring(0, xpath.length() - "[name]".length());
isAttrName = true;
}
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
int type = vn.getTokenType(vn.getCurrentIndex());
if (type == VTDNav.TOKEN_STARTING_TAG) {
long l = vn.getElementFragment();
String strTmpValue = vn.toString((int) l, (int) (l >> 32));
if (isAllowRepeat) {
values.add(strTmpValue);
} else {
if (!values.contains(strTmpValue)) {
values.add(strTmpValue);
}
}
} else if (type == VTDNav.TOKEN_ATTR_NAME || type == VTDNav.TOKEN_ATTR_NS) {
String strTmpValue;
if (isAttrName) {
strTmpValue = vn.toString(vn.getCurrentIndex());
} else {
strTmpValue = vn.toString(vn.getCurrentIndex() + 1);
}
if (isAllowRepeat) {
values.add(strTmpValue);
} else {
if (!values.contains(strTmpValue)) {
values.add(strTmpValue);
}
}
} else {
String strTmpValue = vn.toString(vn.getCurrentIndex());
if (isAllowRepeat) {
values.add(strTmpValue);
} else {
if (!values.contains(strTmpValue)) {
values.add(strTmpValue);
}
}
}
}
}
} catch (XPathParseException e) {
LOGGER.error("", e);
} catch (XPathEvalException e) {
LOGGER.error("", e);
} catch (NavException e) {
LOGGER.error("", e);
} finally {
vn.pop();
}
if (values.size() == 0) {
values = null;
}
return values;
}
use of com.ximpleware.NavException 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;
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class ImportTbx method executeImport.
/**
* (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();
}
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XLFHandler method updateAltTrans.
private void updateAltTrans(VTDUtils vu, XMLModifier xm, String rowId, List<AltTransBean> newAltTrans, List<String> oldAltTransToolId) {
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
try {
ap.selectXPath(tuXPath);
if (ap.evalXPath() != -1) {
StringBuffer bf = new StringBuffer();
for (String toolId : oldAltTransToolId) {
bf.append("@tool-id='").append(toolId).append("' | ");
}
if (bf.length() > 0) {
String toolId = bf.substring(0, bf.lastIndexOf("|"));
String deleteXpath = "./alt-trans[" + toolId + "]";
AutoPilot _ap = new AutoPilot(vu.getVTDNav());
_ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
_ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
vu.delete(_ap, xm, deleteXpath, VTDUtils.PILOT_TO_END);
}
StringBuffer xmlBf = new StringBuffer();
for (AltTransBean altTran : newAltTrans) {
xmlBf.append(altTran.toXMLString());
}
if (xmlBf.length() > 0) {
xm.insertBeforeTail(xmlBf.toString());
}
}
} catch (XPathParseException e) {
LOGGER.error("", e);
} catch (XPathEvalException e) {
LOGGER.error("", e);
} catch (ModifyException e) {
LOGGER.error("", e);
} catch (UnsupportedEncodingException e) {
LOGGER.error("", e);
} catch (NavException e) {
LOGGER.error("", e);
}
}
use of com.ximpleware.NavException 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 "";
}
Aggregations