1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Singleton |
|
|
| 72.3% |
Uncovered Elements: 18 (65) |
Complexity: 17 |
Complexity Density: 0.39 |
|
63 |
|
public class DefaultIOTargetService implements IOTargetService |
64 |
|
{ |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private ComponentManager componentManager; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@Inject |
75 |
|
private DocumentAccessBridge dab; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
@Named("configured") |
82 |
|
private DocumentDisplayer documentDisplayer; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@Inject |
88 |
|
private TypedStringEntityReferenceResolver referenceResolver; |
89 |
|
|
90 |
|
@Inject |
91 |
|
private RenderingContext renderingContext; |
92 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
93 |
11 |
@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 |
|
|
104 |
|
|
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 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
112 |
19 |
@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 |
|
|
120 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
0 |
@Override... |
132 |
|
public XDOM getXDOM(String reference) throws IOServiceException |
133 |
|
{ |
134 |
0 |
return getXDOM(reference, null); |
135 |
|
} |
136 |
|
|
|
|
| 64.7% |
Uncovered Elements: 6 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
137 |
8 |
@Override... |
138 |
|
public XDOM getXDOM(String reference, String syntax) throws IOServiceException |
139 |
|
{ |
140 |
8 |
String sourceSyntaxId = syntax; |
141 |
|
|
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 |
|
|
154 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
162 |
8 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
171 |
0 |
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
187 |
4 |
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 |
|
} |