KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > output > FragmentResult


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.output;
10
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16
17 import org.jboss.portal.server.WindowContext;
18
19 /**
20  * Data produced.
21  *
22  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
23  * @version $Revision: 1.2 $
24  */

25 public class FragmentResult extends Result
26 {
27    /** The output as a bytes if any. */
28    private ByteArrayOutputStream JavaDoc bytes;
29
30    /** The output as chars if any. */
31    private StringWriter JavaDoc chars;
32
33    /** The writer that will produce the chars output if any. */
34    private PrintWriter JavaDoc writer;
35
36    /** The result content type if any. */
37    private String JavaDoc contentType;
38
39    /** The title if any. */
40    private String JavaDoc title;
41
42    public FragmentResult(WindowContext producer)
43    {
44       super(producer);
45       this.bytes = null;
46       this.chars = null;
47       this.writer = null;
48       this.contentType = null;
49       this.title = null;
50    }
51
52    public ByteArrayOutputStream JavaDoc getBytes()
53    {
54       return bytes;
55    }
56
57    public StringWriter JavaDoc getChars()
58    {
59       return chars;
60    }
61
62    public String JavaDoc getTitle()
63    {
64       return title;
65    }
66
67    public void setTitle(String JavaDoc title)
68    {
69       this.title = title;
70    }
71
72    public String JavaDoc getContentType()
73    {
74       return contentType;
75    }
76
77    public void setContentType(String JavaDoc contentType)
78    {
79       this.contentType = contentType;
80    }
81
82    /**
83     * @throws IllegalStateException if the output stream is already used or if no content type is defined
84     */

85    public PrintWriter JavaDoc getWriter()throws IllegalStateException JavaDoc
86    {
87       if (bytes != null)
88       {
89          throw new IllegalStateException JavaDoc("The window output stream is already used");
90       }
91       if (contentType == null)
92       {
93          throw new IllegalStateException JavaDoc("No content type defined");
94       }
95       if (chars == null)
96       {
97          chars = new StringWriter JavaDoc();
98          writer = new PrintWriter JavaDoc(chars);
99       }
100       return writer;
101    }
102
103    /**
104     * @throws IOException
105     * @throws IllegalStateException if the window writer is already used or if no content type is defined
106     */

107    public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc, IllegalStateException JavaDoc
108    {
109       if (chars != null)
110       {
111          throw new IllegalStateException JavaDoc("The window writer is already used");
112       }
113       if (contentType == null)
114       {
115          throw new IllegalStateException JavaDoc("No content type defined");
116       }
117       if (bytes == null)
118       {
119          bytes = new ByteArrayOutputStream JavaDoc();
120       }
121       return bytes;
122    }
123
124    public void resetBuffer()
125    {
126       if (bytes != null)
127       {
128          bytes.reset();
129       }
130       else if (chars != null)
131       {
132          chars.flush();
133          chars.getBuffer().setLength(0);
134       }
135    }
136 }
137
Popular Tags