1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.annotation.internal

File DefaultAnnotationService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

4
41
8
1
189
129
18
0.44
5.12
8
2.25

Classes

Class Line # Actions
DefaultAnnotationService 54 41 0% 18 21
0.603773660.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Default annotation service, using the default {@link IOTargetService} and and {@link IOTargetService}, dispatching
47    * calls and implementing the rendering of the content based on these data from the 2 services.
48    *
49    * @version $Id: 28f5f430b2cb499532061396efa019657b5dc7a6 $
50    * @since 2.3M1
51    */
52    @Component
53    @Singleton
 
54    public class DefaultAnnotationService implements AnnotationService
55    {
56    /**
57    * The storage service for annotations.
58    */
59    @Inject
60    private IOService ioService;
61   
62    /**
63    * Component manager used to lookup the annotation renderer needed for the specific source.
64    */
65    @Inject
66    private ComponentManager componentManager;
67   
68    /**
69    * The storage service for annotation targets (documents).
70    */
71    @Inject
72    private IOTargetService targetIoService;
73   
 
74  4 toggle @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    // create the annotation with this data and send it to the storage service
80    // TODO: also think of mapping the annotation on the document at add time and fail it if it's not mappable,
81    // for extra security
82    // TODO: normalize spaces at this level
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    // skip these fields as we don't want to overwrite them with whatever is in this map. Setters should be used
88    // for these values or constructor
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   
 
104  8 toggle @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    // build the annotations renderer hint for the specified output syntax
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    // set the annotations for this renderer
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   
 
128  0 toggle @Override
129    public String getAnnotatedHTML(String sourceReference) throws AnnotationServiceException
130    {
131  0 return getAnnotatedRenderedContent(sourceReference, null, "xhtml/1.0", getValidAnnotations(sourceReference));
132    }
133   
 
134  9 toggle @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   
 
144  0 toggle @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   
 
160  4 toggle @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   
 
170  0 toggle @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   
 
180  8 toggle @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    }