找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 3106|回复: 8

[练手习作]TMX与Excel互相转换与单独模版生成程序

[复制链接]
发表于 2006-5-2 17:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

×
挺冷清的,发一个前一段给邻居做的小程序,活跃一下学习气氛,呵呵


翻译记忆交换标准 (TMX)
--------------------------------------------------------------

翻译记忆交换标准的 TMX 是“ T ranslation M emory e X change ”的缩写。 TMX 是中立的、开放的 XML 标准之一,它的目的是促进不同计算机辅助翻译( CAT )和本地化工具创建的翻译记忆( TM )数据交换。遵守 TMX 标准,不同工具、不同本地化公司创建的翻译记忆文件可以很方便的交换翻译记忆数据。

TMX标准的最初讨论需要追溯到1997 年 6 月,当年参加本地化行业标准协会(LISA)会议的与会者,包括本地化客户、工具提供商及本地化服务提供商召开了一个小型的会议,针对与日俱增的本地化工具的翻译记忆数据不兼容问题进行了讨论,会后这些成员形成了 LLSA 的一个专门团体,即 OSCAR(Open Standards for Container/Content Allowing Re-use),而 TMX 规范正是 OSCAR 的一个最重要的成果。

TMX 发布于 1998 年,是第一个 XML 标准。至今全球已经有 20 万用户正在应用这个标准。 TMX 标准实行认证方式应用,本地化工具开发公司的本地化工具如果通过了 LISA 的 TMX 标准测试,可以在本地化产品中加上TMX 图标。本地化开发商为了使产品符合行业标准,纷纷推出通过 TMX 认证的工具,否则很难被本地化服务公司购买和使用。

TMX规范的下载页面:
http://www.lisa.org/standards/tmx/specification.html


软件目标:
---------------------------------------------

程序的目的很简单,就是能够实现TMX与Excel之间的互相转换,以方便用户在不同环境下的使用(有无特定编辑器,语法检查器等等),以最大限度的利用不同软件的强项和方便与别人进行文件交换。也可以生成空的带有如果预设值的Excel和TMX文件。

软件设计思路与分析:
------------------------------------------------
本程序为个人版定制开发,所以预设值都是写死的,而且由于功能及其单一,无需图形界面功能,所以只提供命令行界面。而且由于功能要求有限,并且对规范的特性应用很有限(只用了一小部分tmx1.1的特性),不需要作过多的灵活性处理。

技术选择与实现思路:
----------------------------------------------------
小程序一个,无需考虑过多,为方便调试,使用log4j,xml操作使用jdom,excel操作使用apache的poi。
为方便命令行执行,要能够在原文件名后加入日期和时间,再加上新的文件类型后缀。
为方便windows鼠标操作,计划通过注册表文件来提供右键菜单功能,不过这个没实现,用户没要求,可以留待以后继续完善。

程序部署方式与执行方法的考虑与设想
------------------------------------------------------
使用java可执行jar方式,可方便拷贝执行。只要机器上有java运行环境,即可以用命令行执行。另外由于要操作TMX文件,xml解析器要求jar文件同目录下(其实是当前工作目录或path,只是放在同一目录下更方便)必须有tmx11.dtd。
命令行用法
TMX2XLS Usage:                                                                 
                                                                              
Invocation:                                                                    
java -jar tmx2xls.jar %parameters%                                             
or                                                                             
java -cp tmx2xls.jar com.greenflute.tmx.TMX2XLS %parameters%                  
                                                                              
Parameters:                                                                    
1st param: 2xls|2tmx|newxls|newtmx                                             
2nd param: <source file name>|<full path file name>                           
           when newtmx or newxls was used, this file will be created.         
3rd param: optional, <target file name>|<full path file name>,                 
           autogenerated filename: <source file name>.yyyymmddhhmiss.[tmx|xls]
                                                                              
