001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.serialize;
007:
008: import java.io.IOException;
009: import java.io.OutputStream;
010: import java.io.UnsupportedEncodingException;
011: import java.io.Writer;
012:
013: /**
014: * Caching version of the HTML serializer
015: * @author Peter Kharchenko
016: * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
017: * @see Serializer
018: */
019: public final class CachingHTMLSerializer extends HTMLSerializer
020: implements CachingSerializer {
021:
022: CharacterCachingWriter cacher;
023: String encoding;
024:
025: /**
026: * Constructs a new serializer. The serializer cannot be used without
027: * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
028: * first.
029: */
030: public CachingHTMLSerializer() {
031: super ();
032: }
033:
034: /**
035: * Constructs a new HTML/XHTML serializer depending on the value of
036: * <tt>xhtml</tt>. The serializer cannot be used without calling
037: * init() first.
038: *
039: * @param xhtml True if XHTML serializing
040: */
041: protected CachingHTMLSerializer(boolean xhtml, OutputFormat format) {
042: super (xhtml, format);
043: if (format != null) {
044: this .encoding = format.getEncoding();
045: }
046: }
047:
048: /**
049: * Constructs a new serializer. The serializer cannot be used without
050: * calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
051: * first.
052: */
053: public CachingHTMLSerializer(OutputFormat format) {
054: super (format);
055: if (format != null) {
056: this .encoding = format.getEncoding();
057: }
058: }
059:
060: /**
061: * Constructs a new serializer that writes to the specified writer
062: * using the specified output format. If <tt>format</tt> is null,
063: * will use a default output format.
064: *
065: * @param writer The writer to use
066: * @param format The output format to use, null for the default
067: */
068: public CachingHTMLSerializer(Writer writer, OutputFormat format) {
069: super (writer, format);
070: CachingWriter cw = new CachingWriter(writer);
071: this .cacher = cw;
072: setOutputCharStream(cw);
073: if (format != null) {
074: this .encoding = format.getEncoding();
075: }
076: }
077:
078: public void setOutputCharStream(Writer writer) {
079: CachingWriter cw = new CachingWriter(writer);
080: this .cacher = cw;
081: super .setOutputCharStream(cw);
082: }
083:
084: /**
085: * Constructs a new serializer that writes to the specified output
086: * stream using the specified output format. If <tt>format</tt>
087: * is null, will use a default output format.
088: *
089: * @param output The output stream to use
090: * @param format The output format to use, null for the default
091: */
092: public CachingHTMLSerializer(OutputStream output,
093: OutputFormat format) {
094: super (output, format);
095: CachingOutputStream cos = new CachingOutputStream(output);
096: this .cacher = cos;
097: setOutputByteStream(cos);
098: if (format != null) {
099: this .encoding = format.getEncoding();
100: }
101: }
102:
103: public void setOutputByteStream(OutputStream output) {
104: CachingOutputStream cos = new CachingOutputStream(output);
105: this .cacher = cos;
106: super .setOutputByteStream(cos);
107: }
108:
109: public void setOutputFormat(OutputFormat format) {
110: super .setOutputFormat(format);
111: if (format != null) {
112: this .encoding = format.getEncoding();
113: }
114: }
115:
116: // caching methods
117: /**
118: * When starting caching if we are inside an opening tag the ">" will
119: * be written in order for the ">" to be included with the correct cache.
120: *
121: * Normally the serializer doesn't know if a ">" or "/>" should be written
122: * until some content is received or the tag is closed. When starting
123: * caching after an opening tag the tag will be assumed to have some content
124: * and will write out the ">" before starting the cache.
125: */
126: public boolean startCaching() throws IOException {
127: content();
128: _printer.flush();
129: return cacher.startCaching();
130: }
131:
132: /**
133: * When stopping caching if we are inside an opening tag the ">" will
134: * be written in order for the ">" to be included with the correct cache.
135: *
136: * Normally the serializer doesn't know if a ">" or "/>" should be written
137: * until some content is received or the tag is closed. When starting
138: * caching after an opening tag the tag will be assumed to have some content
139: * and will write out the ">" before starting the cache.
140: */
141: public boolean stopCaching() throws IOException {
142: content();
143: _printer.flush();
144: return cacher.stopCaching();
145: }
146:
147: public String getCache() throws UnsupportedEncodingException,
148: IOException {
149: _printer.flush();
150: return cacher.getCache(this .encoding);
151: }
152:
153: /**
154: * Allows one to print a <code>String</code> of characters directly to the output stream.
155: *
156: * @param text a <code>String</code> value
157: */
158: public void printRawCharacters(String text) throws IOException {
159: content();
160: _printer.printText(text);
161: // _printer.flush();
162: }
163:
164: /**
165: * Let the serializer know if the document has already been started.
166: *
167: * @param setting a <code>boolean</code> value
168: */
169: public void setDocumentStarted(boolean setting) {
170: _started = setting;
171: }
172:
173: public void flush() throws IOException {
174: _printer.flush();
175: cacher.flush();
176: }
177:
178: }
|