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