1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.xwiki.annotation.Annotation; |
32 |
|
import org.xwiki.annotation.AnnotationService; |
33 |
|
import org.xwiki.annotation.AnnotationServiceException; |
34 |
|
import org.xwiki.annotation.io.IOService; |
35 |
|
import org.xwiki.annotation.io.IOServiceException; |
36 |
|
import org.xwiki.annotation.io.IOTargetService; |
37 |
|
import org.xwiki.annotation.maintainer.AnnotationState; |
38 |
|
import org.xwiki.annotation.renderer.AnnotationPrintRenderer; |
39 |
|
import org.xwiki.component.annotation.Component; |
40 |
|
import org.xwiki.component.manager.ComponentManager; |
41 |
|
import org.xwiki.rendering.block.XDOM; |
42 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
43 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
44 |
|
|
45 |
|
|
46 |
|
@link@link |
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 60.4% |
Uncovered Elements: 21 (53) |
Complexity: 18 |
Complexity Density: 0.44 |
|
54 |
|
public class DefaultAnnotationService implements AnnotationService |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@Inject |
60 |
|
private IOService ioService; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@Inject |
66 |
|
private ComponentManager componentManager; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@Inject |
72 |
|
private IOTargetService targetIoService; |
73 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
74 |
4 |
@Override... |
75 |
|
public void addAnnotation(String target, String selection, String selectionContext, int offset, String author, |
76 |
|
Map<String, Object> metadata) throws AnnotationServiceException |
77 |
|
{ |
78 |
4 |
try { |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
4 |
String leftContext = selectionContext.substring(0, offset); |
84 |
4 |
String rightContext = selectionContext.substring(offset + selection.length()); |
85 |
4 |
Annotation annotation = new Annotation(selection, leftContext, rightContext); |
86 |
4 |
annotation.setAuthor(author); |
87 |
|
|
88 |
|
|
89 |
4 |
Collection<String> skippedFields = |
90 |
|
Arrays.asList(new String[] {Annotation.SELECTION_FIELD, Annotation.SELECTION_LEFT_CONTEXT_FIELD, |
91 |
|
Annotation.SELECTION_RIGHT_CONTEXT_FIELD, Annotation.ORIGINAL_SELECTION_FIELD, |
92 |
|
Annotation.AUTHOR_FIELD, Annotation.STATE_FIELD}); |
93 |
4 |
for (Map.Entry<String, Object> field : metadata.entrySet()) { |
94 |
12 |
if (!skippedFields.contains(field.getKey())) { |
95 |
8 |
annotation.set(field.getKey(), field.getValue()); |
96 |
|
} |
97 |
|
} |
98 |
4 |
ioService.addAnnotation(target, annotation); |
99 |
|
} catch (IOServiceException e) { |
100 |
0 |
throw new AnnotationServiceException("An exception occurred when accessing the storage services", e); |
101 |
|
} |
102 |
|
} |
103 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
104 |
8 |
@Override... |
105 |
|
public String getAnnotatedRenderedContent(String sourceReference, String sourceSyntax, String outputSyntax, |
106 |
|
Collection<Annotation> annotations) throws AnnotationServiceException |
107 |
|
{ |
108 |
8 |
try { |
109 |
8 |
XDOM xdom = targetIoService.getXDOM(sourceReference, sourceSyntax); |
110 |
|
|
111 |
|
|
112 |
8 |
String outputSyntaxId = "annotations-" + outputSyntax; |
113 |
8 |
AnnotationPrintRenderer annotationsRenderer = |
114 |
|
componentManager.getInstance(AnnotationPrintRenderer.class, outputSyntaxId); |
115 |
8 |
WikiPrinter printer = new DefaultWikiPrinter(); |
116 |
8 |
annotationsRenderer.setPrinter(printer); |
117 |
|
|
118 |
8 |
annotationsRenderer.setAnnotations(annotations); |
119 |
|
|
120 |
8 |
xdom.traverse(annotationsRenderer); |
121 |
|
|
122 |
8 |
return printer.toString(); |
123 |
|
} catch (Exception exc) { |
124 |
0 |
throw new AnnotationServiceException(exc); |
125 |
|
} |
126 |
|
} |
127 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
0 |
@Override... |
129 |
|
public String getAnnotatedHTML(String sourceReference) throws AnnotationServiceException |
130 |
|
{ |
131 |
0 |
return getAnnotatedRenderedContent(sourceReference, null, "xhtml/1.0", getValidAnnotations(sourceReference)); |
132 |
|
} |
133 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
134 |
9 |
@Override... |
135 |
|
public Collection<Annotation> getAnnotations(String target) throws AnnotationServiceException |
136 |
|
{ |
137 |
9 |
try { |
138 |
9 |
return ioService.getAnnotations(target); |
139 |
|
} catch (IOServiceException e) { |
140 |
0 |
throw new AnnotationServiceException(e); |
141 |
|
} |
142 |
|
} |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.57 |
|
144 |
0 |
@Override... |
145 |
|
public Collection<Annotation> getValidAnnotations(String target) throws AnnotationServiceException |
146 |
|
{ |
147 |
0 |
try { |
148 |
0 |
List<Annotation> result = new ArrayList<Annotation>(); |
149 |
0 |
for (Annotation it : ioService.getAnnotations(target)) { |
150 |
0 |
if (it.getState() == AnnotationState.SAFE || it.getState() == AnnotationState.UPDATED) { |
151 |
0 |
result.add(it); |
152 |
|
} |
153 |
|
} |
154 |
0 |
return result; |
155 |
|
} catch (IOServiceException e) { |
156 |
0 |
throw new AnnotationServiceException(e); |
157 |
|
} |
158 |
|
} |
159 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
160 |
4 |
@Override... |
161 |
|
public void removeAnnotation(String target, String annotationID) throws AnnotationServiceException |
162 |
|
{ |
163 |
4 |
try { |
164 |
4 |
ioService.removeAnnotation(target, annotationID); |
165 |
|
} catch (IOServiceException e) { |
166 |
0 |
throw new AnnotationServiceException(e.getMessage()); |
167 |
|
} |
168 |
|
} |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
170 |
0 |
@Override... |
171 |
|
public void updateAnnotation(String target, Annotation annotation) throws AnnotationServiceException |
172 |
|
{ |
173 |
0 |
try { |
174 |
0 |
ioService.updateAnnotations(target, Arrays.asList(annotation)); |
175 |
|
} catch (IOServiceException e) { |
176 |
0 |
throw new AnnotationServiceException(e.getMessage()); |
177 |
|
} |
178 |
|
} |
179 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
180 |
8 |
@Override... |
181 |
|
public Annotation getAnnotation(String target, String id) throws AnnotationServiceException |
182 |
|
{ |
183 |
8 |
try { |
184 |
8 |
return ioService.getAnnotation(target, id); |
185 |
|
} catch (IOServiceException e) { |
186 |
0 |
throw new AnnotationServiceException(e.getMessage()); |
187 |
|
} |
188 |
|
} |
189 |
|
} |