Guest User

Untitled

a guest
Jan 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. package com.tillster.tools;
  2.  
  3. import java.io.File;
  4. import java.util.*;
  5.  
  6. public final class SysProps {
  7.  
  8. private SysProps() { }
  9.  
  10. public static void main(String[] args) {
  11. Properties props = System.getProperties();
  12. Map<String, String> propMap = new TreeMap<>();
  13. int maxLength = 0;
  14. for (String key: props.stringPropertyNames()) {
  15. propMap.put(key, props.getProperty(key));
  16. maxLength = Math.max(maxLength, key.length());
  17. }
  18. String format = String.format("%c-%ds = %s", '%', maxLength, "%s%n"); // Gives "%-29s = %s%n"
  19. for (Map.Entry<String, String> stringStringEntry : propMap.entrySet()) {
  20. String value = asPath(stringStringEntry.getValue());
  21. System.out.printf(format, stringStringEntry.getKey(), value);
  22. }
  23. }
  24.  
  25. private static String asPath(String text) {
  26. if (text.length() < 5) {
  27. return escapedPath(text);
  28. }
  29. if (text.contains("://")) {
  30. return text; // text is a URL, not a path.
  31. }
  32. char separator = File.pathSeparatorChar;
  33. int sepCount = count(text, separator);
  34. if (sepCount > 0) {
  35. char fileSeparator = File.separatorChar;
  36. if (count(text, fileSeparator) > sepCount) {
  37. return pathString(text);
  38. }
  39. }
  40. return text;
  41. }
  42.  
  43. /**
  44. * Escape the non-printing characters. We only bother with this for short Strings. As of this writing, there's
  45. * really only one property that needs this: line.separator.
  46. * @param s The string to escape
  47. * @return The escaped String, with characters less than the space character or bigger than 127 displayed
  48. * using backslash-u notation.
  49. */
  50. private static String escapedPath(String s) {
  51. StringBuilder builder = new StringBuilder();
  52. for (int ii=0; ii<s.length(); ++ii) {
  53. char c = s.charAt(ii);
  54. if (c < 0x20 || c > 0xFF) {
  55. String fmt = String.format("%04x", (int)c);
  56. builder.append("\\u").append(fmt);
  57. } else {
  58. builder.append(c);
  59. }
  60. }
  61. return builder.toString();
  62. }
  63.  
  64. /**
  65. * counts how many times char c appears in String s
  66. * @param s The String
  67. * @param c The character to count
  68. * @return The number of times c appears in s
  69. */
  70. private static int count(String s, char c) {
  71. int count = 0;
  72. int index = 0;
  73. index = s.indexOf(c, index);
  74. while (index >= 0) {
  75. count++;
  76. index = s.indexOf(c, index+1);
  77. }
  78. return count;
  79. }
  80.  
  81. /**
  82. * If s is a path (if it has at least one path separator, and has more file separators than path separators),
  83. * returns a formatted path, which is a path where separators are followed by line breaks and indentation.
  84. * @param s A String that has been determined to be a path
  85. * @return a formatted path derived from s
  86. */
  87. private static String pathString(String s) {
  88. String lead = "\n ";
  89. StringBuilder builder = new StringBuilder(s);
  90. int tail = s.length()-1;
  91. while (tail >= 0) {
  92. if (builder.charAt(tail) == File.pathSeparatorChar) {
  93. builder.insert(tail+1, lead);
  94. }
  95. tail--;
  96. }
  97. builder.insert(0, lead);
  98. return builder.toString();
  99. }
  100. }
Add Comment
Please, Sign In to add comment