Reg file:                                                                     
Adds a context menu for tmx|xls file, only for convience under Windows.        
                                                                              
Warning:                                                                       
It's a free tool, no guarantee for data corruption or errors.                  



程序设计与分析
----------------------------------------------------
为方便处理命令行和最大限度的代码重用,以及功能与控制分开的目的(方便以后转变为其他界面或执行方式,或加入其他软件包),程序分为两个类。

TMX2XLS作为Controller,负责处理命令行输入,usage显示,和程序主体部分的执行流程,用一段类似于factory method的代码来控制不同类型文件的解析和生成,保存。提供3个方法,main,usage,和getUniqName用来生成唯一文件名。

TMX作为实际的功能类,其生命周期由TMX2XLS控制,只提供两大类4个核心功能方法(parseTMX,saveTMX: parseXLS,saveXLS),核心的数据载体用一个Hashtable来保存文件属性,一个ArrayList(成员为String数组)来保存实际的多语言国际化数据。通过这两个载体实现数据的双向转换,即:无论来源文件是什么,都先转换为结构无关的中心数据载体,再进行后续操作。生成新文件也是如此,先操作中心数据载体,再进行后续工作。这样做使程序结构和控制逻辑简化了许多。
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2006-5-2 17:16 | 显示全部楼层

TMX2XLS.java


  1. package com.greenflute.tmx;

  2. import org.apache.log4j.Logger;
  3. import org.apache.log4j.PropertyConfigurator;

  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.Calendar;
  7. import java.util.Properties;

  8. /**
  9. * TMX2XLS v0.1
  10. * <p/>
  11. * convert tmx file to xls, or vice versa
  12. * only work with tmx11.dtd, and only very simple functions were provided
  13. * <p/>
  14. * XLS Fromat Specification
  15. * 1st sheet:
  16. * Name: body
  17. * Content: body of tmx file
  18. * Columns: 2
  19. * 1st Column: Source language content
  20. * 2st Column: Target language content
  21. * Rows: 2+
  22. * 1st Row: Label of language name
  23. * 2nd Row and following: translation content,
  24. * every row is a tu,
  25. * every cell is a tuv and embeding seg
  26. * last row: tmx2xls tmx2xls
  27. * <p/>
  28. * 2nd sheet:
  29. * Name: header
  30. * Content: tmx header infos
  31. * 1st Column: header name
  32. * 2st Column: header content
  33. * Rows: fixed
  34. * 1: tmx2xls   tmx2xls
  35. * 2: xml.version   1.0
  36. * 3: xml.encoding  UTF-8
  37. * <?xml version="1.0" encoding="UTF-8"?>
  38. * 4: tmx.doctype
  39. * <!DOCTYPE tmx SYSTEM "tmx11.dtd">
  40. * 5: tmx.version   1.1
  41. * 6: header.creationtool   TMX2XLS
  42. * 7: header.creationtoolversion    0.1
  43. * 8: header.segtype    sentence
  44. * segtype="sentence"
  45. * 9: header.o-tmf  TMX2XLS
  46. * 10: header.adminlang EN-US
  47. * adminlang="EN-US"
  48. * 11: srclang  xxx
  49. * it's besser to make a reference formula mit cell body.A1
  50. * 12: header.datatype plaintext
  51. * datatype="plaintext"
  52. */
  53. public class TMX2XLS {
  54.     /**
  55.      * 1st param: 2xls or 2tmx
  56.      * 2nd param: original file name
  57.      * 3rd param: optional, target file name, when not provided, file name will be: originalname.yyyymmddhhmiss.[tmx|xls]
  58.      *
  59.      * @param args
  60.      */
  61.     public static void main(String[] args) {       //todo popup dialog to show error
  62.         //init logger
  63.         logger = Logger.getLogger(TMX2XLS.class);
  64.         Properties prop = new Properties();
  65.         try {
  66.             prop.load(TMX2XLS.class.getResourceAsStream("/log4j.properties"));
  67.             PropertyConfigurator.configure(prop);
  68.         } catch (IOException e) {
  69.             //not a fatal error!
  70.             e.printStackTrace();
  71.         }
  72.         prop = null;

  73.         int task = NOTIMPLAMENTED;
  74.         String srcname = null, targetname = null;

  75.         if (args.length >= 1 && args.length <= 3) {
  76.             //validate the command parameter
  77.             task = args[0].equalsIgnoreCase(TOTMX_TEXT) ? TOTMX :
  78.                     args[0].equalsIgnoreCase(TOXLS_TEXT) ? TOXLS :
  79.                     args[0].equalsIgnoreCase(NEWTMX_TEXT) ? NEWTMX :
  80.                     args[0].equalsIgnoreCase(NEWXLS_TEXT) ? NEWXLS : NOTIMPLAMENTED;

  81.             //validate file existence
  82.             if (task == TOTMX || task == TOXLS) {
  83.                 //must provide at least source file
  84.                 if (args.length >= 2) {
  85.                     //srcname
  86.                     if (!new File(args[1]).exists()) {
  87.                         task = NOTIMPLAMENTED;
  88.                         logger.error("Source file must exist!");
  89.                     } else {
  90.                         srcname = args[1];

  91.                         //targetname
  92.                         if (args.length == 3) {
  93.                             if (new File(args[2]).exists()) {
  94.                                 task = NOTIMPLAMENTED;
  95.                                 logger.error("Target file exists, choose another name!");
  96.                             } else {
  97.                                 targetname = args[2];
  98.                             }
  99.                         } else {
  100.                             //make a new name
  101.                             targetname = args[1].substring(0, args[1].length() - 4) + "." +
  102.                                     getUniqName() + (task == TOTMX ? ".tmx" : ".xls");
  103.                         }
  104.                     }
  105.                 } else {
  106.                     task = NOTIMPLAMENTED;
  107.                     logger.error("Please specify source file!");
  108.                 }

  109.             } else if (task == NEWTMX || task == NEWXLS) {
  110.                 if (args.length == 1) {
  111.                     targetname = getUniqName() + (task == NEWTMX ? ".tmx" : ".xls");
  112.                 } else {
  113.                     if (new File(args[1]).exists()) {
  114.                         task = NOTIMPLAMENTED;
  115.                         logger.error("Target file exist, choose another name!");
  116.                     } else {
  117.                         targetname = args[1];
  118.                     }
  119.                 }
  120.             }
  121.         }

  122.         //realwork start here
  123.         if (task != NOTIMPLAMENTED) {
  124.             TMX tmx = new TMX();
  125.             //parse
  126.             if (task == TOTMX) {
  127.                 tmx.parseXLS(srcname);
  128.             } else if (task == TOXLS) {
  129.                 tmx.parseTMX(srcname);
  130.             }

  131.             //write
  132.             if (task == TOTMX || task == NEWTMX) {
  133.                 tmx.saveTMX(targetname);
  134.             } else if (task == TOXLS || task == NEWXLS) {
  135.                 tmx.saveXLS(targetname);
  136.             }
  137.         } else {
  138.             //usage
  139.             usage();
  140.         }


  141.         logger.debug("" + task);
  142.         logger.debug(srcname);
  143.         logger.debug(targetname);

  144.     }


  145.     private static void usage() {
  146.         System.out.println("*******************************************************************************");
  147.         System.out.println("TMX2XLS Usage:                                                                 ");
  148.         System.out.println("                                                                               ");
  149.         System.out.println("Invocation:                                                                    ");
  150.         System.out.println("java -jar tmx2xls.jar %parameters%                                             ");
  151.         System.out.println("or                                                                             ");
  152.         System.out.println("java -cp tmx2xls.jar com.greenflute.tmx.TMX2XLS %parameters%                   ");
  153.         System.out.println("                                                                               ");
  154.         System.out.println("Parameters:                                                                    ");
  155.         System.out.println("1st param: 2xls|2tmx|newxls|newtmx                                             ");
  156.         System.out.println("2nd param: <source file name>|<full path file name>                            ");
  157.         System.out.println("           when newtmx or newxls was used, this file will be created.          ");
  158.         System.out.println("3rd param: optional, <target file name>|<full path file name>,                 ");
  159.         System.out.println("           autogenerated filename: <source file name>.yyyymmddhhmiss.[tmx|xls] ");
  160.         System.out.println("                                                                               ");
  161.         System.out.println("Reg file:                                                                      ");
  162.         System.out.println("Adds a context menu for tmx|xls file, only for convience under Windows.        ");
  163.         System.out.println("                                                                               ");
  164.         System.out.println("Warning:                                                                       ");
  165.         System.out.println("It's a free tool, no guarantee for data corruption or errors.                  ");
  166.         System.out.println("*******************************************************************************");
  167.     }

  168.     private static String getUniqName() {
  169.         Calendar cal = Calendar.getInstance();

  170.         return cal.get(Calendar.YEAR) +
  171.                 ((cal.get(Calendar.MONTH) + 1) <= 9 ? ("0" + (cal.get(Calendar.MONTH) + 1)) : (cal.get(Calendar.MONTH) + 1) + "") +
  172.                 (cal.get(Calendar.DAY_OF_MONTH) <= 9 ? ("0" + cal.get(Calendar.DAY_OF_MONTH)) : cal.get(Calendar.DAY_OF_MONTH) + "") +
  173.                 (cal.get(Calendar.HOUR_OF_DAY) <= 9 ? ("0" + cal.get(Calendar.HOUR_OF_DAY)) : cal.get(Calendar.HOUR_OF_DAY) + "") +
  174.                 (cal.get(Calendar.MINUTE) <= 9 ? ("0" + cal.get(Calendar.MINUTE)) : cal.get(Calendar.MINUTE) + "") +
  175.                 (cal.get(Calendar.SECOND) <= 9 ? ("0" + cal.get(Calendar.SECOND)) : cal.get(Calendar.SECOND) + "");

  176.     }

  177.     private static Logger logger = null;
  178.     private static final String TOTMX_TEXT = "2tmx";
  179.     private static final String TOXLS_TEXT = "2xls";
  180.     private static final String NEWTMX_TEXT = "newtmx";
  181.     private static final String NEWXLS_TEXT = "newxls";
  182.     private static final int TOTMX = 1;
  183.     private static final int TOXLS = 2;
  184.     private static final int NEWTMX = 3;
  185.     private static final int NEWXLS = 4;
  186.     private static final int NOTIMPLAMENTED = 0;
  187. }

