Changeset 28384

Show
Ignore:
Timestamp:
10.10.2013 21:12:21 (6 years ago)
Author:
ak19
Message:

1. Fix: 3rd constructor had its arguments in the wrong order. 2. Added support for custom cmd-line parameters that are to be passed on into the XSLT. This is necessary to allow the xsd-to-mds.xsl XSLT that Jenny recently added to be more general.

Files:
1 modified

Legend:

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

    r25285 r28384  
    2929 
    3030import java.io.*; 
     31import java.util.HashMap; 
     32import java.util.Iterator; 
     33import java.util.Map; 
     34import java.util.Map.Entry; 
    3135 
    3236import javax.xml.transform.Transformer; 
     
    6670    private String sourcelang; 
    6771    private String targetlang; 
    68  
    69     public ApplyXSLT(String sourcelang, String targetlang){ 
    70     initLanguages(sourcelang, targetlang); 
     72    private HashMap paramMap; 
     73 
     74    public ApplyXSLT(String sourcelang, String targetlang, HashMap param_map){ 
     75    initParams(sourcelang, targetlang, param_map); 
    7176    } 
    7277    
    73     public ApplyXSLT(String xsl_file, String sourcelang, String targetlang) 
     78    public ApplyXSLT(String xsl_file, String sourcelang, String targetlang, HashMap param_map) 
    7479  { 
    7580    this.xsl_file = xsl_file;  
    76     initLanguages(sourcelang, targetlang); 
    77   } 
    78  
    79     public ApplyXSLT(String xsl_file, String sourcelang, String targetlang, String mapping_file) { 
     81    initParams(sourcelang, targetlang, param_map); 
     82  } 
     83 
     84    public ApplyXSLT(String xsl_file, String mapping_file, String sourcelang, String targetlang, HashMap param_map) { 
    8085    this.xsl_file = xsl_file;  
    8186    this.mapping_file = mapping_file; 
    82     initLanguages(sourcelang, targetlang); 
    83   } 
    84  
    85     private void initLanguages(String sourcelang, String targetlang) 
     87    initParams(sourcelang, targetlang, param_map); 
     88  } 
     89 
     90    private void initParams(String sourcelang, String targetlang, HashMap param_map) 
    8691    { 
    8792    this.sourcelang = sourcelang; 
     
    9196        this.sourcelang = "en"; 
    9297    } 
     98 
     99    // any custom parameters to be passed into the XSLT would be in the map by now 
     100    paramMap = param_map; 
    93101    } 
    94102 
     
    170178    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 
    171179 
    172     setTransformerLanguageParams(transformer); // sourcelang and targetlang 
     180    setTransformerParams(transformer); // sourcelang and targetlang and any further custom parameters to be passed into the XSLT 
    173181 
    174182    transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); 
     
    189197         
    190198      transformer.setParameter("mapping",mapping); 
    191       setTransformerLanguageParams(transformer); // sourcelang and targetlang 
     199      setTransformerParams(transformer); // sourcelang and targetlang and any further custom parameters to be passed into the XSLT 
    192200         
    193201      Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 
     
    305313  }  
    306314 
    307     private void  setTransformerLanguageParams(Transformer transformer)  
     315    private void  setTransformerParams(Transformer transformer)  
    308316    { 
    309317    if(targetlang != "") { 
    310318        transformer.setParameter("sourcelang",sourcelang); 
    311319        transformer.setParameter("targetlang",targetlang); 
    312     }   
     320    } 
     321 
     322    // handle any custom parameters that are also to be passed into the XSLT 
     323    Iterator i = paramMap.entrySet().iterator();  
     324    while(i.hasNext()) { 
     325        Map.Entry entry = (Map.Entry)i.next(); 
     326        String paramName = (String)entry.getKey(); 
     327        String paramValue = (String)entry.getValue(); 
     328 
     329        transformer.setParameter(paramName, paramValue); 
     330    } 
     331     
    313332    } 
    314333 
     
    326345    } 
    327346 
    328     setTransformerLanguageParams(transformer); // sourcelang and targetlang 
     347    setTransformerParams(transformer); // sourcelang and targetlang and any further custom parameters to be passed into the XSLT 
    329348    transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output)); 
    330349         
     
    393412    String targetlang=""; 
    394413 
     414    HashMap paramMap = new HashMap(); 
     415    int index = -1; // index of the '=' sign in cmdline argument specifying custom parameters to be passed into the XSLT 
     416 
    395417    // Checking Arguments 
    396418    if(args.length < 1) 
     
    427449    printUsage(); 
    428450      } 
     451      else if ((index = args[i].indexOf("=")) != -1) { // custom parameters provided on the cmdline in the form paramName1=paramValue1 paramName2=paramValue2 etc  
     452      // that are to be passed into the XSLT 
     453      String paramName = args[i].substring(0, index); 
     454      String paramValue = args[i].substring(index+1); // skip the = sign 
     455      paramMap.put(paramName, paramValue); 
     456      index = -1; 
     457      } 
    429458      else{ 
    430459    printUsage(); 
    431460      } 
    432            
     461       
    433462    } 
    434463 
     
    438467    if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line 
    439468      if (mapping_file.equals("")){  
    440       core = new ApplyXSLT(xsl_file, sourcelang, targetlang); 
     469      core = new ApplyXSLT(xsl_file, sourcelang, targetlang, paramMap); 
    441470      } 
    442471      else{ 
    443       core = new ApplyXSLT(xsl_file,mapping_file, sourcelang, targetlang);   
     472      core = new ApplyXSLT(xsl_file, mapping_file, sourcelang, targetlang, paramMap);   
    444473      } 
    445474             
     
    452481    } 
    453482    else if(!xml_file.equals("") && !xsl_file.equals("")){ 
    454     core = new ApplyXSLT(sourcelang, targetlang); 
     483    core = new ApplyXSLT(sourcelang, targetlang, paramMap); 
    455484      try { 
    456485    core.translate(xml_file,xsl_file,output_file);  
     
    473502   
    474503  private  static void printUsage(){ 
    475     System.out.println("Usage: ApplyXSLT -x File -t File [-m File] [-o File] [-s sourcelang] [-l targetlang]"); 
     504    System.out.println("Usage: ApplyXSLT -x File -t File [-m File] [-o File] [-s sourcelang] [-l targetlang] [param-name=param-value]"); 
    476505    System.out.println("\t-x specifies the xml file (Note: optional for piped xml data)"); 
    477506    System.out.println("\t-t specifies the xsl file"); 
     
    480509    System.out.println("\t-s specifies the input language code for generating TMX file. Defaults to 'en' if none is provided"); 
    481510    System.out.println("\t-l specifies the output language code. Required if generating a TMX file."); 
     511    System.out.println("\tFor general transformations of an XML by an XSLT, you can pass in parameter name=value pairs if any need to passed on into the XSLT as xsl params."); 
    482512    System.exit(-1); 
    483513  }