1 28 29 30 package com.caucho.widget; 31 32 import com.caucho.portal.generic.FastPrintWriter; 33 import com.caucho.util.L10N; 34 35 import java.io.IOException ; 36 import java.io.Writer ; 37 import java.util.logging.Logger ; 38 39 public class WidgetWriter 40 extends FastPrintWriter 41 { 42 private static L10N L = new L10N( WidgetWriter.class ); 43 44 static protected final Logger log = 45 Logger.getLogger( WidgetWriter.class.getName() ); 46 47 private boolean _isCompact; 48 private boolean _closeElementNeeded; 49 private boolean _closeElementNewline; 50 51 public WidgetWriter( Writer writer ) 52 throws IOException 53 { 54 super( writer ); 55 } 56 57 60 public void setCompact( boolean compact ) 61 { 62 _isCompact = compact; 63 } 64 65 69 public void flush() 70 { 71 closeElementIfNeeded(); 72 } 73 74 public void startElement( String name ) 75 throws IOException 76 { 77 startElement( name, false ); 78 } 79 80 85 public void startElement( String name, boolean newline ) 86 throws IOException 87 { 88 closeElementIfNeeded(); 89 90 write( '<' ); 91 write( name ); 92 93 _closeElementNeeded = true; 94 _closeElementNewline = newline; 95 } 96 97 public void writeAttribute( String name, Object value ) 98 throws IOException 99 { 100 write(' '); 101 writeEscaped( name ); 102 write("=\""); 103 104 writeEscaped( value ); 105 write('\"'); 106 } 107 108 public void writeAttribute( String name, WidgetURL url ) 109 throws IOException 110 { 111 write(' '); 112 write( name ); 113 write("=\""); 114 115 write( url.toString() ); 116 write('\"'); 117 } 118 119 public void writeComment( Object comment ) 120 throws IOException 121 { 122 closeElementIfNeeded(); 123 124 write( "<!--" ); 125 writeEscaped( comment.toString() ); 126 write( "-->" ); 127 } 128 129 132 public void writeText( Object object ) 133 throws IOException 134 { 135 closeElementIfNeeded(); 136 137 String string = object.toString(); 138 int len = string.length(); 139 140 for (int i = 0; i < len; i++) { 141 writeEscaped( string.charAt(i) ); 142 } 143 } 144 145 148 public void writeText( char buf[] ) 149 throws IOException 150 { 151 closeElementIfNeeded(); 152 153 int endIndex = buf.length; 154 155 for (int i = 0; i < endIndex; i++) { 156 writeEscaped( buf[i] ); 157 } 158 } 159 160 163 public void writeText( char buf[], int offset, int length ) 164 throws IOException 165 { 166 closeElementIfNeeded(); 167 168 int endIndex = offset + length; 169 170 for (int i = offset; i < endIndex; i++) { 171 writeEscaped( buf[i] ); 172 } 173 } 174 175 public void writeText( char ch ) 176 throws IOException 177 { 178 closeElementIfNeeded(); 179 180 writeEscaped( ch ); 181 } 182 183 public void endElement( String name ) 184 throws IOException 185 { 186 endElement( name, false ); 187 } 188 189 194 public void endElement( String name, boolean newline ) 195 throws IOException 196 { 197 closeElementIfNeeded(); 198 199 write( "</" ); 200 write( name ); 201 write( ">" ); 202 203 if ( newline ) 204 printlnUnlessCompact(); 205 } 206 207 public void close() 208 { 209 closeElementIfNeeded(); 210 211 super.close(); 212 } 213 214 protected void closeElementIfNeeded() 215 { 216 if ( _closeElementNeeded ) { 217 _closeElementNeeded = false; 218 write( '>' ); 219 220 if ( _closeElementNewline ) { 221 printlnUnlessCompact(); 222 _closeElementNewline = false; 223 } 224 } 225 } 226 227 private void printlnUnlessCompact() 228 { 229 if ( ! _isCompact ) 230 println(); 231 } 232 233 private void writeEscaped( Object object ) 234 throws IOException 235 { 236 String string = object.toString(); 237 238 int len = string.length(); 239 240 for (int i = 0; i < len; i++) { 241 writeEscaped( string.charAt(i) ); 242 } 243 } 244 245 private void writeEscaped( char ch ) 246 throws IOException 247 { 248 switch ( ch ) { 249 case '<': write( "<" ); break; 250 case '>': write( ">" ); break; 251 case '&': write( "&" ); break; 252 case '\"': write( """ ); break; 253 case '\'': write( "’" ); break; 254 default: write( ch ); 255 } 256 } 257 } 258 | Popular Tags |