1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared.resource; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.io.PushbackInputStream; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
import java.util.logging.Level; |
27 | |
import java.util.logging.Logger; |
28 | |
|
29 | |
import javax.el.ELContext; |
30 | |
import javax.el.ELException; |
31 | |
import javax.el.ValueExpression; |
32 | |
import javax.faces.application.Resource; |
33 | |
import javax.faces.context.FacesContext; |
34 | |
import javax.faces.event.ExceptionQueuedEvent; |
35 | |
import javax.faces.event.ExceptionQueuedEventContext; |
36 | |
|
37 | |
import org.apache.myfaces.shared.util.io.DynamicPushbackInputStream; |
38 | |
|
39 | |
public class ValueExpressionFilterInputStream extends InputStream |
40 | |
{ |
41 | |
private PushbackInputStream delegate; |
42 | |
private String libraryName; |
43 | |
private String resourceName; |
44 | |
private String contractName; |
45 | |
|
46 | |
public ValueExpressionFilterInputStream(InputStream in, String libraryName, String resourceName) |
47 | |
{ |
48 | 0 | super(); |
49 | 0 | delegate = new DynamicPushbackInputStream(in,300); |
50 | 0 | this.libraryName = libraryName; |
51 | 0 | this.resourceName = resourceName; |
52 | 0 | this.contractName = null; |
53 | 0 | } |
54 | |
|
55 | |
public ValueExpressionFilterInputStream(InputStream in, Resource resource) |
56 | |
{ |
57 | 0 | super(); |
58 | 0 | delegate = new DynamicPushbackInputStream(in,300); |
59 | 0 | this.libraryName = resource.getLibraryName(); |
60 | 0 | this.resourceName = resource.getResourceName(); |
61 | 0 | this.contractName = (resource instanceof ContractResource) ? |
62 | |
((ContractResource)resource).getContractName() : null; |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public int read() throws IOException |
67 | |
{ |
68 | 0 | int c1 = delegate.read(); |
69 | |
|
70 | 0 | if (c1 == -1) |
71 | |
{ |
72 | 0 | return -1; |
73 | |
} |
74 | |
|
75 | 0 | if ( ((char)c1) == '#') |
76 | |
{ |
77 | 0 | int c2 = delegate.read(); |
78 | 0 | if (c2 == -1) |
79 | |
{ |
80 | 0 | return -1; |
81 | |
} |
82 | 0 | if (((char)c2) == '{') |
83 | |
{ |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 0 | List<Integer> expressionList = new ArrayList<Integer>(); |
89 | 0 | int c3 = delegate.read(); |
90 | 0 | while ( c3 != -1 && ((char)c3) != '}' ) |
91 | |
{ |
92 | 0 | expressionList.add(c3); |
93 | 0 | c3 = delegate.read(); |
94 | |
} |
95 | |
|
96 | 0 | if (c3 == -1) |
97 | |
{ |
98 | |
|
99 | |
|
100 | 0 | for (int i = 0; i < expressionList.size(); i++) |
101 | |
{ |
102 | 0 | delegate.unread(expressionList.get(i)); |
103 | |
} |
104 | 0 | delegate.unread(c2); |
105 | 0 | return c1; |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | |
|
110 | |
|
111 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
112 | 0 | ELContext elContext = context.getELContext(); |
113 | |
try |
114 | |
{ |
115 | 0 | if (libraryName != null) |
116 | |
{ |
117 | 0 | ResourceELUtils.saveResourceLibraryForResolver(context, libraryName); |
118 | |
} |
119 | 0 | if (contractName != null) |
120 | |
{ |
121 | 0 | ResourceELUtils.saveResourceContractForResolver(context, contractName); |
122 | |
} |
123 | 0 | ValueExpression ve = context.getApplication(). |
124 | |
getExpressionFactory().createValueExpression( |
125 | |
elContext, |
126 | |
"#{"+convertToExpression(expressionList)+"}", |
127 | |
String.class); |
128 | 0 | String value = (String) ve.getValue(elContext); |
129 | |
|
130 | 0 | for (int i = value.length()-1; i >= 0 ; i--) |
131 | |
{ |
132 | 0 | delegate.unread((int) value.charAt(i)); |
133 | |
} |
134 | |
} |
135 | 0 | catch(ELException e) |
136 | |
{ |
137 | 0 | ExceptionQueuedEventContext equecontext = new ExceptionQueuedEventContext ( |
138 | |
context, e, null); |
139 | 0 | context.getApplication().publishEvent (context, ExceptionQueuedEvent.class, equecontext); |
140 | |
|
141 | 0 | Logger log = Logger.getLogger(ResourceImpl.class.getName()); |
142 | 0 | if (log.isLoggable(Level.SEVERE)) |
143 | |
{ |
144 | 0 | log.severe("Cannot evaluate EL expression " + convertToExpression(expressionList) |
145 | |
+ " in resource " + (libraryName == null?"":libraryName) + ":" + |
146 | |
(resourceName == null?"":resourceName)); |
147 | |
} |
148 | |
|
149 | 0 | delegate.unread(c3); |
150 | 0 | for (int i = expressionList.size()-1; i >= 0; i--) |
151 | |
{ |
152 | 0 | delegate.unread(expressionList.get(i)); |
153 | |
} |
154 | 0 | delegate.unread(c2); |
155 | 0 | return c1; |
156 | |
} |
157 | |
finally |
158 | |
{ |
159 | 0 | if (libraryName != null) |
160 | |
{ |
161 | 0 | ResourceELUtils.removeResourceLibraryForResolver(context); |
162 | |
} |
163 | 0 | if (contractName != null) |
164 | |
{ |
165 | 0 | ResourceELUtils.removeResourceContractForResolver(context); |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
|
170 | 0 | return delegate.read(); |
171 | |
} |
172 | |
} |
173 | |
else |
174 | |
{ |
175 | 0 | delegate.unread(c2); |
176 | 0 | return c1; |
177 | |
} |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | |
|
182 | 0 | return c1; |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
private String convertToExpression(List<Integer> expressionList) |
187 | |
{ |
188 | 0 | char[] exprArray = new char[expressionList.size()]; |
189 | |
|
190 | 0 | for (int i = 0; i < expressionList.size(); i++) |
191 | |
{ |
192 | 0 | exprArray[i] = (char) expressionList.get(i).intValue(); |
193 | |
} |
194 | 0 | return String.valueOf(exprArray); |
195 | |
} |
196 | |
|
197 | |
@Override |
198 | |
public void close() throws IOException |
199 | |
{ |
200 | 0 | delegate.close(); |
201 | 0 | } |
202 | |
} |