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

File DefaultIOTargetService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

14
44
7
1
197
130
17
0.39
6.29
7
2.43

Classes

Class Line # Actions
DefaultIOTargetService 63 44 0% 17 18
0.7230769472.3%
 

Contributing tests

This file is covered by 11 tests. .

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.io.internal;
21   
22    import java.io.StringReader;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.annotation.io.IOServiceException;
29    import org.xwiki.annotation.io.IOTargetService;
30    import org.xwiki.annotation.reference.TypedStringEntityReferenceResolver;
31    import org.xwiki.bridge.DocumentAccessBridge;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.display.internal.DocumentDisplayer;
35    import org.xwiki.display.internal.DocumentDisplayerParameters;
36    import org.xwiki.model.EntityType;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.ObjectPropertyReference;
40    import org.xwiki.rendering.block.XDOM;
41    import org.xwiki.rendering.parser.ParseException;
42    import org.xwiki.rendering.parser.Parser;
43    import org.xwiki.rendering.syntax.SyntaxFactory;
44    import org.xwiki.rendering.transformation.RenderingContext;
45    import org.xwiki.rendering.transformation.TransformationContext;
46    import org.xwiki.rendering.transformation.TransformationException;
47    import org.xwiki.rendering.transformation.TransformationManager;
48   
49    import com.xpn.xwiki.objects.BaseObjectReference;
50   
51    /**
52    * Default {@link IOTargetService} implementation, based on resolving XWiki documents and object properties as
53    * annotations targets. The references manipulated by this implementation are XWiki references, such as xwiki:Space.Page
54    * for documents or with an object and property reference if the target is an object property. Use the reference module
55    * to generate the references passed to this module, so that they can be resolved to XWiki content back by this
56    * implementation.
57    *
58    * @version $Id: e106ea3c4dc59211ece84da69d06541e28e659d6 $
59    * @since 2.3M1
60    */
61    @Component
62    @Singleton
 
63    public class DefaultIOTargetService implements IOTargetService
64    {
65    /**
66    * Component manager used to lookup the parsers.
67    */
68    @Inject
69    private ComponentManager componentManager;
70   
71    /**
72    * Document access bridge to manipulate xwiki documents.
73    */
74    @Inject
75    private DocumentAccessBridge dab;
76   
77    /**
78    * Document displayer.
79    */
80    @Inject
81    @Named("configured")
82    private DocumentDisplayer documentDisplayer;
83   
84    /**
85    * Entity reference handler to resolve the reference.
86    */
87    @Inject
88    private TypedStringEntityReferenceResolver referenceResolver;
89   
90    @Inject
91    private RenderingContext renderingContext;
92   
 
93  11 toggle @Override
94    public String getSource(String reference) throws IOServiceException
95    {
96  11 try {
97  11 EntityReference ref = referenceResolver.resolve(reference, EntityType.DOCUMENT);
98  11 if (ref.getType() == EntityType.OBJECT_PROPERTY) {
99  4 return getObjectPropertyContent(new ObjectPropertyReference(ref));
100  7 } else if (ref.getType() == EntityType.DOCUMENT) {
101  6 return dab.getDocument(new DocumentReference(ref)).getContent();
102    } else {
103    // it was parsed as something else, just ignore the parsing and get the document content as its initial
104    // name was
105  1 return dab.getDocumentContent(reference);
106    }
107    } catch (Exception e) {
108  0 throw new IOServiceException("An exception has occurred while getting the source for " + reference, e);
109    }
110    }
111   
 
112  19 toggle @Override
113    public String getSourceSyntax(String reference) throws IOServiceException
114    {
115  19 try {
116  19 EntityReference ref = referenceResolver.resolve(reference, EntityType.DOCUMENT);
117  19 EntityReference docRef = ref.extractReference(EntityType.DOCUMENT);
118  19 if (docRef != null) {
119    // return the syntax of the document in this reference, regardless of the type of reference, obj prop or
120    // doc
121  18 return dab.getDocument(new DocumentReference(docRef)).getSyntax().toIdString();
122    } else {
123  1 return dab.getDocumentSyntaxId(reference);
124    }
125    } catch (Exception e) {
126  0 throw new IOServiceException("An exception has occurred while getting the syntax of the source for "
127    + reference, e);
128    }
129    }
130   
 
131  0 toggle @Override
132    public XDOM getXDOM(String reference) throws IOServiceException
133    {
134  0 return getXDOM(reference, null);
135    }
136   
 
137  8 toggle @Override
138    public XDOM getXDOM(String reference, String syntax) throws IOServiceException
139    {
140  8 String sourceSyntaxId = syntax;
141    // get if unspecified, get the source from the io service
142  8 if (sourceSyntaxId == null) {
143  8 sourceSyntaxId = getSourceSyntax(reference);
144    }
145  8 try {
146  8 EntityReference ref = referenceResolver.resolve(reference, EntityType.DOCUMENT);
147  8 if (ref.getType() == EntityType.OBJECT_PROPERTY) {
148  0 return getTransformedXDOM(getObjectPropertyContent(new ObjectPropertyReference(ref)),
149    sourceSyntaxId);
150  8 } else if (ref.getType() == EntityType.DOCUMENT) {
151  8 return getDocumentXDOM(new DocumentReference(ref));
152    } else {
153    // it was parsed as something else, just ignore the parsing and get the document content as its initial
154    // name was
155  0 return getTransformedXDOM(dab.getDocumentContent(reference), sourceSyntaxId);
156    }
157    } catch (Exception e) {
158  0 throw new IOServiceException("An exception has occurred while getting the XDOM for " + reference, e);
159    }
160    }
161   
 
162  8 toggle private XDOM getDocumentXDOM(DocumentReference reference) throws Exception
163    {
164  8 DocumentDisplayerParameters parameters = new DocumentDisplayerParameters();
165  8 parameters.setExecutionContextIsolated(true);
166  8 parameters.setContentTranslated(true);
167  8 parameters.setTargetSyntax(renderingContext.getTargetSyntax());
168  8 return documentDisplayer.display(dab.getDocument(reference), parameters);
169    }
170   
 
171  0 toggle private XDOM getTransformedXDOM(String content, String sourceSyntaxId)
172    throws ParseException, org.xwiki.component.manager.ComponentLookupException, TransformationException
173    {
174  0 Parser parser = componentManager.getInstance(Parser.class, sourceSyntaxId);
175  0 XDOM xdom = parser.parse(new StringReader(content));
176   
177    // run transformations
178  0 SyntaxFactory syntaxFactory = componentManager.getInstance(SyntaxFactory.class);
179  0 TransformationContext txContext =
180    new TransformationContext(xdom, syntaxFactory.createSyntaxFromIdString(sourceSyntaxId));
181  0 TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class);
182  0 transformationManager.performTransformations(xdom, txContext);
183   
184  0 return xdom;
185    }
186   
 
187  4 toggle private String getObjectPropertyContent(ObjectPropertyReference reference) {
188  4 BaseObjectReference objRef = new BaseObjectReference(reference.getParent());
189  4 DocumentReference docRef = new DocumentReference(objRef.getParent());
190  4 if (objRef.getObjectNumber() != null) {
191  2 return dab.getProperty(docRef, objRef.getXClassReference(),
192    objRef.getObjectNumber(), reference.getName()).toString();
193    } else {
194  2 return dab.getProperty(docRef, objRef.getXClassReference(), reference.getName()).toString();
195    }
196    }
197    }