1 /*
   2  * Copyright (c) 2016, 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 package org.graalvm.compiler.debug;
  24 
  25 import java.io.PrintStream;
  26 
  27 /**
  28  * Utilities and global definitions for creating CSV output.
  29  */
  30 public final class CSVUtil {
  31     public static final char SEPARATOR = ';';
  32     public static final String SEPARATOR_STR = String.valueOf(SEPARATOR);
  33     public static final char QUOTE = '"';
  34     public static final String QUOTE_STR = String.valueOf(QUOTE);
  35     public static final char ESCAPE = '\\';
  36     public static final String ESCAPE_STR = String.valueOf(ESCAPE);
  37     public static final String ESCAPED_QUOTE_STR = ESCAPE_STR + QUOTE_STR;
  38     public static final String ESCAPED_ESCAPE_STR = ESCAPE_STR + ESCAPE_STR;
  39 
  40     public static String buildFormatString(String format, int num) {
  41         return buildFormatString(format, SEPARATOR, num);
  42     }
  43 
  44     public static String buildFormatString(String... format) {
  45         return String.join(SEPARATOR_STR, format);
  46     }
  47 
  48     public static String buildFormatString(String format, char separator, int num) {
  49         StringBuilder sb = new StringBuilder(num * (format.length() + 1) - 1);
  50         sb.append(format);
  51         for (int i = 1; i < num; i++) {
  52             sb.append(separator).append(format);
  53         }
  54         return sb.toString();
  55     }
  56 
  57     public static final class Escape {
  58 
  59         public static PrintStream println(PrintStream out, String format, Object... args) {
  60             return println(out, SEPARATOR, QUOTE, ESCAPE, format, args);
  61         }
  62 
  63         public static LogStream println(LogStream out, String format, Object... args) {
  64             return println(out, SEPARATOR, QUOTE, ESCAPE, format, args);
  65         }
  66 
  67         public static String escape(String str) {
  68             return escape(str, SEPARATOR, QUOTE, ESCAPE);
  69         }
  70 
  71         public static String escapeRaw(String str) {
  72             return escapeRaw(str, QUOTE, ESCAPE);
  73         }
  74 
  75         public static PrintStream println(PrintStream out, char separator, char quote, char escape, String format, Object... args) {
  76             out.printf(format, escapeArgs(separator, quote, escape, args));
  77             out.println();
  78             return out;
  79         }
  80 
  81         public static LogStream println(LogStream out, char separator, char quote, char escape, String format, Object... args) {
  82             out.printf(format, escapeArgs(separator, quote, escape, args));
  83             out.println();
  84             return out;
  85         }
  86 
  87         private static Object[] escapeArgs(char separator, char quote, char escape, Object... args) {
  88             String separatorStr = String.valueOf(separator);
  89             for (int i = 0; i < args.length; i++) {
  90                 Object obj = args[i];
  91                 if (obj instanceof String) {
  92                     String str = (String) obj;
  93                     if (str.contains(separatorStr)) {
  94                         args[i] = escapeRaw(str, quote, escape);
  95                     }
  96                 }
  97             }
  98             return args;
  99         }
 100 
 101         public static String escape(String str, char separator, char quote, char escape) {
 102             String separatorStr = String.valueOf(separator);
 103             if (str.contains(separatorStr)) {
 104                 return escapeRaw(str, quote, escape);
 105             }
 106             return str;
 107         }
 108 
 109         public static String escapeRaw(String str, char quote, char escape) {
 110             String quoteStr = String.valueOf(quote);
 111             String escapeStr = String.valueOf(escape);
 112             String escapedEscapeStr = escapeStr + escape;
 113             String escapedQuoteStr = escapeStr + quote;
 114             String str1 = str.replace(escapeStr, escapedEscapeStr);
 115             String str2 = str1.replace(quoteStr, escapedQuoteStr);
 116             String str3 = quoteStr + str2 + quote;
 117             return str3;
 118         }
 119     }
 120 }