Changeset 25241

Show
Ignore:
Timestamp:
21.03.2012 18:23:44 (8 years ago)
Author:
ak19
Message:

Updated ApplyXSLT to work with the 2 new XSLT files gti-generate-tmx-xml and gti-tmx-to-txt which require an additional parameter (the targetlanguage) and further take an optional parameter (source language).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java

    r22736 r25241  
    6464  private String mapping_file; 
    6565 
    66   public ApplyXSLT(){} 
     66    private String sourcelang; 
     67    private String targetlang; 
     68 
     69    public ApplyXSLT(String sourcelang, String targetlang){ 
     70    initLanguages(sourcelang, targetlang); 
     71    } 
    6772    
    68   public ApplyXSLT(String xsl_file) 
     73    public ApplyXSLT(String xsl_file, String sourcelang, String targetlang) 
    6974  { 
    7075    this.xsl_file = xsl_file;  
    71   } 
    72  
    73   public ApplyXSLT(String xsl_file,String mapping_file){ 
     76    initLanguages(sourcelang, targetlang); 
     77  } 
     78 
     79    public ApplyXSLT(String xsl_file, String sourcelang, String targetlang, String mapping_file) { 
    7480    this.xsl_file = xsl_file;  
    7581    this.mapping_file = mapping_file; 
    76   } 
     82    initLanguages(sourcelang, targetlang); 
     83  } 
     84 
     85    private void initLanguages(String sourcelang, String targetlang) 
     86    { 
     87    this.sourcelang = sourcelang; 
     88    this.targetlang = targetlang; 
     89    // if only target language is provided, assume source language is English 
     90    if(sourcelang.equals("") && !targetlang.equals("")) { 
     91        this.sourcelang = "en"; 
     92    } 
     93    } 
    7794 
    7895  private boolean process() 
     
    152169    TransformerFactory tFactory = TransformerFactory.newInstance(); 
    153170    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 
     171 
     172    setTransformerLanguageParams(transformer); // sourcelang and targetlang 
     173 
    154174    transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); 
    155175    return true; 
     
    169189         
    170190      transformer.setParameter("mapping",mapping); 
     191      setTransformerLanguageParams(transformer); // sourcelang and targetlang 
    171192         
    172193      Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 
     
    284305  }  
    285306 
     307    private void  setTransformerLanguageParams(Transformer transformer)  
     308    { 
     309    if(targetlang != "") { 
     310        transformer.setParameter("sourcelang",sourcelang); 
     311        transformer.setParameter("targetlang",targetlang); 
     312    }   
     313    } 
    286314 
    287315  private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{              
     
    297325      output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8");  
    298326    } 
    299         
     327 
     328    setTransformerLanguageParams(transformer); // sourcelang and targetlang 
    300329    transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output)); 
    301330         
     
    315344    String mapping_file=""; 
    316345    String output_file=""; 
    317        
     346 
     347    String sourcelang=""; 
     348    String targetlang=""; 
     349 
    318350    // Checking Arguments 
    319351    if(args.length < 1) 
     
    339371         
    340372      } 
     373      // The two language parameters (-s and -l) are for the gti-generate-tmx-xml file  
     374      // which requires the target lang (code), and will accept the optional source lang (code) 
     375      else if(args[i].equals("-s") && i+1 < args.length && !args[i+1].startsWith("-")){ 
     376    sourcelang = args[++i];      
     377      } 
     378      else if(args[i].equals("-l") && i+1 < args.length && !args[i+1].startsWith("-")){ 
     379    targetlang = args[++i];      
     380      } 
    341381      else if(args[i].equals("-h")){ 
    342382    printUsage(); 
     
    353393    if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line 
    354394      if (mapping_file.equals("")){  
    355     core = new ApplyXSLT(xsl_file); 
     395      core = new ApplyXSLT(xsl_file, sourcelang, targetlang); 
    356396      } 
    357397      else{ 
    358     core = new ApplyXSLT(xsl_file,mapping_file);   
     398      core = new ApplyXSLT(xsl_file,mapping_file, sourcelang, targetlang);   
    359399      } 
    360400             
     
    367407    } 
    368408    else if(!xml_file.equals("") && !xsl_file.equals("")){ 
    369       core = new ApplyXSLT(); 
     409    core = new ApplyXSLT(sourcelang, targetlang); 
    370410      try { 
    371411    core.translate(xml_file,xsl_file,output_file);  
     
    388428   
    389429  private  static void printUsage(){ 
    390     System.out.println("Usage:ApplyXSLT  -x File -t File [-m File] [-o File]"); 
     430    System.out.println("Usage: ApplyXSLT -x File -t File [-m File] [-o File] [-s sourcelang] [-l targetlang]"); 
    391431    System.out.println("\t-x specifies the xml file (Note: optional for piped xml data)"); 
    392432    System.out.println("\t-t specifies the xsl file"); 
    393433    System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)"); 
    394434    System.out.println("\t-o specifies the output file name (output to screen if this option is absent)"); 
     435    System.out.println("\t-s specifies the input language code for generating TMX file. Defaults to 'en' if none is provided"); 
     436    System.out.println("\t-l specifies the output language code. Required if generating a TMX file."); 
    395437    System.exit(-1); 
    396438  }