use of net.heartsome.cat.common.bean.TmxSegement in project translationstudio8 by heartsome.
the class ImportTmx method checkDuplicate.
private List<TmxTU> checkDuplicate(TmxTU tu) throws SQLException {
TmxSegement srcTuv = tu.getSource();
List<TmxSegement> tuvs = tu.getSegments();
if (srcTuv == null || tuvs == null || tu.getSegments().size() == 0) {
return null;
}
String pureText = srcTuv.getPureText();
if (pureText == null) {
return null;
}
int hash = pureText.hashCode();
String tuId = tu.getTuId();
if (tuId == null || tuId.equals("")) {
tuId = generateTuId();
tu.setTuId(tuId);
}
// long l = System.currentTimeMillis();
List<TmxTU> dbTus = db.getTUInfoByTuvInfo(hash, Utils.convertLangCode(srcTuv.getLangCode()), tuId);
// System.out.println("checkDuplicate tu: "+ (System.currentTimeMillis() - l));
return dbTus;
}
use of net.heartsome.cat.common.bean.TmxSegement in project translationstudio8 by heartsome.
the class ImportTmx method addTu.
private boolean addTu(TmxTU tu) throws SQLException {
TmxSegement srcTuv = tu.getSource();
List<TmxSegement> tuvs = tu.getSegments();
if ((srcTuv == null && tuvs == null) || (srcTuv == null && tuvs.size() < 2)) {
// check TU
return false;
}
String tuId = tu.getTuId();
if (tuId == null || tuId.equals("")) {
tuId = generateTuId();
}
int tuPk = db.insertTU(0, tuId, tu.getCreationUser(), tu.getCreationDate(), tu.getChangeUser(), tu.getChangeDate(), tu.getCreationTool(), tu.getCreationToolVersion(), null, null, null);
List<TmxNote> notes = tu.getNotes();
if (notes != null && notes.size() != 0) {
for (TmxNote note : notes) {
db.insertTMXNote(tuPk, "TU", note.getContent(), null, null, null, null, note.getEncoding(), note.getXmlLang());
}
}
List<TmxProp> props = tu.getProps();
if (props != null && props.size() != 0) {
for (TmxProp prop : props) {
db.insertTMXProp(tuPk, "TU", prop.getName(), null, null, prop.getValue());
}
}
String preContext = null;
String nextContext = null;
if (tu.getContexts() != null) {
preContext = tu.getContexts().getPreContext();
nextContext = tu.getContexts().getNextContext();
}
if (srcTuv != null) {
addTuv(tuPk, srcTuv, preContext, nextContext);
preContext = null;
nextContext = null;
}
if (tuvs != null) {
for (TmxSegement tuv : tuvs) {
addTuv(tuPk, tuv, preContext, nextContext);
}
}
return true;
}
use of net.heartsome.cat.common.bean.TmxSegement in project translationstudio8 by heartsome.
the class ImportTmx method flush.
private void flush() throws SQLException {
if (importStrategy == Constants.IMPORT_MODEL_ALWAYSADD) {
// 始终增加
// transaction control
// long l = System.currentTimeMillis();
db.beginTransaction();
try {
for (TmxTU tu : tmxTuCache) {
addTu(tu);
monitor.worked(1);
if (monitor != null && monitor.isCanceled()) {
break;
}
}
} catch (SQLException e) {
db.rollBack();
throw e;
}
db.commit();
tmxTuCache.clear();
// System.out.println((System.currentTimeMillis() - l));
} else if (importStrategy == Constants.IMPORT_MODEL_OVERWRITE) {
Map<TmxTU, List<TmxTU>> duplicateTuCache = new HashMap<TmxTU, List<TmxTU>>();
// Map<TmxTU, List<TmxSegement>> duplicateTuvPkCache = new HashMap<TmxTU, List<TmxSegement>>();
// long l2 = System.currentTimeMillis();
List<TmxTU> filterSrcSameTu = filterSrcSameTu(tmxTuCache, Constants.IMPORT_MODEL_OVERWRITE);
for (TmxTU tu : filterSrcSameTu) {
if (monitor != null && monitor.isCanceled()) {
return;
}
// long l1 = System.currentTimeMillis();
List<TmxTU> dbTus = checkDuplicate(tu);
if (dbTus == null) {
// error TU
continue;
}
// cached the DB exist TU
duplicateTuCache.put(tu, dbTus);
if (dbTus.size() != 0) {
// long l = System.currentTimeMillis();
for (TmxTU dbTu : dbTus) {
List<TmxSegement> tuvs = tu.getSegments();
List<TmxSegement> dbTuvs = new ArrayList<TmxSegement>();
for (TmxSegement tuv : tuvs) {
dbTuvs.addAll(db.getTextDataIdByGroupIdLang(dbTu.getTmId(), "M", tuv.getLangCode()));
}
dbTu.setSegments(dbTuvs);
List<TmxProp> dbProps = db.getTuMprops(dbTu.getTmId(), "TU");
dbTu.setProps(dbProps);
List<TmxNote> dbNotes = db.getTuMNote(dbTu.getTmId(), "TU");
dbTu.setNotes(dbNotes);
// duplicateTuvPkCache.put(dbTu, dbTuvs);
}
// System.out.println("getTextDataidByGroup:"+ (System.currentTimeMillis() - l));
}
// System.out.println("tu check with db:"+ (System.currentTimeMillis() - l1));
}
// System.out.println("200 tu check with db:"+ (System.currentTimeMillis() - l2));
tmxTuCache.clear();
filterSrcSameTu.clear();
// Transaction control
db.beginTransaction();
try {
Iterator<TmxTU> tuIt = duplicateTuCache.keySet().iterator();
while (tuIt.hasNext()) {
if (monitor != null && monitor.isCanceled()) {
break;
}
TmxTU tu = tuIt.next();
List<TmxTU> dbTus = duplicateTuCache.get(tu);
if (dbTus.size() == 0) {
addTu(tu);
} else {
for (TmxTU dbTu : dbTus) {
String changeDate = tu.getChangeDate() == null ? "" : tu.getChangeDate();
String changeId = tu.getChangeUser() == null ? "" : tu.getChangeUser();
String dbChangeDate = dbTu.getChangeDate() == null ? "" : dbTu.getChangeDate();
String dbChangeId = dbTu.getChangeUser() == null ? "" : dbTu.getChangeUser();
// Update TU Attribute (XmlElement Attribute)
if (!changeDate.equals(dbChangeDate) || !changeId.equals(dbChangeId)) {
db.updateTuChangeInfo(dbTu.getTmId(), tu.getTuId(), changeId, changeDate);
}
// Update Prop
List<TmxProp> props = tu.getProps();
List<TmxProp> dbProps = dbTu.getProps();
if (dbProps.size() == 0 && props != null) {
for (TmxProp _prop : props) {
db.insertTMXProp(dbTu.getTmId(), "TU", _prop.getName(), null, null, _prop.getValue());
}
} else if (props != null) {
for (TmxProp prop : props) {
boolean exist = false;
for (TmxProp dbProp : dbProps) {
if (prop.equals(dbProp)) {
exist = true;
break;
}
}
if (!exist) {
db.deleteMprop("TU", dbTu.getTmId() + "");
for (TmxProp _prop : props) {
db.insertTMXProp(dbTu.getTmId(), "TU", _prop.getName(), null, null, _prop.getValue());
}
break;
}
}
}
// Update Note
List<TmxNote> notes = tu.getNotes();
List<TmxNote> dbNotes = dbTu.getNotes();
if (dbNotes.size() == 0 && notes != null) {
for (TmxNote note : notes) {
db.insertTMXNote(dbTu.getTmId(), "TU", note.getContent(), null, null, null, null, note.getEncoding(), note.getXmlLang());
}
} else if (notes != null) {
for (TmxNote note : notes) {
boolean exist = false;
for (TmxNote dbNote : dbNotes) {
if (note.equals(dbNote)) {
exist = true;
break;
}
}
if (!exist) {
db.deleteMNote("TU", dbTu.getTmId() + "");
for (TmxNote _note : notes) {
db.insertTMXNote(dbTu.getTmId(), "TU", _note.getContent(), null, null, null, null, _note.getEncoding(), _note.getXmlLang());
}
break;
}
}
}
// Update TUVS except source TUV
List<TmxSegement> tuvs = tu.getSegments();
if (tuvs != null) {
for (TmxSegement tuv : tuvs) {
List<TmxSegement> dbTuvs = dbTu.getSegments();
String lang = tuv.getLangCode();
String content = tuv.getFullText();
if (content == null) {
continue;
}
if (dbTuvs.size() != 0) {
boolean flg = false;
boolean isDuplicate = false;
for (TmxSegement dbTuv : dbTuvs) {
if (dbTuv.getLangCode().equalsIgnoreCase(lang)) {
String dbContent = dbTuv.getFullText();
if (!dbContent.equals(content)) {
db.deleteAllTuvRelations(Arrays.asList(new Integer[] { dbTuv.getDbPk() }), lang);
flg = true;
}
isDuplicate = true;
}
}
if (flg == true || !isDuplicate) {
addTuv(dbTu.getTmId(), tuv, null, null);
}
} else {
addTuv(dbTu.getTmId(), tuv, null, null);
}
}
}
// Update context
TmxContexts dbContexts = dbTu.getContexts();
if (tu.getContexts() != null) {
String preContext = tu.getContexts().getPreContext();
String nextContext = tu.getContexts().getNextContext();
if (dbContexts == null) {
db.updateTuvContext(dbTu.getTmId(), tu.getSource().getLangCode(), preContext, nextContext);
} else {
String dbPreContext = dbContexts.getPreContext();
String dbNextContext = dbContexts.getNextContext();
if (!dbPreContext.equals(preContext) || !dbNextContext.equals(nextContext)) {
db.updateTuvContext(dbTu.getTmId(), tu.getSource().getLangCode(), preContext, nextContext);
}
}
}
}
}
monitor.worked(1);
}
} catch (SQLException e) {
db.rollBack();
throw e;
}
db.commit();
duplicateTuCache.clear();
} else if (importStrategy == Constants.IMPORT_MODEL_IGNORE) {
List<TmxTU> needAddTus = new ArrayList<TmxTU>();
List<TmxTU> filterSrcSameTu = filterSrcSameTu(tmxTuCache, Constants.IMPORT_MODEL_IGNORE);
for (TmxTU tu : filterSrcSameTu) {
if (monitor != null && monitor.isCanceled()) {
return;
}
List<TmxTU> dbTus = checkDuplicate(tu);
if (dbTus == null) {
// error TU
continue;
}
// cached the DB exist TU
if (dbTus.size() == 0) {
needAddTus.add(tu);
}
}
tmxTuCache.clear();
filterSrcSameTu.clear();
// Transaction control
db.beginTransaction();
try {
for (TmxTU tu : needAddTus) {
if (monitor != null && monitor.isCanceled()) {
break;
}
addTu(tu);
monitor.worked(1);
}
} catch (SQLException e) {
db.rollBack();
throw e;
}
db.commit();
needAddTus.clear();
}
}
use of net.heartsome.cat.common.bean.TmxSegement in project translationstudio8 by heartsome.
the class TmxReader method readTuTuvElement.
private void readTuTuvElement(TmxTU tu) throws VTDException {
VTDNav vn = vu.getVTDNav();
vn.push();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("./tuv");
// TUV 节点下的Note,Prop节点暂时不处理,所以此处暂时不解析
while (ap.evalXPath() != -1) {
int inx = vn.getAttrVal("xml:lang");
inx = inx == -1 ? vn.getAttrVal("lang") : inx;
String lang = inx != -1 ? vn.toString(inx) : null;
if (lang == null) {
continue;
}
vn.push();
if (vu.pilot("./seg") != -1) {
String fullText = vu.getElementContent().trim();
String pureText = DocUtils.getTmxTbxPureText(vu).trim();
if (fullText == null || pureText == null || fullText.equals("") || pureText.equals("")) {
// fix Bug #2928 by Jason SQLite--导入TMX异常, 导入程序正常退出,但是未完全导入所有内容,此处在continue时应该先调用vn.pop()
vn.pop();
continue;
}
TmxSegement segment = new TmxSegement();
segment.setLangCode(Utils.convertLangCode(lang));
// fix Bug #3406 by yule --xliff中的标记可能与TMX标记不兼容。
if (tmxFilter == null)
// segment.setFullText(InnerTagClearUtil.clearXliffTag4Tmx(fullText));
// 不在导入时清理
segment.setFullText(fullText);
else {
String text = tmxFilter.clearString(fullText);
// text = InnerTagClearUtil.clearXliffTag4Tmx(text);// 不在导入时清理
segment.setFullText(text);
}
segment.setPureText(pureText);
if (lang.equalsIgnoreCase(header.getSrclang())) {
tu.setSource(segment);
} else {
tu.appendSegement(segment);
}
}
vn.pop();
}
vn.pop();
}
use of net.heartsome.cat.common.bean.TmxSegement in project translationstudio8 by heartsome.
the class DBOperator method deleteTerm.
/**
* 删除TermEntry,删除BTERMENTRY表中的记录
* @param termEntryId
* Term Entry在表中的主键
* @param lang
* 语言
* @throws SQLException
* ;
*/
public void deleteTerm(String termEntryId, String lang) throws SQLException {
PreparedStatement stmt = null;
try {
// 删除martrix_lang表中的记录
List<TmxSegement> textDataPk = getTextDataIdByGroupIdLang(Integer.parseInt(termEntryId), "B", lang);
if (textDataPk.size() == 0) {
return;
}
String set = textDataPk.get(0).getDbPk() + "";
for (int i = 1; i < textDataPk.size(); i++) {
set = set + "," + textDataPk.get(i).getDbPk();
}
deleteMatrxLang(set, lang);
// 删除TextData表中的记录
String sql = dbConfig.getOperateDbSQL("delete-textData-bygoupid");
stmt = conn.prepareStatement(sql);
stmt.setInt(1, Integer.parseInt(termEntryId));
stmt.setString(2, lang);
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
}
}
Aggregations