Skip to content

Commit 00edc45

Browse files
author
lcaouen
committed
show in red incorrect properties in settings.ini
1 parent 3ba99e5 commit 00edc45

File tree

2 files changed

+118
-17
lines changed

2 files changed

+118
-17
lines changed

core/framework/src/main/java/org/phoebus/framework/preferences/PropertyPreferenceWriter.java

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77
*******************************************************************************/
88
package org.phoebus.framework.preferences;
99

10+
import java.io.BufferedReader;
11+
import java.io.ByteArrayInputStream;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.InputStreamReader;
1016
import java.io.OutputStream;
1117
import java.io.OutputStreamWriter;
1218
import java.io.Writer;
19+
import java.util.Enumeration;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Properties;
23+
import java.util.StringTokenizer;
24+
import java.util.jar.JarEntry;
25+
import java.util.jar.JarFile;
1326
import java.util.prefs.Preferences;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
1429

1530
/** Write preferences in property file format
1631
* @author Kay Kasemir
@@ -33,34 +48,116 @@ public static void save(final OutputStream stream) throws Exception
3348
{
3449
try
3550
(
51+
3652
final OutputStreamWriter out = new OutputStreamWriter(stream);
3753
)
3854
{
39-
out.append("# Preference settings\n");
40-
out.append("# Format:\n");
41-
out.append("# the.package.name/key=value\n");
42-
listSettings(out, Preferences.userRoot());
43-
out.append("# End.\n");
55+
out.append("# Preference settings<br/>\n");
56+
out.append("# Format:<br/>\n");
57+
out.append("# the.package.name/key=value<br/>\n");
58+
listSettings(getAllPropertyKeys(), out, Preferences.userRoot());
59+
out.append("# End.<br/>\n");
4460
out.flush();
4561
}
4662
}
4763

48-
private static void listSettings(final Writer out, final Preferences node) throws Exception
64+
private static void listSettings(Map<String, String> allKeysWithPackages, final Writer out, final Preferences node) throws Exception
4965
{
5066
for (String key : node.keys())
51-
formatSetting(out, node, key);
67+
formatSetting(allKeysWithPackages, out, node, key);
5268
for (String child : node.childrenNames())
53-
listSettings(out, node.node(child));
69+
listSettings(allKeysWithPackages, out, node.node(child));
5470
}
5571

56-
private static void formatSetting(final Writer out, final Preferences node, final String key) throws Exception
72+
private static void formatSetting(Map<String, String> allKeysWithPackages, final Writer out, final Preferences node, final String key) throws Exception
5773
{
5874
final String path = node.absolutePath();
59-
out.append(path.substring(1).replace('/', '.'))
60-
.append('/')
61-
.append(key)
75+
String fullKey = path.substring(1).replace('/', '.') + '/' + key;
76+
String keyFound = allKeysWithPackages.get(fullKey);
77+
boolean bNotFound = keyFound == null ? true : false;
78+
if (bNotFound) out.append("<div style='color: red; font-weight: bold'>");
79+
out.append(fullKey)
6280
.append('=')
6381
.append(node.get(key, ""))
64-
.append('\n');
82+
.append("<br/>\n");
83+
if (bNotFound) out.append("</div>");
6584
}
85+
86+
private static Map<String, String> getAllPropertyKeys() throws Exception
87+
{
88+
Map<String, String> allKeysWithPackages = new HashMap<>();
89+
90+
String classpath = System.getProperty("java.class.path");
91+
StringTokenizer tokenizer = new StringTokenizer(classpath, System.getProperty("path.separator"));
92+
93+
while (tokenizer.hasMoreTokens()) {
94+
String path = tokenizer.nextToken();
95+
File file = new File(path);
96+
97+
if (path.endsWith(".jar")) {
98+
try (JarFile jarFile = new JarFile(file)) {
99+
Enumeration<JarEntry> entries = jarFile.entries();
100+
101+
while (entries.hasMoreElements()) {
102+
JarEntry entry = entries.nextElement();
103+
String entryName = entry.getName();
104+
105+
if (entryName.endsWith("preferences.properties")) {
106+
parsePropertiesWithPackage(
107+
jarFile.getInputStream(entry),
108+
entryName,
109+
allKeysWithPackages
110+
);
111+
}
112+
}
113+
} catch (IOException e) {
114+
System.err.println("Error opening JAR : " + path);
115+
e.printStackTrace();
116+
}
117+
}
118+
}
119+
120+
return allKeysWithPackages;
121+
}
122+
123+
private static void parsePropertiesWithPackage(
124+
InputStream inputStream,
125+
String fileName,
126+
Map<String, String> allKeysWithPackages
127+
) {
128+
Properties props = new Properties();
129+
String packageName = null;
130+
131+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
132+
String line;
133+
StringBuilder content = new StringBuilder();
134+
135+
while ((line = reader.readLine()) != null) {
136+
line = line.trim();
137+
if (line.startsWith("#") && line.contains("Package")) {
138+
// Find package name
139+
Pattern pattern = Pattern.compile("#\\s*Package\\s+([^\\s]+)");
140+
Matcher matcher = pattern.matcher(line);
141+
if (matcher.find()) {
142+
packageName = matcher.group(1);
143+
}
144+
} else if (!line.startsWith("#")) {
145+
content.append(line).append("\n");
146+
}
147+
}
148+
149+
if (content.length() > 0) {
150+
props.load(new ByteArrayInputStream(content.toString().getBytes()));
151+
}
152+
153+
// properties found
154+
for (String key : props.stringPropertyNames()) {
155+
String prefixedKey = (packageName != null) ? packageName + "/" + key : key;
156+
allKeysWithPackages.put(prefixedKey, props.getProperty(key));
157+
}
158+
} catch (IOException e) {
159+
System.err.println("Error when reading file " + fileName);
160+
e.printStackTrace();
161+
}
162+
}
66163
}

core/ui/src/main/java/org/phoebus/ui/help/OpenAbout.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import javafx.scene.image.Image;
4343
import javafx.scene.layout.Priority;
4444
import javafx.scene.layout.VBox;
45+
import javafx.scene.web.WebView;
4546

4647
/** Menu entry to open 'about'
4748
* @author Kay Kasemir
@@ -204,12 +205,15 @@ private Node createDetailSection()
204205
logger.log(Level.WARNING, "Cannot list preferences", ex);
205206
}
206207

207-
area = new TextArea(prefs_buf.toString());
208-
area.setEditable(false);
208+
WebView webView = new WebView();
209+
String content = "<html><body>";
210+
content += prefs_buf.toString();
211+
content += "</body></html>";
212+
webView.getEngine().loadContent(content);
209213

210-
VBox.setVgrow(area, Priority.ALWAYS);
214+
VBox.setVgrow(webView, Priority.ALWAYS);
211215

212-
final Tab prefs = new Tab(Messages.HelpAboutPrefs, area);
216+
final Tab prefs = new Tab(Messages.HelpAboutPrefs, webView);
213217

214218
final TabPane tabs = new TabPane(apps, envs, props, prefs);
215219
return tabs;

0 commit comments

Comments
 (0)