001    // Copyright 2010 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // 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
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.json;
016    
017    import java.io.CharArrayWriter;
018    import java.io.PrintWriter;
019    
020    /**
021     * Base class for {@link JSONArray} and {@link JSONObject} that exists to organize the code
022     * for printing such objects (either compact or pretty).
023     * 
024     * @since 5.2.0
025     */
026    public abstract class JSONCollection
027    {
028        /**
029         * Converts this JSON collection into a parsable string representation.
030         * <p/>
031         * Warning: This method assumes that the data structure is acyclical.
032         * <p>
033         * Starting in release 5.2, the result will be pretty printed for readability.
034         * 
035         * @return a printable, displayable, portable, transmittable representation of the object, beginning with
036         *         <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}</code>&nbsp;<small>(right
037         *         brace)</small>.
038         */
039        @Override
040        public String toString()
041        {
042            CharArrayWriter caw = new CharArrayWriter();
043            PrintWriter pw = new PrintWriter(caw);
044    
045            JSONPrintSession session = new PrettyPrintSession(pw);
046    
047            print(session);
048    
049            pw.close();
050    
051            return caw.toString();
052        }
053    
054        /**
055         * Converts the JSONObject to a compact or pretty-print string representation
056         * 
057         * @param compact
058         *            if true, return minimal format string.
059         * @since 5.2.0
060         */
061        public String toString(boolean compact)
062        {
063            return compact ? toCompactString() : toString();
064        }
065    
066        /**
067         * Prints the JSONObject as a compact string (not extra punctuation). This is, essentially, what
068         * Tapestry 5.1 did inside {@link #toString()}.
069         */
070        public String toCompactString()
071        {
072            CharArrayWriter caw = new CharArrayWriter();
073            PrintWriter pw = new PrintWriter(caw);
074    
075            print(pw);
076    
077            pw.close();
078    
079            return caw.toString();
080        }
081    
082        /**
083         * Prints the JSONObject to the write (compactly or not).
084         * 
085         * @param writer
086         *            to write content to
087         * @param compact
088         *            if true, then write compactly, if false, write with pretty printing
089         * @since 5.2.1
090         */
091        public void print(PrintWriter writer, boolean compact)
092        {
093            JSONPrintSession session = compact ? new CompactSession(writer) : new PrettyPrintSession(writer);
094    
095            print(session);
096        }
097    
098        /**
099         * Prints the JSONObject to the writer compactly (with no extra whitespace).
100         */
101        public void print(PrintWriter writer)
102        {
103            print(writer, true);
104        }
105    
106        /**
107         * Prints the JSONObject to the writer using indentation (two spaces per indentation level).
108         */
109        public void prettyPrint(PrintWriter writer)
110        {
111            print(writer, false);
112        }
113    
114        /**
115         * Print the collection in a parsable format using the session to (optionally) inject extra
116         * whitespace (for "pretty printing").
117         */
118        abstract void print(JSONPrintSession session);
119    }