Salome HOME
Copyrights update 2015.
[tools/siman.git] / Workspace / Siman / src / org / splat / launcher / WindowsRegistry.java
1 package org.splat.launcher;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.StringWriter;
8
9 public class WindowsRegistry {
10
11         private static final String REG_SZ = "REG_SZ";
12
13         static class StreamReader extends Thread { //RKV: NOPMD: TODO: Clarify the purpose of this class and then fix
14                 private transient final InputStream _is;
15                 private transient final StringWriter _sw = new StringWriter();;
16
17                 public StreamReader(final InputStream is) {
18                         super();
19                         this._is = is;
20                 }
21
22                 @Override
23                 public void run() {
24                         try {
25                                 int c = _is.read();
26                                 while (c != -1) {
27                                         _sw.write(c);
28                                         c = _is.read();
29                                 }
30                         } catch (IOException e) {
31                                 e.printStackTrace(); // RKV: NOPMD: TODO: try to use logger
32                         }
33                 }
34
35                 public String getResult() {
36                         return _sw.toString();
37                 }
38         }
39
40         public static final String readValue(final String location, final String key) {
41                 String res = null;
42                 try {
43                         // Run reg query, then read output with StreamReader (internal class)
44                         String aKey;
45                         if (key.length() == 0) {
46                                 aKey = "/ve"; // Looking for the default key
47                         } else {
48                                 aKey = "/v " + key;
49                         }
50
51                         Process process = Runtime.getRuntime().exec(
52                                         "reg query " + location + " " + aKey);
53                         BufferedReader buffer = new BufferedReader(new InputStreamReader(
54                                         process.getInputStream()));
55
56                         // Output has the following format:
57                         // \n<Version information>\n\n<key>\t<registry type>\t<value>
58                         String output = buffer.readLine();
59                         while (output != null) {
60                                 if (output.contains(REG_SZ)) {
61                                         int index = output.indexOf(REG_SZ) + REG_SZ.length() + 1;
62                                         res = output.substring(index).trim();
63                                         break;
64                                 }
65                                 output = buffer.readLine();
66                         }
67                         // StreamReader reader = new StreamReader(process.getInputStream());
68
69                         // reader.start();
70                         // process.waitFor();
71                         // reader.join();
72                         // String output = reader.getResult();
73
74                         // Output has the following format:
75                         // \n<Version information>\n\n<key>\t<registry type>\t<value>
76                         // if( ! output.contains("\t") ){
77                         // return null;
78                         // }
79                         // Parse out the value
80                         // String[] parsed = output.split("\t");
81                         // return parsed[parsed.length-1];
82                 } catch (Exception e) {
83                         e.printStackTrace(); // RKV: NOPMD: TODO: try to use logger
84                 }
85                 return res;
86         }
87 }