1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.annotation.io.internal

File DefaultIOTargetService.java

 

Coverage histogram

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

Code metrics

14
43
7
1
197
129
17
0.4
6.14
7
2.43

Classes

Class Line # Actions
DefaultIOTargetService 63 43 0% 17 17
0.73437573.4%
 

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.Syntax;
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: 1f923ef0309ae507a75ef49b8c2b66f67f9a80f3 $
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.getTranslatedDocumentInstance(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.getTranslatedDocumentInstance(new DocumentReference(docRef)).getSyntax().toIdString();
122    } else {
123  1 return dab.getDocumentSyntaxId(reference);
124    }
125    } catch (Exception e) {
126  0 throw new IOServiceException(
127    "An exception has occurred while getting the syntax of the source for " + 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)), sourceSyntaxId);
149  8 } else if (ref.getType() == EntityType.DOCUMENT) {
150  8 return getDocumentXDOM(new DocumentReference(ref));
151    } else {
152    // it was parsed as something else, just ignore the parsing and get the document content as its initial
153    // name was
154  0 return getTransformedXDOM(dab.getDocumentContent(reference), sourceSyntaxId);
155    }
156    } catch (Exception e) {
157  0 throw new IOServiceException("An exception has occurred while getting the XDOM for " + reference, e);
158    }
159    }
160   
 
161  8 toggle private XDOM getDocumentXDOM(DocumentReference reference) throws Exception
162    {
163  8 DocumentDisplayerParameters parameters = new DocumentDisplayerParameters();
164  8 parameters.setExecutionContextIsolated(true);
165  8 parameters.setContentTranslated(true);
166  8 parameters.setTargetSyntax(renderingContext.getTargetSyntax());
167   
168  8 return documentDisplayer.display(dab.getDocumentInstance(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 TransformationContext txContext =
179    new TransformationContext(xdom, Syntax.valueOf(sourceSyntaxId));
180  0 TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class);
181  0 transformationManager.performTransformations(xdom, txContext);
182   
183  0 return xdom;
184    }
185   
 
186  4 toggle private String getObjectPropertyContent(ObjectPropertyReference reference)
187    {
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(), objRef.getObjectNumber(), reference.getName())
192    .toString();
193    } else {
194  2 return dab.getProperty(docRef, objRef.getXClassReference(), reference.getName()).toString();
195    }
196    }
197    }