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 Library</a> 028 * @version 2.1.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 ) throws NullPointerException 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 /** 067 * Get the current namespace. 068 * @return the namespace 069 */ 070 public String getEnclosingNamespace() 071 { 072 return m_namespace; 073 } 074 075 /** 076 * Get the space indent offset. 077 * @return the offset value 078 */ 079 public String getOffset() 080 { 081 return m_pad; 082 } 083 084 /** 085 * Write a value to the buffer. 086 * @param value the value to write to the buffer 087 * @exception IOException if an IO error occurs 088 */ 089 public void write( String value ) throws IOException 090 { 091 m_writer.write( value ); 092 } 093 094 /** 095 * Write an int value to the buffer. 096 * @param n the int value to write to the buffer 097 * @exception IOException if an IO error occurs 098 */ 099 public void write( int n ) throws IOException 100 { 101 m_writer.write( n ); 102 } 103 104 /** 105 * Write an character array value to the buffer. 106 * @param array the array value to write to the buffer 107 * @exception IOException if an IO error occurs 108 */ 109 public void write( char[] array ) throws IOException 110 { 111 m_writer.write( array ); 112 } 113 114 /** 115 * Write a value to the buffer following a nl character. 116 * @param value the value to write to the buffer 117 * @exception IOException if an IO error occurs 118 */ 119 public void nl( String value ) throws IOException 120 { 121 m_writer.write( "\n" + m_pad + value ); 122 } 123 124 /** 125 * Write an int value to the buffer following a nl character. 126 * @param n the int value to write to the buffer 127 * @exception IOException if an IO error occurs 128 */ 129 public void nl( int n ) throws IOException 130 { 131 m_writer.write( "\n" + m_pad + n ); 132 } 133 134 /** 135 * Write an character array value to the buffer following a nl character. 136 * @param array the array value to write to the buffer 137 * @exception IOException if an IO error occurs 138 */ 139 public void nl( char[] array ) throws IOException 140 { 141 m_writer.write( "\n" + m_pad ); 142 m_writer.write( array ); 143 } 144 145 /** 146 * Indent the current offset value by 2 space characters. 147 * @return a buffer 148 */ 149 public Buffer indent() 150 { 151 return indent( " " ); 152 } 153 154 /** 155 * Indent the current offset value by a supplied value. 156 * @param indent the indent value 157 * @return a new buffer 158 */ 159 public Buffer indent( String indent ) 160 { 161 return new Buffer( m_writer, m_namespace, m_pad + indent ); 162 } 163 164 /** 165 * Test id the supplied namespace is the current namesapce. 166 * @param namespace to namespace to compare with the current namespace 167 * @return true if the namesapce is current 168 */ 169 public boolean isNamespace( String namespace ) 170 { 171 return m_namespace.equals( namespace ); 172 } 173 174 /** 175 * Creation of a new buffer mapped to the supplied namespace. 176 * @param namespace the namespace 177 * @return a buffer 178 */ 179 public Buffer namespace( String namespace ) 180 { 181 if( m_namespace.equals( namespace ) ) 182 { 183 return this; 184 } 185 else 186 { 187 return new Buffer( m_writer, namespace, m_pad ); 188 } 189 } 190 }