Formatters.java

  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. import java.io.StringReader;
  29. import java.io.StringWriter;
  30. import java.time.Duration;
  31. import java.time.temporal.ChronoUnit;
  32. import java.util.Base64;
  33. import javax.annotation.Nonnull;
  34. import javax.xml.transform.OutputKeys;
  35. import javax.xml.transform.Transformer;
  36. import javax.xml.transform.TransformerException;
  37. import javax.xml.transform.TransformerFactory;
  38. import javax.xml.transform.stream.StreamResult;
  39. import javax.xml.transform.stream.StreamSource;
  40. import lombok.AccessLevel;
  41. import lombok.NoArgsConstructor;

  42. /***********************************************************************************************************************
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  48. public final class Formatters
  49.   {
  50.     /*******************************************************************************************************************
  51.      *
  52.      *
  53.      ******************************************************************************************************************/
  54.     @Nonnull
  55.     public static String xmlPrettyPrinted (@Nonnull final String xml)
  56.       {
  57.         try
  58.           {
  59.             final TransformerFactory transformerFactory = TransformerFactory.newInstance();
  60.             transformerFactory.setAttribute("indent-number", 4);
  61.             final Transformer transformer = transformerFactory.newTransformer();
  62.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  63.             final StringWriter out = new StringWriter();
  64.             transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(out));

  65.             return out.toString();
  66.           }
  67.         catch (TransformerException e)
  68.           {
  69.             throw new RuntimeException(e);
  70.           }
  71.       }

  72.     /*******************************************************************************************************************
  73.      *
  74.      *
  75.      ******************************************************************************************************************/
  76.     @Nonnull
  77.     public static String format (@Nonnull final Duration duration)
  78.       {
  79.         final long s = duration.get(ChronoUnit.SECONDS);
  80.         final long hours = s / 3600;
  81.         final long minutes = (s / 60) % 60;
  82.         final long seconds = s % 60;

  83.         return (hours == 0) ? String.format("%02d:%02d", minutes, seconds)
  84.                             : String.format("%02d:%02d:%02d", hours, minutes, seconds);
  85.       }

  86.     /*******************************************************************************************************************
  87.      *
  88.      *
  89.      *
  90.      ******************************************************************************************************************/
  91.     @Nonnull
  92.     public static String toHexString (@Nonnull final byte[] bytes)
  93.       {
  94.         final StringBuilder builder = new StringBuilder();

  95.         for (final byte b : bytes)
  96.           {
  97.             final int value = b & 0xff;
  98.             builder.append(Integer.toHexString(value >>> 4)).append(Integer.toHexString(value & 0x0f));
  99.           }

  100.         return builder.toString();
  101.       }

  102.     /*******************************************************************************************************************
  103.      *
  104.      *
  105.      *
  106.      ******************************************************************************************************************/
  107.     @Nonnull
  108.     public static String toBase64String (@Nonnull final byte[] bytes)
  109.       {
  110.         return  Base64.getUrlEncoder().encodeToString(bytes);
  111.       }
  112.   }