View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.jrst;
24  
25  import java.io.BufferedReader;
26  import java.io.IOException;
27  import java.io.InputStreamReader;
28  
29  import org.apache.commons.io.IOUtils;
30  
31  /**
32   * Class qui redirige la sortie standard pour la laisser en interne
33   */
34  public class ThreadRedirection extends Thread {
35  
36      public static final String LINE_SEPARATOR = "\n";
37      protected StringBuffer str;
38      protected Process process;
39  
40      public ThreadRedirection(Process p) {
41          process = p;
42          str = new StringBuffer();
43      }
44  
45      @Override
46      public void run() {
47      	BufferedReader reader =null;
48          try {
49          	
50              reader = new BufferedReader(new InputStreamReader(
51                      process.getInputStream()),8193);
52              
53                  String line;
54                  while ((line = reader.readLine()) != null) {
55                  	
56                  	/*Le doctype est modifie pour eviter que sax telecharge la DTD du HTML sur le net, car le traitement prend un certain temps.
57                  	    On doit en revanche spécifier la signification de nbsp.
58                  	*/
59                  	
60                      if(!line.startsWith("<!DOCTYPE html "))
61                          {
62                          str.append(line + LINE_SEPARATOR);
63                          }
64                      else {
65                         str.append("<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp '&#160;'>]> " + LINE_SEPARATOR);
66                      }
67                  }
68  
69              
70              }
71          catch (IOException ioe) {
72              ioe.printStackTrace();
73          } 
74          finally {
75             IOUtils.closeQuietly(reader);}
76      }
77  
78      public String getOutput() {
79          return str.toString();
80      }
81  }