ezvcard.io.json
Class JCardWriter

java.lang.Object
  extended by ezvcard.io.json.JCardWriter
All Implemented Interfaces:
Closeable, Flushable

public class JCardWriter
extends Object
implements Closeable, Flushable

Writes VCard objects to a JSON data stream (jCard format).

Example:

 VCard vcard1 = ...
 VCard vcard2 = ...
 
 File file = new File("vcard.json");
 JCardWriter jcardWriter = new JCardWriter(file);
 jcardWriter.write(vcard1);
 jcardWriter.write(vcard2);
 jcardWriter.close(); //"close()" must be called in order to terminate the JSON object
 

Author:
Michael Angstadt

Constructor Summary
JCardWriter(File file)
          Creates a writer that writes jCards to a file (UTF-8 encoding will be used).
JCardWriter(File file, boolean wrapInArray)
          Creates a writer that writes jCards to a file (UTF-8 encoding will be used).
JCardWriter(OutputStream out)
          Creates a writer that writes jCards to an output stream (UTF-8 encoding will be used).
JCardWriter(OutputStream out, boolean wrapInArray)
          Creates a writer that writes jCards to an output stream (UTF-8 encoding will be used).
JCardWriter(Writer writer)
          Creates a writer that writes jCards to a writer.
JCardWriter(Writer writer, boolean wrapInArray)
          Creates a writer that writes jCards to a writer.
 
Method Summary
 void close()
          Ends the jCard data stream and closes the underlying writer.
 void closeJsonStream()
          Ends the jCard data stream, but does not close the underlying writer.
 void flush()
          Flushes the jCard data stream.
 ScribeIndex getScribeIndex()
          Gets the scribe index.
 boolean isAddProdId()
          Gets whether or not a "PRODID" property will be added to each vCard, saying that the vCard was generated by this library.
 boolean isIndent()
          Gets whether or not the JSON will be pretty-printed.
 boolean isVersionStrict()
          Gets whether properties that do not support jCard (vCard version 4.0) will be excluded from the written vCard.
 void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe)
           Registers a property scribe.
 void setAddProdId(boolean addProdId)
          Sets whether or not to add a "PRODID" property to each vCard, saying that the vCard was generated by this library.
 void setIndent(boolean indent)
          Sets whether or not to pretty-print the JSON.
 void setScribeIndex(ScribeIndex index)
          Sets the scribe index.
 void setVersionStrict(boolean versionStrict)
          Sets whether properties that do not support jCard (vCard version 4.0) will be excluded from the written vCard.
 void write(VCard vcard)
          Writes a vCard to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JCardWriter

public JCardWriter(OutputStream out)
Creates a writer that writes jCards to an output stream (UTF-8 encoding will be used).

Parameters:
out - the output stream to write the vCard to

JCardWriter

public JCardWriter(OutputStream out,
                   boolean wrapInArray)
Creates a writer that writes jCards to an output stream (UTF-8 encoding will be used).

Parameters:
out - the output stream to write the vCard to
wrapInArray - true to enclose all written vCards in a JSON array, false not to

JCardWriter

public JCardWriter(File file)
            throws IOException
Creates a writer that writes jCards to a file (UTF-8 encoding will be used).

Parameters:
file - the file to write the vCard to
Throws:
IOException - if there's a problem opening the file

JCardWriter

public JCardWriter(File file,
                   boolean wrapInArray)
            throws IOException
Creates a writer that writes jCards to a file (UTF-8 encoding will be used).

Parameters:
file - the file to write the vCard to
wrapInArray - true to enclose all written vCards in a JSON array, false not to
Throws:
IOException - if there's a problem opening the file

JCardWriter

public JCardWriter(Writer writer)
Creates a writer that writes jCards to a writer.

Parameters:
writer - the writer to write the vCard to

JCardWriter

public JCardWriter(Writer writer,
                   boolean wrapInArray)
Creates a writer that writes jCards to a writer.

Parameters:
writer - the writer to write the vCard to
wrapInArray - true to enclose all written vCards in a JSON array, false not to
Method Detail

write

public void write(VCard vcard)
           throws IOException
Writes a vCard to the stream.

Parameters:
vcard - the vCard to write
Throws:
IOException - if there's a problem writing to the output stream
IllegalArgumentException - if a scribe hasn't been registered for a custom property class (see: registerScribe(ezvcard.io.scribe.VCardPropertyScribe))

isAddProdId

public boolean isAddProdId()
Gets whether or not a "PRODID" property will be added to each vCard, saying that the vCard was generated by this library.

Returns:
true if this property will be added, false if not (defaults to true)

setAddProdId

public void setAddProdId(boolean addProdId)
Sets whether or not to add a "PRODID" property to each vCard, saying that the vCard was generated by this library.

Parameters:
addProdId - true to add this property, false not to (defaults to true)

isVersionStrict

public boolean isVersionStrict()
Gets whether properties that do not support jCard (vCard version 4.0) will be excluded from the written vCard.

Returns:
true to exclude properties that do not support jCard, false to include them anyway (defaults to true)

setVersionStrict

public void setVersionStrict(boolean versionStrict)
Sets whether properties that do not support jCard (vCard version 4.0) will be excluded from the written vCard.

Parameters:
versionStrict - true to exclude properties that do not support jCard, false to include them anyway (defaults to true)

isIndent

public boolean isIndent()
Gets whether or not the JSON will be pretty-printed.

Returns:
true if it will be pretty-printed, false if not (defaults to false)

setIndent

public void setIndent(boolean indent)
Sets whether or not to pretty-print the JSON.

Parameters:
indent - true to pretty-print it, false not to (defaults to false)

registerScribe

public void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe)

Registers a property scribe. This is the same as calling:

getScribeIndex().register(scribe)

Parameters:
scribe - the scribe to register

getScribeIndex

public ScribeIndex getScribeIndex()
Gets the scribe index.

Returns:
the scribe index

setScribeIndex

public void setScribeIndex(ScribeIndex index)
Sets the scribe index.

Parameters:
index - the scribe index

flush

public void flush()
           throws IOException
Flushes the jCard data stream.

Specified by:
flush in interface Flushable
Throws:
IOException - if there's a problem flushing the stream

closeJsonStream

public void closeJsonStream()
                     throws IOException
Ends the jCard data stream, but does not close the underlying writer.

Throws:
IOException - if there's a problem closing the stream

close

public void close()
           throws IOException
Ends the jCard data stream and closes the underlying writer.

Specified by:
close in interface Closeable
Throws:
IOException - if there's a problem closing the stream


Copyright © 2012-2014 Michael Angstadt. All Rights Reserved.