77 *******************************************************************************/
88package 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 ;
1016import java .io .OutputStream ;
1117import java .io .OutputStreamWriter ;
1218import 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 ;
1326import 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}
0 commit comments