LibreOffice Module odk (master) 1
WinRegKey.java
Go to the documentation of this file.
1/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20package com.sun.star.lib.loader;
21
22import java.io.BufferedReader;
23import java.io.IOException;
24import java.io.InputStreamReader;
25import java.nio.charset.Charset;
26import java.util.regex.Matcher;
27import java.util.regex.Pattern;
28
33final class WinRegKey {
34
35 private final String m_keyName;
36
40 public WinRegKey( String keyName ) {
41 m_keyName = keyName;
42 }
43
44 private static void close(BufferedReader c) {
45 if (c == null) return;
46 try {
47 c.close();
48 } catch (IOException e) {
49 e.printStackTrace();
50 }
51 }
52
56 public String getStringValue() throws WinRegKeyException {
57 BufferedReader r = null;
58 try {
59 Process p = Runtime.getRuntime().exec(new String[]{"reg", "QUERY", m_keyName});
60 r = new BufferedReader(
61 new InputStreamReader(p.getInputStream(), Charset.defaultCharset()));
62 String v = null;
63 Pattern pt = Pattern.compile("\\s+\\(Default\\)\\s+REG_SZ\\s+(.+)");
64 for (;;) {
65 String s = r.readLine();
66 if (s == null) {
67 break;
68 }
69 Matcher m = pt.matcher(s);
70 if (m.matches()) {
71 if (v != null) {
72 throw new WinRegKeyException("reg QUERY did not provided expected output");
73 }
74 v = m.group(1);
75 }
76 }
77 p.waitFor();
78 int e = p.exitValue();
79 if (e != 0) {
80 throw new WinRegKeyException("reg QUERY exited with " + e);
81 }
82 if (v == null) {
83 throw new WinRegKeyException("reg QUERY did not provided expected output");
84 }
85 return v;
86 } catch (WinRegKeyException e) {
87 throw e;
88 } catch (Exception e) {
89 throw new WinRegKeyException(e);
90 }
91 finally {
92 close(r);
93 }
94 }
95}
96
97/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool close
float v
void * p
m