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