The CSVQuoter is a helper class to encode a string for the CSV file format. : CSV File « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » CSV FileScreenshots 
The CSVQuoter is a helper class to encode a string for the CSV file format.
  
/**
 
 * JFreeReport : a free Java reporting library
 
 *
 * Project Info:  http://reporting.pentaho.org/
 *
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * CSVQuoter.java
 * ------------
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 */


/**
 * The <code>CSVQuoter</code> is a helper class to encode a string for the CSV file
 * format.
 *
 @author Thomas Morgner.
 */
public class CSVQuoter
{
  /**
   * The separator used in the CSV file.
   */
  private char separator;
  /**
   * The quoting character or a single quote.
   */
  private char quate;
  /**
   * The double quote. This is a string containing the quate two times.
   */
  private String doubleQuate;

  /**
   * Creates a new CSVQuoter, which uses a comma as the default separator.
   */
  public CSVQuoter ()
  {
    this(',''"');
  }

  /**
   * Creates a new <code>CSVQuoter</code>, which uses the defined separator.
   *
   @param separator the separator.
   @throws NullPointerException if the given separator is <code>null</code>.
   */
  public CSVQuoter (final char separator)
  {
    this(separator,  '"');
  }

  /**
   * Creates a new CSVQuoter with the given separator and quoting character.
   *
   @param separator the separator
   @param quate the quoting character
   */
  public CSVQuoter (final char separator, final char quate)
  {
    this.separator = separator;
    this.quate = quate;
    this.doubleQuate = String.valueOf(quate+ quate;
  }

  /**
   * Encodes the string, so that the string can safely be used in CSV files. If the string
   * does not need quoting, the original string is returned unchanged.
   *
   @param original the unquoted string.
   @return The quoted string
   */
  public String doQuoting (final String original)
  {
    if (isQuotingNeeded(original))
    {
      final StringBuffer retval = new StringBuffer();
      retval.append(quate);
      applyQuote(retval, original);
      retval.append(quate);
      return retval.toString();
    }
    else
    {
      return original;
    }
  }

  /**
   * Decodes the string, so that all escape sequences get removed. If the string was not
   * quoted, then the string is returned unchanged.
   *
   @param nativeString the quoted string.
   @return The unquoted string.
   */
  public String undoQuoting (final String nativeString)
  {
    if (isQuotingNeeded(nativeString))
    {
      final StringBuffer b = new StringBuffer(nativeString.length());
      final int length = nativeString.length() 1;
      int start = 1;

      int pos = start;
      while (pos != -1)
      {
        pos = nativeString.indexOf(doubleQuate, start);
        if (pos == -1)
        {
          b.append(nativeString.substring(start, length));
        }
        else
        {
          b.append(nativeString.substring(start, pos));
          start = pos + 1;
        }
      }
      return b.toString();
    }
    else
    {
      return nativeString;
    }
  }

  /**
   * Tests, whether this string needs to be quoted. A string is encoded if the string
   * contains a newline character, a quote character or the defined separator.
   *
   @param str the string that should be tested.
   @return true, if quoting needs to be applied, false otherwise.
   */
  private boolean isQuotingNeeded (final String str)
  {
    if (str.indexOf(separator!= -1)
    {
      return true;
    }
    if (str.indexOf('\n'!= -1)
    {
      return true;
    }
    if (str.indexOf(quate, 1!= -1)
    {
      return true;
    }
    return false;
  }

  /**
   * Applies the quoting to a given string, and stores the result in the StringBuffer
   * <code>b</code>.
   *
   @param b        the result buffer
   @param original the string, that should be quoted.
   */
  private void applyQuote (final StringBuffer b, final String original)
  {
    // This solution needs improvements. Copy blocks instead of single
    // characters.
    final int length = original.length();

    for (int i = 0; i < length; i++)
    {
      final char c = original.charAt(i);
      if (c == quate)
      {
        b.append(doubleQuate);
      }
      else
      {
        b.append(c);
      }
    }
  }

  /**
   * Gets the separator used in this quoter and the CSV file.
   *
   @return the separator (never <code>null</code>).
   */
  public char getSeparator ()
  {
    return separator;
  }

  /**
   * Returns the quoting character.
   *
   @return the quote character.
   */
  public char getQuate ()
  {
    return quate;
  }
}

   
    
  
Related examples in the same category
1. A utility class that parses a Comma Separated Values (CSV) file
2. Simple demo of CSV parser classSimple demo of CSV parser class
3. CSV in action: lines from a file and printCSV in action: lines from a file and print
4. Simple demo of CSV matching using Regular Expressions
5. Helper class to write table data to a csv-file (comma separated values).
6. Builds a bracketed CSV list from the array
7. Builds a CSV list from the specified String[], separator string and quote string
8. Builds a CSV list from the specified two dimensional String[][], separator string and quote string.
9. The csv tokenizer class allows an application to break a Comma Separated Value format into tokens.
10. A stream based parser for parsing delimited text data from a file or a stream
11. Reads CSV (Comma Separated Value) files
12. Writes CSV (Comma Separated Value) files
13. Csv Converter
14. CVS reader
15. CSV Writer
16. CSV parser
17. Csv Reader
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.