复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:17 | 显示全部楼层

主控业务逻辑


  1. //realwork start here
  2.         if (task != NOTIMPLAMENTED) {
  3.             TMX tmx = new TMX();
  4.             //parse
  5.             if (task == TOTMX) {
  6.                 tmx.parseXLS(srcname);
  7.             } else if (task == TOXLS) {
  8.                 tmx.parseTMX(srcname);
  9.             }

  10.             //write
  11.             if (task == TOTMX || task == NEWTMX) {
  12.                 tmx.saveTMX(targetname);
  13.             } else if (task == TOXLS || task == NEWXLS) {
  14.                 tmx.saveXLS(targetname);
  15.             }
复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:18 | 显示全部楼层

TMX.java


  1. package com.greenflute.tmx;

  2. import org.apache.poi.hssf.usermodel.HSSFSheet;
  3. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  4. import org.apache.poi.hssf.usermodel.HSSFRow;
  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.log4j.Logger;
  7. import org.apache.log4j.PropertyConfigurator;
  8. import org.jdom.Document;
  9. import org.jdom.JDOMException;
  10. import org.jdom.Element;
  11. import org.jdom.DocType;
  12. import org.jdom.input.SAXBuilder;
  13. import org.jdom.output.XMLOutputter;
  14. import org.jdom.output.Format;

  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.*;

  19. public class TMX {
  20.     private Hashtable properties = null;
  21.     private ArrayList segments = null;

  22.     public TMX() {
  23.         logger = Logger.getLogger(TMX.class);
  24.         properties = new Hashtable();
  25.         segments = new ArrayList();
  26.     }

  27.     public void parseTMX(String filename) {
  28.         try {
  29.             SAXBuilder builder = new SAXBuilder();
  30.             Document document = builder.build(filename);

  31.             //doctpye
  32.             DocType doctype = document.getDocType();
  33.             properties.put("doctype.name", doctype.getElementName());
  34.             properties.put("doctpye.systemid", doctype.getSystemID());

  35.             logger.debug(doctype.getElementName());
  36.             logger.debug(doctype.getPublicID());
  37.             logger.debug(doctype.getSystemID());
  38.             logger.debug(doctype.toString());

  39.             //root element
  40.             Element root = document.getRootElement();
  41.             properties.put("tmx.version", root.getAttributeValue("version"));

  42.             //header
  43.             Element header = root.getChild("header");
  44.             properties.put("header.creationtool", header.getAttributeValue("creationtool"));
  45.             properties.put("header.creationtoolversion", header.getAttributeValue("creationtoolversion"));
  46.             properties.put("header.segtype", header.getAttributeValue("segtype"));
  47.             properties.put("header.o-tmf", header.getAttributeValue("o-tmf"));
  48.             properties.put("header.adminlang", header.getAttributeValue("adminlang"));
  49.             properties.put("header.srclang", header.getAttributeValue("srclang"));
  50.             properties.put("header.datatype", header.getAttributeValue("datatype"));


  51.             //body
  52.             Element body = root.getChild("body");
  53.             List tus = body.getChildren();
  54.             Iterator it = tus.iterator();

  55.             //loop through every tu,tuv,seg
  56.             List tuvs = null;
  57.             String src = null, target = null;
  58.             Object temp = null;
  59.             Element tu = null;
  60.             boolean targetlangok = false;   //targetlanguage
  61.             while (it.hasNext()) {
  62.                 tu = (Element) it.next();

  63.                 tuvs = tu.getChildren();
  64.                 temp = tuvs.get(0);
  65.                 src = (temp == null) ? "" : ((Element) (temp)).getChild("seg").getText();

  66.                 temp = tuvs.get(1);
  67.                 target = (temp == null) ? "" : ((Element) (temp)).getChild("seg").getText();

  68.                 if (!targetlangok) {
  69.                     properties.put("header.targetlang", ((Element) (temp)).getAttributeValue("lang"));
  70.                     targetlangok = true;
  71.                 } else {
  72.                     segments.add(new String[]{src, target});
  73.                 }
  74.             }

  75.             document = null;
  76.             builder = null;
  77.         } catch (JDOMException e) {
  78.             logger.error("Error occured in parsing TMX file!", e);
  79.         } catch (IOException e) {
  80.             logger.error("Error occured in parsing TMX file!", e);
  81.         }
  82.     }

  83.     public void saveTMX(String filename) {
  84.         Document document = new Document();
  85.         Object temp = null;
  86.         String src = null, target = null; //language

  87.         //doctype
  88.         temp = properties.get("doctpye.name");
  89.         DocType doctype = new DocType(temp == null ? "tmx" : (String) temp);
  90.         temp = properties.get("doctpye.systemid");
  91.         doctype.setSystemID(temp == null ? "tmx11.dtd" : (String) temp);
  92.         document.setDocType(doctype);

  93.         //root

  94.         Element root = document.setRootElement(new Element("tmx")).getRootElement();
  95.         temp = properties.get("tmx.version");
  96.         root.setAttribute("version", (temp == null ? "1.1" : (String) temp));

  97.         //header
  98.         Element header = new Element("header");
  99.         temp = properties.get("header.creationtool");
  100.         header.setAttribute("creationtool", temp == null ? "TMX2XLS" : (String) temp);
  101.         temp = properties.get("header.creationtoolversion");
  102.         header.setAttribute("creationtoolversion", temp == null ? "0.1" : (String) temp);
  103.         temp = properties.get("header.segtype");
  104.         header.setAttribute("segtype", temp == null ? "sentence" : (String) temp);
  105.         temp = properties.get("header.o-tmf");
  106.         header.setAttribute("o-tmf", temp == null ? "TMX2XLS" : (String) temp);
  107.         temp = properties.get("header.adminlang");
  108.         header.setAttribute("adminlang", temp == null ? "EN-US" : (String) temp);
  109.         temp = properties.get("header.srclang");
  110.         header.setAttribute("srclang", temp == null ? "DE" : (String) temp);
  111.         src = temp == null ? "DE" : (String) temp;     //srclanguage
  112.         temp = properties.get("header.datatype");
  113.         header.setAttribute("datatype", temp == null ? "plaintext" : (String) temp);
  114.         root.addContent(header);

  115.         temp = properties.get("header.targetlang");
  116.         target = temp == null ? "LT" : (String) temp;  //targetlanguage

  117.         //body
  118.         Element body = new Element("body");
  119.         Element tu = null, tuv = null;
  120.         String[] sentence = null;
  121.         for (int i = 0; i < segments.size(); i++) {
  122.             sentence = (String[]) segments.get(i);
  123.             tu = new Element("tu");
  124.             //src tuv
  125.             tuv = new Element("tuv").
  126.                     setAttribute("lang", src).
  127.                     addContent(new Element("seg").setText(sentence[0]));
  128.             tu.addContent(tuv);

  129.             //target tuv
  130.             tuv = new Element("tuv").
  131.                     setAttribute("lang", target).
  132.                     addContent(new Element("seg").setText(sentence[1]));
  133.             tu.addContent(tuv);

  134.             //add to body
  135.             body.addContent(tu);
  136.         }
  137.         root.addContent(body);

  138.         try {
  139.             XMLOutputter outputter = new XMLOutputter();
  140.             outputter.setFormat(Format.getPrettyFormat());
  141.             outputter.output(document, new FileOutputStream(filename));

  142.             //outputter.output(document, System.out);

  143.             outputter = null;
  144.             document = null;
  145.         } catch (IOException e) {
  146.             logger.error("Error occured in saving TMX file!", e);
  147.         }
  148.     }

  149.     public void parseXLS(String filename) {
  150.         try {
  151.             HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(filename));
  152.             HSSFSheet header = book.getSheet("header");
  153.             HSSFRow row = null;

  154.             //check the file format
  155.             if (header == null) {
  156.                 throw new Exception("Invalid XLS Format, Can't transform to TMX file!");
  157.             }

  158.             Iterator it = header.rowIterator();
  159.             while (it.hasNext()) {
  160.                 row = (HSSFRow) it.next();
  161.                 properties.put(row.getCell((short) 0).getStringCellValue(),
  162.                         row.getCell((short) 1).getStringCellValue());
  163.                 logger.debug(row.getCell((short) 0).getStringCellValue());
  164.                 logger.debug(row.getCell((short) 1).getStringCellValue());
  165.             }

  166.             HSSFSheet body = book.getSheet("body");
  167.             it = body.rowIterator();
  168.             boolean targetlangok = false;   //targetlanguage
  169.             while (it.hasNext()) {
  170.                 //this is the first row!!
  171.                 if (!targetlangok) {
  172.                     properties.put("header.targetlang", ((HSSFRow) it.next()).getCell((short) 1).getStringCellValue());
  173.                     targetlangok = true;
  174.                 } else {
  175.                     row = (HSSFRow) it.next();
  176.                     segments.add(new String[]{row.getCell((short) 0).getStringCellValue(),
  177.                                               row.getCell((short) 1).getStringCellValue()});
  178.                     logger.debug(row.getCell((short) 0).getStringCellValue());
  179.                     logger.debug(row.getCell((short) 1).getStringCellValue());
  180.                 }
  181.             }
  182.         } catch (IOException e) {
  183.             logger.error("Error occured in parsing XLS file!", e);
  184.         } catch (Exception e) {
  185.             logger.error("Error occured in parsing XLS file!", e);
  186.         }
  187.     }

  188.     public void saveXLS(String filename) {
  189.         Object temp = null;
  190.         String src = null, target = null;

  191.         temp = properties.get("header.srclang");
  192.         src = temp == null ? "DE" : (String) temp;     //srclanguage

  193.         temp = properties.get("header.targetlang");
  194.         target = temp == null ? "LT" : (String) temp;  //targetlanguage

  195.         HSSFWorkbook book = new HSSFWorkbook();
  196.         HSSFSheet body = book.createSheet("body");

  197.         HSSFRow row = null;
  198.         HSSFCell cell = null;

  199.         //first row, language title
  200.         row = body.createRow(0);
  201.         row.createCell((short) 0).setCellValue(src);
  202.         row.createCell((short) 1).setCellValue(target);

  203.         //sentences
  204.         String[] sentence = null;
  205.         for (int i = 0; i < segments.size(); i++) {

  206.             sentence = (String[]) segments.get(i);

  207.             row = body.createRow(i + 1);

  208.             cell = row.createCell((short) 0);
  209.             cell.setEncoding((short) 1);
  210.             cell.setCellValue(sentence[0]);

  211.             cell = row.createCell((short) 1);
  212.             cell.setEncoding((short) 1);
  213.             cell.setCellValue(sentence[1]);

  214.             logger.debug(sentence[0]);
  215.             logger.debug(sentence[1]);
  216.         }

  217.         //header
  218.         HSSFSheet header = book.createSheet("header");

  219.         temp = properties.get("doctpye.name");
  220.         row = header.createRow(0);
  221.         row.createCell((short) 0).setCellValue("doctpye.name");
  222.         row.createCell((short) 1).setCellValue(temp == null ? "tmx" : (String) temp);

  223.         row = header.createRow(1);
  224.         temp = properties.get("doctpye.systemid");
  225.         row.createCell((short) 0).setCellValue("doctpye.systemid");
  226.         row.createCell((short) 1).setCellValue(temp == null ? "tmx11.dtd" : (String) temp);

  227.         row = header.createRow(2);
  228.         temp = properties.get("tmx.version");
  229.         row.createCell((short) 0).setCellValue("tmx.version");
  230.         row.createCell((short) 1).setCellValue(temp == null ? "1.1" : (String) temp);

  231.         row = header.createRow(3);
  232.         temp = properties.get("header.creationtool");
  233.         row.createCell((short) 0).setCellValue("header.creationtool");
  234.         row.createCell((short) 1).setCellValue(temp == null ? "TMX2XLS" : (String) temp);

  235.         row = header.createRow(4);
  236.         temp = properties.get("header.creationtoolversion");
  237.         row.createCell((short) 0).setCellValue("header.creationtoolversion");
  238.         row.createCell((short) 1).setCellValue(temp == null ? "0.1" : (String) temp);

  239.         row = header.createRow(5);
  240.         temp = properties.get("header.segtype");
  241.         row.createCell((short) 0).setCellValue("header.segtype");
  242.         row.createCell((short) 1).setCellValue(temp == null ? "sentence" : (String) temp);

  243.         row = header.createRow(6);
  244.         temp = properties.get("header.o-tmf");
  245.         row.createCell((short) 0).setCellValue("header.o-tmf");
  246.         row.createCell((short) 1).setCellValue(temp == null ? "TMX2XLS" : (String) temp);

  247.         row = header.createRow(7);
  248.         temp = properties.get("header.adminlang");
  249.         row.createCell((short) 0).setCellValue("header.adminlang");
  250.         row.createCell((short) 1).setCellValue(temp == null ? "EN-US" : (String) temp);

  251.         row = header.createRow(8);
  252.         //temp = properties.get("header.srclang");
  253.         row.createCell((short) 0).setCellValue("header.srclang");
  254.         row.createCell((short) 1).setCellFormula("body!a1");

  255.         row = header.createRow(9);
  256.         temp = properties.get("header.datatype");
  257.         row.createCell((short) 0).setCellValue("header.datatype");
  258.         row.createCell((short) 1).setCellValue(temp == null ? "plaintext" : (String) temp);

  259.         row = header.createRow(10);
  260.         row.createCell((short) 0).setCellValue("header.targetlang");
  261.         row.createCell((short) 1).setCellFormula("body!b1");

  262.         try {
  263.             book.write(new FileOutputStream(filename));
  264.         } catch (IOException e) {
  265.             logger.error("Error occured in saving XLS file!", e);
  266.         }
  267.     }

  268.     private static Logger logger = null;
  269. }

