1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets; |
20 | |
|
21 | |
import org.apache.myfaces.view.facelets.util.FastWriter; |
22 | |
|
23 | |
import javax.faces.context.FacesContext; |
24 | |
import java.io.IOException; |
25 | |
import java.io.Writer; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public final class StateWriter extends Writer |
48 | |
{ |
49 | |
|
50 | |
private static final String CURRENT_WRITER_KEY = "org.apache.myfaces.view.facelets.StateWriter.CURRENT_WRITER"; |
51 | |
|
52 | |
private int initialSize; |
53 | |
private Writer out; |
54 | |
private FastWriter fast; |
55 | |
private boolean writtenState; |
56 | |
private boolean writtenStateWithoutWrapper; |
57 | |
|
58 | |
static public StateWriter getCurrentInstance() |
59 | |
{ |
60 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
61 | |
|
62 | 0 | return (StateWriter)facesContext.getAttributes().get(CURRENT_WRITER_KEY); |
63 | |
} |
64 | |
|
65 | |
static public StateWriter getCurrentInstance(FacesContext facesContext) |
66 | |
{ |
67 | 0 | return (StateWriter)facesContext.getAttributes().get(CURRENT_WRITER_KEY); |
68 | |
} |
69 | |
|
70 | |
private static void setCurrentInstance(StateWriter stateWriter) |
71 | |
{ |
72 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
73 | |
|
74 | 0 | if (stateWriter == null) |
75 | |
{ |
76 | 0 | facesContext.getAttributes().remove(CURRENT_WRITER_KEY); |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 0 | facesContext.getAttributes().put(CURRENT_WRITER_KEY, stateWriter); |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
private static void setCurrentInstance(StateWriter stateWriter, FacesContext facesContext) |
85 | |
{ |
86 | |
|
87 | |
|
88 | 0 | if (stateWriter == null) |
89 | |
{ |
90 | 0 | facesContext.getAttributes().remove(CURRENT_WRITER_KEY); |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | 0 | facesContext.getAttributes().put(CURRENT_WRITER_KEY, stateWriter); |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
public StateWriter(Writer initialOut, int initialSize) |
99 | 0 | { |
100 | 0 | if (initialSize < 0) |
101 | |
{ |
102 | 0 | throw new IllegalArgumentException("Initial Size cannot be less than 0"); |
103 | |
} |
104 | |
|
105 | 0 | this.initialSize = initialSize; |
106 | 0 | this.out = initialOut; |
107 | 0 | setCurrentInstance(this); |
108 | 0 | } |
109 | |
|
110 | |
public StateWriter(Writer initialOut, int initialSize, FacesContext facesContext) |
111 | 0 | { |
112 | 0 | if (initialSize < 0) |
113 | |
{ |
114 | 0 | throw new IllegalArgumentException("Initial Size cannot be less than 0"); |
115 | |
} |
116 | |
|
117 | 0 | this.initialSize = initialSize; |
118 | 0 | this.out = initialOut; |
119 | 0 | setCurrentInstance(this, facesContext); |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void writingState() |
129 | |
{ |
130 | 0 | if (!this.writtenState) |
131 | |
{ |
132 | 0 | this.writtenState = true; |
133 | 0 | this.writtenStateWithoutWrapper = false; |
134 | 0 | this.fast = new FastWriter(this.initialSize); |
135 | 0 | this.out = this.fast; |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
public boolean isStateWritten() |
140 | |
{ |
141 | 0 | return this.writtenState; |
142 | |
} |
143 | |
|
144 | |
public void writingStateWithoutWrapper() |
145 | |
{ |
146 | 0 | if (!this.writtenState && !this.writtenStateWithoutWrapper) |
147 | |
{ |
148 | 0 | this.writtenStateWithoutWrapper = true; |
149 | |
} |
150 | 0 | } |
151 | |
|
152 | |
public boolean isStateWrittenWithoutWrapper() |
153 | |
{ |
154 | 0 | return this.writtenStateWithoutWrapper; |
155 | |
} |
156 | |
|
157 | |
public void close() throws IOException |
158 | |
{ |
159 | |
|
160 | 0 | } |
161 | |
|
162 | |
public void flush() throws IOException |
163 | |
{ |
164 | 0 | if (!this.writtenState) |
165 | |
{ |
166 | 0 | this.out.flush(); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
public void write(char[] cbuf, int off, int len) throws IOException |
171 | |
{ |
172 | 0 | this.out.write(cbuf, off, len); |
173 | 0 | } |
174 | |
|
175 | |
public void write(char[] cbuf) throws IOException |
176 | |
{ |
177 | 0 | this.out.write(cbuf); |
178 | 0 | } |
179 | |
|
180 | |
public void write(int c) throws IOException |
181 | |
{ |
182 | 0 | this.out.write(c); |
183 | 0 | } |
184 | |
|
185 | |
public void write(String str, int off, int len) throws IOException |
186 | |
{ |
187 | 0 | this.out.write(str, off, len); |
188 | 0 | } |
189 | |
|
190 | |
public void write(String str) throws IOException |
191 | |
{ |
192 | 0 | this.out.write(str); |
193 | 0 | } |
194 | |
|
195 | |
public String getAndResetBuffer() |
196 | |
{ |
197 | 0 | if (!this.writtenState) |
198 | |
{ |
199 | 0 | throw new IllegalStateException("Did not write state; no buffer is available"); |
200 | |
} |
201 | |
|
202 | 0 | String result = this.fast.toString(); |
203 | 0 | this.fast.reset(); |
204 | 0 | return result; |
205 | |
} |
206 | |
|
207 | |
public void release() |
208 | |
{ |
209 | |
|
210 | 0 | setCurrentInstance(null); |
211 | 0 | } |
212 | |
|
213 | |
public void release(FacesContext facesContext) |
214 | |
{ |
215 | |
|
216 | 0 | setCurrentInstance(null, facesContext); |
217 | 0 | } |
218 | |
|
219 | |
} |