1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.wikibridge; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.xwiki.bridge.DocumentModelBridge; |
31 |
|
import org.xwiki.bridge.event.AbstractDocumentEvent; |
32 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
33 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
34 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.observation.EventListener; |
38 |
|
import org.xwiki.observation.event.Event; |
39 |
|
import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; |
40 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacro; |
41 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroException; |
42 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory; |
43 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroManager; |
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("wikimacrolistener") |
54 |
|
@Singleton |
|
|
| 84.8% |
Uncovered Elements: 7 (46) |
Complexity: 17 |
Complexity Density: 0.61 |
|
55 |
|
public class WikiMacroEventListener implements EventListener |
56 |
|
{ |
57 |
|
|
58 |
|
@link |
59 |
|
|
60 |
|
@Inject |
61 |
|
private WikiMacroFactory macroFactory; |
62 |
|
|
63 |
|
|
64 |
|
@link |
65 |
|
|
66 |
|
@Inject |
67 |
|
private WikiMacroManager wikiMacroManager; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private Logger logger; |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
226 |
@Override... |
76 |
|
public String getName() |
77 |
|
{ |
78 |
226 |
return "wikimacrolistener"; |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
30 |
@Override... |
82 |
|
public List<Event> getEvents() |
83 |
|
{ |
84 |
30 |
return Arrays |
85 |
|
.<Event> asList(new DocumentCreatedEvent(), new DocumentUpdatedEvent(), new DocumentDeletedEvent()); |
86 |
|
} |
87 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
88 |
416 |
@Override... |
89 |
|
public void onEvent(Event event, Object source, Object data) |
90 |
|
{ |
91 |
416 |
if (event instanceof AbstractDocumentEvent) { |
92 |
416 |
DocumentModelBridge document = (DocumentModelBridge) source; |
93 |
416 |
DocumentReference documentReference = document.getDocumentReference(); |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
416 |
if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) { |
105 |
372 |
registerMacro(documentReference); |
106 |
44 |
} else if (event instanceof DocumentDeletedEvent) { |
107 |
44 |
unregisterMacro(documentReference); |
108 |
|
} |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
115 |
372 |
private void registerMacro(DocumentReference documentReference)... |
116 |
|
{ |
117 |
|
|
118 |
372 |
if (unregisterMacro(documentReference)) { |
119 |
|
|
120 |
372 |
if (this.macroFactory.containsWikiMacro(documentReference)) { |
121 |
|
|
122 |
19 |
WikiMacro wikiMacro; |
123 |
19 |
try { |
124 |
19 |
wikiMacro = this.macroFactory.createWikiMacro(documentReference); |
125 |
|
} catch (WikiMacroException e) { |
126 |
0 |
this.logger.debug(String.format("Failed to create wiki macro [%s]", documentReference), e); |
127 |
0 |
return; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
19 |
registerMacro(documentReference, wikiMacro); |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@param |
141 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 3 |
Complexity Density: 0.75 |
|
142 |
19 |
private void registerMacro(DocumentReference documentReference, WikiMacro wikiMacro)... |
143 |
|
{ |
144 |
19 |
try { |
145 |
19 |
this.wikiMacroManager.registerWikiMacro(documentReference, wikiMacro); |
146 |
|
} catch (WikiMacroException e) { |
147 |
0 |
this.logger.debug( |
148 |
|
String.format("Unable to register macro [%s] in document [%s]", wikiMacro.getDescriptor().getId() |
149 |
|
.getId(), documentReference), e); |
150 |
|
} catch (InsufficientPrivilegesException e) { |
151 |
0 |
this.logger.debug(String.format("Insufficient privileges for registering macro [%s] in document [%s]", |
152 |
|
wikiMacro.getDescriptor().getId().getId(), documentReference), e); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@return |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
162 |
416 |
private boolean unregisterMacro(DocumentReference documentReference)... |
163 |
|
{ |
164 |
416 |
boolean result = true; |
165 |
416 |
if (this.wikiMacroManager.hasWikiMacro(documentReference)) { |
166 |
18 |
try { |
167 |
18 |
this.wikiMacroManager.unregisterWikiMacro(documentReference); |
168 |
|
} catch (WikiMacroException e) { |
169 |
3 |
this.logger.debug(String.format("Unable to unregister macro in document [%s]", documentReference), e); |
170 |
3 |
result = false; |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
416 |
return result; |
175 |
|
} |
176 |
|
} |