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.util;
020import java.io.PrintStream;
021import java.util.regex.Pattern;
022
023public class LogUtil {
024    public static final Pattern xmlReservedPattern = Pattern.compile("&|<|>");
025
026    public static void dump (PrintStream p, String indent, String s) {
027        try {
028            boolean expanded = s.length() > 60;
029            if (needsCDATA(s)) {
030                if (expanded) {
031                    p.println("<![CDATA[");
032                    p.println(s);
033                    p.println("]]>");
034                } else {
035                    p.print(indent + "<![CDATA[");
036                    p.print(s);
037                    p.println("]]>");
038                }
039            } else
040                p.print(s);
041        } catch (Exception e) {
042            p.println(e.getMessage());
043        }
044    }
045
046    public static boolean needsCDATA(String s) {
047        return xmlReservedPattern.matcher(s).find();
048    }
049}