001    /*
002     * Copyright 2005 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.lang;
020    
021    import java.io.Writer;
022    import java.io.IOException;
023    
024    /**
025     * Utility class used as a destination during generalized object encoding.
026     *
027     * @author <a href="http://www.dpml.net">Digital Product Management Laboratory</a>
028     * @version 2.0.1
029     */
030    public final class Buffer
031    {
032        private final String m_namespace;
033        private final String m_pad;
034        private final Writer m_writer;
035        
036       /**
037        * Construct a new encoding buffer.
038        *
039        * @param writer the output stream writer
040        * @param namespace the current namespace
041        * @param pad the current offset
042        * @exception NullPointerException of the writer or namespace arguments are null
043        */
044        public Buffer( final Writer writer, final String namespace, final String pad )
045        {
046            if( null == writer )
047            {
048                throw new NullPointerException( "writer" );
049            }
050            if( null == namespace )
051            {
052                throw new NullPointerException( "namespace" );
053            }
054            if( null == pad )
055            {
056                m_pad = "";
057            }
058            else
059            {
060                m_pad = pad;
061            }
062            m_namespace = namespace;
063            m_writer = writer;
064        }
065        
066        public String getEnclosingNamespace()
067        {
068            return m_namespace;
069        }
070        
071        public String getOffset()
072        {
073            return m_pad;
074        }
075        
076        public void write( String value ) throws IOException
077        {
078            m_writer.write( value );
079        }
080        
081        public void write( int n ) throws IOException
082        {
083            m_writer.write( n );
084        }
085        
086        public void write( char[] array ) throws IOException
087        {
088            m_writer.write( array );
089        }
090        
091        public void nl( String value ) throws IOException
092        {
093            m_writer.write( "\n" + m_pad + value );
094        }
095        
096        public void nl( int n ) throws IOException
097        {
098            m_writer.write( "\n" + m_pad + n );
099        }
100        
101        public void nl( char[] array ) throws IOException
102        {
103            m_writer.write( "\n" + m_pad );
104            m_writer.write( array );
105        }
106        
107        public Buffer indent()
108        {
109            return indent( "  " );
110        }
111        
112        public Buffer indent( String indent )
113        {
114            return new Buffer( m_writer, m_namespace, m_pad + indent );
115        }
116        
117        public boolean isNamespace( String namespace )
118        {
119            return m_namespace.equals( namespace );
120        }
121        
122        public Buffer namespace( String namespace )
123        {
124            if( m_namespace.equals( namespace ) )
125            {
126                return this;
127            }
128            else
129            {
130                return new Buffer( m_writer, namespace, m_pad );
131            }
132        }
133    }