Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3c7724750b7f92af423b021383c5cc73f0330cb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util.windows;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.util.Processes;

public class Registry {

   public static final String HKEY_LOCAL_MACHINE = "HKLM";
   public static final String HKEY_CURRENT_USER = "HKCU";
   public static final String REG_SZ = "REG_SZ";
   public static final String REG_HELP_CMD = "reg /?";

   public abstract interface RegVersion {
      abstract public String getQueryCmd(String root, String path, String key);

      abstract public String getQueryPattern(String key);

      abstract public String getUpdateCommand(String root, String path, String key, String value, String[] regArray);

      abstract public String isVersion();
   }

   public class RegVersion_1_00 implements RegVersion {
      @Override
      public String getQueryCmd(String root, String path, String key) {
         return "reg query " + root + "\\" + path + "\\" + key;
      }

      @Override
      public String getQueryPattern(String key) {
         return "out:\\s*(\\w*)\\s*" + key + "\\s*(.*)";
      }

      @Override
      public String getUpdateCommand(String root, String path, String key, String value, String[] regArray) {
         String cmd = "";
         if (regArray != null) {
            value = value + File.pathSeparator + regArray[1];
            System.out.println("THE VALUE IS: " + value);
            cmd = "reg update \"" + root + "\\" + path + "\\" + key + "=" + value + "\"";
         } else {
            cmd = "reg add \"" + root + "\\" + path + "\\" + key + "=" + value + "\"";
         }

         System.out.println("THE CMD IS: " + cmd);

         return cmd;
      }

      @Override
      public String isVersion() {
         return "1.00";
      }
   }

   public class RegVersion_3_0 implements RegVersion {

      @Override
      public String getQueryCmd(String root, String path, String key) {
         return "reg query " + root + "\\" + path + " /v " + key;
      }

      @Override
      public String getQueryPattern(String key) {
         return ".*?" + key + "\\s*(\\w*)\\s*(\\S*).*";
      }

      @Override
      public String getUpdateCommand(String root, String path, String key, String value, String[] regArray) {
         String type = Registry.REG_SZ;
         if (regArray != null) {
            value = value + File.pathSeparator + regArray[1];
            type = regArray[0];
         }
         return "reg add " + root + "\\" + path + " /v " + key + " /t " + type + " /f /d \"" + value + "\"";
      }

      @Override
      public String isVersion() {
         return "3.0";
      }
   }

   public static boolean isRegVersion(double lowerBound, double upperBound) throws IOException {
      boolean matched = false;
      String out = Processes.executeCommandToString(Registry.REG_HELP_CMD);

      Pattern pattern = Pattern.compile(".*version\\s(\\d*\\.\\d+).*", Pattern.DOTALL);
      Matcher matcher = pattern.matcher(out.toString());

      if (matcher.matches()) {
         String regVersion = matcher.group(1).trim();

         double foundVersion = Double.parseDouble(regVersion);
         if (foundVersion >= lowerBound && foundVersion <= upperBound) {
            matched = true;
         }
      }
      return matched;
   }

   public static RegVersion getVersion() throws IOException {
      Registry r = new Registry();
      RegVersion regVersion = null;
      if (Registry.isRegVersion(1.00, 1.99)) {
         regVersion = r.new RegVersion_1_00();
      } else {
         regVersion = r.new RegVersion_3_0();
      }
      return regVersion;
   }

   public static String[] getValue(RegVersion version, String root, String path, String key) throws IOException {
      String toReturn[] = null;

      String out = Processes.executeCommandToString(version.getQueryCmd(root, path, key));

      Pattern pattern = Pattern.compile(version.getQueryPattern(key), Pattern.DOTALL);
      Matcher matcher = pattern.matcher(out.toString());
      if (matcher.matches()) {
         toReturn = new String[matcher.groupCount()];

         for (int i = 1; i <= matcher.groupCount(); i++) {
            toReturn[i - 1] = matcher.group(i).trim();
         }
      }

      if (toReturn != null) {
         for (String temp : toReturn) {
            System.out.println("VAL: " + temp);
         }
      }
      return toReturn;
   }

   public static boolean prependRegistryValue(File updatedReg, String root, String path, String key, String value) throws IOException {

      RegVersion version = Registry.getVersion();

      String[] regArray = Registry.getValue(version, root, path, key);

      if (regArray != null) {
         if (regArray[1].contains(value)) {
            System.out.println("Value is already there.");
            return true;
         }
      }

      String command = version.getUpdateCommand(root, path, key, value, regArray);

      /*
       * If we are using a 1.0 version then use a provided reg.exe executable to use commands. If not operation will
       * fail.
       */
      if (version.isVersion().equals("1.00")) {
         if (updatedReg.exists() && updatedReg.isFile()) {
            command = updatedReg.getAbsolutePath() + command.replaceAll("reg", "");
            ;
         }
      }

      String out = Processes.executeCommandToString(command);
      System.out.println("The string: " + out);

      if (out.toString().contains("err:")) {
         return false;
      }
      return true;
   }

   public static boolean prependRegistryValue(String root, String path, String key, String value) throws IOException {

      RegVersion version = Registry.getVersion();

      String[] regArray = Registry.getValue(version, root, path, key);

      String out = Processes.executeCommandToString(version.getUpdateCommand(root, path, key, value, regArray));

      System.out.println("The string: " + out);

      if (out.toString().contains("err:")) {
         return false;
      }
      return true;
   }

   public static void main(String[] args) {
      try {
         Registry.prependRegistryValue(Registry.HKEY_CURRENT_USER, "environment", "path", "HI THERE");
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }

}

Back to the top