001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.csv;
018
019import java.io.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.serializer.*;
023
024/**
025 * Specialized writer for serializing CSV.
026 *
027 * <h5 class='section'>Notes:</h5><ul>
028 *    <li class='note'>
029 *       This class is not intended for external use.
030 * </ul>
031 */
032public class CsvWriter extends SerializerWriter {
033
034   /**
035    * Constructor.
036    *
037    * @param out The writer being wrapped.
038    * @param useWhitespace If <jk>true</jk>, tabs and spaces will be used in output.
039    * @param maxIndent The maximum indentation level.
040    * @param quoteChar The quote character to use (i.e. <js>'\''</js> or <js>'"'</js>)
041    * @param trimStrings If <jk>true</jk>, strings will be trimmed before being serialized.
042    * @param uriResolver The URI resolver for resolving URIs to absolute or root-relative form.
043    */
044   protected CsvWriter(Writer out, boolean useWhitespace, int maxIndent, char quoteChar,
045         boolean trimStrings, UriResolver uriResolver) {
046      super(out, useWhitespace, maxIndent, trimStrings, quoteChar, uriResolver);
047   }
048
049   /**
050    * Writes an entry to the writer.
051    *
052    * @param value The value to write.
053    */
054   public void writeEntry(Object value) {
055      if (value == null)
056         w("null");
057      else {
058         String s = value.toString();
059         boolean mustQuote = false;
060         for (int i = 0; i < s.length() && ! mustQuote; i++) {
061            char c = s.charAt(i);
062            if (Character.isWhitespace(c) || c == ',')
063               mustQuote = true;
064         }
065         if (mustQuote)
066            w('"').w(s).w('"');
067         else
068            w(s);
069      }
070   }
071
072   @Override /* SerializerWriter */
073   public CsvWriter appendln(int indent, String text) {
074      super.appendln(indent, text);
075      return this;
076   }
077   @Override /* SerializerWriter */
078   public CsvWriter appendln(String text) {
079      super.appendln(text);
080      return this;
081   }
082   @Override /* SerializerWriter */
083   public CsvWriter append(int indent, String text) {
084      super.append(indent, text);
085      return this;
086   }
087   @Override /* SerializerWriter */
088   public CsvWriter append(int indent, char c) {
089      super.append(indent, c);
090      return this;
091   }
092   @Override /* SerializerWriter */
093   public CsvWriter s() {
094      super.s();
095      return this;
096   }
097   @Override /* SerializerWriter */
098   public CsvWriter q() {
099      super.q();
100      return this;
101   }
102   @Override /* SerializerWriter */
103   public CsvWriter i(int indent) {
104      super.i(indent);
105      return this;
106   }
107   @Override /* SerializerWriter */
108   public CsvWriter nl(int indent) {
109      super.nl(indent);
110      return this;
111   }
112   @Override /* SerializerWriter */
113   public CsvWriter append(Object text) {
114      super.append(text);
115      return this;
116   }
117   @Override /* SerializerWriter */
118   public CsvWriter append(String text) {
119      super.append(text);
120      return this;
121   }
122   @Override /* SerializerWriter */
123   public CsvWriter appendIf(boolean b, String text) {
124      super.appendIf(b, text);
125      return this;
126   }
127   @Override /* SerializerWriter */
128   public CsvWriter appendIf(boolean b, char c) {
129      super.appendIf(b, c);
130      return this;
131   }
132   @Override /* SerializerWriter */
133   public CsvWriter append(char c) {
134      super.append(c);
135      return this;
136   }
137}