class AnsiMessagePrinter implements MessagePrinter {

  public String format(final String message) {
    logger.debug("Message: {}", message);
    if (null == message) {
      return null;
    }
    final StringReader reader = new StringReader(message);

    int ch = 0;
    State state = new Normal(new Stack<>(), new StringWriter());
    try {
      while (0 <= (ch = reader.read())) {
        logger.trace("Char: {} - {}", ch < 32 ? ' ' : (char) ch, ch);
        final State old = state;
        state = state.next(ch);
        if (old != state) {
          logger.debug("{} -> {}", old, state);
        }
      }
    } catch (final IOException ex) {
      throw new IllegalStateException();
    }
    logger.debug("State: {}", state);
    logger.debug("Tags: {}", state.getTags());
    assertTrue(state instanceof Normal, "Expression is invalid[" + state + ": " + message);
    assertTrue(state.getTags().isEmpty(),
        "The closing tag[" + state.getTags() + "] missed: " + message);
    return state.getWriter().toString();
  }

  public  AnsiMessagePrinter(final PrintStream out);

}