1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.security.authorization.testwikibuilding; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileNotFoundException; |
24 |
|
import java.net.URL; |
25 |
|
import java.util.Formatter; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.Map; |
28 |
|
import java.util.Stack; |
29 |
|
|
30 |
|
import javax.xml.XMLConstants; |
31 |
|
import javax.xml.parsers.SAXParser; |
32 |
|
import javax.xml.parsers.SAXParserFactory; |
33 |
|
import javax.xml.validation.Schema; |
34 |
|
import javax.xml.validation.SchemaFactory; |
35 |
|
|
36 |
|
import org.xml.sax.Attributes; |
37 |
|
import org.xml.sax.SAXException; |
38 |
|
import org.xml.sax.helpers.DefaultHandler; |
39 |
|
|
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@since |
48 |
|
@version |
49 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 2 |
Complexity Density: 0.2 |
|
50 |
|
public abstract class AbstractTestWiki |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
private final static String TEST_WIKI_DEFINITIONS_DIRECTORY = "testwikis"; |
55 |
|
|
56 |
|
|
57 |
|
private HasWikiContents currentWiki; |
58 |
|
|
59 |
|
|
60 |
|
private HasDocuments currentSpace; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private final Stack<HasAcl> currentRightsHolder = new Stack<HasAcl>(); |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private final Stack<HasUsers> currentUsersHolder = new Stack<HasUsers>(); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@param |
78 |
|
@param |
79 |
|
|
80 |
|
@param |
81 |
|
|
82 |
|
protected abstract HasWikiContents addWiki(String name, String owner, boolean isMainWiki, boolean isReadOnly, |
83 |
|
String alt); |
84 |
|
|
85 |
|
|
86 |
|
@return |
87 |
|
|
88 |
|
public abstract XWikiContext getXWikiContext(); |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
93 |
|
private interface ElementBuilder |
94 |
|
{ |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@param |
99 |
|
|
100 |
|
void startElement(Attributes attributes); |
101 |
|
|
102 |
|
|
103 |
|
void endElement(); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
|
|
| 92.3% |
Uncovered Elements: 5 (65) |
Complexity: 19 |
Complexity Density: 0.43 |
|
109 |
|
private class WikiBuilder extends DefaultHandler |
110 |
|
{ |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 2 |
Complexity Density: - |
|
115 |
|
private abstract class AbstractElementBuilder implements ElementBuilder |
116 |
|
{ |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
117 |
18 |
@Override... |
118 |
|
public void startElement(Attributes attributes) { |
119 |
|
} |
120 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
121 |
70 |
@Override... |
122 |
|
public void endElement() { |
123 |
|
} |
124 |
|
|
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.25 |
|
130 |
|
private abstract class AbstractRightElementBuilder extends AbstractElementBuilder |
131 |
|
{ |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
133 |
30 |
@Override... |
134 |
|
public void startElement(Attributes attributes) { |
135 |
30 |
String name = attributes.getValue("name"); |
136 |
30 |
String type = attributes.getValue("type"); |
137 |
|
|
138 |
30 |
HasAcl rightsHolder = currentRightsHolder.peek(); |
139 |
|
|
140 |
30 |
addRight(rightsHolder, name, type); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@param |
146 |
|
@param |
147 |
|
@param |
148 |
|
|
149 |
|
protected abstract void addRight(HasAcl rightsHolder, String name, String type); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
private final Map<String, ElementBuilder> elementBuilders = new HashMap<String, ElementBuilder>(); |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.5 |
|
160 |
|
private class DeclareElementBuilders { |
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
@param |
165 |
|
@return |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
167 |
180 |
public DeclareElementBuilders declare(String name, ElementBuilder elementBuilder)... |
168 |
|
{ |
169 |
180 |
elementBuilders.put(name, elementBuilder); |
170 |
180 |
return this; |
171 |
|
} |
172 |
|
|
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
175 |
18 |
{... |
176 |
18 |
new DeclareElementBuilders() |
177 |
|
.declare( |
178 |
|
"wikis", |
179 |
|
new AbstractElementBuilder() { |
180 |
|
}) |
181 |
|
.declare( |
182 |
|
"wiki", |
183 |
|
new ElementBuilder() { |
184 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
185 |
26 |
@Override... |
186 |
|
public void startElement(Attributes attributes) { |
187 |
26 |
String name = attributes.getValue("name"); |
188 |
26 |
String owner = attributes.getValue("owner"); |
189 |
26 |
boolean isMainWiki = "true".equals(attributes.getValue("mainWiki")); |
190 |
26 |
boolean isReadOnly = "true".equals(attributes.getValue("readOnly")); |
191 |
26 |
String alt = attributes.getValue("alt"); |
192 |
26 |
HasWikiContents wiki = addWiki(name, owner, isMainWiki, isReadOnly, alt); |
193 |
|
|
194 |
26 |
currentWiki = wiki; |
195 |
26 |
currentRightsHolder.push(currentWiki); |
196 |
26 |
currentUsersHolder.push(currentWiki); |
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
199 |
26 |
@Override... |
200 |
|
public void endElement() { |
201 |
26 |
currentWiki = null; |
202 |
26 |
currentRightsHolder.pop(); |
203 |
26 |
currentUsersHolder.pop(); |
204 |
|
} |
205 |
|
}) |
206 |
|
.declare( |
207 |
|
"user", |
208 |
|
new AbstractElementBuilder() { |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
210 |
22 |
@Override... |
211 |
|
public void startElement(Attributes attributes) { |
212 |
22 |
String name = attributes.getValue("name"); |
213 |
|
|
214 |
22 |
HasUsers usersHolder = currentUsersHolder.peek(); |
215 |
22 |
usersHolder.addUser(name); |
216 |
|
} |
217 |
|
}) |
218 |
|
.declare( |
219 |
|
"group", |
220 |
|
new ElementBuilder() { |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
222 |
5 |
@Override... |
223 |
|
public void startElement(Attributes attributes) { |
224 |
5 |
String name = attributes.getValue("name"); |
225 |
|
|
226 |
5 |
HasUsers group = currentWiki.addGroup(name); |
227 |
5 |
currentUsersHolder.push(group); |
228 |
|
} |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
230 |
5 |
@Override... |
231 |
|
public void endElement() { |
232 |
5 |
currentUsersHolder.pop(); |
233 |
|
} |
234 |
|
}) |
235 |
|
.declare( |
236 |
|
"space", |
237 |
|
new ElementBuilder() { |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
239 |
22 |
@Override... |
240 |
|
public void startElement(Attributes attributes) { |
241 |
22 |
String name = attributes.getValue("name"); |
242 |
22 |
String alt = attributes.getValue("alt"); |
243 |
|
|
244 |
22 |
currentSpace = currentWiki.addSpace(name, alt); |
245 |
22 |
currentRightsHolder.push(currentSpace); |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
248 |
22 |
@Override... |
249 |
|
public void endElement() { |
250 |
22 |
currentSpace = null; |
251 |
22 |
currentRightsHolder.pop(); |
252 |
|
} |
253 |
|
}) |
254 |
|
.declare( |
255 |
|
"document", |
256 |
|
new ElementBuilder() { |
257 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
258 |
24 |
@Override... |
259 |
|
public void startElement(Attributes attributes) { |
260 |
24 |
String name = attributes.getValue("name"); |
261 |
24 |
String creator = attributes.getValue("creator"); |
262 |
24 |
String alt = attributes.getValue("alt"); |
263 |
|
|
264 |
24 |
if (creator == null) { |
265 |
24 |
creator = "XWiki.Admin"; |
266 |
|
} |
267 |
|
|
268 |
24 |
HasAcl document = currentSpace.addDocument(name, creator, alt); |
269 |
24 |
currentRightsHolder.push(document); |
270 |
|
} |
271 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
272 |
24 |
@Override... |
273 |
|
public void endElement() { |
274 |
24 |
currentRightsHolder.pop(); |
275 |
|
} |
276 |
|
}) |
277 |
|
.declare( |
278 |
|
"allowUser", |
279 |
|
new AbstractRightElementBuilder() { |
280 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
281 |
18 |
@Override... |
282 |
|
public void addRight(HasAcl rightsHolder, String name, String type) { |
283 |
18 |
rightsHolder.addAllowUser(name, type); |
284 |
|
} |
285 |
|
}) |
286 |
|
.declare( |
287 |
|
"denyUser", |
288 |
|
new AbstractRightElementBuilder() { |
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
290 |
2 |
@Override... |
291 |
|
public void addRight(HasAcl rightsHolder, String name, String type) { |
292 |
2 |
rightsHolder.addDenyUser(name, type); |
293 |
|
} |
294 |
|
}) |
295 |
|
.declare( |
296 |
|
"allowGroup", |
297 |
|
new AbstractRightElementBuilder() { |
298 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
299 |
10 |
@Override... |
300 |
|
public void addRight(HasAcl rightsHolder, String name, String type) { |
301 |
10 |
rightsHolder.addAllowGroup(name, type); |
302 |
|
} |
303 |
|
}) |
304 |
|
.declare( |
305 |
|
"denyGroup", |
306 |
|
new AbstractRightElementBuilder() { |
307 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
308 |
0 |
@Override... |
309 |
|
public void addRight(HasAcl rightsHolder, String name, String type) { |
310 |
0 |
rightsHolder.addDenyGroup(name, type); |
311 |
|
} |
312 |
|
}); |
313 |
|
|
314 |
|
} |
315 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
316 |
147 |
@Override... |
317 |
|
public void startElement(String uri, |
318 |
|
String localName, |
319 |
|
String qName, |
320 |
|
Attributes attributes) throws SAXException |
321 |
|
{ |
322 |
147 |
getElementBuilder(qName).startElement(attributes); |
323 |
|
} |
324 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
325 |
147 |
@Override... |
326 |
|
public void endElement(String uri, String localName, String qName) |
327 |
|
throws SAXException { |
328 |
|
|
329 |
147 |
getElementBuilder(qName).endElement(); |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
@param |
334 |
|
@return |
335 |
|
@throws |
336 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
337 |
294 |
private ElementBuilder getElementBuilder(String qName)... |
338 |
|
throws SAXException |
339 |
|
{ |
340 |
294 |
ElementBuilder elementBuilder = elementBuilders.get(qName); |
341 |
|
|
342 |
294 |
if (elementBuilder == null) { |
343 |
0 |
throw new SAXException(new Formatter().format("Invalid element name: '%s'", qName).toString()); |
344 |
|
} |
345 |
|
|
346 |
294 |
return elementBuilder; |
347 |
|
} |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
|
@link |
352 |
|
|
353 |
|
|
354 |
|
@param |
355 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
356 |
18 |
protected void loadTestWiki(String name) throws Exception... |
357 |
|
{ |
358 |
18 |
URL testwikiUrl = ClassLoader.getSystemResource(TEST_WIKI_DEFINITIONS_DIRECTORY + File.separatorChar + name); |
359 |
|
|
360 |
18 |
if (testwikiUrl == null) { |
361 |
0 |
throw new FileNotFoundException(name); |
362 |
|
} |
363 |
|
|
364 |
18 |
URL schemaUrl = ClassLoader.getSystemResource("schemas" + File.separatorChar + "wikitest.xsd"); |
365 |
|
|
366 |
18 |
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaUrl); |
367 |
|
|
368 |
18 |
SAXParserFactory parserFactory = SAXParserFactory.newInstance(); |
369 |
|
|
370 |
18 |
parserFactory.setSchema(schema); |
371 |
|
|
372 |
18 |
SAXParser parser = parserFactory.newSAXParser(); |
373 |
|
|
374 |
18 |
String filename = testwikiUrl.toURI().toString(); |
375 |
|
|
376 |
18 |
parser.parse(filename, new WikiBuilder()); |
377 |
|
} |
378 |
|
|
379 |
|
|
380 |
|
} |