Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tillster.tools;
- import java.io.File;
- import java.util.*;
- public final class SysProps {
- private SysProps() { }
- public static void main(String[] args) {
- Properties props = System.getProperties();
- Map<String, String> propMap = new TreeMap<>();
- int maxLength = 0;
- for (String key: props.stringPropertyNames()) {
- propMap.put(key, props.getProperty(key));
- maxLength = Math.max(maxLength, key.length());
- }
- String format = String.format("%c-%ds = %s", '%', maxLength, "%s%n"); // Gives "%-29s = %s%n"
- for (Map.Entry<String, String> stringStringEntry : propMap.entrySet()) {
- String value = asPath(stringStringEntry.getValue());
- System.out.printf(format, stringStringEntry.getKey(), value);
- }
- }
- private static String asPath(String text) {
- if (text.length() < 5) {
- return escapedPath(text);
- }
- if (text.contains("://")) {
- return text; // text is a URL, not a path.
- }
- char separator = File.pathSeparatorChar;
- int sepCount = count(text, separator);
- if (sepCount > 0) {
- char fileSeparator = File.separatorChar;
- if (count(text, fileSeparator) > sepCount) {
- return pathString(text);
- }
- }
- return text;
- }
- /**
- * Escape the non-printing characters. We only bother with this for short Strings. As of this writing, there's
- * really only one property that needs this: line.separator.
- * @param s The string to escape
- * @return The escaped String, with characters less than the space character or bigger than 127 displayed
- * using backslash-u notation.
- */
- private static String escapedPath(String s) {
- StringBuilder builder = new StringBuilder();
- for (int ii=0; ii<s.length(); ++ii) {
- char c = s.charAt(ii);
- if (c < 0x20 || c > 0xFF) {
- String fmt = String.format("%04x", (int)c);
- builder.append("\\u").append(fmt);
- } else {
- builder.append(c);
- }
- }
- return builder.toString();
- }
- /**
- * counts how many times char c appears in String s
- * @param s The String
- * @param c The character to count
- * @return The number of times c appears in s
- */
- private static int count(String s, char c) {
- int count = 0;
- int index = 0;
- index = s.indexOf(c, index);
- while (index >= 0) {
- count++;
- index = s.indexOf(c, index+1);
- }
- return count;
- }
- /**
- * If s is a path (if it has at least one path separator, and has more file separators than path separators),
- * returns a formatted path, which is a path where separators are followed by line breaks and indentation.
- * @param s A String that has been determined to be a path
- * @return a formatted path derived from s
- */
- private static String pathString(String s) {
- String lead = "\n ";
- StringBuilder builder = new StringBuilder(s);
- int tail = s.length()-1;
- while (tail >= 0) {
- if (builder.charAt(tail) == File.pathSeparatorChar) {
- builder.insert(tail+1, lead);
- }
- tail--;
- }
- builder.insert(0, lead);
- return builder.toString();
- }
- }
Add Comment
Please, Sign In to add comment