/** * tuToMultiLingualObject converts a given tu element into a MultiLingualObject * * @param tu the tu Element * @return the MultiLingualObject for the given tu */ public MultiLingualObject tuToMultiLingualObject(Element tu) { LinguisticProperties linguisticProperties = createLinguisticProperties(tu); MultiLingualObject multi = new MultiLingualObject(linguisticProperties, LinguisticTypes.TMX); LinguisticProperty tuProp = new LinguisticProperty("tu", tu); multi.addLinguisticProperty(tuProp); String uniquidvalue = (String) linguisticProperties.search("unique-id"); if ((uniquidvalue != null) && !uniquidvalue.equals("")) { multi.setStUniqueID(uniquidvalue); } String stlastAccessTime = (String) linguisticProperties.search("lastAccessTime"); if ((stlastAccessTime != null) && !stlastAccessTime.equals("")) { try { long lastAccessTime = Long.parseLong(stlastAccessTime); multi.setLastAccessTime(lastAccessTime); } catch (NumberFormatException e) { } } List<Element> tuvs = this.getTuvList(tu); for (int i = 0; i < tuvs.size(); i++) { MonoLingualObject mono = tuvToMonoLingualObject(tuvs.get(i)); if (mono != null) multi.addMonoLingualObject(mono); } return multi; }
/** * main * * @param args */ public static void main(String[] args) { try { String tmxfile = args[0]; Timer timer = new Timer(); timer.startTimer(); File f = new File(tmxfile); TmxDocument doc = new TmxDocument(); // load the xml file doc.loadXmlFile(f); timer.stopTimer(); System.out.println( timer.timerString("TMX file read " + tmxfile + ": Version " + doc.getTmxVersion())); timer.startTimer(); // save it doc.saveToXmlFile(tmxfile + ".copy.tmx"); timer.stopTimer(); Element header = doc.getTmxHeader(); System.out.println("TMX Header:\n" + doc.createLinguisticProperties(header).format()); System.out.println( timer.timerString("TMX file save " + tmxfile + ": Version " + doc.getTmxVersion())); System.out.println("TMX file #tuvs = " + doc.getTuList().size()); for (int i = 0; i < doc.getTuList().size(); i++) { Element tu = doc.getTuList().get(i); String tustring = doc.elementToString(tu); System.out.println("TUV string (" + i + ") = \"" + tustring + "\""); List<Element> tuvs = doc.getTuvList(tu); for (int j = 0; j < tuvs.size(); j++) { Element tuv = tuvs.get(j); Element seg = doc.getSeg(tuv); System.out.println("\tTU string a (" + j + ") = \"" + doc.elementToString(seg) + "\""); System.out.println( "\tTU string b (" + j + ") = \"" + doc.elementContentToString(seg) + "\""); } MultiLingualObject multi = doc.tuToMultiLingualObject(tu); System.out.println(multi.format()); } doc = null; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }
/** * saveModifiedLinguisticProperties * * @param multiLingualObject */ @SuppressWarnings("unchecked") public Element saveModifiedLinguisticProperties(MultiLingualObject multiLingualObject) { LinguisticProperty lingtu = multiLingualObject.getObjectLinguisticProperty("tu"); Element tu = (Element) lingtu.getValue(); if (tu == null) return tu; LinguisticProperties linguisticProperties = multiLingualObject.getLinguisticProperties(); if (linguisticProperties == null) return tu; Set<String> enumprop = linguisticProperties.keySet(); Iterator<String> it = enumprop.iterator(); while (it.hasNext()) { String key = (String) it.next(); Object value = (Object) linguisticProperties.get(key); Object data = null; TmxProp tmxProp = null; LinguisticProperty ling = null; if (value.getClass().getName().equals("java.util.Vector")) { for (int i = 0; i < ((Vector<LinguisticProperty>) value).size(); i++) { ling = ((Vector<LinguisticProperty>) value).get(i); if (ling.getPropStatus().equals(LinguisticProperty.PropStatus.NEW)) continue; Object lingValue = ling.getValue(); String classname = lingValue.getClass().getName(); if (classname.equals("de.folt.models.documentmodel.tmx.TmxProp")) { tmxProp = (TmxProp) lingValue; data = ling.getData(); handleTmxProp(tmxProp, ling, tu, data); } } } else { ling = (LinguisticProperty) linguisticProperties.get(key); Object lingValue = ling.getValue(); String classname = lingValue.getClass().getName(); if (classname.equals("de.folt.models.documentmodel.tmx.TmxProp")) { if (ling.getPropStatus().equals(LinguisticProperty.PropStatus.OLD)) continue; tmxProp = (TmxProp) lingValue; data = ling.getData(); handleTmxProp(tmxProp, ling, tu, data); } } } return tu; }