SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.configuration.NameIdentified;
21 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.PrintWriter;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
38 * @author Ing. František Kučera (frantovo.cz)
40 public class Functions {
42 private static final String NBSP = " ";
43 private static final Pattern WHITESPACE_TO_REPLACE = Pattern.compile("\\n|\\r|\\t|" + NBSP);
48 public static boolean equalz(Object a, Object b) {
49 return a == null ? b == null : a.equals(b);
54 * @param text String to be examinated
55 * @param trim whether text should be trimmed before examination
56 * @return whether text is not empty and one or more characters long (after prospective trim)
58 public static boolean isEmpty(String text, boolean trim) {
65 return text.isEmpty();
70 * @see #isEmpty(java.lang.String, boolean)
72 public static boolean isNotEmpty(String text, boolean trim) {
73 return !isEmpty(text, trim);
76 public boolean isEmpty(Collection c) {
77 return c == null || c.isEmpty();
80 public boolean isNotEmpty(Collection c) {
84 public boolean isEmpty(Map m) {
85 return m == null || m.isEmpty();
88 public boolean isNotEmpty(Map m) {
93 * @return empty collection if given one is null | or the original one
95 public static <T> Collection<T> notNull(Collection<T> c) {
97 return Collections.emptyList();
103 public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
104 for (T element : notNull(collection)) {
105 if (element != null && equalz(element.getName(), name)) {
114 * Copy file from Java resources to file system.
116 public static void installResource(String resourceName, File target) throws IOException {
117 try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
118 try (PrintWriter writer = new PrintWriter(target)) {
120 String line = reader.readLine();
124 writer.println(line);
131 public static String rpad(String s, int n) {
133 return String.format("%1$-" + n + "s", s);
139 public static String lpad(String s, int n) {
141 return String.format("%1$" + n + "s", s);
147 public static String repeat(char ch, int count) {
148 char[] array = new char[count];
149 Arrays.fill(array, ch);
150 return new String(array);
152 private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
154 public static String toHex(byte[] bytes) {
155 char[] hexChars = new char[bytes.length * 2];
156 for (int j = 0; j < bytes.length; j++) {
157 int v = bytes[j] & 0xFF;
158 hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
159 hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
161 return new String(hexChars);
164 public static String readString(InputStream in) throws IOException {
165 try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
166 StringBuilder result = new StringBuilder();
167 for (String line = br.readLine(); line != null; line = br.readLine()) {
171 return result.toString();
176 * @param <P> type of the last parent
177 * @param <T> type of the examined class
178 * @param type examined class
179 * @param lastParent the last parent type to stop at
180 * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
182 public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
183 List<Class<? extends P>> hierarchy = new ArrayList<>();
185 for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
186 hierarchy.add(current);
193 * TODO: support background or styles and move to ColorfulPrintWriter
200 public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
202 Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
206 while (m.find(start)) {
208 printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
212 out.print(escapeColor, "↲");
215 out.print(escapeColor, "⏎");
218 out.print(escapeColor, "↹");
221 out.print(escapeColor, "⎵");
224 throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
230 printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
233 private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
237 out.print(color, text);