001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.log; 020 021import java.io.ByteArrayOutputStream; 022import java.io.PrintStream; 023 024public interface LogRenderer<T> { 025 void render (T obj, PrintStream ps, String indent); 026 Class<?> clazz(); 027 Type type(); 028 029 default void render (T obj, PrintStream ps) { 030 render (obj, ps, ""); 031 } 032 033 default String render (T obj, String indent) { 034 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 035 PrintStream ps = new PrintStream(baos); 036 render (obj, ps, indent); 037 return baos.toString(); 038 } 039 040 default String render (T obj) { 041 return render (obj, ""); 042 } 043 044 default String indent (String indent, String s) { 045 if (s == null || s.isEmpty() || indent==null || indent.isEmpty()) { 046 return s; 047 } 048 String[] lines = s.split("\n", -1); // Preserve trailing empty strings 049 StringBuilder indentedString = new StringBuilder(); 050 for (int i = 0; i < lines.length; i++) { 051 indentedString.append(indent).append(lines[i]); 052 if (i < lines.length - 1) { 053 indentedString.append("\n"); 054 } 055 } 056 return indentedString.toString(); 057 } 058 059 060 enum Type { 061 XML, 062 JSON, 063 TXT, 064 MARKDOWN 065 } 066}