KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > widget > WidgetWriter


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

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 JavaDoc;
36 import java.io.Writer JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 public class WidgetWriter
40   extends FastPrintWriter
41 {
42   private static L10N L = new L10N( WidgetWriter.class );
43
44   static protected final Logger JavaDoc 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 JavaDoc
53   {
54     super( writer );
55   }
56
57   /**
58    * If true do not print unnecessary newlines, default false.
59    */

60   public void setCompact( boolean compact )
61   {
62     _isCompact = compact;
63   }
64
65   /**
66    * Write the '%gt;' of an element if needed, but do not flush the underlying
67    * writer.
68    */

69   public void flush()
70   {
71     closeElementIfNeeded();
72   }
73
74   public void startElement( String JavaDoc name )
75     throws IOException JavaDoc
76   {
77     startElement( name, false );
78   }
79
80   /**
81    * @param name the name of the element
82    * @param newline write a newline after starting the element, unless
83    * compact rendering has been set.
84    */

85   public void startElement( String JavaDoc name, boolean newline )
86     throws IOException JavaDoc
87   {
88     closeElementIfNeeded();
89
90     write( '<' );
91     write( name );
92
93     _closeElementNeeded = true;
94     _closeElementNewline = newline;
95   }
96
97   public void writeAttribute( String JavaDoc name, Object JavaDoc value )
98     throws IOException JavaDoc
99   {
100     write(' ');
101     writeEscaped( name );
102     write("=\"");
103
104     writeEscaped( value );
105     write('\"');
106   }
107
108   public void writeAttribute( String JavaDoc name, WidgetURL url )
109     throws IOException JavaDoc
110   {
111     write(' ');
112     write( name );
113     write("=\"");
114
115     write( url.toString() );
116     write('\"');
117   }
118
119   public void writeComment( Object JavaDoc comment )
120     throws IOException JavaDoc
121   {
122     closeElementIfNeeded();
123
124     write( "<!--" );
125     writeEscaped( comment.toString() );
126     write( "-->" );
127   }
128
129   /**
130    * Write the toString() of the object, escaping as needed
131    */

132   public void writeText( Object JavaDoc object )
133     throws IOException JavaDoc
134   {
135     closeElementIfNeeded();
136
137     String JavaDoc 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   /**
146    * Write the char[], escaping as needed
147    */

148   public void writeText( char buf[] )
149     throws IOException JavaDoc
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   /**
161    * Write the char[], escaping as needed
162    */

163   public void writeText( char buf[], int offset, int length )
164     throws IOException JavaDoc
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 JavaDoc
177   {
178     closeElementIfNeeded();
179
180     writeEscaped( ch );
181   }
182
183   public void endElement( String JavaDoc name )
184     throws IOException JavaDoc
185   {
186     endElement( name, false );
187   }
188
189   /**
190    * @param name the name of the element
191    * @param newline write a newline after closing the element, unless
192    * compact rendering has been set
193    */

194   public void endElement( String JavaDoc name, boolean newline )
195     throws IOException JavaDoc
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 JavaDoc object )
234     throws IOException JavaDoc
235   {
236     String JavaDoc 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 JavaDoc
247   {
248     switch ( ch ) {
249       case '<': write( "&lt;" ); break;
250       case '>': write( "&gt;" ); break;
251       case '&': write( "&amp;" ); break;
252       case '\"': write( "&quot;" ); break;
253       case '\'': write( "&rsquo;" ); break;
254       default: write( ch );
255     }
256   }
257 }
258
Popular Tags