复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:19 | 显示全部楼层

自定义的Excel数据结构

* XLS Fromat Specification
* 1st sheet:
* Name: body
* Content: body of tmx file
* Columns: 2
* 1st Column: Source language content
* 2st Column: Target language content
* Rows: 2+
* 1st Row: Label of language name
* 2nd Row and following: translation content,
* every row is a tu,
* every cell is a tuv and embeding seg
* last row: tmx2xls tmx2xls
* <p/>
* 2nd sheet:
* Name: header
* Content: tmx header infos
* 1st Column: header name
* 2st Column: header content
* Rows: fixed
* 1: tmx2xls   tmx2xls
* 2: xml.version   1.0
* 3: xml.encoding  UTF-8
* <?xml version="1.0" encoding="UTF-8"?>
* 4: tmx.doctype
* <!DOCTYPE tmx SYSTEM "tmx11.dtd">
* 5: tmx.version   1.1
* 6: header.creationtool   TMX2XLS
* 7: header.creationtoolversion    0.1
* 8: header.segtype    sentence
* segtype="sentence"
* 9: header.o-tmf  TMX2XLS
* 10: header.adminlang EN-US
* adminlang="EN-US"
* 11: srclang  xxx
* it's besser to make a reference formula mit cell body.A1
* 12: header.datatype plaintext
* datatype="plaintext"
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:20 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:21 | 显示全部楼层

log4j.properties

差点儿忘了

  1. #OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL
  2. log4j.threshold=ALL

  3. log4j.rootLogger=,filelog

  4. # For appender named appenderName, set its class.
  5. # Note: The appender name can contain dots.
  6. log4j.appender.filelog=org.apache.log4j.DailyRollingFileAppender
  7. log4j.appender.filelog.File=${java.io.tmpdir}/TMX2XLS_log.html
  8. log4j.appender.filelog.DatePattern='.'yyyy-MM-dd'.html'

  9. For each named appender you can configure its Layout. The syntax for configuring an appender's layout is:
  10. log4j.appender.filelog.layout=org.apache.log4j.HTMLLayout
  11. log4j.appender.filelog.layout.Title=TMX2XLS


复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:24 | 显示全部楼层

jar打包

为了方便部署,将jdom,log4j,poi各包解开,与程序一起打包。

manifest

  1. Manifest-Version: 1.0
  2. Created-By: 1.4.2 (Sun Microsystems Inc.)
  3. Main-Class: com.greenflute.tmx.TMX2XLS

复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-2 17:25 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

Archiver|手机版|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+2, 2025-7-7 17:31 , Processed in 0.102338 second(s), 31 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表