Changeset 28973 for main/trunk/greenstone2/build-src/src
- Timestamp:
- 2014-04-14T22:20:14+12:00 (10 years ago)
- Location:
- main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java
r28709 r28973 160 160 161 161 } 162 163 //if(br != null) { 164 // br.close(); 165 // br = null; 166 //} 167 162 168 }catch (Exception e) 163 169 { … … 166 172 167 173 return false; 174 } 175 176 // reads xml from stdin, but no <?DocStart?><?DocEnd?> markers, and sends output to STDOUT 177 private boolean processPipedFromStdIn() 178 { 179 try{ 180 //System.err.println("Received nothing\n"); 181 182 ReadStreamGobbler readInStreamGobbler = new ReadStreamGobbler(System.in, true); 183 readInStreamGobbler.start(); 184 readInStreamGobbler.join(); 185 String outputstr = readInStreamGobbler.getOutput(); 186 187 // Using join() above, even though we use only one streamGobbler thread, and even 188 // though we're not dealing with the input/output/error streams of a Process object. 189 // But the join() call here didn't break things. 190 // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 191 192 boolean result = false; 193 if (mapping_file !=null && !mapping_file.equals("")){ 194 result = translateXMLWithMapping(outputstr, null); // null: no outputfile, send to STDOUT 195 } 196 else{ 197 result = translateXML(outputstr, null); // null: no outputfile, send to STDOUT 198 } 199 200 if (!result){ 201 System.err.println("Translation Failed!!"); 202 return false; 203 } else { 204 return true; 205 } 206 }catch (Exception e) { 207 System.err.println("Receiving piped data error!" + e.toString()); 208 } 209 return false; 168 210 } 169 211 … … 180 222 setTransformerParams(transformer); // sourcelang and targetlang and any further custom parameters to be passed into the XSLT 181 223 182 transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); 224 if(output_file != null) { 225 transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file))); 226 } else { 227 transformer.transform(new StreamSource(str), new StreamResult(System.out)); 228 } 183 229 return true; 184 230 } … … 437 483 String targetlang=""; 438 484 485 boolean readFromStdInFlag = false; 486 439 487 HashMap paramMap = new HashMap(); 440 488 int index = -1; // index of the '=' sign in cmdline argument specifying custom parameters to be passed into the XSLT … … 470 518 else if(args[i].equals("-l") && i+1 < args.length && !args[i+1].startsWith("-")){ 471 519 targetlang = args[++i]; 520 } 521 else if(args[i].equals("-c")){ 522 readFromStdInFlag = true; 472 523 } 473 524 else if(args[i].equals("-h")){ … … 489 540 490 541 ApplyXSLT core = null; 491 542 492 543 if (xml_file.equals("") && !xsl_file.equals("")){//read from pipe line 493 544 if (mapping_file.equals("")){ … … 499 550 500 551 if (core != null){ 501 core.process(); 552 if(readFromStdInFlag) { // ApplyXSLT was run with -c: read from pipe but no <?DocStart?><?DocEnd?> markers 553 core.processPipedFromStdIn(); 554 } 555 core.process(); //read from pipe line, but expecting <?DocStart?><?DocEnd?> embedding markers 502 556 } 503 557 else{ … … 533 587 System.out.println("Usage: ApplyXSLT -x File -t File [-m File] [-o File] [-s sourcelang] [-l targetlang] [param-name=param-value]"); 534 588 System.out.println("\t-x specifies the xml file (Note: optional for piped xml data)"); 589 System.out.println("\t-c read xml file piped from stdin but without DocStart/DocEnd markers. Writes to stdout"); 535 590 System.out.println("\t-t specifies the xsl file"); 536 591 System.out.println("\t-m specifies the mapping file (for MARCXMLPlugout.pm only)"); … … 541 596 System.exit(-1); 542 597 } 598 599 600 // StreamGobblers used in reading/writing to Process' input and outputstreams can be re-used more generally. 601 // Here in ApplyXSTL.java we use it to read from a pipe line (stdin piped into this ApplyXSLT.java) 602 // Code based on http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 603 class ReadStreamGobbler extends Thread 604 { 605 InputStream is = null; 606 StringBuffer outputstr = new StringBuffer(); 607 boolean split_newlines = false; 608 609 610 public ReadStreamGobbler(InputStream is) 611 { 612 this.is = is; 613 split_newlines = false; 614 } 615 616 public ReadStreamGobbler(InputStream is, boolean split_newlines) 617 { 618 this.is = is; 619 this.split_newlines = split_newlines; 620 } 621 622 public void run() 623 { 624 BufferedReader br = null; 625 try { 626 br = new BufferedReader(new InputStreamReader(is, "UTF-8")); 627 String line=null; 628 while ( (line = br.readLine()) != null) { 629 //System.out.println("@@@ GOT LINE: " + line); 630 outputstr.append(line); 631 if(split_newlines) { 632 outputstr.append("\n"); 633 } 634 } 635 } catch (IOException ioe) { 636 ioe.printStackTrace(); 637 } finally { 638 closeResource(br); 639 } 640 } 641 642 public String getOutput() { 643 return outputstr.toString(); 644 } 645 646 // http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html 647 // http://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks 648 public void closeResource(Closeable resourceHandle) { 649 try { 650 if(resourceHandle != null) { 651 resourceHandle.close(); 652 resourceHandle = null; 653 } 654 } catch(Exception e) { 655 System.err.println("Exception closing resource: " + e.getMessage()); 656 e.printStackTrace(); 657 } 658 } 659 } 660 543 661 } 544 662 -
main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl/Makefile.in
r23356 r28973 15 15 ApplyXSLT.jar: $(CLASSES) 16 16 $(JAR) cvf ApplyXSLT.jar -C ../../.. org/nzdl/gsdl/ApplyXSLT.class \ 17 -C ../../.. org/nzdl/gsdl/ApplyXSLTUtil.class 17 -C ../../.. org/nzdl/gsdl/ApplyXSLTUtil.class \ 18 -C ../../.. org/nzdl/gsdl/ApplyXSLT\$$ReadStreamGobbler.class 18 19 19 20
Note:
See TracChangeset
for help on using the changeset viewer.