Skip to contentPackage: Formatters
Formatters
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.util;
28:
29: import javax.annotation.Nonnull;
30: import java.time.Duration;
31: import java.time.temporal.ChronoUnit;
32: import java.util.Base64;
33: import java.io.StringReader;
34: import java.io.StringWriter;
35: import javax.xml.transform.OutputKeys;
36: import javax.xml.transform.Transformer;
37: import javax.xml.transform.TransformerException;
38: import javax.xml.transform.TransformerFactory;
39: import javax.xml.transform.stream.StreamResult;
40: import javax.xml.transform.stream.StreamSource;
41: import lombok.AccessLevel;
42: import lombok.NoArgsConstructor;
43:
44: /***********************************************************************************************************************
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49: @NoArgsConstructor(access = AccessLevel.PRIVATE)
50: public final class Formatters
51: {
52: /*******************************************************************************************************************
53: *
54: *
55: ******************************************************************************************************************/
56: @Nonnull
57: public static String xmlPrettyPrinted (@Nonnull final String xml)
58: {
59: try
60: {
61: final TransformerFactory transformerFactory = TransformerFactory.newInstance();
62: transformerFactory.setAttribute("indent-number", 4);
63: final Transformer transformer = transformerFactory.newTransformer();
64: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
65: final StringWriter out = new StringWriter();
66: transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(out));
67:
68: return out.toString();
69: }
70: catch (TransformerException e)
71: {
72: throw new RuntimeException(e);
73: }
74: }
75:
76: /*******************************************************************************************************************
77: *
78: *
79: ******************************************************************************************************************/
80: @Nonnull
81: public static String format (@Nonnull final Duration duration)
82: {
83: final long s = duration.get(ChronoUnit.SECONDS);
84: final long hours = s / 3600;
85: final long minutes = (s / 60) % 60;
86: final long seconds = s % 60;
87:
88:• return (hours == 0) ? String.format("%02d:%02d", minutes, seconds)
89: : String.format("%02d:%02d:%02d", hours, minutes, seconds);
90: }
91:
92: /*******************************************************************************************************************
93: *
94: *
95: *
96: ******************************************************************************************************************/
97: @Nonnull
98: public static String toHexString (@Nonnull final byte[] bytes)
99: {
100: final StringBuilder builder = new StringBuilder();
101:
102:• for (final byte b : bytes)
103: {
104: final int value = b & 0xff;
105: builder.append(Integer.toHexString(value >>> 4)).append(Integer.toHexString(value & 0x0f));
106: }
107:
108: return builder.toString();
109: }
110:
111: /*******************************************************************************************************************
112: *
113: *
114: *
115: ******************************************************************************************************************/
116: @Nonnull
117: public static String toBase64String (@Nonnull final byte[] bytes)
118: {
119: return Base64.getUrlEncoder().encodeToString(bytes);
120: }
121: }