Changeset 25241 for main/trunk/greenstone2/build-src/src/java
- Timestamp:
- 2012-03-21T18:23:44+13:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java
r22736 r25241 64 64 private String mapping_file; 65 65 66 public ApplyXSLT(){} 66 private String sourcelang; 67 private String targetlang; 68 69 public ApplyXSLT(String sourcelang, String targetlang){ 70 initLanguages(sourcelang, targetlang); 71 } 67 72 68 public ApplyXSLT(String xsl_file)73 public ApplyXSLT(String xsl_file, String sourcelang, String targetlang) 69 74 { 70 75 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) { 74 80 this.xsl_file = xsl_file; 75 81 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 } 77 94 78 95 private boolean process() … … 152 169 TransformerFactory tFactory = TransformerFactory.newInstance(); 153 170 Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file)); 171 172 setTransformerLanguageParams(transformer); // sourcelang and targetlang 173 154 174 transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); 155 175 return true; … … 169 189 170 190 transformer.setParameter("mapping",mapping); 191 setTransformerLanguageParams(transformer); // sourcelang and targetlang 171 192 172 193 Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); … … 284 305 } 285 306 307 private void setTransformerLanguageParams(Transformer transformer) 308 { 309 if(targetlang != "") { 310 transformer.setParameter("sourcelang",sourcelang); 311 transformer.setParameter("targetlang",targetlang); 312 } 313 } 286 314 287 315 private void translate(String xml_file, String xsl_file, String output_file)throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException, IOException{ … … 297 325 output = new OutputStreamWriter(new FileOutputStream(output_file), "UTF-8"); 298 326 } 299 327 328 setTransformerLanguageParams(transformer); // sourcelang and targetlang 300 329 transformer.transform(new StreamSource(new File(xml_file)),new StreamResult(output)); 301 330 … … 315 344 String mapping_file=""; 316 345 String output_file=""; 317 346 347 String sourcelang=""; 348 String targetlang=""; 349 318 350 // Checking Arguments 319 351 if(args.length < 1) … … 339 371 340 372 } 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 } 341 381 else if(args[i].equals("-h")){ 342 382 printUsage(); … … 353 393 if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line 354 394 if (mapping_file.equals("")){ 355 core = new ApplyXSLT(xsl_file);395 core = new ApplyXSLT(xsl_file, sourcelang, targetlang); 356 396 } 357 397 else{ 358 core = new ApplyXSLT(xsl_file,mapping_file);398 core = new ApplyXSLT(xsl_file,mapping_file, sourcelang, targetlang); 359 399 } 360 400 … … 367 407 } 368 408 else if(!xml_file.equals("") && !xsl_file.equals("")){ 369 core = new ApplyXSLT();409 core = new ApplyXSLT(sourcelang, targetlang); 370 410 try { 371 411 core.translate(xml_file,xsl_file,output_file); … … 388 428 389 429 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]"); 391 431 System.out.println("\t-x specifies the xml file (Note: optional for piped xml data)"); 392 432 System.out.println("\t-t specifies the xsl file"); 393 433 System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)"); 394 434 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."); 395 437 System.exit(-1); 396 438 }
Note:
See TracChangeset
for help on using the changeset viewer.