1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.debug;
  26 
  27 import java.io.PrintStream;
  28 
  29 /**
  30  * Utilities and global definitions for creating CSV output.
  31  */
  32 public final class CSVUtil {
  33     public static final char SEPARATOR = ';';
  34     public static final String SEPARATOR_STR = String.valueOf(SEPARATOR);
  35     public static final char QUOTE = '"';
  36     public static final String QUOTE_STR = String.valueOf(QUOTE);
  37     public static final char ESCAPE = '\\';
  38     public static final String ESCAPE_STR = String.valueOf(ESCAPE);
  39     public static final String ESCAPED_QUOTE_STR = ESCAPE_STR + QUOTE_STR;
  40     public static final String ESCAPED_ESCAPE_STR = ESCAPE_STR + ESCAPE_STR;
  41 
  42     public static String buildFormatString(String format, int num) {
  43         return buildFormatString(format, SEPARATOR, num);
  44     }
  45 
  46     public static String buildFormatString(String... format) {
  47         return String.join(SEPARATOR_STR, format);
  48     }
  49 
  50     public static String buildFormatString(String format, char separator, int num) {
  51         StringBuilder sb = new StringBuilder(num * (format.length() + 1) - 1);
  52         sb.append(format);
  53         for (int i = 1; i < num; i++) {
  54             sb.append(separator).append(format);
  55         }
  56         return sb.toString();
  57     }
  58 
  59     public static final class Escape {
  60 
  61         public static PrintStream println(PrintStream out, String format, Object... args) {
  62             return println(out, SEPARATOR, QUOTE, ESCAPE, format, args);
  63         }
  64 
  65         public static LogStream println(LogStream out, String format, Object... args) {
  66             return println(out, SEPARATOR, QUOTE, ESCAPE, format, args);
  67         }
  68 
  69         public static String escape(String str) {
  70             return escape(str, SEPARATOR, QUOTE, ESCAPE);
  71         }
  72 
  73         public static String escapeRaw(String str) {
  74             return escapeRaw(str, QUOTE, ESCAPE);
  75         }
  76 
  77         public static PrintStream println(PrintStream out, char separator, char quote, char escape, String format, Object... args) {
  78             out.printf(format, escapeArgs(separator, quote, escape, args));
  79             out.println();
  80             return out;
  81         }
  82 
  83         public static LogStream println(LogStream out, char separator, char quote, char escape, String format, Object... args) {
  84             out.printf(format, escapeArgs(separator, quote, escape, args));
  85             out.println();
  86             return out;
  87         }
  88 
  89         public static Object[] escapeArgs(Object... args) {
  90             return escapeArgs(SEPARATOR, QUOTE, ESCAPE, args);
  91         }
  92 
  93         public static Object[] escapeArgs(char separator, char quote, char escape, Object... args) {
  94             String separatorStr = String.valueOf(separator);
  95             for (int i = 0; i < args.length; i++) {
  96                 Object obj = args[i];
  97                 if (obj instanceof String) {
  98                     String str = (String) obj;
  99                     if (str.contains(separatorStr)) {
 100                         args[i] = escapeRaw(str, quote, escape);
 101                     }
 102                 }
 103             }
 104             return args;
 105         }
 106 
 107         public static String escape(String str, char separator, char quote, char escape) {
 108             String separatorStr = String.valueOf(separator);
 109             if (str.contains(separatorStr)) {
 110                 return escapeRaw(str, quote, escape);
 111             }
 112             return str;
 113         }
 114 
 115         public static String escapeRaw(String str, char quote, char escape) {
 116             String quoteStr = String.valueOf(quote);
 117             String escapeStr = String.valueOf(escape);
 118             String escapedEscapeStr = escapeStr + escape;
 119             String escapedQuoteStr = escapeStr + quote;
 120             String str1 = str.replace(escapeStr, escapedEscapeStr);
 121             String str2 = str1.replace(quoteStr, escapedQuoteStr);
 122             String str3 = quoteStr + str2 + quote;
 123             return str3;
 124         }
 125     }
 126 }