44
55package core .model ;
66
7- import java .io .BufferedReader ;
7+ import com .intellij .execution .ExecutionException ;
8+ import com .intellij .execution .configurations .GeneralCommandLine ;
9+ import com .intellij .execution .process .ProcessOutput ;
10+ import com .intellij .execution .util .ExecUtil ;
11+
812import java .io .IOException ;
9- import java .io .InputStreamReader ;
1013import java .io .PrintWriter ;
14+ import java .nio .charset .Charset ;
1115
1216public class SRCMLHandler {
1317
@@ -91,37 +95,27 @@ public static String addXMLForProject(SRCMLxml srcml, String filePath) {
9195 * @return xml String
9296 */
9397 private static String createXMLForFile (String path ) {
94- return runShellCommand (new String []{"srcml" , path })[ 0 ] ;
98+ return runShellCommand (new String []{"/usr/local/bin/ srcml" , path });
9599 }
96100
97101
98102 /**
99103 * run the shell command
100104 *
101105 * @param command (terminal command)
102- * @return output and errors
106+ * @return output
103107 */
104- private static String [] runShellCommand (String [] command ) {
105- try {
106- Process p = Runtime .getRuntime ().exec (command );
107-
108- BufferedReader stdInput = new BufferedReader (new InputStreamReader (p .getInputStream ()));
109- BufferedReader stdError = new BufferedReader (new InputStreamReader (p .getErrorStream ()));
110-
111- String line , resultInput = "" , resultError = "" ;
112- while ((line = stdInput .readLine ()) != null ) {
113- resultInput = resultInput + line + '\n' ;
114- }
108+ private static String runShellCommand (String [] command ) {
109+ GeneralCommandLine generalCommandLine = new GeneralCommandLine (command );
110+ generalCommandLine .setCharset (Charset .forName ("UTF-8" ));
115111
116- while ((line = stdError .readLine ()) != null ) {
117- resultError = resultError + line + '\n' ;
118- }
119- return new String []{resultInput , resultError };
112+ try {
113+ ProcessOutput processOutput = ExecUtil .execAndGetOutput (generalCommandLine );
114+ return String .join ("\n " , processOutput .getStdoutLines ());
120115
121- } catch (IOException e ) {
122- System .out .println ("Exception: " );
116+ } catch (ExecutionException e ) {
123117 e .printStackTrace ();
124- return new String []{ "" , "" } ;
118+ return e . toString () ;
125119 }
126120
127121 }
@@ -135,13 +129,13 @@ private static String[] runShellCommand(String[] command) {
135129 */
136130 public static int findLineNumber (String fullPath ) {
137131
138- String [] str = new String []{"srcml" , "--unit" , "1" , fullPath };
139- String [] src = runShellCommand (str );
132+ String [] str = new String []{"/usr/local/bin/ srcml" , "--unit" , "1" , fullPath };
133+ String src = runShellCommand (str );
140134
141135 //File filePath = new File(fullPath);
142136 //filePath.delete();
143137
144- return src [ 0 ] .length ();
138+ return src .length ();
145139 }
146140
147141
0 commit comments