001    /*
002    // $Id: ParseTreeWriter.java 482 2012-01-05 23:27:27Z jhyde $
003    //
004    // Licensed to Julian Hyde under one or more contributor license
005    // agreements. See the NOTICE file distributed with this work for
006    // additional information regarding copyright ownership.
007    //
008    // Julian Hyde licenses this file to you under the Apache License,
009    // Version 2.0 (the "License"); you may not use this file except in
010    // compliance with the License. You may obtain a copy of the License at:
011    //
012    // http://www.apache.org/licenses/LICENSE-2.0
013    //
014    // Unless required by applicable law or agreed to in writing, software
015    // distributed under the License is distributed on an "AS IS" BASIS,
016    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017    // See the License for the specific language governing permissions and
018    // limitations under the License.
019    */
020    package org.olap4j.mdx;
021    
022    import java.io.PrintWriter;
023    import java.io.Writer;
024    
025    /**
026     * Writer for MDX parse tree.
027     *
028     * <p>Typical use is with the {@link ParseTreeNode#unparse(ParseTreeWriter)}
029     * method as follows:
030     *
031     * <blockquote>
032     * <pre>
033     * ParseTreeNode node;
034     * StringWriter sw = new StringWriter();
035     * PrintWriter pw = new PrintWriter(sw);
036     * ParseTreeWriter mdxWriter = new ParseTreeWriter(pw);
037     * node.unparse(mdxWriter);
038     * pw.flush();
039     * String mdx = sw.toString();
040     * </pre>
041     * </blockquote>
042     *
043     *
044     * @see org.olap4j.mdx.ParseTreeNode#unparse(ParseTreeWriter)
045     *
046     * @author jhyde
047     * @version $Id: ParseTreeWriter.java 482 2012-01-05 23:27:27Z jhyde $
048     * @since Jun 4, 2007
049     */
050    public class ParseTreeWriter {
051        private final PrintWriter pw;
052        private int linePrefixLength;
053        private String linePrefix;
054    
055        private static final int INDENT = 4;
056        private static String bigString = "                ";
057    
058        /**
059         * Creates a ParseTreeWriter.
060         *
061         * @param pw Underlying writer
062         */
063        public ParseTreeWriter(PrintWriter pw) {
064            this((Writer)pw);
065        }
066    
067        /**
068         * Creates a ParseTreeWriter.
069         *
070         * @param w Underlying writer
071         */
072        public ParseTreeWriter(Writer w) {
073            this.pw = new PrintWriter(w) {
074                @Override
075                public void println() {
076                    super.println();
077                    print(linePrefix);
078                }
079            };
080            this.linePrefixLength = 0;
081            setPrefix();
082        }
083    
084        /**
085         * Returns the print writer.
086         *
087         * @return print writer
088         */
089        public PrintWriter getPrintWriter() {
090            return pw;
091        }
092    
093        /**
094         * Increases the indentation level.
095         */
096        public void indent() {
097            linePrefixLength += INDENT;
098            setPrefix();
099        }
100    
101        private void setPrefix() {
102            linePrefix = spaces(linePrefixLength);
103        }
104    
105        /**
106         * Decreases the indentation level.
107         */
108        public void outdent() {
109            linePrefixLength -= INDENT;
110            setPrefix();
111        }
112    
113        /**
114         * Returns a string of N spaces.
115         * @param n Number of spaces
116         * @return String of N spaces
117         */
118        private static synchronized String spaces(int n)
119        {
120            while (n > bigString.length()) {
121                bigString = bigString + bigString;
122            }
123            return bigString.substring(0, n);
124        }
125    }
126    
127    // End ParseTreeWriter.java