package votorola.g.script; // Copyright 2011-2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import votorola.g.lang.*; /** A writer that outputs JSON string content, properly escaping any characters that * require it. * * @see RFC 4627 */ public @ThreadSafe final class JSONStringWriter extends Writer { /** Constructs a JSONStringWriter. * * @param _out the underlying writer. */ public JSONStringWriter( Writer _out ) { if( _out == null ) throw new NullPointerException(); // fail fast out = _out; } // - A p p e n d a b l e -------------------------------------------------------------- public @Override Writer append( final char c ) throws IOException { switch( c ) { case '\\': case '"': out.append( '\\' ).append( c ); break; case '\b': out.append( '\\' ).append( 'b' ); break; case '\f': out.append( '\\' ).append( 'f' ); break; case '\n': out.append( '\\' ).append( 'n' ); break; case '\r': out.append( '\\' ).append( 'r' ); break; case '\t': out.append( '\\' ).append( 't' ); break; default: if( c > 0x1F ) out.append( c ); else { // Note that JSON string content, like Java, is always Unicode. So no // escaping need be done for out-of-range characters, only for these // control characters. out.append( '\\' ).append( 'u' ); out.append( String.format( "%04x", (int)c )); } } return JSONStringWriter.this; } public @Override Writer append( final CharSequence csq ) throws IOException { final int cN = csq.length(); for( int c = 0; c < cN; ++c ) append( csq.charAt( c )); return JSONStringWriter.this; } /** Throws {@linkplain UnsupportedOperationException UnsupportedOperationException} * because it is not yet needed. */ public @Override Writer append( CharSequence _csq, int _start, int _end ) throws IOException { throw new UnsupportedOperationException(); } // - C l o s e a b l e ---------------------------------------------------------------- /** Does nothing but close the underlying writer. This writer itself need not be * closed. */ public @Override void close() throws IOException { out.close(); } // Closed state is never tested during operations on this writer. Closure has no // effect on its state. The underlying writer (out) is relied on to fulfill the // contract in this regard. // - F l u s h a b l e ---------------------------------------------------------------- /** Does nothing but flush the underlying writer. This writer itself need not be * flushed. */ public @Override void flush() throws IOException { out.flush(); } // - W r i t e r ---------------------------------------------------------------------- /** Throws {@linkplain UnsupportedOperationException UnsupportedOperationException} * because it is not yet needed. */ public @Override void write( char[] _cbuf ) throws IOException { throw new UnsupportedOperationException(); } /** Throws {@linkplain UnsupportedOperationException UnsupportedOperationException} * because it is not yet needed. */ public @Override void write( char[] _cbuf, int _off, int _len ) throws IOException { throw new UnsupportedOperationException(); } public @Override void write( final int c ) throws IOException { append( (char)c ); } /** Throws {@linkplain UnsupportedOperationException UnsupportedOperationException} * because it is not yet needed. */ public @Override void write( String _str, int _off, int _len ) throws IOException { throw new UnsupportedOperationException(); } public @Override void write( final String str ) throws IOException { append( str ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Writer out; }